Skip to content

Commit f0f6a67

Browse files
authored
Merge pull request #92 from tferr/file-widget-fix
Fix Copy+Paste, Drag&Drop and Improve all Widgets
2 parents 62203fb + 4c9d7fe commit f0f6a67

19 files changed

Lines changed: 716 additions & 221 deletions

‎.github/workflows/build.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919
- name: Set up Java
2020
uses: actions/setup-java@v5
2121
with:
22-
java-version: '8'
22+
java-version: '17'
2323
distribution: 'zulu'
2424
cache: 'maven'
2525
- name: Set up CI environment

‎pom.xml‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@
102102
</ciManagement>
103103

104104
<properties>
105-
<scijava.jvm.version>8</scijava.jvm.version>
105+
<scijava.jvm.version>17</scijava.jvm.version>
106106
<package-name>org.scijava.ui.swing</package-name>
107107
<license.licenseName>bsd_2</license.licenseName>
108108
<license.copyrightOwners>SciJava developers.</license.copyrightOwners>
@@ -111,7 +111,7 @@
111111
<!-- NB: Deploy releases to the SciJava Maven repository. -->
112112
<releaseProfiles>sign,deploy-to-scijava</releaseProfiles>
113113

114-
<jdatepicker.version>1.3.2</jdatepicker.version>
114+
<jdatepicker.version>2.0.1</jdatepicker.version>
115115
<object-inspector.version>0.1</object-inspector.version>
116116

117117
<!-- TEMP: for Task#log(); drop once propagated to pom-scijava. -->
@@ -171,7 +171,7 @@
171171
<version>${object-inspector.version}</version>
172172
</dependency>
173173
<dependency>
174-
<groupId>net.sourceforge.jdatepicker</groupId>
174+
<groupId>org.jdatepicker</groupId>
175175
<artifactId>jdatepicker</artifactId>
176176
<version>${jdatepicker.version}</version>
177177
</dependency>

‎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/commands/CommandFinderPanel.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
import javax.swing.table.TableColumn;
6161
import javax.swing.table.TableColumnModel;
6262

63+
import com.formdev.flatlaf.FlatClientProperties;
6364
import net.miginfocom.swing.MigLayout;
6465

