|
@@ -0,0 +1,104 @@
|
|
1
|
+/*
|
|
2
|
+ * Copyright (c) 2006-2014 DMDirc Developers
|
|
3
|
+ *
|
|
4
|
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+ * of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+ * in the Software without restriction, including without limitation the rights
|
|
7
|
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+ * copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+ * furnished to do so, subject to the following conditions:
|
|
10
|
+ *
|
|
11
|
+ * The above copyright notice and this permission notice shall be included in
|
|
12
|
+ * all copies or substantial portions of the Software.
|
|
13
|
+ *
|
|
14
|
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+ * SOFTWARE.
|
|
21
|
+ */
|
|
22
|
+
|
|
23
|
+package com.dmdirc.addons.ui_swing.injection;
|
|
24
|
+
|
|
25
|
+import static com.dmdirc.addons.ui_swing.SwingPreconditions.checkOnEDT;
|
|
26
|
+import com.dmdirc.addons.ui_swing.dialogs.StandardDialog;
|
|
27
|
+
|
|
28
|
+import java.awt.event.WindowAdapter;
|
|
29
|
+import java.awt.event.WindowEvent;
|
|
30
|
+import javax.inject.Provider;
|
|
31
|
+
|
|
32
|
+/**
|
|
33
|
+ * Simple provider for {@link StandardDialog} based windows.
|
|
34
|
+ *
|
|
35
|
+ * <p>This provider will cache instances that are created until the windows are closed. Once a
|
|
36
|
+ * window has been closed, the next call to {@link #get()} or {@link #displayOrRequestFocus()}
|
|
37
|
+ * will result in a new instance being created.
|
|
38
|
+ *
|
|
39
|
+ * @param <T> The type of dialog that will be managed.
|
|
40
|
+ */
|
|
41
|
+public class DialogProvider<T extends StandardDialog> {
|
|
42
|
+
|
|
43
|
+ /** Provider used to generate new instances. */
|
|
44
|
+ private final Provider<T> provider;
|
|
45
|
+ /** The existing instance being displayed, if any. */
|
|
46
|
+ private T instance;
|
|
47
|
+
|
|
48
|
+ /**
|
|
49
|
+ * Creates a new instance of {@link DialogProvider}.
|
|
50
|
+ *
|
|
51
|
+ * @param provider The provider to use to generate new instances of the dialog, when required.
|
|
52
|
+ */
|
|
53
|
+ public DialogProvider(final Provider<T> provider) {
|
|
54
|
+ this.provider = provider;
|
|
55
|
+ }
|
|
56
|
+
|
|
57
|
+ /**
|
|
58
|
+ * Gets an instance of the dialog provided by this class.
|
|
59
|
+ *
|
|
60
|
+ * <p>If there is an existing instance that hasn't been closed, it will be returned. Otherwise
|
|
61
|
+ * a new instance will be created and returned. New instances will not be automatically be
|
|
62
|
+ * displayed to the user - use {@link #displayOrRequestFocus()} to do so.
|
|
63
|
+ *
|
|
64
|
+ * <p>This method <em>must</em> be called on the Event Despatch Thread.
|
|
65
|
+ *
|
|
66
|
+ * @return An instance of the dialog.
|
|
67
|
+ */
|
|
68
|
+ public T get() {
|
|
69
|
+ checkOnEDT();
|
|
70
|
+ if (instance == null) {
|
|
71
|
+ instance = provider.get();
|
|
72
|
+ instance.addWindowListener(new Listener());
|
|
73
|
+ }
|
|
74
|
+ return instance;
|
|
75
|
+ }
|
|
76
|
+
|
|
77
|
+ /**
|
|
78
|
+ * Ensures the dialog is visible to the user.
|
|
79
|
+ *
|
|
80
|
+ * <p>If no dialog currently exists, a new one will be created and displayed to the user. If
|
|
81
|
+ * a dialog existed prior to this method being invoked, it will request focus to bring it to
|
|
82
|
+ * the user's attention.
|
|
83
|
+ *
|
|
84
|
+ * <p>This method <em>must</em> be called on the Event Despatch Thread.
|
|
85
|
+ */
|
|
86
|
+ public void displayOrRequestFocus() {
|
|
87
|
+ checkOnEDT();
|
|
88
|
+ get().displayOrRequestFocus();
|
|
89
|
+ }
|
|
90
|
+
|
|
91
|
+ /**
|
|
92
|
+ * Listens to window closing events to remove the cached instance of the dialog.
|
|
93
|
+ */
|
|
94
|
+ private class Listener extends WindowAdapter {
|
|
95
|
+
|
|
96
|
+ @Override
|
|
97
|
+ public void windowClosing(final WindowEvent e) {
|
|
98
|
+ super.windowClosing(e);
|
|
99
|
+ instance = null;
|
|
100
|
+ }
|
|
101
|
+
|
|
102
|
+ }
|
|
103
|
+
|
|
104
|
+}
|