Skip to content

Commit 49bd653

Browse files
committed
Persist split pane states along with window size
1 parent 72a9633 commit 49bd653

File tree

2 files changed

+49
-13
lines changed

2 files changed

+49
-13
lines changed

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

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,9 @@ public class TextEditor extends JFrame implements ActionListener,
184184
public static final String WINDOW_WIDTH = "script.editor.width";
185185
public static final int DEFAULT_WINDOW_WIDTH = 800;
186186
public static final int DEFAULT_WINDOW_HEIGHT = 600;
187+
public static final String MAIN_DIV_LOCATION = "script.editor.main.divLocation";
188+
public static final String TAB_DIV_LOCATION = "script.editor.tab.divLocation";
189+
public static final String TAB_DIV_ORIENTATION = "script.editor.tab.divOrientation";
187190

188191
static {
189192
// Add known script template paths.
@@ -217,6 +220,7 @@ public class TextEditor extends JFrame implements ActionListener,
217220
private JTextArea errorScreen = new JTextArea();
218221

219222
private final FileSystemTree tree;
223+
private final JSplitPane body;
220224

221225
private int compileStartOffset;
222226
private Position compileStartPosition;
@@ -260,6 +264,7 @@ public class TextEditor extends JFrame implements ActionListener,
260264
private ScriptModule module;
261265
private boolean incremental = false;
262266
private DragSource dragSource;
267+
private boolean layoutLoading = true;
263268

264269
public static final ArrayList<TextEditor> instances = new ArrayList<>();
265270
public static final ArrayList<Context> contexts = new ArrayList<>();
@@ -270,7 +275,6 @@ public TextEditor(final Context context) {
270275
contexts.add(context);
271276
context.inject(this);
272277
initializeTokenMakers();
273-
loadPreferences();
274278

275279
// -- BEGIN MENUS --
276280

@@ -706,8 +710,11 @@ public TextEditor(final Context context) {
706710
scrolltree.setBackground(Color.white);
707711
scrolltree.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEmptyBorder(0,5,0,5)));
708712
scrolltree.setPreferredSize(new Dimension(200, 600));
709-
final JSplitPane body = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, scrolltree, tabbed);
713+
body = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, scrolltree, tabbed);
710714
body.setOneTouchExpandable(true);
715+
body.addPropertyChangeListener(evt -> {
716+
if ("dividerLocation".equals(evt.getPropertyName())) saveWindowSizeToPrefs();
717+
});
711718
getContentPane().add(body);
712719

713720
// for Eclipse and MS Visual Studio lovers
@@ -754,6 +761,8 @@ public void windowGainedFocus(final WindowEvent e) {
754761
pack();
755762
body.setDividerLocation(0.2);
756763
getTab().getScreenAndPromptSplit().setDividerLocation(1.0);
764+
loadPreferences();
765+
pack();
757766
});
758767
}
759768
catch (final Exception ie) {
@@ -762,7 +771,7 @@ public void windowGainedFocus(final WindowEvent e) {
762771
}
763772
findDialog = new FindAndReplaceDialog(this);
764773

765-
// Save the size of the window in the preferences
774+
// Save the layout when window is resized.
766775
addComponentListener(new ComponentAdapter() {
767776

768777
@Override
@@ -895,35 +904,55 @@ private void onEvent(
895904
}
896905

897906
/**
898-
* Loads the preferences for the JFrame from file
907+
* Loads the Script Editor layout from persisted storage.
908+
* @see #saveWindowSizeToPrefs()
899909
*/
900910
public void loadPreferences() {
911+
layoutLoading = true;
912+
901913
final Dimension dim = getSize();
902914

903915
// If a dimension is 0 then use the default dimension size
904-
if (0 == dim.width) {
905-
dim.width = DEFAULT_WINDOW_WIDTH;
906-
}
907-
if (0 == dim.height) {
908-
dim.height = DEFAULT_WINDOW_HEIGHT;
909-
}
916+
if (0 == dim.width) dim.width = DEFAULT_WINDOW_WIDTH;
917+
if (0 == dim.height) dim.height = DEFAULT_WINDOW_HEIGHT;
918+
919+
final int windowWidth = prefService.getInt(getClass(), WINDOW_WIDTH, dim.width);
920+
final int windowHeight = prefService.getInt(getClass(), WINDOW_HEIGHT, dim.height);
921+
setPreferredSize(new Dimension(windowWidth, windowHeight));
922+
923+
final int mainDivLocation = prefService.getInt(getClass(), MAIN_DIV_LOCATION, body.getDividerLocation());
924+
body.setDividerLocation(mainDivLocation);
910925

911-
setPreferredSize(new Dimension(prefService.getInt(getClass(), WINDOW_WIDTH,
912-
dim.width), prefService.getInt(getClass(), WINDOW_HEIGHT, dim.height)));
926+
final TextEditorTab tab = getTab();
927+
final int tabDivLocation = prefService.getInt(getClass(), TAB_DIV_LOCATION, tab.getDividerLocation());
928+
final int tabDivOrientation = prefService.getInt(getClass(), TAB_DIV_ORIENTATION, tab.getOrientation());
929+
tab.setDividerLocation(tabDivLocation);
930+
tab.setOrientation(tabDivOrientation);
931+
932+
layoutLoading = false;
913933
}
914934

915935
/**
916-
* Saves the window size to preferences.
936+
* Saves the Script Editor layout to persisted storage.
917937
* <p>
918938
* Separated from savePreferences because we always want to save the window
919939
* size when it's resized, however, we don't want to automatically save the
920940
* font, tab size, etc. without the user pressing "Save Preferences"
921941
* </p>
942+
* @see #loadPreferences()
922943
*/
923944
public void saveWindowSizeToPrefs() {
945+
if (layoutLoading) return;
946+
924947
final Dimension dim = getSize();
925948
prefService.put(getClass(), WINDOW_HEIGHT, dim.height);
926949
prefService.put(getClass(), WINDOW_WIDTH, dim.width);
950+
951+
prefService.put(getClass(), MAIN_DIV_LOCATION, body.getDividerLocation());
952+
953+
final TextEditorTab tab = getTab();
954+
prefService.put(getClass(), TAB_DIV_LOCATION, tab.getDividerLocation());
955+
prefService.put(getClass(), TAB_DIV_ORIENTATION, tab.getOrientation());
927956
}
928957

929958
final public RSyntaxTextArea getTextArea() {

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import java.awt.dnd.DropTargetListener;
4646
import java.awt.event.ActionEvent;
4747
import java.awt.event.ActionListener;
48+
import java.beans.PropertyChangeListener;
4849
import java.io.File;
4950
import java.util.List;
5051

@@ -327,6 +328,12 @@ public void actionPerformed(ActionEvent a) {
327328
super.setRightComponent(screenAndPromptSplit);
328329
screenAndPromptSplit.setDividerLocation(600);
329330
screenAndPromptSplit.setDividerLocation(1.0);
331+
332+
// Persist Script Editor layout whenever split pane divider is adjusted.
333+
addPropertyChangeListener(evt -> {
334+
if ("dividerLocation".equals(evt.getPropertyName()))
335+
textEditor.saveWindowSizeToPrefs();
336+
});
330337
}
331338

332339
// Package-private

0 commit comments

Comments
 (0)