svn-gvsig-desktop / tags / v2_0_0_Build_2057 / frameworks / _fwAndami / src / org / gvsig / andami / ui / mdiFrame / DefaultThreadSafeDialogs.java @ 39145
History | View | Annotate | Download (9.36 KB)
1 |
package org.gvsig.andami.ui.mdiFrame; |
---|---|
2 |
|
3 |
import java.awt.BorderLayout; |
4 |
import java.awt.Component; |
5 |
import java.awt.Dimension; |
6 |
import java.awt.GridBagConstraints; |
7 |
import java.awt.event.ComponentEvent; |
8 |
import java.awt.event.ComponentListener; |
9 |
import java.lang.reflect.Constructor; |
10 |
|
11 |
import javax.swing.JOptionPane; |
12 |
import javax.swing.JPanel; |
13 |
import javax.swing.SwingUtilities; |
14 |
|
15 |
import org.gvsig.andami.PluginServices; |
16 |
import org.gvsig.andami.ui.mdiManager.IWindow; |
17 |
import org.gvsig.andami.ui.mdiManager.MDIManager; |
18 |
import org.gvsig.andami.ui.mdiManager.WindowInfo; |
19 |
import org.gvsig.tools.task.CancellableTask; |
20 |
import org.gvsig.tools.task.RunnableWithParameters; |
21 |
import org.slf4j.Logger; |
22 |
import org.slf4j.LoggerFactory; |
23 |
|
24 |
/**
|
25 |
* Thread safe functions for showing dialogs
|
26 |
*
|
27 |
* @author jjdelcerro
|
28 |
*
|
29 |
*/
|
30 |
public class DefaultThreadSafeDialogs implements ThreadSafeDialogs { |
31 |
|
32 |
private static Logger logger = LoggerFactory |
33 |
.getLogger(DefaultThreadSafeDialogs.class); |
34 |
private Component rootComponent; |
35 |
private NewStatusBar statusbar;
|
36 |
|
37 |
public DefaultThreadSafeDialogs() {
|
38 |
this.rootComponent = null; |
39 |
this.statusbar = null; |
40 |
} |
41 |
|
42 |
public DefaultThreadSafeDialogs(Component rootComponent, |
43 |
NewStatusBar statusbar) { |
44 |
this.statusbar = statusbar;
|
45 |
this.rootComponent = rootComponent;
|
46 |
} |
47 |
|
48 |
private Component getRootComponent() { |
49 |
if (this.rootComponent == null) { |
50 |
try {
|
51 |
this.rootComponent = (MDIFrame) PluginServices.getMainFrame();
|
52 |
} catch (Throwable t) { |
53 |
// Ignore and return null
|
54 |
} |
55 |
} |
56 |
return this.rootComponent; |
57 |
} |
58 |
|
59 |
private NewStatusBar getStatusbar() {
|
60 |
if (this.statusbar == null) { |
61 |
this.statusbar = PluginServices.getMainFrame().getStatusBar();
|
62 |
} |
63 |
return this.statusbar; |
64 |
} |
65 |
|
66 |
private void message(String message, int messageType) { |
67 |
this.getStatusbar().message(message, messageType);
|
68 |
} |
69 |
|
70 |
private String translate(String message) { |
71 |
return translate(message, null); |
72 |
} |
73 |
|
74 |
private String translate(String message, String[] args) { |
75 |
String msg = message;
|
76 |
if (msg == null) { |
77 |
msg = "";
|
78 |
} |
79 |
if (msg.startsWith("_")) { |
80 |
msg = org.gvsig.i18n.Messages.getText(msg, args); |
81 |
if (msg == null) { |
82 |
msg = "_" + message.replace("_", " "); |
83 |
} |
84 |
} |
85 |
return msg;
|
86 |
} |
87 |
|
88 |
public int confirmDialog(final String message, final String title, final int optionType, |
89 |
final int messageType) { |
90 |
RunnableWithParameters runnable = new RunnableWithParameters() {
|
91 |
public void run() { |
92 |
this.returnValue = JOptionPane.showConfirmDialog( |
93 |
getRootComponent(), message,title, optionType, messageType); |
94 |
} |
95 |
}; |
96 |
if (SwingUtilities.isEventDispatchThread()) { |
97 |
runnable.run(); |
98 |
} else {
|
99 |
try {
|
100 |
SwingUtilities.invokeAndWait(runnable);
|
101 |
} catch (Exception e) { |
102 |
logger.info("Can't show input dialog '" + message + "'.", e); |
103 |
} |
104 |
} |
105 |
return (Integer) runnable.getReturnValue(); |
106 |
} |
107 |
|
108 |
public String inputDialog(final String message, final String title, final int messageType, |
109 |
final String initialValue) { |
110 |
// inputDialog dlg = new inputDialog();
|
111 |
// return dlg.show(translate(message), translate(title), messageType,
|
112 |
// initialValue);
|
113 |
//
|
114 |
RunnableWithParameters runnable = new RunnableWithParameters() {
|
115 |
public void run() { |
116 |
this.returnValue = JOptionPane.showInputDialog( |
117 |
getRootComponent(), translate(message), |
118 |
translate(title), |
119 |
messageType, null, null, |
120 |
initialValue); |
121 |
} |
122 |
}; |
123 |
if (SwingUtilities.isEventDispatchThread()) { |
124 |
runnable.run(); |
125 |
} else {
|
126 |
try {
|
127 |
SwingUtilities.invokeAndWait(runnable);
|
128 |
} catch (Exception e) { |
129 |
logger.info("Can't show input dialog '" + message + "'.", e); |
130 |
} |
131 |
} |
132 |
return (String) runnable.getReturnValue(); |
133 |
} |
134 |
|
135 |
public String inputDialog(final String message, final String title) { |
136 |
RunnableWithParameters runnable = new RunnableWithParameters() {
|
137 |
public void run() { |
138 |
this.returnValue = JOptionPane.showInputDialog( |
139 |
getRootComponent(), (String) translate(message),
|
140 |
translate(title), |
141 |
JOptionPane.QUESTION_MESSAGE, null, null, null); |
142 |
} |
143 |
}; |
144 |
if (SwingUtilities.isEventDispatchThread()) { |
145 |
runnable.run(); |
146 |
} else {
|
147 |
try {
|
148 |
SwingUtilities.invokeAndWait(runnable);
|
149 |
} catch (Exception e) { |
150 |
logger.info("Can't show input dialog '" + message + "'.", e); |
151 |
} |
152 |
} |
153 |
return (String) runnable.getReturnValue(); |
154 |
} |
155 |
|
156 |
public void messageDialog(String message, String title, int messageType) { |
157 |
messageDialog(message, null, title, messageType);
|
158 |
} |
159 |
|
160 |
public void messageDialog(final String message, final String messageArgs[], |
161 |
final String title, final int messageType) { |
162 |
if (!SwingUtilities.isEventDispatchThread()) { |
163 |
try {
|
164 |
SwingUtilities.invokeAndWait(new Runnable() { |
165 |
public void run() { |
166 |
messageDialog(message, messageArgs, title, messageType); |
167 |
} |
168 |
}); |
169 |
} catch (Exception e) { |
170 |
logger.info("Can't show message dialog '" + message
|
171 |
+ "'. redirect to status bar", e);
|
172 |
this.message(message, messageType);
|
173 |
} |
174 |
return;
|
175 |
} |
176 |
|
177 |
if (message == null) { |
178 |
logger.info("message if null, message dialog not show.");
|
179 |
return;
|
180 |
} |
181 |
JOptionPane.showMessageDialog(getRootComponent(),
|
182 |
translate(message, messageArgs), translate(title), messageType); |
183 |
} |
184 |
|
185 |
public void showDialog(final Component contents, final String title) { |
186 |
if (SwingUtilities.isEventDispatchThread()) { |
187 |
final DialogWindow window = new DialogWindow(); |
188 |
window.setDialogFlag(); |
189 |
window.setContents(contents, title); |
190 |
MDIManager manager = PluginServices.getMDIManager(); |
191 |
manager.addWindow(window, GridBagConstraints.CENTER);
|
192 |
} else {
|
193 |
final DialogWindow window = new DialogWindow(); |
194 |
SwingUtilities.invokeLater(new Runnable() { |
195 |
public void run() { |
196 |
window.setContents(contents, title); |
197 |
MDIManager manager = PluginServices.getMDIManager(); |
198 |
manager.addWindow(window, GridBagConstraints.CENTER);
|
199 |
} |
200 |
}); |
201 |
try {
|
202 |
synchronized (window) {
|
203 |
if (contents instanceof CancellableTask) { |
204 |
while( contents.isVisible() ) {
|
205 |
if( ((CancellableTask)contents).isCancellationRequested() ) {
|
206 |
SwingUtilities.invokeLater(new Runnable() { |
207 |
public void run() { |
208 |
contents.setVisible(false);
|
209 |
} |
210 |
}); |
211 |
} |
212 |
window.wait(10000);
|
213 |
} |
214 |
} else {
|
215 |
while( contents.isVisible() ) {
|
216 |
window.wait(); |
217 |
} |
218 |
} |
219 |
} |
220 |
} catch (InterruptedException e) { |
221 |
logger.info("showDialog can wait to close dialog.", e);
|
222 |
} |
223 |
} |
224 |
} |
225 |
|
226 |
class DialogWindow extends JPanel implements IWindow, ComponentListener { |
227 |
|
228 |
/**
|
229 |
*
|
230 |
*/
|
231 |
private static final long serialVersionUID = 6283975319620714565L; |
232 |
protected WindowInfo windowInfo;
|
233 |
protected Object profile; |
234 |
protected Component contents; |
235 |
private int code = 0; |
236 |
|
237 |
public DialogWindow() {
|
238 |
code = WindowInfo.RESIZABLE | WindowInfo.MAXIMIZABLE |
239 |
| WindowInfo.ICONIFIABLE; |
240 |
profile = WindowInfo.DIALOG_PROFILE; |
241 |
} |
242 |
|
243 |
public void setDialogFlag() { |
244 |
code |= WindowInfo.MODALDIALOG; |
245 |
} |
246 |
|
247 |
public void setContents(Component contents, String title) { |
248 |
this.windowInfo = new WindowInfo(code); |
249 |
this.windowInfo.setTitle(title);
|
250 |
|
251 |
this.contents = contents;
|
252 |
|
253 |
Dimension size = this.contents.getPreferredSize(); |
254 |
this.windowInfo.setHeight(size.height);
|
255 |
this.windowInfo.setWidth(size.width);
|
256 |
|
257 |
this.setLayout(new BorderLayout()); |
258 |
this.add(this.contents, BorderLayout.CENTER); |
259 |
|
260 |
this.contents.addComponentListener(this); |
261 |
} |
262 |
|
263 |
public WindowInfo getWindowInfo() {
|
264 |
return this.windowInfo; |
265 |
} |
266 |
|
267 |
public Object getWindowProfile() { |
268 |
return this.profile; |
269 |
} |
270 |
|
271 |
public void componentHidden(ComponentEvent arg0) { |
272 |
// Close window when hide contents panel.
|
273 |
MDIManager manager = PluginServices.getMDIManager(); |
274 |
manager.closeWindow(this);
|
275 |
if ((code & WindowInfo.MODALDIALOG) == 0) { |
276 |
synchronized (this) { |
277 |
this.notifyAll();
|
278 |
} |
279 |
} |
280 |
} |
281 |
|
282 |
public void componentMoved(ComponentEvent arg0) { |
283 |
// Do nothing
|
284 |
} |
285 |
|
286 |
public void componentResized(ComponentEvent arg0) { |
287 |
// Do nothing
|
288 |
} |
289 |
|
290 |
public void componentShown(ComponentEvent arg0) { |
291 |
// Do nothing
|
292 |
} |
293 |
|
294 |
} |
295 |
|
296 |
public Component createComponent(final Class<? extends Component> theClass, |
297 |
final Object... parameters) { |
298 |
return createComponentWithParams(theClass, parameters);
|
299 |
} |
300 |
|
301 |
public Component createComponentWithParams( |
302 |
final Class<? extends Component> theClass, final Object[] parameters) { |
303 |
final Class<?>[] parameterTypes = new Class<?>[parameters.length]; |
304 |
for (int i = 0; i < parameters.length; i++) { |
305 |
parameterTypes[i] = parameters[i].getClass(); |
306 |
} |
307 |
final Component component; |
308 |
final Constructor<?> constructor; |
309 |
try {
|
310 |
constructor = theClass.getConstructor(parameterTypes); |
311 |
} catch (Exception e) { |
312 |
throw new IllegalArgumentException(e); |
313 |
} |
314 |
if (SwingUtilities.isEventDispatchThread()) { |
315 |
try {
|
316 |
component = (Component) constructor.newInstance(parameters);
|
317 |
} catch (Exception e) { |
318 |
throw new IllegalArgumentException(e); |
319 |
} |
320 |
} else {
|
321 |
try {
|
322 |
RunnableWithParameters runnable = new RunnableWithParameters(parameters) {
|
323 |
public void run() { |
324 |
Constructor<?> cons = constructor;
|
325 |
try {
|
326 |
this.returnValue = cons.newInstance(parameters.toArray());
|
327 |
} catch (Exception e) { |
328 |
String msg ="Can't create instance of components, constructor="+cons.toString()+", parameters="+this.parameters.toString()+"."; |
329 |
logger.info(msg,e); |
330 |
throw new IllegalArgumentException(e); |
331 |
} |
332 |
} |
333 |
}; |
334 |
SwingUtilities.invokeAndWait(runnable);
|
335 |
component = (Component) runnable.getReturnValue();
|
336 |
} catch (Exception e) { |
337 |
throw new IllegalArgumentException(e); |
338 |
} |
339 |
} |
340 |
return component;
|
341 |
} |
342 |
} |