Skip to content

Commit b4d919a

Browse files
committed
CommandPalette: Fixes and code cleanup
- Fix unlisted actions from action map - Fix table with zero height under flatlaf - Fix duplicated entries - Add ability to register non-menu commands - Add entry to rebuild index - Highlight recordable actions - Remove hacks no longer needed with flatlaf - Misc improvements - Code cleanup
1 parent 4dc775d commit b4d919a

File tree

8 files changed

+265
-233
lines changed

8 files changed

+265
-233
lines changed

src/main/java/org/scijava/ui/swing/script/CommandPalette.java

Lines changed: 180 additions & 96 deletions
Large diffs are not rendered by default.

src/main/java/org/scijava/ui/swing/script/EditorPane.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,19 +324,22 @@ private JMenu geSyntaxForNoneLang() {
324324
private JMenuItem getSyntaxItem(final ButtonGroup bg, final String label, final String syntaxId) {
325325
final JRadioButtonMenuItem item = new JRadioButtonMenuItem(label);
326326
bg.add(item);
327-
item.setActionCommand(syntaxId);
328327
item.addActionListener(e -> {
329328
if (getCurrentLanguage() == null) {
330329
setSyntaxEditingStyle(syntaxId);
331330
} else {
332-
log.error("[BUG] Unknown state: Non-executable syntaxes cannot be applied to valid languages");
331+
error("Non-executable syntaxes can only be applied when scripting language is 'None'.");
333332
bg.getElements().nextElement().setSelected(true); //select none
334333
setSyntaxEditingStyle(SYNTAX_STYLE_NONE);
335334
}
336335
});
337336
return item;
338337
}
339338

339+
void error(final String message) {
340+
JOptionPane.showMessageDialog(this, message, "Error", JOptionPane.ERROR_MESSAGE);
341+
}
342+
340343
private void updateNoneLangSyntaxMenu(final ScriptLanguage language) {
341344
noneLangSyntaxMenu.setEnabled(language == null);
342345
if (language == null) {

src/main/java/org/scijava/ui/swing/script/FileFunctions.java

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,14 @@ public List<String> extractSourceJar(final String path, final File workspace)
9393
final File baseDirectory = new File(workspace, baseName);
9494

9595
final List<String> result = new ArrayList<>();
96-
final JarFile jar = new JarFile(path);
97-
for (final JarEntry entry : Collections.list(jar.entries())) {
98-
final String name = entry.getName();
99-
if (name.endsWith(".class") || name.endsWith("/")) continue;
100-
final String destination = baseDirectory + name;
101-
copyTo(jar.getInputStream(entry), destination);
102-
result.add(destination);
96+
try (JarFile jar = new JarFile(path)) {
97+
for (final JarEntry entry : Collections.list(jar.entries())) {
98+
final String name = entry.getName();
99+
if (name.endsWith(".class") || name.endsWith("/")) continue;
100+
final String destination = baseDirectory + name;
101+
copyTo(jar.getInputStream(entry), destination);
102+
result.add(destination);
103+
}
103104
}
104105
return result;
105106
}
@@ -223,7 +224,7 @@ public String findSourcePath(final String className, final File workspace) {
223224
}
224225
if (paths.size() == 1) return new File(workspace, paths.get(0))
225226
.getAbsolutePath();
226-
final String[] names = paths.toArray(new String[paths.size()]);
227+
//final String[] names = paths.toArray(new String[paths.size()]);
227228
final JFileChooser chooser = new JFileChooser(workspace);
228229
chooser.setDialogTitle("Choose path");
229230
if (chooser.showOpenDialog(parent) != JFileChooser.APPROVE_OPTION) return null;
@@ -304,8 +305,7 @@ public List<String> getResourceList(String url) {
304305
final String prefix = url.substring(bang + 2);
305306
final int prefixLength = prefix.length();
306307

307-
try {
308-
final JarFile jar = new JarFile(jarURL);
308+
try (JarFile jar = new JarFile(jarURL)) {
309309
final Enumeration<JarEntry> e = jar.entries();
310310
while (e.hasMoreElements()) {
311311
final JarEntry entry = e.nextElement();
@@ -504,11 +504,11 @@ public void openInGitweb(final File file, final File gitDirectory,
504504
final int line)
505505
{
506506
if (file == null || gitDirectory == null) {
507-
error("No file or git directory");
507+
parent.error("No file or git directory.");
508508
return;
509509
}
510510
final String url = getGitwebURL(file, gitDirectory, line);
511-
if (url == null) error("Could not get gitweb URL for " + file);
511+
if (url == null) parent.error("Could not get gitweb URL for " + file + ".");
512512
else try {
513513
parent.getPlatformService().open(new URL(url));
514514
}
@@ -599,11 +599,6 @@ protected String stripSuffix(final String string, final String suffix) {
599599
return string;
600600
}
601601

602-
protected boolean error(final String message) {
603-
JOptionPane.showMessageDialog(parent, message);
604-
return false;
605-
}
606-
607602
/**
608603
* @deprecated Use {@link FileUtils#findResources(String, String, File)}
609604
* instead.

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

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,10 @@
3232
import java.awt.Color;
3333
import java.awt.Desktop;
3434
import java.awt.Dimension;
35-
import java.awt.Font;
3635
import java.awt.FontMetrics;
37-
import java.awt.Graphics2D;
3836
import java.awt.GridBagConstraints;
3937
import java.awt.GridBagLayout;
4038
import java.awt.Insets;
41-
import java.awt.RenderingHints;
4239
import java.awt.event.FocusAdapter;
4340
import java.awt.event.FocusEvent;
4441
import java.awt.event.KeyAdapter;
@@ -53,7 +50,6 @@
5350
import java.util.stream.Collectors;
5451

5552
import javax.swing.BorderFactory;
56-
import javax.swing.FocusManager;
5753
import javax.swing.JButton;
5854
import javax.swing.JCheckBoxMenuItem;
5955
import javax.swing.JFileChooser;
@@ -62,7 +58,6 @@
6258
import javax.swing.JPanel;
6359
import javax.swing.JPopupMenu;
6460
import javax.swing.JScrollPane;
65-
import javax.swing.JTextField;
6661
import javax.swing.UIManager;
6762
import javax.swing.tree.DefaultMutableTreeNode;
6863
import javax.swing.tree.TreePath;
@@ -199,7 +194,7 @@ public void keyPressed(final KeyEvent ke) {
199194
return field;
200195
}
201196

202-
private JButton thinButton(final String label) {
197+
private JButton thinButton(final String label, final float factor) {
203198
final JButton b = new JButton(label);
204199
try {
205200
if ("com.apple.laf.AquaLookAndFeel".equals(UIManager.getLookAndFeel().getClass().getName())) {
@@ -209,10 +204,9 @@ private JButton thinButton(final String label) {
209204
b.setBorder(BorderFactory.createEmptyBorder());
210205
b.setMargin(new Insets(0, 2, 0, 2));
211206
} else {
212-
final double FACTOR = .25;
213207
final Insets insets = b.getMargin();
214-
b.setMargin(new Insets(insets.top, (int) (insets.left * FACTOR), insets.bottom,
215-
(int) (insets.right * FACTOR)));
208+
b.setMargin(new Insets((int) (insets.top * factor), (int) (insets.left * factor),
209+
(int) (insets.bottom * factor), (int) (insets.right * factor)));
216210
}
217211
} catch (final Exception ignored) {
218212
// do nothing
@@ -225,7 +219,7 @@ private JButton thinButton(final String label) {
225219
}
226220

227221
private JButton addDirectoryButton() {
228-
final JButton add_directory = thinButton("<HTML>&#43;");
222+
final JButton add_directory = thinButton("+", .25f);
229223
add_directory.setToolTipText("Add a directory");
230224
add_directory.addActionListener(e -> {
231225
final String folders = tree.getTopLevelFoldersString();
@@ -257,7 +251,7 @@ private JButton addDirectoryButton() {
257251
}
258252

259253
private JButton removeDirectoryButton() {
260-
final JButton remove_directory = thinButton("<HTML>&#8722;");
254+
final JButton remove_directory = thinButton("−", .25f);
261255
remove_directory.setToolTipText("Remove a top-level directory");
262256
remove_directory.addActionListener(e -> {
263257
final TreePath p = tree.getSelectionPath();
@@ -279,7 +273,7 @@ private JButton removeDirectoryButton() {
279273
}
280274

281275
private JButton searchOptionsButton() {
282-
final JButton options = thinButton("<HTML>&#8942;");
276+
final JButton options = thinButton("⋮", .05f);
283277
options.setToolTipText("Filtering options");
284278
final JPopupMenu popup = new JPopupMenu();
285279
final JCheckBoxMenuItem jcbmi1 = new JCheckBoxMenuItem("Case Sensitive", isCaseSensitive());
@@ -437,23 +431,22 @@ private void setRegexEnabled(final boolean b) {
437431
searchField.update();
438432
}
439433

440-
private class SearchField extends JTextField {
434+
private class SearchField extends TextEditor.TextFieldWithPlaceholder {
441435

442436
private static final long serialVersionUID = 7004232238240585434L;
443437
private static final String REGEX_HOLDER = "[?*]";
444438
private static final String CASE_HOLDER = "[Aa]";
445439
private static final String DEF_HOLDER = "File filter... ";
446440

447441
SearchField() {
448-
super();
449442
try {
450443
// make sure pane is large enough to display placeholders
451444
final FontMetrics fm = getFontMetrics(getFont());
452445
final FontRenderContext frc = fm.getFontRenderContext();
453446
final String buf = CASE_HOLDER + REGEX_HOLDER + DEF_HOLDER;
454447
final Rectangle2D rect = getFont().getStringBounds(buf, frc);
455448
final int prefWidth = (int) rect.getWidth();
456-
setColumns(prefWidth / super.getColumnWidth());
449+
setColumns(prefWidth / getColumnWidth());
457450
} catch (final Exception ignored) {
458451
// do nothing
459452
}
@@ -464,21 +457,13 @@ void update() {
464457
}
465458

466459
@Override
467-
protected void paintComponent(final java.awt.Graphics g) {
468-
super.paintComponent(g);
469-
if (super.getText().isEmpty() && !(FocusManager.getCurrentKeyboardFocusManager().getFocusOwner() == this)) {
470-
final Graphics2D g2 = (Graphics2D) g.create();
471-
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
472-
g2.setColor(Color.GRAY);
473-
g2.setFont(getFont().deriveFont(Font.ITALIC));
474-
final StringBuilder sb = new StringBuilder(DEF_HOLDER);
475-
if (isCaseSensitive())
476-
sb.append(CASE_HOLDER);
477-
if (isRegexEnabled())
478-
sb.append(REGEX_HOLDER);
479-
g2.drawString(sb.toString(), 4, g2.getFontMetrics().getHeight());
480-
g2.dispose();
481-
}
460+
String getPlaceholder() {
461+
final StringBuilder sb = new StringBuilder(DEF_HOLDER);
462+
if (isCaseSensitive())
463+
sb.append(CASE_HOLDER);
464+
if (isRegexEnabled())
465+
sb.append(REGEX_HOLDER);
466+
return sb.toString();
482467
}
483468
}
484469
}

src/main/java/org/scijava/ui/swing/script/FindAndReplaceDialog.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ final public RSyntaxTextArea getTextArea() {
151151
return getTextAreaAsEditorPane();
152152
}
153153

154+
@SuppressWarnings("deprecation")
154155
@Override
155156
public void show(final boolean replace) {
156157
if (replace && restrictToConsole)
@@ -166,7 +167,7 @@ public void show(final boolean replace) {
166167
searchField.selectAll();
167168
replaceField.selectAll();
168169
getRootPane().setDefaultButton(findNext);
169-
show();
170+
show(); // cannot call setVisible
170171
}
171172

172173
private JTextField createField(final String name, final Container container,

src/main/java/org/scijava/ui/swing/script/OutlineTreePanel.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class OutlineTreePanel extends JScrollPane {
6767
private boolean sorted;
6868
private boolean major;
6969

70-
OutlineTreePanel(final TextEditor editor) {
70+
OutlineTreePanel() {
7171
super();
7272
fontSize = getFont().getSize();
7373
setViewportView(new UnsupportedLangTree());

0 commit comments

Comments
 (0)