Skip to content

Commit f8e34b8

Browse files
hinermCopilot
andcommitted
Avoid duplicate extensions when creating scripts
Detect the script language from the supplied title before normalizing the filename. This lets the editor recognize an existing extension and prevents names such as Macro.ijm.ijm. Add regression coverage for already-suffixed filenames. Closes #78 Co-authored-by: Copilot <copilot@github.com>
1 parent db76156 commit f8e34b8

2 files changed

Lines changed: 62 additions & 5 deletions

File tree

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

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,38 @@
102102
import java.util.concurrent.ExecutionException;
103103
import java.util.jar.JarEntry;
104104
import java.util.jar.JarOutputStream;
105-
import java.util.zip.ZipException;
106105
import java.util.regex.Matcher;
107106
import java.util.regex.Pattern;
107+
import java.util.zip.ZipException;
108108

109109
import javax.script.ScriptEngine;
110110
import javax.script.ScriptException;
111-
import javax.swing.*;
112-
import javax.swing.border.BevelBorder;
111+
import javax.swing.AbstractAction;
112+
import javax.swing.AbstractButton;
113+
import javax.swing.BoxLayout;
114+
import javax.swing.ButtonGroup;
115+
import javax.swing.JButton;
116+
import javax.swing.JCheckBoxMenuItem;
117+
import javax.swing.JDialog;
118+
import javax.swing.JFrame;
119+
import javax.swing.JLabel;
120+
import javax.swing.JMenu;
121+
import javax.swing.JMenuBar;
122+
import javax.swing.JMenuItem;
123+
import javax.swing.JOptionPane;
124+
import javax.swing.JPanel;
125+
import javax.swing.JPopupMenu;
126+
import javax.swing.JRadioButtonMenuItem;
127+
import javax.swing.JScrollPane;
128+
import javax.swing.JSplitPane;
129+
import javax.swing.JTabbedPane;
130+
import javax.swing.JTextArea;
131+
import javax.swing.JTextField;
132+
import javax.swing.JTextPane;
133+
import javax.swing.JTree;
134+
import javax.swing.KeyStroke;
135+
import javax.swing.SwingUtilities;
136+
import javax.swing.UIManager;
113137
import javax.swing.event.ChangeEvent;
114138
import javax.swing.event.ChangeListener;
115139
import javax.swing.event.DocumentEvent;
@@ -1569,9 +1593,8 @@ public void createNewDocument(final String title, final String text) {
15691593
open(null);
15701594
final EditorPane editorPane = getEditorPane();
15711595
editorPane.setText(text);
1572-
setEditorPaneFileName(title);
1573-
15741596
editorPane.setLanguageByFileName(title);
1597+
setEditorPaneFileName(title);
15751598
updateLanguageMenu(editorPane.getCurrentLanguage());
15761599
}
15771600

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.scijava.ui.swing.script;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import org.junit.After;
6+
import org.junit.Before;
7+
import org.junit.Test;
8+
import org.scijava.Context;
9+
10+
public class EditorPaneFileNameTest {
11+
12+
private Context context;
13+
14+
@Before
15+
public void setUp() {
16+
context = new Context();
17+
}
18+
19+
@After
20+
public void tearDown() {
21+
context.dispose();
22+
}
23+
24+
@Test
25+
public void testSuppliedLanguageExtensionIsNotDuplicated() {
26+
final EditorPane editorPane = new EditorPane();
27+
context.inject(editorPane);
28+
29+
editorPane.setLanguageByFileName("Macro.java");
30+
editorPane.setFileName("Macro.java");
31+
32+
assertEquals("Macro.java", editorPane.getFileName());
33+
}
34+
}

0 commit comments

Comments
 (0)