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+ }
0 commit comments