6566
import org.scijava.MenuEntry;
@@ -97,6 +98,7 @@ public CommandFinderPanel(final ModuleService moduleService,
9798
setPreferredSize(new Dimension(800, 600));
9899

99100
searchField = new JTextField(12);
101+
searchField.putClientProperty(FlatClientProperties.TEXT_FIELD_SHOW_CLEAR_BUTTON, true);
100102
commandsList = new JTable(20, CommandTableModel.COLUMN_COUNT);
101103
commandsList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
102104
commandsList.setRowSelectionAllowed(true);

‎src/main/java/org/scijava/ui/swing/commands/WatchEventsFrame.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,8 @@ private Iterable<DefaultMutableTreeNode> children(
397397
final DefaultMutableTreeNode node)
398398
{
399399
@SuppressWarnings("unchecked")
400-
final Enumeration<DefaultMutableTreeNode> en = node.children();
400+
final Enumeration<DefaultMutableTreeNode> en =
401+
(Enumeration<DefaultMutableTreeNode>) (Enumeration<?>) node.children();
401402
return new IteratorPlus<>(en);
402403
}
403404

‎src/main/java/org/scijava/ui/swing/console/ConsolePanel.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ private synchronized void initGui() {
137137
final FontMetrics fm = scrollPane.getFontMetrics(textPane.getFont());
138138
final int charWidth = fm.charWidth('a');
139139
final int lineHeight = fm.getHeight();
140-
scrollPane.setPreferredSize(new Dimension(charWidth * 80, lineHeight * 10)); //80 columns, 10 lines
140+
scrollPane.setPreferredSize(new Dimension(charWidth * 60, lineHeight * 15)); //60 columns, 15 lines
141141
scrollPane.getHorizontalScrollBar().setUnitIncrement(charWidth);
142142
scrollPane.getVerticalScrollBar().setUnitIncrement(2 * lineHeight);
143143
textPane.setComponentPopupMenu(initMenu());

‎src/main/java/org/scijava/ui/swing/console/ItemTextPane.java‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
package org.scijava.ui.swing.console;
3131

32+
import java.awt.Color;
3233
import java.awt.Font;
3334
import java.util.Collections;
3435
import java.util.Iterator;
@@ -37,6 +38,7 @@
3738
import javax.swing.JPopupMenu;
3839
import javax.swing.JScrollPane;
3940
import javax.swing.JTextPane;
41+
import javax.swing.UIManager;
4042
import javax.swing.text.AttributeSet;
4143
import javax.swing.text.BadLocationException;
4244
import javax.swing.text.DefaultStyledDocument;
@@ -68,7 +70,18 @@ class ItemTextPane {
6870

6971
private DocumentCalculator initialCalculator = null;
7072

71-
private JTextPane textPane = new JTextPane();
73+
private JTextPane textPane = new JTextPane() {
74+
75+
@Override
76+
public void updateUI() {
77+
super.updateUI();
78+
// NB: FlatLaf paints non-editable text with a gray inactive background.
79+
// Use the (plain, non-UIResource) tree background instead, so that the
80+
// log matches the sources panel. Re-applied on look and feel changes
81+
final Color bg = UIManager.getColor("Tree.background");
82+
if (bg != null) setBackground(new Color(bg.getRGB()));
83+
}
84+
};
7285

7386
private JScrollPane scrollPane = new JScrollPane(textPane);
7487

‎src/main/java/org/scijava/ui/swing/console/LogSourcesPanel.java‎

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
package org.scijava.ui.swing.console;
3131

3232
import java.awt.BorderLayout;
33+
import java.awt.Font;
3334
import java.awt.event.FocusEvent;
3435
import java.awt.event.FocusListener;
3536
import java.util.Arrays;
@@ -47,21 +48,16 @@
4748
import java.util.stream.Collectors;
4849
import java.util.stream.Stream;
4950

50-
import javax.swing.JButton;
51-
import javax.swing.JComponent;
52-
import javax.swing.JFrame;
53-
import javax.swing.JLabel;
54-
import javax.swing.JMenuItem;
55-
import javax.swing.JPanel;
56-
import javax.swing.JPopupMenu;
57-
import javax.swing.JScrollPane;
58-
import javax.swing.JTree;
51+
import javax.swing.*;
5952
import javax.swing.event.TreeSelectionEvent;
6053
import javax.swing.tree.DefaultMutableTreeNode;
6154
import javax.swing.tree.DefaultTreeCellRenderer;
6255
import javax.swing.tree.DefaultTreeModel;
6356
import javax.swing.tree.TreePath;
6457

58+
import com.formdev.flatlaf.FlatClientProperties;
59+
import com.formdev.flatlaf.FlatLaf;
60+
6561
import net.miginfocom.swing.MigLayout;
6662

6763
import org.scijava.log.LogLevel;
@@ -89,20 +85,31 @@ class LogSourcesPanel extends JPanel {
8985

9086
private Predicate<LogMessage> filter = message -> true;
9187
private Runnable changeListener = null;
92-
private List<LogSource> selected;
88+
private List<LogSource> selected = Collections.emptyList();
9389

9490
// -- constructor --
9591

96-
LogSourcesPanel(JButton reloadButton) {
92+
LogSourcesPanel(JButton reloadButton, Runnable toggleAction) {
9793
initMenu();
9894
initTreeView();
99-
JButton visibilityButton = initVisibilityButton();
100-
setLayout(new MigLayout("inset 0", "[grow]", "[][grow][]"));
101-
add(new JLabel("Log Sources:"), "grow, wrap");
95+
final JButton visibilityButton = initVisibilityButton();
96+
final JButton collapseButton = new JButton("\u2715");
97+
collapseButton.addActionListener(a -> toggleAction.run());
98+
99+
visibilityButton.putClientProperty(FlatClientProperties.STYLE_CLASS, "small");
100+
reloadButton.putClientProperty(FlatClientProperties.STYLE_CLASS, "small");
101+
collapseButton.putClientProperty(FlatClientProperties.STYLE_CLASS, "small");
102+
103+
final JToolBar toolBar = new JToolBar();
104+
toolBar.setFloatable(false);
105+
toolBar.add(collapseButton);
106+
toolBar.add(Box.createHorizontalGlue());
107+
toolBar.add(reloadButton);
108+
toolBar.add(visibilityButton);
109+
// tree takes the free height, the toolbar keeps its preferred height
110+
setLayout(new MigLayout("inset 0", "[grow]", "[grow][]"));
102111
add(new JScrollPane(tree), "grow, wrap");
103-
add(new JLabel(""), "split 3, grow");
104-
add(reloadButton);
105-
add(visibilityButton);
112+
add(toolBar, "growx");
106113
actionWhenFocusLost(() -> tree.clearSelection(), Arrays.asList(tree, visibilityButton));
107114
}
108115

@@ -129,11 +136,14 @@ private void initTreeView() {
129136
treeModel = new DefaultTreeModel(rootNode);
130137
tree = new JTree(treeModel);
131138
tree.setRootVisible(false);
132-
DefaultTreeCellRenderer cellRenderer = new DefaultTreeCellRenderer();
133-
cellRenderer.setIcon(null);
134-
cellRenderer.setLeafIcon(null);
135-
cellRenderer.setOpenIcon(null);
136-
tree.setCellRenderer(cellRenderer);
139+
// NB: FlatLaf trees have no default icons, so keep its own renderer
140+
if (!(UIManager.getLookAndFeel() instanceof FlatLaf)) {
141+
DefaultTreeCellRenderer cellRenderer = new DefaultTreeCellRenderer();
142+
cellRenderer.setIcon(null);
143+
cellRenderer.setLeafIcon(null);
144+
cellRenderer.setOpenIcon(null);
145+
tree.setCellRenderer(cellRenderer);
146+
}
137147
tree.setComponentPopupMenu(menu);
138148
tree.setEditable(false);
139149
tree.setShowsRootHandles(true);
@@ -146,7 +156,8 @@ private void selectionChanged(TreeSelectionEvent treeSelectionEvent) {
146156
}
147157

148158
private JButton initVisibilityButton() {
149-
JButton button = new JButton("Visibility");
159+
JButton button = new JButton("Visibility \u25BE");
160+
button.setToolTipText("Set which log levels are shown for the selected sources");
150161
button.addActionListener(a -> menu.show(button, 0, button.getHeight()));
151162
return button;
152163
}
@@ -340,7 +351,7 @@ static Level of(int x) {
340351

341352
public static void main(String... args) {
342353
JFrame frame = new JFrame();
343-
LogSourcesPanel logLevelPanel = new LogSourcesPanel(new JButton("dummy"));
354+
LogSourcesPanel logLevelPanel = new LogSourcesPanel(new JButton("dummy"), null);
344355
LogSource root = LogSource.newRoot();
345356
Set<LogSource> loggers = new HashSet<>(Arrays.asList(
346357
root.subSource("Hello:World"),

0 commit comments

Comments
 (0)