Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/main/java/org/scijava/ui/swing/script/FileSystemTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,33 @@ private void addRoot(final String dir, final boolean checkIfChild) {
getModel().insertNodeInto(new Node(dirPath), root, root.getChildCount());
}

/**
* Selects and scrolls to the given file or folder, expanding the tree as needed. If it is not under any
* top-level folder, its parent folder is added as one.
*
* @return true if the file itself was found (false: only its folder, if any, was revealed)
*/
public boolean reveal(final File file) {
if (file == null) return false;
restoreHiddenTops();
final String target = withSlash(file.getAbsolutePath());
for (int attempt = 0; attempt < 2; attempt++) {
final Node root = root();
for (int i = 0; i < root.getChildCount(); i++) {
final TreePath path = pathTo(root.getChildAt(i), target);
if (path != null) {
expandPath(path.getParentPath());
setSelectionPath(path);
scrollPathToVisible(path);
return true;
}
}
final File dir = file.isDirectory() ? file : file.getParentFile();
if (attempt == 0 && dir != null) addRoot(dir.getAbsolutePath(), true);
}
return false; // e.g., hidden by the filter: the folder was revealed by addRoot()
}

/** Expands nodes downwards until reaching dirPath. Returns null if not found */
private TreePath pathTo(Node node, final String dirPath) {
while (dirPath.startsWith(node.path)) {
Expand Down
23 changes: 6 additions & 17 deletions src/main/java/org/scijava/ui/swing/script/FileSystemTreePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,17 @@ class FileSystemTreePanel extends JPanel {
bc.fill = GridBagConstraints.BOTH;
bc.weightx = 1;
bc.weighty = 0;
add(searchField, bc);
add(GuiUtils.getHeader("Files", this::showHelpMsg), bc);
bc.gridy = 1;
add(searchField, bc);
bc.gridy = 2;
bc.weightx = 1.0;
bc.weighty = 1.0;
bc.fill = GridBagConstraints.BOTH;
final JScrollPane treePane = new JScrollPane(tree);
GuiUtils.flattenScrollBars(treePane);
add(treePane, bc);
bc.gridy = 2;
bc.gridy = 3;
bc.weightx = 0;
bc.weighty = 0;
add(statusToolbar(), bc);
Expand Down Expand Up @@ -171,18 +173,9 @@ private JToolBar statusToolbar() {
toolbar.setBackground(tree.getBackground());
// Continue the outline of the tree's scroll pane (the latter provides the top edge)
toolbar.setBorder(BorderFactory.createCompoundBorder(new SideBorder(), toolbar.getBorder()));
final JToggleButton statusFast = new JToggleButton("Fast Mode");
statusFast.putClientProperty(FlatClientProperties.STYLE_CLASS, "small");
statusFast.setForeground(GuiUtils.secondaryColor());
final JToggleButton statusFast = GuiUtils.smallToggleButton("Fast Mode");
statusFast.setToolTipText("Fast mode for large folders: turns off live updates and the search of collapsed folders");
statusFast.addActionListener(e -> {
final boolean selected = statusFast.isSelected();
setFastMode(selected);
final Color fGrnd = (selected) ? UIManager.getColor("Tree.selectionForeground") : GuiUtils.secondaryColor();
final Color bGrnd = (selected) ? UIManager.getColor("Tree.selectionBackground") : toolbar.getBackground();
if (fGrnd!=null) statusFast.setForeground(fGrnd);
if (bGrnd!=null) statusFast.setBackground(bGrnd);
});
statusFast.addActionListener(e -> setFastMode(statusFast.isSelected()));
final JButton statusOptions = new JButton("Options");
final FlatFileChooserDetailsViewIcon optionsIcon = new FlatFileChooserDetailsViewIcon();
optionsIcon.setScale(.8f); // matches the font size set by the "small" style
Expand All @@ -193,10 +186,6 @@ private JToolBar statusToolbar() {
toolbar.add(statusFast);
toolbar.addSeparator();
toolbar.add(statusOptions);
toolbar.add(Box.createHorizontalGlue());
final JButton statusHelp = GuiUtils.helpButton(null);
statusHelp.addActionListener(e -> showHelpMsg());
toolbar.add(statusHelp);
return toolbar;
}

Expand Down
134 changes: 97 additions & 37 deletions src/main/java/org/scijava/ui/swing/script/GuiUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,7 @@

package org.scijava.ui.swing.script;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.RenderingHints;
import java.awt.Insets;
import java.awt.Graphics2D;
import java.awt.Graphics;
import java.awt.FontMetrics;
import java.awt.Point;
import java.awt.*;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
Expand Down Expand Up @@ -327,6 +319,13 @@ static void addMenubarSeparator(final JMenu menu, final String header) {
menu.addSeparator();
}
try {
if (usesScreenMenuBar()) {
// The native macOS menu bar only translates menu items, not arbitrary components
final JMenuItem item = new JMenuItem(header);
item.setEnabled(false);
menu.add(item);
return;
}
final JLabel label = new JLabel(" "+ header);
label.setEnabled(false);
label.setForeground(getDisabledComponentColor());
Expand All @@ -336,6 +335,11 @@ static void addMenubarSeparator(final JMenu menu, final String header) {
}
}

/** Whether menu bars are displayed in the native macOS menu bar (at the top of the screen) */
private static boolean usesScreenMenuBar() {
return PlatformUtils.isMac() && Boolean.parseBoolean(System.getProperty("apple.laf.useScreenMenuBar"));
}

static void addPopupMenuSeparator(final JPopupMenu menu, final String header) {
if (menu.getComponentCount() > 1) {
menu.addSeparator();
Expand Down Expand Up @@ -474,7 +478,7 @@ static Color accentColor() {
try {
Color c = UIManager.getColor("Component.accentColor");
if (c == null) c = UIManager.getColor("Component.focusColor");
return (c == null) ? new Color(0x3E73B9) : new Color(c.getRGB()); // plain color: UIResource ones are reset
return (c == null) ? new Color(0x3E73B9) : new Color(c.getRGB()); // plain color: UIResource ones are reset!?
} catch (final Exception ignored) {
// do nothing
}
Expand All @@ -490,13 +494,43 @@ static boolean isDark(final Color c) {
return (c.getRed() * 0.299 + c.getGreen() * 0.587 + c.getBlue() * 0.114) < 186;
}

/** Small "+" button for the trailing area of a tabbed pane */
static JButton newTabButton(final Runnable action) {
final JButton b = new JButton("+");
b.setToolTipText("New file");
b.setFocusable(false);
b.putClientProperty(FlatClientProperties.BUTTON_TYPE, FlatClientProperties.BUTTON_TYPE_TOOLBAR_BUTTON);
b.addActionListener(e -> action.run());
/** Small toggle button that looks like a tree selection when selected */
static JToggleButton smallToggleButton(final String text) {
return smallToggleButton(text, null);
}

/**
* As {@link #smallToggleButton(String)}.
*
* @param extraStyle Optional. FlatLaf style properties to add to the ones set here. NB: Setting the
* style property of the button later would replace them
*/
static JToggleButton smallToggleButton(final String text, final String extraStyle) {
return smallToggleButton(text, extraStyle, true);
}

/**
* As {@link #smallToggleButton(String, String)}.
*
* @param secondary If true, the text of the unselected button is in secondary color. If false, in
* the default (primary) color of buttons
*/
static JToggleButton smallToggleButton(final String text, final String extraStyle, final boolean secondary) {
final JToggleButton b = new JToggleButton(text);
b.putClientProperty(FlatClientProperties.STYLE_CLASS, "small");
// NB: setBackground() would not do, as FlatLaf darkens it
b.putClientProperty(FlatClientProperties.STYLE,
"selectedBackground: $Tree.selectionBackground; toolbar.selectedBackground: $Tree.selectionBackground"
+ (extraStyle == null || extraStyle.isEmpty() ? "" : "; " + extraStyle));
// NB: Foregrounds must be set after the style: applying it resets them
final Runnable updateForeground = () -> {
final Color sel = UIManager.getColor("Tree.selectionForeground");
final Color rest = secondary ? secondaryColor() : UIManager.getColor("Button.foreground");
final Color fGrnd = (b.isSelected() && sel != null) ? sel : rest;
if (fGrnd != null) b.setForeground(fGrnd);
};
updateForeground.run();
b.addItemListener(e -> updateForeground.run());
return b;
}

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


/** Small round "?" button, in the style of the editor's other help buttons */
static JButton helpButton(final Runnable action) {
return helpButton(action, .75f, true);
}

private static JButton helpButton(final Runnable action, final float scale, final boolean borderless) {
/**
* Round help "?" button
*
* @param sizeToComponent Optional. If not null, the icon will be scaled to height of this component.
*/
static JButton helpButton(final Runnable action, final Component sizeToComponent) {
final FlatHelpButtonIcon icon = new FlatHelpButtonIcon();
icon.setScale(scale);
icon.applyStyleProperty("questionMarkColor", secondaryColor());
icon.applyStyleProperty("background", UIManager.getColor("Panel.background"));
final JButton b = new JButton(icon);
b.setFocusable(false);
b.putClientProperty(FlatClientProperties.BUTTON_TYPE, FlatClientProperties.BUTTON_TYPE_TOOLBAR_BUTTON);
if (borderless) {
b.putClientProperty("JButton.buttonType", "borderless");
if (sizeToComponent != null) {
b.setMargin(null);
b.setBorder(null);
b.putClientProperty("JButton.buttonType", "borderless"); // probably not needed?
icon.setScale(Icons.scaleToFont(icon, sizeToComponent));
}
if (action != null) b.addActionListener(e -> action.run());
return b;
Expand Down Expand Up @@ -581,15 +615,7 @@ static void installSplitHandle(final JComponent handle, final JSplitPane split)
split.setDividerSize(active ? 0 : regularSize);
handle.setCursor(active ? java.awt.Cursor.getPredefinedCursor(java.awt.Cursor.N_RESIZE_CURSOR) : null);
};
// children must not inherit the resize cursor
final java.util.function.Consumer<java.awt.Container>[] resetCursors = new java.util.function.Consumer[1];
resetCursors[0] = parent -> {
for (final java.awt.Component child : parent.getComponents()) {
if (!child.isCursorSet()) child.setCursor(java.awt.Cursor.getDefaultCursor());
if (child instanceof java.awt.Container) resetCursors[0].accept((java.awt.Container) child);
}
};
resetCursors[0].accept(handle);
defaultCursorForInteractive(handle);
update.run();
split.addPropertyChangeListener(JSplitPane.ORIENTATION_PROPERTY, evt -> update.run());
final int[] grab = new int[1];
Expand All @@ -611,6 +637,20 @@ public void mouseDragged(final java.awt.event.MouseEvent e) {
});
}

/**
* Components that handle mouse events themselves must not inherit the resize cursor of a
* split handle. Labels, glue and gaps do (they move the split)
*/
private static void defaultCursorForInteractive(final java.awt.Container parent) {
for (final java.awt.Component child : parent.getComponents()) {
final boolean interactive = child instanceof AbstractButton
|| child instanceof javax.swing.text.JTextComponent || child instanceof JComboBox
|| child instanceof JSpinner || child instanceof JList || child instanceof JSlider;
if (interactive && !child.isCursorSet()) child.setCursor(java.awt.Cursor.getDefaultCursor());
if (child instanceof java.awt.Container) defaultCursorForInteractive((java.awt.Container) child);
}
}

static void collapseAllTreeNodes(final JTree tree) {
final int row1 = (tree.isRootVisible()) ? 1 : 0;
for (int i = row1; i < tree.getRowCount(); i++)
Expand All @@ -631,6 +671,8 @@ static JTabbedPane getJTabbedPane() {
static JTabbedPane getJTabbedPane(final int initialPlacement) {
final JTabbedPane tabbed = new JTabbedPane();
tabbed.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
tabbed.putClientProperty(FlatClientProperties.TABBED_PANE_TAB_AREA_ALIGNMENT,
FlatClientProperties.TABBED_PANE_ALIGN_LEADING);
tabbed.putClientProperty(FlatClientProperties.TABBED_PANE_TABS_POPUP_POLICY, "asNeeded"); // overflow dropdown
tabbed.putClientProperty(FlatClientProperties.TABBED_PANE_SCROLL_BUTTONS_POLICY, "never"); // do not show arrows
tabbed.putClientProperty(FlatClientProperties.TABBED_PANE_TAB_ROTATION,
Expand Down Expand Up @@ -693,6 +735,24 @@ static JTabbedPane getJTabbedPane(final int initialPlacement) {
return tabbed;
}

/**
* Assembles a secondary pane heading
* @param text the header text
* @param help optional An action to be attached to an option help button to the right of the heading
* @return a small header in secondary color
*/
static JComponent getHeader(final String text, final Runnable help) {
final JLabel label = new JLabel(text);
label.putClientProperty(FlatClientProperties.STYLE_CLASS, "small");
label.setForeground(GuiUtils.secondaryColor());
final JPanel panel = new JPanel(new BorderLayout());
panel.setBorder(BorderFactory.createEmptyBorder(0, 2, 0, 0));
panel.setOpaque(false);
panel.add(label, BorderLayout.CENTER);
if (help != null) panel.add(helpButton(help, label), BorderLayout.EAST);
return panel;
}

private static String placementName(final int placement) {
switch (placement) {
case JTabbedPane.LEFT: return "Left";
Expand Down
23 changes: 17 additions & 6 deletions src/main/java/org/scijava/ui/swing/script/Icons.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,22 @@ static Icon ellipsisVertical() {
return new GlyphIcon(ELLIPSIS_VERTICAL, null, null, false, 0, 0, 640, 640, 18);
}

/** Small error icon for status messages */
static Icon error() {
final FlatAbstractIcon icon = new FlatOptionPaneErrorIcon();
icon.setScale(.35f);
return icon;
/**
* Error icon
* @param sizeToComponent Optional. If not null, the icon will be scaled to height of this component.
*/
static Icon error(final Component sizeToComponent) {
final FlatAbstractIcon errorIcon = new FlatOptionPaneErrorIcon();
if (sizeToComponent != null)
errorIcon.setScale(scaleToFont(errorIcon, sizeToComponent));
return errorIcon;
}

/** Scale that makes the icon as tall as the text of the given component */
static float scaleToFont(final FlatAbstractIcon icon, final Component ref) {
assert(icon != null && ref != null);
final FontMetrics fm = ref.getFontMetrics(ref.getFont());
return fm.getHeight() / (float) icon.getIconHeight(); // icon created at scale 1
}

/** Folder icon for the Files side tab (follows the component foreground) */
Expand Down Expand Up @@ -137,7 +148,7 @@ static Icon stop(final boolean disabled) {
static Icon trash(final boolean disabled) {
return new GlyphIcon(
"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",
new Color(0x6E6E6E), new Color(0xA9A9A9), disabled);
null, null, disabled);
}

/**
Expand Down
17 changes: 4 additions & 13 deletions src/main/java/org/scijava/ui/swing/script/NotesPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
import javax.swing.event.DocumentListener;
import javax.swing.text.JTextComponent;

import com.formdev.flatlaf.FlatClientProperties;
import org.fife.ui.rtextarea.ClipboardHistory;
import org.scijava.prefs.PrefService;

Expand Down Expand Up @@ -109,8 +108,8 @@ protected void paintComponent(final java.awt.Graphics g) {
@Override
protected void paintComponent(final java.awt.Graphics g) {
super.paintComponent(g);
if (getDocument().getLength() == 0) GuiUtils.paintEmptyHint(g, this, "Jot down snippets",
"They are saved between sessions");
if (getDocument().getLength() == 0) GuiUtils.paintEmptyHint(g, this, "Jot down notes or snippets",
"Saved between sessions");
}
};
scratch.setLineWrap(prefs.getBoolean(prefsOwner, PREFS_WRAP, true));
Expand All @@ -121,7 +120,7 @@ protected void paintComponent(final java.awt.Graphics g) {

// Scratchpad
final JPanel pane1 = new JPanel(new BorderLayout());
final JLabel scratchHeader = header("Scratchpad");
final JComponent scratchHeader = GuiUtils.getHeader("Scratchpad", null);
pane1.add(scratchHeader, BorderLayout.NORTH);
pane1.add(scratchPane, BorderLayout.CENTER);

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

Expand Down Expand Up @@ -281,14 +280,6 @@ void refresh() {
list.setSelectedValue(selected, true);
}

private JLabel header(final String text) {
final JLabel label = new JLabel(text);
label.setBorder(BorderFactory.createEmptyBorder(2, 2,0,0));
label.putClientProperty(FlatClientProperties.STYLE_CLASS, "small");
label.setForeground(GuiUtils.secondaryColor());
return label;
}

private void maybePopup(final MouseEvent e) {
if (!e.isPopupTrigger()) return;
final int idx = list.locationToIndex(e.getPoint());
Expand Down
Loading
Loading