1 | |
package org.jtheque.core.managers.view.impl; |
2 | |
|
3 | |
import org.jdesktop.swingx.JXErrorPane; |
4 | |
import org.jdesktop.swingx.error.ErrorInfo; |
5 | |
import org.jtheque.core.managers.Managers; |
6 | |
import org.jtheque.core.managers.error.JThequeError; |
7 | |
import org.jtheque.core.managers.log.ILoggingManager; |
8 | |
import org.jtheque.core.managers.view.Views; |
9 | |
import org.jtheque.core.managers.view.able.IView; |
10 | |
import org.jtheque.core.managers.view.able.IViewManager; |
11 | |
import org.jtheque.core.managers.view.able.ViewDelegate; |
12 | |
import org.jtheque.utils.io.SimpleFilter; |
13 | |
import org.jtheque.utils.ui.SwingUtils; |
14 | |
|
15 | |
import javax.annotation.Resource; |
16 | |
import javax.swing.*; |
17 | |
import java.awt.Component; |
18 | |
import java.awt.GraphicsEnvironment; |
19 | |
import java.awt.Window; |
20 | |
import java.io.File; |
21 | |
import java.lang.reflect.InvocationTargetException; |
22 | |
import java.util.logging.Level; |
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | 0 | public final class SwingViewDelegate implements ViewDelegate { |
44 | |
@Resource |
45 | |
private Views windowManager; |
46 | |
|
47 | |
private JFileChooser chooser; |
48 | |
|
49 | |
@Override |
50 | |
public boolean askYesOrNo(final String text, final String title) { |
51 | 0 | boolean yes = false; |
52 | |
|
53 | 0 | Window parent = null; |
54 | |
|
55 | 0 | if (Managers.getManager(IViewManager.class).getViews().getMainView() != null) { |
56 | 0 | parent = (Window) Managers.getManager(IViewManager.class).getViews().getMainView().getImpl(); |
57 | |
} |
58 | |
|
59 | 0 | final Window p = parent; |
60 | |
|
61 | 0 | final int[] response = new int[1]; |
62 | |
|
63 | 0 | if(SwingUtilities.isEventDispatchThread()){ |
64 | 0 | response[0] = JOptionPane.showConfirmDialog(parent, text, title, JOptionPane.YES_NO_OPTION); |
65 | |
} else { |
66 | |
try { |
67 | 0 | SwingUtilities.invokeAndWait(new Runnable(){ |
68 | |
@Override |
69 | |
public void run() { |
70 | 0 | response[0] = JOptionPane.showConfirmDialog(p, text, title, JOptionPane.YES_NO_OPTION); |
71 | 0 | } |
72 | |
}); |
73 | 0 | } catch (InterruptedException e) { |
74 | 0 | Managers.getManager(ILoggingManager.class).getLogger(getClass()).error(e); |
75 | 0 | } catch (InvocationTargetException e) { |
76 | 0 | Managers.getManager(ILoggingManager.class).getLogger(getClass()).error(e); |
77 | 0 | } |
78 | |
} |
79 | |
|
80 | 0 | if (response[0] == JOptionPane.YES_OPTION) { |
81 | 0 | yes = true; |
82 | |
} |
83 | |
|
84 | 0 | return yes; |
85 | |
} |
86 | |
|
87 | |
@Override |
88 | |
public void displayError(JThequeError error) { |
89 | 0 | final ErrorInfo info = new ErrorInfo("Error", error.getMessage(), error.getDetails(), "", error.getException(), Level.SEVERE, null); |
90 | |
|
91 | 0 | run(new DisplayErrorRunnable(info)); |
92 | 0 | } |
93 | |
|
94 | |
@Override |
95 | |
public void displayText(String text) { |
96 | 0 | run(new DisplayTextRunnable(text)); |
97 | 0 | } |
98 | |
|
99 | |
@Override |
100 | |
public String chooseFile(SimpleFilter filter) { |
101 | 0 | File file = null; |
102 | |
|
103 | 0 | if (chooser == null) { |
104 | 0 | chooser = new JFileChooser(); |
105 | |
} |
106 | |
|
107 | 0 | if (filter == null) { |
108 | 0 | chooser.setAcceptAllFileFilterUsed(true); |
109 | |
} else { |
110 | 0 | chooser.addChoosableFileFilter(new SwingFileFilter(filter)); |
111 | 0 | chooser.setAcceptAllFileFilterUsed(false); |
112 | |
} |
113 | |
|
114 | 0 | int answer = chooser.showOpenDialog(Managers.getManager(IViewManager.class).getSplashManager().getMainView()); |
115 | |
|
116 | 0 | if (answer == JFileChooser.APPROVE_OPTION) { |
117 | 0 | file = chooser.getSelectedFile(); |
118 | |
} |
119 | |
|
120 | 0 | return file == null ? null : file.getAbsolutePath(); |
121 | |
} |
122 | |
|
123 | |
@Override |
124 | |
public String chooseDirectory() { |
125 | 0 | File file = null; |
126 | |
|
127 | 0 | if (chooser == null) { |
128 | 0 | chooser = new JFileChooser(); |
129 | |
} |
130 | |
|
131 | 0 | chooser.setAcceptAllFileFilterUsed(false); |
132 | 0 | chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); |
133 | |
|
134 | 0 | int returnCode = chooser.showOpenDialog(new JFrame()); |
135 | |
|
136 | 0 | if (returnCode == JFileChooser.APPROVE_OPTION) { |
137 | 0 | file = chooser.getSelectedFile(); |
138 | |
} |
139 | |
|
140 | 0 | return file == null ? null : file.getAbsolutePath(); |
141 | |
} |
142 | |
|
143 | |
@Override |
144 | |
public void run(Runnable runnable) { |
145 | 0 | SwingUtils.inEdt(runnable); |
146 | 0 | } |
147 | |
|
148 | |
@Override |
149 | |
public void refresh(Object c) { |
150 | 0 | SwingUtils.refresh((Component) c); |
151 | 0 | } |
152 | |
|
153 | |
@Override |
154 | |
public void applyGlassPane(Object component) { |
155 | 0 | final Component glass = (Component) component; |
156 | |
|
157 | 0 | Runnable display = new Runnable() { |
158 | |
@Override |
159 | |
public void run() { |
160 | 0 | windowManager.getMainView().setGlassPane(glass); |
161 | |
|
162 | 0 | glass.setVisible(true); |
163 | |
|
164 | 0 | glass.repaint(); |
165 | |
|
166 | 0 | refresh(glass); |
167 | 0 | windowManager.getMainView().refresh(); |
168 | 0 | } |
169 | |
}; |
170 | |
|
171 | 0 | run(display); |
172 | 0 | } |
173 | |
|
174 | |
@Override |
175 | |
public void setSize(IView view, int defaultWidth, int defaultHeight) { |
176 | 0 | ((Component) view).setSize(defaultWidth, defaultHeight); |
177 | 0 | SwingUtils.centerFrame((Window) view); |
178 | 0 | } |
179 | |
|
180 | |
@Override |
181 | |
public void fill(WindowConfiguration configuration, IView view) { |
182 | 0 | Component window = (Component) view; |
183 | |
|
184 | 0 | configuration.setWidth(window.getWidth()); |
185 | 0 | configuration.setHeight(window.getHeight()); |
186 | 0 | configuration.setPositionX(window.getLocation().x); |
187 | 0 | configuration.setPositionY(window.getLocation().y); |
188 | 0 | } |
189 | |
|
190 | |
@Override |
191 | |
public void configure(WindowConfiguration configuration, IView view) { |
192 | 0 | Window window = (Window) view; |
193 | |
|
194 | 0 | window.setSize(configuration.getWidth(), configuration.getHeight()); |
195 | |
|
196 | 0 | if (configuration.getPositionX() == -1 || configuration.getPositionY() == -1) { |
197 | 0 | SwingUtils.centerFrame(window); |
198 | |
} else { |
199 | 0 | if (GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().contains( |
200 | |
configuration.getPositionX(), configuration.getPositionY())) { |
201 | 0 | window.setLocation(configuration.getPositionX(), configuration.getPositionY()); |
202 | |
} else { |
203 | 0 | SwingUtils.centerFrame(window); |
204 | |
} |
205 | |
} |
206 | 0 | } |
207 | |
|
208 | |
@Override |
209 | |
public String askText(String text) { |
210 | 0 | Window parent = null; |
211 | |
|
212 | 0 | if (Managers.getManager(IViewManager.class).getViews().getMainView() != null) { |
213 | 0 | parent = (Window) Managers.getManager(IViewManager.class).getViews().getMainView().getImpl(); |
214 | |
} |
215 | |
|
216 | 0 | return JOptionPane.showInputDialog(parent, text); |
217 | |
} |
218 | |
|
219 | |
|
220 | |
|
221 | |
|
222 | |
|
223 | |
|
224 | |
private static class DisplayErrorRunnable implements Runnable { |
225 | |
private final ErrorInfo info; |
226 | |
|
227 | |
|
228 | |
|
229 | |
|
230 | |
|
231 | |
|
232 | 0 | DisplayErrorRunnable(ErrorInfo info){ |
233 | 0 | this.info = info; |
234 | 0 | } |
235 | |
|
236 | |
@Override |
237 | |
public void run() { |
238 | 0 | JXErrorPane.showDialog((Component) Managers.getManager(IViewManager.class).getViews().getMainView().getImpl(), info); |
239 | 0 | } |
240 | |
} |
241 | |
|
242 | |
|
243 | |
|
244 | |
|
245 | |
|
246 | |
|
247 | 0 | private static class DisplayTextRunnable implements Runnable { |
248 | |
private final String text; |
249 | |
|
250 | |
|
251 | |
|
252 | |
|
253 | |
|
254 | |
|
255 | 0 | DisplayTextRunnable(String text){ |
256 | 0 | this.text = text; |
257 | 0 | } |
258 | |
|
259 | |
@Override |
260 | |
public void run() { |
261 | 0 | JOptionPane.showMessageDialog((Component) Managers.getManager(IViewManager.class).getViews().getMainView().getImpl(), text); |
262 | 0 | } |
263 | |
} |
264 | |
} |