Skip to content

Commit 3979034

Browse files
committed
changing how editor state info is handled, restoring sketches, recent menu
1 parent c15a468 commit 3979034

File tree

16 files changed

+588
-235
lines changed

16 files changed

+588
-235
lines changed

app/src/processing/app/Base.java

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

app/src/processing/app/Editor.java

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
*/
4646
public abstract class Editor extends JFrame implements RunnerListener {
4747
protected Base base;
48+
protected EditorState state;
4849
protected Mode mode;
4950

5051
// otherwise, if the window is resized with the message label
@@ -109,9 +110,12 @@ public abstract class Editor extends JFrame implements RunnerListener {
109110
ArrayList<ToolContribution> coreTools;
110111
public ArrayList<ToolContribution> contribTools;
111112

112-
protected Editor(final Base base, String path, int[] location, final Mode mode) {
113-
super("Processing");
113+
114+
// protected Editor(final Base base, String path, int[] location, final Mode mode) {
115+
protected Editor(final Base base, String path, EditorState state, final Mode mode) {
116+
super("Processing", state.checkConfig());
114117
this.base = base;
118+
this.state = state;
115119
this.mode = mode;
116120

117121
Base.setIcon(this); // TODO should this be per-mode?
@@ -244,7 +248,8 @@ public void caretUpdate(CaretEvent e) {
244248
pack();
245249

246250
// Set the window bounds and the divider location before setting it visible
247-
setPlacement(location);
251+
// setPlacement(location);
252+
state.apply(this);
248253

249254
// Set the minimum size for the editor window
250255
setMinimumSize(new Dimension(Preferences.getInteger("editor.window.width.min"),
@@ -393,28 +398,38 @@ public JMenu getModeMenu() {
393398
abstract public Formatter createFormatter();
394399

395400

396-
protected void setPlacement(int[] location) {
397-
setBounds(location[0], location[1], location[2], location[3]);
398-
if (location[4] != 0) {
399-
splitPane.setDividerLocation(location[4]);
400-
}
401+
// protected void setPlacement(int[] location) {
402+
// setBounds(location[0], location[1], location[2], location[3]);
403+
// if (location[4] != 0) {
404+
// splitPane.setDividerLocation(location[4]);
405+
// }
406+
// }
407+
//
408+
//
409+
// protected int[] getPlacement() {
410+
// int[] location = new int[5];
411+
//
412+
// // Get the dimensions of the Frame
413+
// Rectangle bounds = getBounds();
414+
// location[0] = bounds.x;
415+
// location[1] = bounds.y;
416+
// location[2] = bounds.width;
417+
// location[3] = bounds.height;
418+
//
419+
// // Get the current placement of the divider
420+
// location[4] = splitPane.getDividerLocation();
421+
//
422+
// return location;
423+
// }
424+
425+
426+
protected void setDividerLocation(int pos) {
427+
splitPane.setDividerLocation(pos);
401428
}
402-
403-
404-
protected int[] getPlacement() {
405-
int[] location = new int[5];
406-
407-
// Get the dimensions of the Frame
408-
Rectangle bounds = getBounds();
409-
location[0] = bounds.x;
410-
location[1] = bounds.y;
411-
location[2] = bounds.width;
412-
location[3] = bounds.height;
413-
414-
// Get the current placement of the divider
415-
location[4] = splitPane.getDividerLocation();
416-
417-
return location;
429+
430+
431+
protected int getDividerLocation() {
432+
return splitPane.getDividerLocation();
418433
}
419434

420435

@@ -2004,7 +2019,7 @@ protected boolean handleOpenInternal(String path) {
20042019

20052020
// Store information on who's open and running
20062021
// (in case there's a crash or something that can't be recovered)
2007-
base.storeSketches();
2022+
// base.storeSketches();
20082023
Preferences.save();
20092024

20102025
// opening was successful
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2+
3+
/*
4+
Part of the Processing project - http://processing.org
5+
6+
Copyright (c) 2004-11 Ben Fry and Casey Reas
7+
Copyright (c) 2001-04 Massachusetts Institute of Technology
8+
9+
This program is free software; you can redistribute it and/or modify
10+
it under the terms of the GNU General Public License version 2
11+
as published by the Free Software Foundation.
12+
13+
This program is distributed in the hope that it will be useful,
14+
but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
GNU General Public License for more details.
17+
18+
You should have received a copy of the GNU General Public License
19+
along with this program; if not, write to the Free Software Foundation,
20+
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21+
*/
22+
23+
package processing.app;
24+
25+
import java.awt.GraphicsConfiguration;
26+
import java.awt.GraphicsDevice;
27+
import java.awt.GraphicsEnvironment;
28+
import java.awt.Rectangle;
29+
import java.io.BufferedReader;
30+
import java.io.IOException;
31+
import java.io.PrintWriter;
32+
import java.util.List;
33+
34+
35+
import processing.core.PApplet;
36+
37+
public class EditorState {
38+
// path to the main .pde file for the sketch
39+
// String path;
40+
// placement of the window
41+
// int windowX, windowY, windowW, windowH;
42+
Rectangle editorBounds;
43+
int dividerLocation;
44+
// width/height of the screen on which this window was placed
45+
// int displayW, displayH;
46+
String deviceName;
47+
Rectangle deviceBounds;
48+
49+
50+
EditorState(List<Editor> editors) {
51+
defaultConfig();
52+
defaultLocation(editors);
53+
}
54+
55+
56+
EditorState(BufferedReader reader) throws IOException {
57+
String line = reader.readLine();
58+
String[] pieces = PApplet.split(line, '\t');
59+
// path = pieces[0];
60+
61+
// windowX = Integer.parseInt(pieces[1]);
62+
// windowY = Integer.parseInt(pieces[2]);
63+
// windowW = Integer.parseInt(pieces[3]);
64+
// windowH = Integer.parseInt(pieces[4]);
65+
66+
// displayW = Integer.parseInt(pieces[5]);
67+
// displayH = Integer.parseInt(pieces[6]);
68+
}
69+
70+
// scenarios:
71+
// 1) new untitled sketch (needs device, needs bounds)
72+
// 2) restoring sketch from recent menu
73+
// - device cannot be found
74+
// - device is found but it's a different size
75+
// - device is found and size is correct
76+
// 3) re-opening sketch in a new mode
77+
78+
GraphicsConfiguration checkConfig() {
79+
GraphicsEnvironment graphicsEnvironment =
80+
GraphicsEnvironment.getLocalGraphicsEnvironment();
81+
GraphicsDevice[] screenDevices = graphicsEnvironment.getScreenDevices();
82+
for (GraphicsDevice device : screenDevices) {
83+
GraphicsConfiguration[] configurations = device.getConfigurations();
84+
for (GraphicsConfiguration config : configurations) {
85+
if (config.getDevice().getIDstring().equals(deviceName)) {
86+
if (deviceBounds != null && config.getBounds().equals(deviceBounds)) {
87+
return config;
88+
} else {
89+
90+
}
91+
}
92+
}
93+
}
94+
// otherwise go to the default config
95+
return defaultConfig();
96+
}
97+
98+
99+
GraphicsConfiguration defaultConfig() {
100+
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
101+
GraphicsDevice device = ge.getDefaultScreenDevice();
102+
GraphicsConfiguration config = device.getDefaultConfiguration();
103+
deviceName = device.getIDstring();
104+
deviceBounds = config.getBounds();
105+
return config;
106+
}
107+
108+
109+
void defaultLocation(List<Editor> editors) {
110+
int defaultWidth = Preferences.getInteger("editor.window.width.default");
111+
int defaultHeight = Preferences.getInteger("editor.window.height.default");
112+
113+
if (editors.size() == 0) {
114+
// If no current active editor, use default placement.
115+
// Center the window on ths screen, taking into account that the
116+
// upper-left corner of the device may have a non (0, 0) origin.
117+
int editorX =
118+
deviceBounds.x + (deviceBounds.width - defaultWidth) / 2;
119+
int editorY =
120+
deviceBounds.y + (deviceBounds.height - defaultHeight) / 2;
121+
editorBounds =
122+
new Rectangle(editorX, editorY, defaultWidth, defaultHeight);
123+
dividerLocation = 0;
124+
125+
} else {
126+
// With a currently active editor, open the new window using the same
127+
// dimensions and divider location, but offset slightly.
128+
synchronized (editors) {
129+
final int OVER = 50;
130+
Editor lastOpened = editors.get(editors.size() - 1);
131+
editorBounds = lastOpened.getBounds();
132+
editorBounds.x += OVER;
133+
editorBounds.y += OVER;
134+
dividerLocation = lastOpened.getDividerLocation();
135+
136+
if (!deviceBounds.contains(editorBounds)) {
137+
// Warp the next window to a randomish location on screen.
138+
editorBounds.x = deviceBounds.x + (int) (Math.random() * (deviceBounds.width - defaultWidth));
139+
editorBounds.y = deviceBounds.y + (int) (Math.random() * (deviceBounds.height - defaultHeight));
140+
}
141+
}
142+
}
143+
}
144+
145+
146+
void update(Editor editor) {
147+
// path = editor.getSketch().getMainFilePath();
148+
editorBounds = editor.getBounds();
149+
dividerLocation = editor.getDividerLocation();
150+
GraphicsConfiguration config = editor.getGraphicsConfiguration();
151+
GraphicsDevice device = config.getDevice();
152+
deviceBounds = config.getBounds();
153+
deviceName = device.getIDstring();
154+
}
155+
156+
157+
void apply(Editor editor) {
158+
editor.setBounds(editorBounds);
159+
if (dividerLocation != 0) {
160+
editor.setDividerLocation(dividerLocation);
161+
}
162+
}
163+
164+
165+
void write(PrintWriter writer) {
166+
// writer.print(path);
167+
writer.print('\t');
168+
writeRect(writer, editorBounds);
169+
writer.print('\t');
170+
writer.print(deviceName);
171+
writer.print('\t');
172+
writeRect(writer, deviceBounds);
173+
}
174+
175+
176+
void writeRect(PrintWriter writer, Rectangle rect) {
177+
writer.print(rect.x);
178+
writer.print('\t');
179+
writer.print(rect.y);
180+
writer.print('\t');
181+
writer.print(rect.width);
182+
writer.print('\t');
183+
writer.print(rect.height);
184+
}
185+
}

app/src/processing/app/Mode.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ public InputStream getContentStream(String path) throws FileNotFoundException {
9494
/**
9595
* Create a new editor associated with this mode.
9696
*/
97-
abstract public Editor createEditor(Base base, String path, int[] location);
97+
abstract public Editor createEditor(Base base, String path, EditorState state);
98+
//abstract public Editor createEditor(Base base, String path, int[] location);
9899

99100

100101
public File getExamplesFolder() {

app/src/processing/app/Preferences.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@
77
Copyright (c) 2001-04 Massachusetts Institute of Technology
88
99
This program is free software; you can redistribute it and/or modify
10-
it under the terms of the GNU General Public License as published by
11-
the Free Software Foundation; either version 2 of the License, or
12-
(at your option) any later version.
10+
it under the terms of the GNU General Public License version 2
11+
as published by the Free Software Foundation.
1312
1413
This program is distributed in the hope that it will be useful,
1514
but WITHOUT ANY WARRANTY; without even the implied warranty of

0 commit comments

Comments
 (0)