Skip to content

Commit 457f9cd

Browse files
committed
File Widget: Fix Filechooser being displayed unaware of parent
1 parent 026ea99 commit 457f9cd

3 files changed

Lines changed: 73 additions & 10 deletions

File tree

‎src/main/java/org/scijava/ui/swing/AbstractSwingUI.java‎

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,20 @@ public SystemClipboard getSystemClipboard() {
137137

138138
@Override
139139
public File chooseFile(final File file, final String style) {
140+
return chooseFile((Component)null, file, style);
141+
}
142+
143+
/**
144+
* Like {@link #chooseFile(File, String)}, but the dialog is shown relative
145+
* to the given component.
146+
*
147+
* @param owner The component to center the dialog on. If {@code null}, the
148+
* application frame is used.
149+
*/
150+
public File chooseFile(final Component owner, final File file,
151+
final String style)
152+
{
153+
final Component parent = owner != null ? owner : appFrame;
140154
final File[] result = new File[1];
141155
try {
142156
// NB: We show the JFileChooser on the EDT because otherwise there could
@@ -148,10 +162,10 @@ public File chooseFile(final File file, final String style) {
148162
}
149163
final int rval;
150164
if (FileWidget.SAVE_STYLE.equals(style)) {
151-
rval = chooser.showSaveDialog(appFrame);
165+
rval = chooser.showSaveDialog(parent);
152166
}
153167
else { // default behavior
154-
rval = chooser.showOpenDialog(appFrame);
168+
rval = chooser.showOpenDialog(parent);
155169
}
156170
if (rval == JFileChooser.APPROVE_OPTION) {
157171
result[0] = chooser.getSelectedFile();
@@ -166,6 +180,20 @@ public File chooseFile(final File file, final String style) {
166180

167181
@Override
168182
public File[] chooseFiles(final File parent, final File[] files, final FileFilter filter, final String style) {
183+
return chooseFiles(null, parent, files, filter, style);
184+
}
185+
186+
/**
187+
* Like {@link #chooseFiles(File, File[], FileFilter, String)}, but the
188+
* dialog is shown relative to the given component.
189+
*
190+
* @param owner The component to center the dialog on. If {@code null}, the
191+
* application frame is used.
192+
*/
193+
public File[] chooseFiles(final Component owner, final File parent,
194+
final File[] files, final FileFilter filter, final String style)
195+
{
196+
final Component dialogParent = owner != null ? owner : appFrame;
169197
final File[][] result = new File[1][];
170198
try {
171199
// NB: We show the JFileChooser on the EDT because otherwise there could
@@ -202,7 +230,7 @@ public boolean accept(File f) {
202230
};
203231
chooser.setFileFilter(fileFilter);
204232
}
205-
int rval = chooser.showOpenDialog(appFrame);
233+
int rval = chooser.showOpenDialog(dialogParent);
206234
if (rval == JFileChooser.APPROVE_OPTION) {
207235
result[0] = chooser.getSelectedFiles();
208236
}

‎src/main/java/org/scijava/ui/swing/widget/SwingFileWidget.java‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,7 @@
5555
import javax.swing.event.DocumentListener;
5656

5757
import com.formdev.flatlaf.FlatClientProperties;
58-
import org.scijava.plugin.Parameter;
5958
import org.scijava.plugin.Plugin;
60-
import org.scijava.ui.UIService;
6159
import org.scijava.widget.FileListWidget;
6260
import org.scijava.widget.FileWidget;
6361
import org.scijava.widget.InputWidget;
@@ -73,9 +71,6 @@ public class SwingFileWidget extends SwingInputWidget<File> implements
7371
FileWidget<JPanel>, ActionListener, DocumentListener
7472
{
7573

76-
@Parameter
77-
private UIService uiService;
78-
7974
private JTextField path;
8075
private JButton browse;
8176

@@ -108,7 +103,7 @@ public void set(final WidgetModel model) {
108103

109104
getComponent().add(Box.createHorizontalStrut(3));
110105

111-
browse = new JButton("Browse");
106+
browse = new JButton("Browse...");
112107
setToolTip(browse);
113108
getComponent().add(browse);
114109
browse.addActionListener(this);
@@ -144,7 +139,7 @@ else if (model.isStyle(FileWidget.SAVE_STYLE)) {
144139
else {
145140
style = FileWidget.OPEN_STYLE;
146141
}
147-
file = uiService.chooseFile(file, style);
142+
file = chooseFile(file, style);
148143
if (file == null) return;
149144

150145
path.setText(file.getAbsolutePath());

‎src/main/java/org/scijava/ui/swing/widget/SwingInputWidget.java‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030
package org.scijava.ui.swing.widget;
3131

3232
import java.awt.Color;
33+
import java.awt.Window;
34+
import java.io.File;
35+
import java.io.FileFilter;
36+
import java.util.Arrays;
3337
import java.util.ArrayList;
3438
import java.util.HashSet;
3539
import java.util.List;
@@ -43,6 +47,7 @@
4347

4448
import org.scijava.ui.AbstractUIInputWidget;
4549
import org.scijava.ui.UserInterface;
50+
import org.scijava.ui.swing.AbstractSwingUI;
4651
import org.scijava.ui.swing.SwingUI;
4752
import org.scijava.widget.WidgetModel;
4853

@@ -98,6 +103,41 @@ protected UserInterface ui() {
98103

99104
// -- Helper methods --
100105

106+
/**
107+
* Asks the user for a file, with the dialog shown relative to this widget
108+
* rather than the application frame.
109+
*
110+
* @see org.scijava.ui.UserInterface#chooseFile(File, String)
111+
*/
112+
protected File chooseFile(final File file, final String style) {
113+
final Window parent = SwingUtilities.getWindowAncestor(getComponent());
114+
final UserInterface ui = ui();
115+
if (ui instanceof AbstractSwingUI) {
116+
return ((AbstractSwingUI) ui).chooseFile(parent, file, style);
117+
}
118+
return ui.chooseFile(file, style);
119+
}
120+
121+
/**
122+
* Asks the user for files, with the dialog shown relative to this widget
123+
* rather than the application frame.
124+
*
125+
* @see org.scijava.ui.UserInterface#chooseFiles(File, List, FileFilter,
126+
* String)
127+
*/
128+
protected List<File> chooseFiles(final File start, final List<File> files,
129+
final FileFilter filter, final String style)
130+
{
131+
final Window parent = SwingUtilities.getWindowAncestor(getComponent());
132+
final UserInterface ui = ui();
133+
if (ui instanceof AbstractSwingUI) {
134+
final File[] result = ((AbstractSwingUI) ui).chooseFiles(parent, start,
135+
files.toArray(new File[0]), filter, style);
136+
return result == null ? null : Arrays.asList(result);
137+
}
138+
return ui.chooseFiles(start, files, filter, style);
139+
}
140+
101141
/** Assigns the model's description as the given component's tool tip. */
102142
protected void setToolTip(final JComponent c) {
103143
tooltipped.add(c);

0 commit comments

Comments
 (0)