Skip to content

Commit df0d173

Browse files
authored
Merge pull request #81 from scijava/modernize
Revamp the Script Editor
2 parents a02419e + 7808e25 commit df0d173

30 files changed

Lines changed: 7154 additions & 3095 deletions

‎.gitignore‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@
66
/.idea/
77
/.serena
88
/script-editor.iml
9+
10+
.DS_Store

‎pom.xml‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,18 @@
140140
</ciManagement>
141141

142142
<properties>
143+
<scijava.jvm.version>11</scijava.jvm.version>
143144
<main-class>org.scijava.ui.swing.script.Main</main-class>
144145
<package-name>org.scijava.ui.swing.script</package-name>
145146
<license.licenseName>bsd_2</license.licenseName>
146147
<license.copyrightOwners>SciJava developers.</license.copyrightOwners>
147148

148149
<!-- NB: Deploy releases to the SciJava Maven repository. -->
149150
<releaseProfiles>sign,deploy-to-scijava</releaseProfiles>
151+
152+
<!-- Dependency versions -->
153+
<flatlaf.version>3.8</flatlaf.version> <!-- scijava-pom 45.1.0: 3.7.2 -->
154+
<rsyntaxtextarea.version>4.0.1</rsyntaxtextarea.version> <!-- scijava-pom 45.1.0: 3.6.3 -->
150155
</properties>
151156

152157
<dependencies>
@@ -163,11 +168,16 @@
163168
<groupId>org.scijava</groupId>
164169
<artifactId>scripting-java</artifactId>
165170
</dependency>
171+
<dependency>
172+
<groupId>org.scijava</groupId>
173+
<artifactId>scijava-ui-swing</artifactId>
174+
</dependency>
166175

167176
<!-- Third-party dependencies -->
168177
<dependency>
169178
<groupId>com.fifesoft</groupId>
170179
<artifactId>rsyntaxtextarea</artifactId>
180+
<version>${rsyntaxtextarea.version}</version>
171181
</dependency>
172182
<dependency>
173183
<groupId>com.fifesoft</groupId>
@@ -193,11 +203,6 @@
193203
<artifactId>parsington</artifactId>
194204
<scope>test</scope>
195205
</dependency>
196-
<dependency>
197-
<groupId>org.scijava</groupId>
198-
<artifactId>scijava-ui-swing</artifactId>
199-
<scope>test</scope>
200-
</dependency>
201206
<dependency>
202207
<groupId>org.scijava</groupId>
203208
<artifactId>scripting-groovy</artifactId>
@@ -206,6 +211,7 @@
206211
<dependency>
207212
<groupId>com.formdev</groupId>
208213
<artifactId>flatlaf</artifactId>
214+
<version>3.8</version>
209215
</dependency>
210216
</dependencies>
211217

