Repository navigation
Expand file tree
/
Copy pathAbstractSwingUI.java
More file actions
380 lines (325 loc) · 11.5 KB
/
Copy pathAbstractSwingUI.java
File metadata and controls
380 lines (325 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*
* #%L
* SciJava UI components for Java Swing.
* %%
* Copyright (C) 2010 - 2026 SciJava developers.
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* #L%
*/
package org.scijava.ui.swing;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileFilter;
import java.lang.reflect.InvocationTargetException;
import javax.swing.JFileChooser;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.WindowConstants;
import org.scijava.app.AppService;
import org.scijava.display.Display;
import org.scijava.event.EventService;
import org.scijava.log.LogService;
import org.scijava.menu.MenuService;
import org.scijava.menu.ShadowMenu;
import org.scijava.platform.event.AppMenusCreatedEvent;
import org.scijava.plugin.Parameter;
import org.scijava.thread.ThreadService;
import org.scijava.ui.AbstractUserInterface;
import org.scijava.ui.SystemClipboard;
import org.scijava.ui.UIService;
import org.scijava.ui.awt.AWTClipboard;
import org.scijava.ui.awt.AWTDropTargetEventDispatcher;
import org.scijava.ui.awt.AWTInputEventDispatcher;
import org.scijava.ui.awt.AWTWindowEventDispatcher;
import org.scijava.ui.console.ConsolePane;
import org.scijava.ui.swing.console.SwingConsolePane;
import org.scijava.ui.swing.laf.SwingLookAndFeelService;
import org.scijava.ui.swing.menu.SwingJMenuBarCreator;
import org.scijava.ui.swing.menu.SwingJPopupMenuCreator;
import org.scijava.ui.viewer.DisplayViewer;
import org.scijava.widget.FileListWidget;
import org.scijava.widget.FileWidget;
/**
* Abstract superclass for Swing-based user interfaces.
*
* @author Curtis Rueden
* @author Lee Kamentsky
* @author Barry DeZonia
* @author Grant Harris
*/
public abstract class AbstractSwingUI extends AbstractUserInterface implements
SwingUI
{
@Parameter
private AppService appService;
@Parameter
private EventService eventService;
@Parameter
private MenuService menuService;
@Parameter
private UIService uiService;
@Parameter(required = false)
private SwingLookAndFeelService lafService;
@Parameter
private ThreadService threadService;
@Parameter
private LogService log;
private SwingApplicationFrame appFrame;
private SwingToolBar toolBar;
private SwingStatusBar statusBar;
private SwingConsolePane consolePane;
private AWTClipboard systemClipboard;
// -- UserInterface methods --
@Override
public SwingApplicationFrame getApplicationFrame() {
return appFrame;
}
@Override
public SwingToolBar getToolBar() {
return toolBar;
}
@Override
public SwingStatusBar getStatusBar() {
return statusBar;
}
@Override
public SwingConsolePane getConsolePane() {
return consolePane;
}
@Override
public SystemClipboard getSystemClipboard() {
return systemClipboard;
}
@Override
public File chooseFile(final File file, final String style) {
return chooseFile((Component)null, file, style);
}
/**
* Like {@link #chooseFile(File, String)}, but the dialog is shown relative
* to the given component.
*
* @param owner The component to center the dialog on. If {@code null}, the
* application frame is used.
*/
public File chooseFile(final Component owner, final File file,
final String style)
{
final Component parent = owner != null ? owner : appFrame;
final File[] result = new File[1];
try {
// NB: We show the JFileChooser on the EDT because otherwise there could
// be a deadlock, particularly on macOS. See scijava/scijava-ui-swing#28.
threadService.invoke(() -> {
final JFileChooser chooser = new JFileChooser(file);
if (FileWidget.DIRECTORY_STYLE.equals(style)) {
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
}
final int rval;
if (FileWidget.SAVE_STYLE.equals(style)) {
rval = chooser.showSaveDialog(parent);
}
else { // default behavior
rval = chooser.showOpenDialog(parent);
}
if (rval == JFileChooser.APPROVE_OPTION) {
result[0] = chooser.getSelectedFile();
}
});
}
catch (final InvocationTargetException | InterruptedException exc) {
log.error(exc);
}
return result[0];
}
@Override
public File[] chooseFiles(final File parent, final File[] files, final FileFilter filter, final String style) {
return chooseFiles(null, parent, files, filter, style);
}
/**
* Like {@link #chooseFiles(File, File[], FileFilter, String)}, but the
* dialog is shown relative to the given component.
*
* @param owner The component to center the dialog on. If {@code null}, the
* application frame is used.
*/
public File[] chooseFiles(final Component owner, final File parent,
final File[] files, final FileFilter filter, final String style)
{
final Component dialogParent = owner != null ? owner : appFrame;
final File[][] result = new File[1][];
try {
// NB: We show the JFileChooser on the EDT because otherwise there could
// be a deadlock, particularly on macOS.
// See the {@link #chooseFile(File, String) chooseFile} method.
threadService.invoke(() -> {
final JFileChooser chooser = new JFileChooser(parent);
chooser.setMultiSelectionEnabled(true);
if (style.equals(FileListWidget.FILES_AND_DIRECTORIES)) {
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
}
else if (style.equals(FileListWidget.DIRECTORIES_ONLY)) {
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
}
else {
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
}
chooser.setSelectedFiles(files);
if (filter != null) {
javax.swing.filechooser.FileFilter fileFilter = new javax.swing.filechooser.FileFilter() {
@Override
public String getDescription() {
return filter.toString();
}
@Override
public boolean accept(File f) {
if (filter.accept(f)) return true;
// directories should always be displayed
// independent from selection mode
return f.isDirectory();
}
};
chooser.setFileFilter(fileFilter);
}
int rval = chooser.showOpenDialog(dialogParent);
if (rval == JFileChooser.APPROVE_OPTION) {
result[0] = chooser.getSelectedFiles();
}
});
}
catch (final InvocationTargetException | InterruptedException exc) {
log.error(exc);
}
return result[0];
}
@Override
public void showContextMenu(final String menuRoot, final Display<?> display,
final int x, final int y)
{
final ShadowMenu shadowMenu = menuService.getMenu(menuRoot);
final JPopupMenu popupMenu = new JPopupMenu();
new SwingJPopupMenuCreator().createMenus(shadowMenu, popupMenu);
final DisplayViewer<?> displayViewer = uiService.getDisplayViewer(display);
if (displayViewer != null) {
final Component invoker = (Component) displayViewer.getPanel();
popupMenu.show(invoker, x, y);
}
}
@Override()
public boolean requiresEDT() {
return true;
}
// -- Disposable methods --
@Override
public void dispose() {
if (appFrame != null) appFrame.dispose();
}
// -- Internal methods --
@Override
protected void createUI() {
// Before we create any UI components, initialize the Look and Feel.
if (lafService != null) lafService.initLookAndFeel();
// NB: Create the console first, because if the Look and Feel
// is busted, it will trigger an infinite loop during console
// initialization, which the SwingConsolePane is smart enough to
// catch and throw a RuntimeException in response, thereby
// avoiding any subsequent output shenanigans past this point.
if (!Boolean.getBoolean(ConsolePane.NO_CONSOLE_PROPERTY)) {
consolePane = new SwingConsolePane(getContext());
}
final JMenuBar menuBar = createMenus();
appFrame = new SwingApplicationFrame(appService.getApp().getTitle());
if (menuBar != null) appFrame.setJMenuBar(menuBar);
toolBar = new SwingToolBar(getContext());
statusBar = new SwingStatusBar(getContext());
systemClipboard = new AWTClipboard();
setupAppFrame();
super.createUI();
// NB: The following setup happens for both SDI and MDI frames.
appFrame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
appFrame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(final WindowEvent evt) {
appService.getApp().quit();
}
});
appFrame.getContentPane().add(toolBar, BorderLayout.NORTH);
appFrame.getContentPane().add(statusBar, BorderLayout.SOUTH);
// listen for input events on all components of the app frame
final AWTInputEventDispatcher inputDispatcher =
new AWTInputEventDispatcher(null, eventService);
appFrame.addEventDispatcher(inputDispatcher);
statusBar.addEventDispatcher(inputDispatcher);
// listen for window events on the app frame
final AWTWindowEventDispatcher windowDispatcher =
new AWTWindowEventDispatcher(eventService);
windowDispatcher.register(appFrame);
// listen for drag and drop events
final AWTDropTargetEventDispatcher dropTargetDispatcher =
new AWTDropTargetEventDispatcher(null, eventService);
dropTargetDispatcher.register(toolBar);
dropTargetDispatcher.register(statusBar);
dropTargetDispatcher.register(appFrame);
setupConsole();
appFrame.pack();
appFrame.setVisible(true);
}
/**
* Creates a {@link JMenuBar} from the master {@link ShadowMenu} structure.
*/
protected JMenuBar createMenus() {
final JMenuBar menuBar =
menuService.createMenus(new SwingJMenuBarCreator(), new JMenuBar());
final AppMenusCreatedEvent appMenusCreatedEvent =
new AppMenusCreatedEvent(menuBar);
eventService.publish(appMenusCreatedEvent);
if (appMenusCreatedEvent.isConsumed()) {
// something else (e.g., MacOSXPlatform) handled the menus
return null;
}
appMenusCreatedEvent.consume();
return menuBar;
}
protected JMenuBar createConsoleMenu() {
final JMenuBar menuBar = new JMenuBar();
final JMenu edit = new JMenu("Edit");
menuBar.add(edit);
final JMenuItem editClear = new JMenuItem("Clear");
editClear.addActionListener(e -> getConsolePane().clear());
edit.add(editClear);
return menuBar;
}
/**
* Configures the application frame for subclass-specific settings (e.g., SDI
* or MDI).
*/
protected abstract void setupAppFrame();
/**
* Configures the console for subclass-specific settings (e.g., SDI or MDI).
*/
protected abstract void setupConsole();
}