Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / frameworks / _fwAndami / src / org / gvsig / andami / ui / mdiFrame / DefaultThreadSafeDialogs.java @ 39484

History | View | Annotate | Download (11.6 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.io.File;
10
import java.lang.reflect.Constructor;
11

    
12
import javax.swing.JFileChooser;
13
import javax.swing.JOptionPane;
14
import javax.swing.JPanel;
15
import javax.swing.SwingUtilities;
16
import javax.swing.filechooser.FileFilter;
17

    
18
import org.gvsig.andami.PluginServices;
19
import org.gvsig.andami.ui.mdiManager.IWindow;
20
import org.gvsig.andami.ui.mdiManager.MDIManager;
21
import org.gvsig.andami.ui.mdiManager.WindowInfo;
22
import org.gvsig.tools.task.CancellableTask;
23
import org.gvsig.tools.task.RunnableWithParameters;
24
import org.slf4j.Logger;
25
import org.slf4j.LoggerFactory;
26

    
27
/**
28
 * Thread safe functions for showing dialogs
29
 * 
30
 * @author jjdelcerro
31
 * 
32
 */
33
public class DefaultThreadSafeDialogs implements ThreadSafeDialogs {
34

    
35
        private static Logger logger = LoggerFactory
36
                        .getLogger(DefaultThreadSafeDialogs.class);
37
        private Component rootComponent;
38
        private NewStatusBar statusbar;
39

    
40
        public DefaultThreadSafeDialogs() {
41
                this.rootComponent = null;
42
                this.statusbar = null;
43
        }
44

    
45
        public DefaultThreadSafeDialogs(Component rootComponent,
46
                        NewStatusBar statusbar) {
47
                this.statusbar = statusbar;
48
                this.rootComponent = rootComponent;
49
        }
50

    
51
        private Component getRootComponent() {
52
                if (this.rootComponent == null) {
53
                        try {
54
                                this.rootComponent = (MDIFrame) PluginServices.getMainFrame();
55
                        } catch (Throwable t) {
56
                                // Ignore and return null
57
                        }
58
                }
59
                return this.rootComponent;
60
        }
61

    
62
        private NewStatusBar getStatusbar() {
63
                if (this.statusbar == null) {
64
                        this.statusbar = PluginServices.getMainFrame().getStatusBar();
65
                }
66
                return this.statusbar;
67
        }
68

    
69
        private void message(String message, int messageType) {
70
                this.getStatusbar().message(message, messageType);
71
        }
72

    
73
        private String translate(String message) {
74
                return translate(message, null);
75
        }
76

    
77
        private String translate(String message, String[] args) {
78
                String msg = message;
79
                if (msg == null) {
80
                        msg = "";
81
                }
82
                if (msg.startsWith("_")) {
83
                        msg = org.gvsig.i18n.Messages.getText(msg, args);
84
                        if (msg == null) {
85
                                msg = "_" + message.replace("_", " ");
86
                        }
87
                }
88
                return msg;
89
        }
90

    
91
        public int confirmDialog(final String message, final String title, final int optionType,
92
                        final int messageType) {
93
                RunnableWithParameters runnable = new RunnableWithParameters() {
94
                        public void run() {
95
                                this.returnValue = JOptionPane.showConfirmDialog(
96
                                                getRootComponent(), message,title, optionType, messageType);
97
                        }
98
                };
99
                if (SwingUtilities.isEventDispatchThread()) {
100
                        runnable.run();
101
                } else {
102
                        try {
103
                                SwingUtilities.invokeAndWait(runnable);
104
                        } catch (Exception e) {
105
                                logger.info("Can't show input dialog '" + message + "'.", e);
106
                        }
107
                }
108
                return (Integer) runnable.getReturnValue();
109
        }
110

    
111
        public String inputDialog(final String message, final String title, final int messageType,
112
                        final String initialValue) {
113
                // inputDialog dlg = new inputDialog();
114
                // return dlg.show(translate(message), translate(title), messageType,
115
                // initialValue);
116
                //
117
                RunnableWithParameters runnable = new RunnableWithParameters() {
118
                        public void run() {
119
                                this.returnValue = JOptionPane.showInputDialog(
120
                                                getRootComponent(), translate(message),
121
                                                translate(title),
122
                                                messageType, null, null,
123
                                                initialValue);
124
                        }
125
                };
126
                if (SwingUtilities.isEventDispatchThread()) {
127
                        runnable.run();
128
                } else {
129
                        try {
130
                                SwingUtilities.invokeAndWait(runnable);
131
                        } catch (Exception e) {
132
                                logger.info("Can't show input dialog '" + message + "'.", e);
133
                        }
134
                }
135
                return (String) runnable.getReturnValue();
136
        }
137

    
138
        public String inputDialog(final String message, final String title) {
139
                RunnableWithParameters runnable = new RunnableWithParameters() {
140
                        public void run() {
141
                                this.returnValue = JOptionPane.showInputDialog(
142
                                                getRootComponent(), (String) translate(message),
143
                                                translate(title),
144
                                                JOptionPane.QUESTION_MESSAGE, null, null, null);
145
                        }
146
                };
147
                if (SwingUtilities.isEventDispatchThread()) {
148
                        runnable.run();
149
                } else {
150
                        try {
151
                                SwingUtilities.invokeAndWait(runnable);
152
                        } catch (Exception e) {
153
                                logger.info("Can't show input dialog '" + message + "'.", e);
154
                        }
155
                }
156
                return (String) runnable.getReturnValue();
157
        }
158

    
159
        public void messageDialog(String message, String title, int messageType) {
160
                messageDialog(message, null, title, messageType);
161
        }
162

    
163
        public void messageDialog(final String message, final String messageArgs[],
164
                        final String title, final int messageType) {
165
                if (!SwingUtilities.isEventDispatchThread()) {
166
                        try {
167
                                SwingUtilities.invokeAndWait(new Runnable() {
168
                                        public void run() {
169
                                                messageDialog(message, messageArgs, title, messageType);
170
                                        }
171
                                });
172
                        } catch (Exception e) {
173
                                logger.info("Can't show message dialog '" + message
174
                                                + "'. redirect to status bar", e);
175
                                this.message(message, messageType);
176
                        }
177
                        return;
178
                }
179

    
180
                if (message == null) {
181
                        logger.info("message if null, message dialog not show.");
182
                        return;
183
                }
184
                JOptionPane.showMessageDialog(getRootComponent(),
185
                                translate(message, messageArgs), translate(title), messageType);
186
        }
187

    
188
        public void showDialog(final Component contents, final String title) {
189
                if (SwingUtilities.isEventDispatchThread()) {
190
                        final DialogWindow window = new DialogWindow();
191
                        window.setDialogFlag();
192
                        window.setContents(contents, title);
193
                        MDIManager manager = PluginServices.getMDIManager();
194
                        manager.addWindow(window, GridBagConstraints.CENTER);
195
                } else {
196
                        final DialogWindow window = new DialogWindow();
197
                        SwingUtilities.invokeLater(new Runnable() {
198
                                public void run() {
199
                                        window.setContents(contents, title);
200
                                        MDIManager manager = PluginServices.getMDIManager();
201
                                        manager.addWindow(window, GridBagConstraints.CENTER);
202
                                }
203
                        });
204
                        try {
205
                                synchronized (window) {
206
                                        if (contents instanceof CancellableTask) {
207
                                                while( contents.isVisible()  ) {
208
                                                        if( ((CancellableTask)contents).isCancellationRequested() ) {
209
                                                                SwingUtilities.invokeLater(new Runnable() {
210
                                                                        public void run() {
211
                                                                                contents.setVisible(false);
212
                                                                        }
213
                                                                });
214
                                                        }
215
                                                        window.wait(10000);
216
                                                }
217
                                        } else {
218
                                                while( contents.isVisible() ) {
219
                                                        window.wait();
220
                                                }
221
                                        }
222
                                }
223
                        } catch (InterruptedException e) {
224
                                logger.info("showDialog can wait to close dialog.", e);
225
                        }
226
                }
227
        }
228

    
229
        class DialogWindow extends JPanel implements IWindow, ComponentListener {
230

    
231
                /**
232
                 * 
233
                 */
234
                private static final long serialVersionUID = 6283975319620714565L;
235
                protected WindowInfo windowInfo;
236
                protected Object profile;
237
                protected Component contents;
238
                private int code = 0;
239

    
240
                public DialogWindow() {
241
                        code = WindowInfo.RESIZABLE | WindowInfo.MAXIMIZABLE
242
                                        | WindowInfo.ICONIFIABLE;
243
                        profile = WindowInfo.DIALOG_PROFILE;
244
                }
245

    
246
                public void setDialogFlag() {
247
                        code |= WindowInfo.MODALDIALOG;
248
                }
249

    
250
                public void setContents(Component contents, String title) {
251
                        this.windowInfo = new WindowInfo(code);
252
                        this.windowInfo.setTitle(title);
253

    
254
                        this.contents = contents;
255

    
256
                        Dimension size = this.contents.getPreferredSize();
257
                        this.windowInfo.setHeight(size.height);
258
                        this.windowInfo.setWidth(size.width);
259

    
260
                        this.setLayout(new BorderLayout());
261
                        this.add(this.contents, BorderLayout.CENTER);
262

    
263
                        this.contents.addComponentListener(this);
264
                }
265

    
266
                public WindowInfo getWindowInfo() {
267
                        return this.windowInfo;
268
                }
269

    
270
                public Object getWindowProfile() {
271
                        return this.profile;
272
                }
273

    
274
                public void componentHidden(ComponentEvent arg0) {
275
                        // Close window when hide contents panel.
276
                        MDIManager manager = PluginServices.getMDIManager();
277
                        manager.closeWindow(this);
278
                        if ((code & WindowInfo.MODALDIALOG) == 0) {
279
                                synchronized (this) {
280
                                        this.notifyAll();
281
                                }
282
                        }
283
                }
284

    
285
                public void componentMoved(ComponentEvent arg0) {
286
                        // Do nothing
287
                }
288

    
289
                public void componentResized(ComponentEvent arg0) {
290
                        // Do nothing
291
                }
292

    
293
                public void componentShown(ComponentEvent arg0) {
294
                        // Do nothing
295
                }
296

    
297
        }
298

    
299
        public Component createComponent(final Class<? extends Component> theClass,
300
                        final Object... parameters) {
301
                return createComponentWithParams(theClass, parameters);
302
        }
303

    
304
        public Component createComponentWithParams(
305
                        final Class<? extends Component> theClass, final Object[] parameters) {
306
                final Class<?>[] parameterTypes = new Class<?>[parameters.length];
307
                for (int i = 0; i < parameters.length; i++) {
308
                        parameterTypes[i] = parameters[i].getClass();
309
                }
310
                final Component component;
311
                final Constructor<?> constructor;
312
                try {
313
                        constructor = theClass.getConstructor(parameterTypes);
314
                } catch (Exception e) {
315
                        throw new IllegalArgumentException(e);
316
                }
317
                if (SwingUtilities.isEventDispatchThread()) {
318
                        try {
319
                                component = (Component) constructor.newInstance(parameters);
320
                        } catch (Exception e) {
321
                                throw new IllegalArgumentException(e);
322
                        }
323
                } else {
324
                        try {
325
                                RunnableWithParameters runnable = new RunnableWithParameters(parameters) {
326
                                        public void run() {
327
                                                Constructor<?> cons = constructor;
328
                                                try {
329
                                                        this.returnValue = cons.newInstance(parameters.toArray());
330
                                                } catch (Exception e) {
331
                                                        String msg ="Can't create instance of components, constructor="+cons.toString()+", parameters="+this.parameters.toString()+"."; 
332
                                                        logger.info(msg,e);
333
                                                        throw new IllegalArgumentException(e);
334
                                                }
335
                                        }
336
                                };
337
                                SwingUtilities.invokeAndWait(runnable);
338
                                component = (Component) runnable.getReturnValue();
339
                        } catch (Exception e) {
340
                                throw new IllegalArgumentException(e);
341
                        }
342
                }
343
                return component;
344
        }
345

    
346
        public File[] showChooserDialog(
347
                        final String title,
348
                        final int type, // SAVE_DIALOG / OPEN_DIALOG
349
                        final int selectionMode, //    JFileChooser.FILES_ONLY, JFileChooser.DIRECTORIES_ONLY, JFileChooser.FILES_AND_DIRECTORIES
350
                        final boolean multiselection, 
351
                        final File initialPath,
352
                        final FileFilter filter,
353
                        final boolean fileHidingEnabled
354
                        ) {
355
                RunnableWithParameters runnable = new RunnableWithParameters() {
356
                        public void run() {
357
                                JFileChooser fc = new JFileChooser();
358
                                fc.setDialogTitle(title);
359
                                fc.setDialogType(type);
360
                                fc.setFileSelectionMode(selectionMode);
361
                                fc.setMultiSelectionEnabled(multiselection);
362
                                fc.setCurrentDirectory(initialPath);
363
                                fc.setFileFilter(filter);
364
                                fc.setFileHidingEnabled(fileHidingEnabled);
365
                                int r = JFileChooser.CANCEL_OPTION;
366
                                switch(type) {
367
                                case JFileChooser.SAVE_DIALOG:
368
                                        r = fc.showSaveDialog(getRootComponent());
369
                                        break;
370
                                case JFileChooser.OPEN_DIALOG:
371
                                default:
372
                                        r = fc.showOpenDialog(getRootComponent());
373
                                        break;
374
                                }
375
                                if( r != JFileChooser.APPROVE_OPTION ) {
376
                                        this.returnValue = null;
377
                                        return;
378
                                }
379
                                if( fc.isMultiSelectionEnabled() ) {
380
                                        this.returnValue = fc.getSelectedFiles();
381
                                } else {
382
                                        this.returnValue = new File[] { fc.getSelectedFile() };
383
                                }
384
                        }
385
                };
386
                if (SwingUtilities.isEventDispatchThread()) {
387
                        runnable.run();
388
                } else {
389
                        try {
390
                                SwingUtilities.invokeAndWait(runnable);
391
                        } catch (Exception e) {
392
                                logger.info("Can't show chooser dialog '" + title + "'.", e);
393
                        }
394
                }
395
                return (File[]) runnable.getReturnValue();
396
        }
397
        
398
        public File[] showOpenDirectoryDialog(String title, File initialPath) {
399
                return showChooserDialog(title, JFileChooser.OPEN_DIALOG, JFileChooser.DIRECTORIES_ONLY, false, initialPath, null, false);
400
        }
401

    
402
        
403
        public File[] showOpenFileDialog(String title, File initialPath) {
404
                return showChooserDialog(title, JFileChooser.OPEN_DIALOG, JFileChooser.FILES_ONLY, false, initialPath, null, false);
405
        }
406

    
407
        
408
        public File[] showSaveFileDialog(String title, File initialPath) {
409
                return showChooserDialog(title, JFileChooser.SAVE_DIALOG, JFileChooser.FILES_ONLY, false, initialPath, null, false);
410
        }
411

    
412
}