Skip to content

Commit 0938965

Browse files
authored
Merge pull request #82 from scijava/modernize2
Report run time and acknowledge completion when running scripts Plus minor adjustments
2 parents df0d173 + d81e38d commit 0938965

7 files changed

Lines changed: 433 additions & 123 deletions

File tree

‎src/main/java/org/scijava/ui/swing/script/FileSystemTree.java‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -872,6 +872,33 @@ private void addRoot(final String dir, final boolean checkIfChild) {
872872
getModel().insertNodeInto(new Node(dirPath), root, root.getChildCount());
873873
}
874874

875+
/**
876+
* Selects and scrolls to the given file or folder, expanding the tree as needed. If it is not under any
877+
* top-level folder, its parent folder is added as one.
878+
*
879+
* @return true if the file itself was found (false: only its folder, if any, was revealed)
880+
*/
881+
public boolean reveal(final File file) {
882+
if (file == null) return false;
883+
restoreHiddenTops();
884+
final String target = withSlash(file.getAbsolutePath());
885+
for (int attempt = 0; attempt < 2; attempt++) {
886+
final Node root = root();
887+
for (int i = 0; i < root.getChildCount(); i++) {
888+
final TreePath path = pathTo(root.getChildAt(i), target);
889+
if (path != null) {
890+
expandPath(path.getParentPath());
891+
setSelectionPath(path);
892+
scrollPathToVisible(path);
893+
return true;
894+
}
895+
}
896+
final File dir = file.isDirectory() ? file : file.getParentFile();
897+
if (attempt == 0 && dir != null) addRoot(dir.getAbsolutePath(), true);
898+
}
899+
return false; // e.g., hidden by the filter: the folder was revealed by addRoot()
900+
}
901+
875902
/** Expands nodes downwards until reaching dirPath. Returns null if not found */
876903
private TreePath pathTo(Node node, final String dirPath) {
877904
while (dirPath.startsWith(node.path)) {

‎src/main/java/org/scijava/ui/swing/script/FileSystemTreePanel.java‎

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,17 @@ class FileSystemTreePanel extends JPanel {
104104
bc.fill = GridBagConstraints.BOTH;
105105
bc.weightx = 1;
106106
bc.weighty = 0;
107-
add(searchField, bc);
107+
add(GuiUtils.getHeader("Files", this::showHelpMsg), bc);
108108
bc.gridy = 1;
109+
add(searchField, bc);
110+
bc.gridy = 2;
109111
bc.weightx = 1.0;
110112
bc.weighty = 1.0;
111113
bc.fill = GridBagConstraints.BOTH;
112114
final JScrollPane treePane = new JScrollPane(tree);
113115
GuiUtils.flattenScrollBars(treePane);
114116
add(treePane, bc);
115-
bc.gridy = 2;
117+
bc.gridy = 3;
116118
bc.weightx = 0;
117119
bc.weighty = 0;
118120
add(statusToolbar(), bc);
@@ -171,18 +173,9 @@ private JToolBar statusToolbar() {
171173
toolbar.setBackground(tree.getBackground());
172174
// Continue the outline of the tree's scroll pane (the latter provides the top edge)
173175
toolbar.setBorder(BorderFactory.createCompoundBorder(new SideBorder(), toolbar.getBorder()));
174-
final JToggleButton statusFast = new JToggleButton("Fast Mode");
175-
statusFast.putClientProperty(FlatClientProperties.STYLE_CLASS, "small");
176-
statusFast.setForeground(GuiUtils.secondaryColor());
176+
final JToggleButton statusFast = GuiUtils.smallToggleButton("Fast Mode");
177177
statusFast.setToolTipText("Fast mode for large folders: turns off live updates and the search of collapsed folders");
178-
statusFast.addActionListener(e -> {
179-
final boolean selected = statusFast.isSelected();
180-
setFastMode(selected);
181-
final Color fGrnd = (selected) ? UIManager.getColor("Tree.selectionForeground") : GuiUtils.secondaryColor();
182-
final Color bGrnd = (selected) ? UIManager.getColor("Tree.selectionBackground") : toolbar.getBackground();
183-
if (fGrnd!=null) statusFast.setForeground(fGrnd);
184-
if (bGrnd!=null) statusFast.setBackground(bGrnd);
185-
});
178+
statusFast.addActionListener(e -> setFastMode(statusFast.isSelected()));
186179
final JButton statusOptions = new JButton("Options");
187180
final FlatFileChooserDetailsViewIcon optionsIcon = new FlatFileChooserDetailsViewIcon();
188181
optionsIcon.setScale(.8f); // matches the font size set by the "small" style
@@ -193,10 +186,6 @@ private JToolBar statusToolbar() {
193186
toolbar.add(statusFast);
194187
toolbar.addSeparator();
195188
toolbar.add(statusOptions);
196-
toolbar.add(Box.createHorizontalGlue());
197-
final JButton statusHelp = GuiUtils.helpButton(null);
198-
statusHelp.addActionListener(e -> showHelpMsg());
199-
toolbar.add(statusHelp);
200189
return toolbar;
201190
}
202191

‎src/main/java/org/scijava/ui/swing/script/GuiUtils.java‎

Lines changed: 97 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,7 @@
2929

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

32-
import java.awt.Color;
33-
import java.awt.Component;
34-
import java.awt.Dimension;
35-
import java.awt.RenderingHints;
36-
import java.awt.Insets;
37-
import java.awt.Graphics2D;
38-
import java.awt.Graphics;
39-
import java.awt.FontMetrics;
40-
import java.awt.Point;
32+
import java.awt.*;
4133
import java.awt.event.InputEvent;
4234
import java.awt.event.KeyEvent;
4335
import java.awt.event.MouseAdapter;
@@ -327,6 +319,13 @@ static void addMenubarSeparator(final JMenu menu, final String header) {
327319
menu.addSeparator();
328320
}
329321
try {
322+
if (usesScreenMenuBar()) {
323+
// The native macOS menu bar only translates menu items, not arbitrary components
324+
final JMenuItem item = new JMenuItem(header);
325+
item.setEnabled(false);
326+
menu.add(item);
327+
return;
328+
}
330329
final JLabel label = new JLabel(" "+ header);
331330
label.setEnabled(false);
332331
label.setForeground(getDisabledComponentColor());
@@ -336,6 +335,11 @@ static void addMenubarSeparator(final JMenu menu, final String header) {
336335
}
337336
}
338337

338+
/** Whether menu bars are displayed in the native macOS menu bar (at the top of the screen) */
339+
private static boolean usesScreenMenuBar() {
340+
return PlatformUtils.isMac() && Boolean.parseBoolean(System.getProperty("apple.laf.useScreenMenuBar"));
341+
}
342+
339343
static void addPopupMenuSeparator(final JPopupMenu menu, final String header) {
340344
if (menu.getComponentCount() > 1) {
341345
menu.addSeparator();
@@ -474,7 +478,7 @@ static Color accentColor() {
474478
try {
475479
Color c = UIManager.getColor("Component.accentColor");
476480
if (c == null) c = UIManager.getColor("Component.focusColor");
477-
return (c == null) ? new Color(0x3E73B9) : new Color(c.getRGB()); // plain color: UIResource ones are reset
481+
return (c == null) ? new Color(0x3E73B9) : new Color(c.getRGB()); // plain color: UIResource ones are reset!?
478482
} catch (final Exception ignored) {
479483
// do nothing
480484
}
@@ -490,13 +494,43 @@ static boolean isDark(final Color c) {
490494
return (c.getRed() * 0.299 + c.getGreen() * 0.587 + c.getBlue() * 0.114) < 186;
491495
}
492496

493-
/** Small "+" button for the trailing area of a tabbed pane */
494-
static JButton newTabButton(final Runnable action) {
495-
final JButton b = new JButton("+");
496-
b.setToolTipText("New file");
497-
b.setFocusable(false);
498-
b.putClientProperty(FlatClientProperties.BUTTON_TYPE, FlatClientProperties.BUTTON_TYPE_TOOLBAR_BUTTON);
499-
b.addActionListener(e -> action.run());
497+
/** Small toggle button that looks like a tree selection when selected */
498+
static JToggleButton smallToggleButton(final String text) {
499+
return smallToggleButton(text, null);
500+
}
501+
502+
/**
503+
* As {@link #smallToggleButton(String)}.
504+
*
505+
* @param extraStyle Optional. FlatLaf style properties to add to the ones set here. NB: Setting the
506+
* style property of the button later would replace them
507+
*/
508+
static JToggleButton smallToggleButton(final String text, final String extraStyle) {
509+
return smallToggleButton(text, extraStyle, true);
510+
}
511+
512+
/**
513+
* As {@link #smallToggleButton(String, String)}.
514+
*
515+
* @param secondary If true, the text of the unselected button is in secondary color. If false, in
516+
* the default (primary) color of buttons
517+
*/
518+
static JToggleButton smallToggleButton(final String text, final String extraStyle, final boolean secondary) {
519+
final JToggleButton b = new JToggleButton(text);
520+
b.putClientProperty(FlatClientProperties.STYLE_CLASS, "small");
521+
// NB: setBackground() would not do, as FlatLaf darkens it
522+
b.putClientProperty(FlatClientProperties.STYLE,
523+
"selectedBackground: $Tree.selectionBackground; toolbar.selectedBackground: $Tree.selectionBackground"
524+
+ (extraStyle == null || extraStyle.isEmpty() ? "" : "; " + extraStyle));
525+
// NB: Foregrounds must be set after the style: applying it resets them
526+
final Runnable updateForeground = () -> {
527+
final Color sel = UIManager.getColor("Tree.selectionForeground");
528+
final Color rest = secondary ? secondaryColor() : UIManager.getColor("Button.foreground");
529+
final Color fGrnd = (b.isSelected() && sel != null) ? sel : rest;
530+
if (fGrnd != null) b.setForeground(fGrnd);
531+
};
532+
updateForeground.run();
533+
b.addItemListener(e -> updateForeground.run());
500534
return b;
501535
}
502536

@@ -505,27 +539,27 @@ static JButton ellipsisButton(final Runnable action) {
505539
b.setFocusable(false);
506540
b.putClientProperty(FlatClientProperties.BUTTON_TYPE, FlatClientProperties.BUTTON_TYPE_TOOLBAR_BUTTON);
507541
if (action != null) b.addActionListener(e -> action.run());
508-
b.setForeground(secondaryColor()); // the icon follows the foreground. Set last: style updates reset UI foregrounds
542+
// NBL he icon follows the foreground. To change its caller setForground() needs to be Set last
509543
return b;
510544
}
511545

512-
513-
/** Small round "?" button, in the style of the editor's other help buttons */
514-
static JButton helpButton(final Runnable action) {
515-
return helpButton(action, .75f, true);
516-
}
517-
518-
private static JButton helpButton(final Runnable action, final float scale, final boolean borderless) {
546+
/**
547+
* Round help "?" button
548+
*
549+
* @param sizeToComponent Optional. If not null, the icon will be scaled to height of this component.
550+
*/
551+
static JButton helpButton(final Runnable action, final Component sizeToComponent) {
519552
final FlatHelpButtonIcon icon = new FlatHelpButtonIcon();
520-
icon.setScale(scale);
521553
icon.applyStyleProperty("questionMarkColor", secondaryColor());
522554
icon.applyStyleProperty("background", UIManager.getColor("Panel.background"));
523555
final JButton b = new JButton(icon);
524556
b.setFocusable(false);
525557
b.putClientProperty(FlatClientProperties.BUTTON_TYPE, FlatClientProperties.BUTTON_TYPE_TOOLBAR_BUTTON);
526-
if (borderless) {
527-
b.putClientProperty("JButton.buttonType", "borderless");
558+
if (sizeToComponent != null) {
559+
b.setMargin(null);
528560
b.setBorder(null);
561+
b.putClientProperty("JButton.buttonType", "borderless"); // probably not needed?
562+
icon.setScale(Icons.scaleToFont(icon, sizeToComponent));
529563
}
530564
if (action != null) b.addActionListener(e -> action.run());
531565
return b;
@@ -581,15 +615,7 @@ static void installSplitHandle(final JComponent handle, final JSplitPane split)
581615
split.setDividerSize(active ? 0 : regularSize);
582616
handle.setCursor(active ? java.awt.Cursor.getPredefinedCursor(java.awt.Cursor.N_RESIZE_CURSOR) : null);
583617
};
584-
// children must not inherit the resize cursor
585-
final java.util.function.Consumer<java.awt.Container>[] resetCursors = new java.util.function.Consumer[1];
586-
resetCursors[0] = parent -> {
587-
for (final java.awt.Component child : parent.getComponents()) {
588-
if (!child.isCursorSet()) child.setCursor(java.awt.Cursor.getDefaultCursor());
589-
if (child instanceof java.awt.Container) resetCursors[0].accept((java.awt.Container) child);
590-
}
591-
};
592-
resetCursors[0].accept(handle);
618+
defaultCursorForInteractive(handle);
593619
update.run();
594620
split.addPropertyChangeListener(JSplitPane.ORIENTATION_PROPERTY, evt -> update.run());
595621
final int[] grab = new int[1];
@@ -611,6 +637,20 @@ public void mouseDragged(final java.awt.event.MouseEvent e) {
611637
});
612638
}
613639

640+
/**
641+
* Components that handle mouse events themselves must not inherit the resize cursor of a
642+
* split handle. Labels, glue and gaps do (they move the split)
643+
*/
644+
private static void defaultCursorForInteractive(final java.awt.Container parent) {
645+
for (final java.awt.Component child : parent.getComponents()) {
646+
final boolean interactive = child instanceof AbstractButton
647+
|| child instanceof javax.swing.text.JTextComponent || child instanceof JComboBox
648+
|| child instanceof JSpinner || child instanceof JList || child instanceof JSlider;
649+
if (interactive && !child.isCursorSet()) child.setCursor(java.awt.Cursor.getDefaultCursor());
650+
if (child instanceof java.awt.Container) defaultCursorForInteractive((java.awt.Container) child);
651+
}
652+
}
653+
614654
static void collapseAllTreeNodes(final JTree tree) {
615655
final int row1 = (tree.isRootVisible()) ? 1 : 0;
616656
for (int i = row1; i < tree.getRowCount(); i++)
@@ -631,6 +671,8 @@ static JTabbedPane getJTabbedPane() {
631671
static JTabbedPane getJTabbedPane(final int initialPlacement) {
632672
final JTabbedPane tabbed = new JTabbedPane();
633673
tabbed.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
674+
tabbed.putClientProperty(FlatClientProperties.TABBED_PANE_TAB_AREA_ALIGNMENT,
675+
FlatClientProperties.TABBED_PANE_ALIGN_LEADING);
634676
tabbed.putClientProperty(FlatClientProperties.TABBED_PANE_TABS_POPUP_POLICY, "asNeeded"); // overflow dropdown
635677
tabbed.putClientProperty(FlatClientProperties.TABBED_PANE_SCROLL_BUTTONS_POLICY, "never"); // do not show arrows
636678
tabbed.putClientProperty(FlatClientProperties.TABBED_PANE_TAB_ROTATION,
@@ -693,6 +735,24 @@ static JTabbedPane getJTabbedPane(final int initialPlacement) {
693735
return tabbed;
694736
}
695737

738+
/**
739+
* Assembles a secondary pane heading
740+
* @param text the header text
741+
* @param help optional An action to be attached to an option help button to the right of the heading
742+
* @return a small header in secondary color
743+
*/
744+
static JComponent getHeader(final String text, final Runnable help) {
745+
final JLabel label = new JLabel(text);
746+
label.putClientProperty(FlatClientProperties.STYLE_CLASS, "small");
747+
label.setForeground(GuiUtils.secondaryColor());
748+
final JPanel panel = new JPanel(new BorderLayout());
749+
panel.setBorder(BorderFactory.createEmptyBorder(0, 2, 0, 0));
750+
panel.setOpaque(false);
751+
panel.add(label, BorderLayout.CENTER);
752+
if (help != null) panel.add(helpButton(help, label), BorderLayout.EAST);
753+
return panel;
754+
}
755+
696756
private static String placementName(final int placement) {
697757
switch (placement) {
698758
case JTabbedPane.LEFT: return "Left";

‎src/main/java/org/scijava/ui/swing/script/Icons.java‎

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,22 @@ static Icon ellipsisVertical() {
7777
return new GlyphIcon(ELLIPSIS_VERTICAL, null, null, false, 0, 0, 640, 640, 18);
7878
}
7979

80-
/** Small error icon for status messages */
81-
static Icon error() {
82-
final FlatAbstractIcon icon = new FlatOptionPaneErrorIcon();
83-
icon.setScale(.35f);
84-
return icon;
80+
/**
81+
* Error icon
82+
* @param sizeToComponent Optional. If not null, the icon will be scaled to height of this component.
83+
*/
84+
static Icon error(final Component sizeToComponent) {
85+
final FlatAbstractIcon errorIcon = new FlatOptionPaneErrorIcon();
86+
if (sizeToComponent != null)
87+
errorIcon.setScale(scaleToFont(errorIcon, sizeToComponent));
88+
return errorIcon;
89+
}
90+
91+
/** Scale that makes the icon as tall as the text of the given component */
92+
static float scaleToFont(final FlatAbstractIcon icon, final Component ref) {
93+
assert(icon != null && ref != null);
94+
final FontMetrics fm = ref.getFontMetrics(ref.getFont());
95+
return fm.getHeight() / (float) icon.getIconHeight(); // icon created at scale 1
8596
}
8697

8798
/** Folder icon for the Files side tab (follows the component foreground) */
@@ -137,7 +148,7 @@ static Icon stop(final boolean disabled) {
137148
static Icon trash(final boolean disabled) {
138149
return new GlyphIcon(
139150
"M232.7 69.9C237.1 56.8 249.3 48 263.1 48L377 48C390.8 48 403 56.8 407.4 69.9L416 96L512 96C529.7 96 544 110.3 544 128C544 145.7 529.7 160 512 160L128 160C110.3 160 96 145.7 96 128C96 110.3 110.3 96 128 96L224 96L232.7 69.9zM128 208L512 208L512 512C512 547.3 483.3 576 448 576L192 576C156.7 576 128 547.3 128 512L128 208zM216 272C202.7 272 192 282.7 192 296L192 488C192 501.3 202.7 512 216 512C229.3 512 240 501.3 240 488L240 296C240 282.7 229.3 272 216 272zM320 272C306.7 272 296 282.7 296 296L296 488C296 501.3 306.7 512 320 512C333.3 512 344 501.3 344 488L344 296C344 282.7 333.3 272 320 272zM424 272C410.7 272 400 282.7 400 296L400 488C400 501.3 410.7 512 424 512C437.3 512 448 501.3 448 488L448 296C448 282.7 437.3 272 424 272z",
140-
new Color(0x6E6E6E), new Color(0xA9A9A9), disabled);
151+
null, null, disabled);
141152
}
142153

143154
/**

‎src/main/java/org/scijava/ui/swing/script/NotesPanel.java‎

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
import javax.swing.event.DocumentListener;
4747
import javax.swing.text.JTextComponent;
4848

49-
import com.formdev.flatlaf.FlatClientProperties;
5049
import org.fife.ui.rtextarea.ClipboardHistory;
5150
import org.scijava.prefs.PrefService;
5251

@@ -109,8 +108,8 @@ protected void paintComponent(final java.awt.Graphics g) {
109108
@Override
110109
protected void paintComponent(final java.awt.Graphics g) {
111110
super.paintComponent(g);
112-
if (getDocument().getLength() == 0) GuiUtils.paintEmptyHint(g, this, "Jot down snippets",
113-
"They are saved between sessions");
111+
if (getDocument().getLength() == 0) GuiUtils.paintEmptyHint(g, this, "Jot down notes or snippets",
112+
"Saved between sessions");
114113
}
115114
};
116115
scratch.setLineWrap(prefs.getBoolean(prefsOwner, PREFS_WRAP, true));
@@ -121,7 +120,7 @@ protected void paintComponent(final java.awt.Graphics g) {
121120

122121
// Scratchpad
123122
final JPanel pane1 = new JPanel(new BorderLayout());
124-
final JLabel scratchHeader = header("Scratchpad");
123+
final JComponent scratchHeader = GuiUtils.getHeader("Scratchpad", null);
125124
pane1.add(scratchHeader, BorderLayout.NORTH);
126125
pane1.add(scratchPane, BorderLayout.CENTER);
127126

@@ -131,7 +130,7 @@ protected void paintComponent(final java.awt.Graphics g) {
131130
final JScrollPane listPane = new JScrollPane(list);
132131
GuiUtils.flattenScrollBars(listPane);
133132
final JPanel pane2 = new JPanel(new BorderLayout());
134-
final JLabel clipboardHeader = header("Clipboard History");
133+
final JComponent clipboardHeader = GuiUtils.getHeader("Clipboard History", null);
135134
pane2.add(clipboardHeader, BorderLayout.NORTH);
136135
pane2.add(listPane, BorderLayout.CENTER);
137136

@@ -281,14 +280,6 @@ void refresh() {
281280
list.setSelectedValue(selected, true);
282281
}
283282

284-
private JLabel header(final String text) {
285-
final JLabel label = new JLabel(text);
286-
label.setBorder(BorderFactory.createEmptyBorder(2, 2,0,0));
287-
label.putClientProperty(FlatClientProperties.STYLE_CLASS, "small");
288-
label.setForeground(GuiUtils.secondaryColor());
289-
return label;
290-
}
291-
292283
private void maybePopup(final MouseEvent e) {
293284
if (!e.isPopupTrigger()) return;
294285
final int idx = list.locationToIndex(e.getPoint());

0 commit comments

Comments
 (0)