Lines changed: 240 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,240 @@
1+
/*
2+
* #%L
3+
* Script Editor and Interpreter for SciJava script languages.
4+
* %%
5+
* Copyright (C) 2009 - 2026 SciJava developers.
6+
* %%
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions are met:
9+
*
10+
* 1. Redistributions of source code must retain the above copyright notice,
11+
* this list of conditions and the following disclaimer.
12+
* 2. Redistributions in binary form must reproduce the above copyright notice,
13+
* this list of conditions and the following disclaimer in the documentation
14+
* and/or other materials provided with the distribution.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
20+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26+
* POSSIBILITY OF SUCH DAMAGE.
27+
* #L%
28+
*/
29+
30+
31+
package org.scijava.ui.swing.script;
32+
33+
import java.awt.BorderLayout;
34+
import java.awt.Component;
35+
import java.awt.Graphics;
36+
import java.awt.event.KeyAdapter;
37+
import java.awt.event.KeyEvent;
38+
import java.awt.event.MouseAdapter;
39+
import java.awt.event.MouseEvent;
40+
import java.util.ArrayList;
41+
import java.util.List;
42+
import java.util.function.Consumer;
43+
import java.util.function.Supplier;
44+
45+
import javax.swing.DefaultListCellRenderer;
46+
import javax.swing.DefaultListModel;
47+
import javax.swing.JList;
48+
import javax.swing.JMenuItem;
49+
import javax.swing.JPanel;
50+
import javax.swing.JPopupMenu;
51+
import javax.swing.JScrollPane;
52+
import javax.swing.ListSelectionModel;
53+
import javax.swing.SwingUtilities;
54+
import javax.swing.Timer;
55+
import javax.swing.border.EmptyBorder;
56+
import javax.swing.text.BadLocationException;
57+
58+
/**
59+
* A "Bookmarks" side panel listing the bookmarks of all open editor tabs.
60+
* Double-clicking (or pressing Enter on) an entry jumps to it. Bookmarks are
61+
* toggled in the editor gutter, so the list is kept up to date by a cheap check
62+
* that only runs while the panel is visible.
63+
*/
64+
class BookmarksPanel extends JPanel {
65+
66+
private static final long serialVersionUID = 1L;
67+
private static final int REFRESH_DELAY_MS = 1500;
68+
private static final int TEXT_MAX_CHARS = 100;
69+
70+
private final Supplier<List<Bookmark>> source;
71+
private final Consumer<Bookmark> jumper;
72+
private final DefaultListModel<Bookmark> model = new DefaultListModel<>();
73+
private final List<String> signature = new ArrayList<>();
74+
private final JList<Bookmark> list;
75+
76+
/**
77+
* @param source supplies the current bookmarks of all tabs (in display order)
78+
* @param jumper moves the editor to the given bookmark
79+
*/
80+
BookmarksPanel(final Supplier<List<Bookmark>> source,
81+
final Consumer<Bookmark> jumper)
82+
{
83+
super(new BorderLayout());
84+
this.source = source;
85+
this.jumper = jumper;
86+
87+
list = new JList<>(model) {
88+
89+
private static final long serialVersionUID = 1L;
90+
91+
@Override
92+
protected void paintComponent(final Graphics g) {
93+
super.paintComponent(g);
94+
if (model.isEmpty()) GuiUtils.paintEmptyHint(g, this, "No bookmarks",
95+
"Click next to a line number to bookmark it");
96+
}
97+
};
98+
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
99+
list.setCellRenderer(new Renderer());
100+
final JScrollPane pane = new JScrollPane(list);
101+
GuiUtils.flattenScrollBars(pane);
102+
add(pane, BorderLayout.CENTER);
103+
104+
list.addMouseListener(new MouseAdapter() {
105+
106+
@Override
107+
public void mouseClicked(final MouseEvent e) {
108+
if (SwingUtilities.isLeftMouseButton(e) && e.getClickCount() == 2) {
109+
final int idx = list.locationToIndex(e.getPoint());
110+
if (idx >= 0 && list.getCellBounds(idx, idx).contains(e.getPoint()))
111+
jumper.accept(model.get(idx));
112+
}
113+
}
114+
115+
@Override
116+
public void mousePressed(final MouseEvent e) {
117+
maybePopup(e);
118+
}
119+
120+
@Override
121+
public void mouseReleased(final MouseEvent e) {
122+
maybePopup(e);
123+
}
124+
});
125+
list.addKeyListener(new KeyAdapter() {
126+
127+
@Override
128+
public void keyPressed(final KeyEvent e) {
129+
final Bookmark sel = list.getSelectedValue();
130+
if (sel == null) return;
131+
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
132+
jumper.accept(sel);
133+
e.consume();
134+
}
135+
else if (e.getKeyCode() == KeyEvent.VK_DELETE) {
136+
remove(sel);
137+
e.consume();
138+
}
139+
}
140+
});
141+
142+
refresh();
143+
final Timer poll = new Timer(REFRESH_DELAY_MS, e -> {
144+
if (isShowing()) refresh();
145+
});
146+
poll.start();
147+
}
148+
149+
/** Brings the list up to date (cheap if nothing changed). */
150+
void refresh() {
151+
final List<Bookmark> current = source.get();
152+
final List<String> sig = new ArrayList<>(current.size());
153+
for (final Bookmark bk : current)
154+
sig.add(System.identityHashCode(bk.tab) + ":" + bk.getLineNumber() + ":" +
155+
lineText(bk));
156+
if (sig.equals(signature)) return;
157+
signature.clear();
158+
signature.addAll(sig);
159+
final int selectedLine = list.getSelectedValue() == null ? -1 : list
160+
.getSelectedIndex();
161+
model.clear();
162+
for (final Bookmark bk : current)
163+
model.addElement(bk);
164+
if (selectedLine >= 0 && selectedLine < model.size()) list
165+
.setSelectedIndex(selectedLine);
166+
}
167+
168+
private void remove(final Bookmark bk) {
169+
bk.tab.editorPane.toggleBookmark(bk.getLineNumber());
170+
refresh();
171+
}
172+
173+
private void clearAll() {
174+
for (final Bookmark bk : new ArrayList<>(source.get()))
175+
bk.tab.editorPane.toggleBookmark(bk.getLineNumber());
176+
refresh();
177+
}
178+
179+
private void maybePopup(final MouseEvent e) {
180+
if (!e.isPopupTrigger()) return;
181+
final int idx = list.locationToIndex(e.getPoint());
182+
if (idx >= 0 && list.getCellBounds(idx, idx).contains(e.getPoint()))
183+
list.setSelectedIndex(idx);
184+
final Bookmark sel = list.getSelectedValue();
185+
final JPopupMenu menu = new JPopupMenu();
186+
JMenuItem mi = new JMenuItem("Go to Bookmark");
187+
mi.setEnabled(sel != null);
188+
mi.addActionListener(a -> jumper.accept(sel));
189+
menu.add(mi);
190+
mi = new JMenuItem("Remove Bookmark");
191+
mi.setEnabled(sel != null);
192+
mi.addActionListener(a -> remove(sel));
193+
menu.add(mi);
194+
menu.addSeparator();
195+
mi = new JMenuItem("Clear All Bookmarks");
196+
mi.setEnabled(!model.isEmpty());
197+
mi.addActionListener(a -> clearAll());
198+
menu.add(mi);
199+
menu.show(list, e.getX(), e.getY());
200+
}
201+
202+
/** The (trimmed) text of the bookmarked line, or an empty string. */
203+
private static String lineText(final Bookmark bk) {
204+
try {
205+
final int line = bk.getLineNumber();
206+
if (line < 0) return "";
207+
final int start = bk.tab.editorPane.getLineStartOffset(line);
208+
final int end = bk.tab.editorPane.getLineEndOffset(line);
209+
String s = bk.tab.editorPane.getText(start, end - start).trim()
210+
.replaceAll("\\s+", " ");
211+
if (s.length() > TEXT_MAX_CHARS) s = s.substring(0, TEXT_MAX_CHARS) +
212+
"...";
213+
return s;
214+
}
215+
catch (final BadLocationException | RuntimeException ignored) {
216+
return "";
217+
}
218+
}
219+
220+
private static class Renderer extends DefaultListCellRenderer {
221+
222+
private static final long serialVersionUID = 1L;
223+
224+
@Override
225+
public Component getListCellRendererComponent(final JList<?> l,
226+
final Object value, final int index, final boolean selected,
227+
final boolean focus)
228+
{
229+
final Bookmark bk = (Bookmark) value;
230+
final String name = bk.tab.editorPane.getFileName();
231+
final String text = lineText(bk);
232+
super.getListCellRendererComponent(l, (name == null ? "Untitled"
233+
: name) + ":" + (bk.getLineNumber() + 1) + (text.isEmpty() ? ""
234+
: " " + text), index, selected, focus);
235+
setBorder(new EmptyBorder(2, 6, 2, 6));
236+
setToolTipText(text.isEmpty() ? null : text);
237+
return this;
238+
}
239+
}
240+
}

0 commit comments

Comments
 (0)