Skip to content

Commit d61f67e

Browse files
committed
Pull Request - User Story #12
1 parent 9c3d45d commit d61f67e

File tree

1 file changed

+141
-1
lines changed

1 file changed

+141
-1
lines changed

app/src/processing/app/ui/Editor.java

Lines changed: 141 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@
6565
* Main editor panel for the Processing Development Environment.
6666
*/
6767
public abstract class Editor extends JFrame implements RunnerListener {
68+
private static final String PREF_LAYOUT_X = "editor.custom.layout.x";
69+
private static final String PREF_LAYOUT_Y = "editor.custom.layout.y";
70+
private static final String PREF_LAYOUT_WIDTH = "editor.custom.layout.width";
71+
private static final String PREF_LAYOUT_HEIGHT = "editor.custom.layout.height";
72+
private static final String PREF_LAYOUT_DIVIDER = "editor.custom.layout.divider";
73+
private static final String PREF_LAYOUT_MAXIMIZED = "editor.custom.layout.maximized";
74+
6875
protected Base base;
6976
protected EditorState state;
7077
protected Mode mode;
@@ -143,6 +150,7 @@ public abstract class Editor extends JFrame implements RunnerListener {
143150
JMenu developMenu;
144151

145152
protected List<Problem> problems = Collections.emptyList();
153+
private boolean suppressCustomizationPersistence;
146154

147155

148156
protected Editor(final Base base, String path, final EditorState state,
@@ -322,6 +330,9 @@ public void caretUpdate(CaretEvent e) {
322330
Toolkit.zoom(Preferences.getInteger("editor.window.height.min"));
323331
setMinimumSize(new Dimension(minWidth, minHeight));
324332

333+
applySavedCustomizationSettings();
334+
installCustomizationPersistence();
335+
325336
// Bring back the general options for the editor
326337
applyPreferences();
327338

@@ -726,6 +737,13 @@ protected JMenu buildFileMenu(JMenuItem[] exportItems) {
726737
fileMenu.add(ei);
727738
}
728739
}
740+
741+
fileMenu.addSeparator();
742+
743+
item = new JMenuItem("Reset Interface Customization");
744+
item.addActionListener(e -> resetCustomizationSettings());
745+
fileMenu.add(item);
746+
729747
fileMenu.addSeparator();
730748

731749
item = Toolkit.newJMenuItemShift(Language.text("menu.file.page_setup"), 'P');
@@ -893,6 +911,128 @@ protected void modifyFontSize(boolean increase){
893911
Preferences.save();
894912
}
895913

914+
915+
private void installCustomizationPersistence() {
916+
splitPane.addPropertyChangeListener(JSplitPane.DIVIDER_LOCATION_PROPERTY,
917+
e -> persistCustomizationSettings());
918+
919+
addComponentListener(new ComponentAdapter() {
920+
@Override
921+
public void componentMoved(ComponentEvent e) {
922+
persistCustomizationSettings();
923+
}
924+
925+
@Override
926+
public void componentResized(ComponentEvent e) {
927+
persistCustomizationSettings();
928+
}
929+
});
930+
931+
addWindowStateListener(e -> persistCustomizationSettings());
932+
}
933+
934+
935+
private void applySavedCustomizationSettings() {
936+
try {
937+
Integer x = getPreferenceInt(PREF_LAYOUT_X);
938+
Integer y = getPreferenceInt(PREF_LAYOUT_Y);
939+
Integer width = getPreferenceInt(PREF_LAYOUT_WIDTH);
940+
Integer height = getPreferenceInt(PREF_LAYOUT_HEIGHT);
941+
Integer divider = getPreferenceInt(PREF_LAYOUT_DIVIDER);
942+
boolean maximized = Preferences.getBoolean(PREF_LAYOUT_MAXIMIZED);
943+
944+
boolean hasBounds = x != null && y != null && width != null && height != null;
945+
if (!hasBounds && divider == null && !maximized) {
946+
return;
947+
}
948+
949+
suppressCustomizationPersistence = true;
950+
setExtendedState(Frame.NORMAL);
951+
952+
if (hasBounds) {
953+
int minWidth = getMinimumSize().width;
954+
int minHeight = getMinimumSize().height;
955+
setBounds(x, y, Math.max(width, minWidth), Math.max(height, minHeight));
956+
}
957+
958+
if (divider != null) {
959+
setDividerLocation(divider);
960+
}
961+
if (maximized) {
962+
setExtendedState(Frame.MAXIMIZED_BOTH);
963+
}
964+
} finally {
965+
suppressCustomizationPersistence = false;
966+
}
967+
}
968+
969+
970+
private void persistCustomizationSettings() {
971+
if (suppressCustomizationPersistence) {
972+
return;
973+
}
974+
975+
boolean maximized = (getExtendedState() & Frame.MAXIMIZED_BOTH) == Frame.MAXIMIZED_BOTH;
976+
977+
Preferences.setBoolean(PREF_LAYOUT_MAXIMIZED, maximized);
978+
979+
if (!maximized) {
980+
Rectangle bounds = getBounds();
981+
Preferences.setInteger(PREF_LAYOUT_X, bounds.x);
982+
Preferences.setInteger(PREF_LAYOUT_Y, bounds.y);
983+
Preferences.setInteger(PREF_LAYOUT_WIDTH, bounds.width);
984+
Preferences.setInteger(PREF_LAYOUT_HEIGHT, bounds.height);
985+
}
986+
987+
int dividerLocation = getDividerLocation();
988+
if (dividerLocation > 0) {
989+
Preferences.setInteger(PREF_LAYOUT_DIVIDER, dividerLocation);
990+
}
991+
992+
Preferences.save();
993+
}
994+
995+
996+
private void resetCustomizationSettings() {
997+
Preferences.unset(PREF_LAYOUT_X);
998+
Preferences.unset(PREF_LAYOUT_Y);
999+
Preferences.unset(PREF_LAYOUT_WIDTH);
1000+
Preferences.unset(PREF_LAYOUT_HEIGHT);
1001+
Preferences.unset(PREF_LAYOUT_DIVIDER);
1002+
Preferences.unset(PREF_LAYOUT_MAXIMIZED);
1003+
Preferences.save();
1004+
1005+
Rectangle deviceBounds = getGraphicsConfiguration().getBounds();
1006+
int defaultWidth = Toolkit.zoom(Preferences.getInteger("editor.window.width.default"));
1007+
int defaultHeight = Toolkit.zoom(Preferences.getInteger("editor.window.height.default"));
1008+
defaultWidth = Math.min(defaultWidth, deviceBounds.width);
1009+
defaultHeight = Math.min(defaultHeight, deviceBounds.height);
1010+
int x = deviceBounds.x + (deviceBounds.width - defaultWidth) / 2;
1011+
int y = deviceBounds.y + (deviceBounds.height - defaultHeight) / 2;
1012+
1013+
suppressCustomizationPersistence = true;
1014+
try {
1015+
setExtendedState(Frame.NORMAL);
1016+
setBounds(x, y, defaultWidth, defaultHeight);
1017+
setDividerLocation(2 * defaultHeight / 3);
1018+
} finally {
1019+
suppressCustomizationPersistence = false;
1020+
}
1021+
}
1022+
1023+
1024+
private Integer getPreferenceInt(String key) {
1025+
String value = Preferences.get(key);
1026+
if (value == null) {
1027+
return null;
1028+
}
1029+
try {
1030+
return Integer.parseInt(value);
1031+
} catch (NumberFormatException ignored) {
1032+
return null;
1033+
}
1034+
}
1035+
8961036
abstract public JMenu buildSketchMenu();
8971037

8981038

@@ -2955,4 +3095,4 @@ public void show(Component component, int x, int y) {
29553095
public String getSketchDiagnostics() {
29563096
return "";
29573097
}
2958-
}
3098+
}

0 commit comments

Comments
 (0)