source: josm/trunk/src/org/openstreetmap/josm/tools/WindowGeometry.java@ 3775

Last change on this file since 3775 was 3083, checked in by bastiK, 14 years ago

added svn:eol-style=native to source files

  • Property svn:eol-style set to native
File size: 7.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Component;
7import java.awt.Dimension;
8import java.awt.Point;
9import java.awt.Toolkit;
10import java.awt.Window;
11import java.util.regex.Matcher;
12import java.util.regex.Pattern;
13
14import org.openstreetmap.josm.Main;
15
16/**
17 * This is a helper class for persisting the geometry of a JOSM window to the preference store
18 * and for restoring it from the preference store.
19 *
20 */
21public class WindowGeometry {
22
23 /**
24 * Replies a window geometry object for a window with a specific size which is
25 * centered on screen
26 *
27 * @param extent the size
28 * @return the geometry object
29 */
30 static public WindowGeometry centerOnScreen(Dimension extent) {
31 Point topLeft = new Point(
32 Math.max(0, (Toolkit.getDefaultToolkit().getScreenSize().width - extent.width) /2),
33 Math.max(0, (Toolkit.getDefaultToolkit().getScreenSize().height - extent.height) /2)
34 );
35 return new WindowGeometry(topLeft, extent);
36 }
37
38 /**
39 * Replies a window geometry object for a window with a specific size which is centered
40 * relative to the parent window of a reference component.
41 *
42 * @param reference the reference component.
43 * @param extent the size
44 * @return the geometry object
45 */
46 static public WindowGeometry centerInWindow(Component reference, Dimension extent) {
47 Window parentWindow = null;
48 while(reference != null && ! (reference instanceof Window) ) {
49 reference = reference.getParent();
50 }
51 if (reference == null)
52 return new WindowGeometry(new Point(0,0), extent);
53 parentWindow = (Window)reference;
54 Point topLeft = new Point(
55 Math.max(0, (parentWindow.getSize().width - extent.width) /2),
56 Math.max(0, (parentWindow.getSize().height - extent.height) /2)
57 );
58 topLeft.x += parentWindow.getLocation().x;
59 topLeft.y += parentWindow.getLocation().y;
60 return new WindowGeometry(topLeft, extent);
61 }
62
63 /**
64 * Exception thrown by the WindowGeometry class if something goes wrong
65 *
66 */
67 static public class WindowGeometryException extends Exception {
68 public WindowGeometryException(String message, Throwable cause) {
69 super(message, cause);
70 }
71
72 public WindowGeometryException(String message) {
73 super(message);
74 }
75 }
76
77 /** the top left point */
78 private Point topLeft;
79 /** the size */
80 private Dimension extent;
81
82 /**
83 *
84 * @param topLeft the top left point
85 * @param extent the extent
86 */
87 public WindowGeometry(Point topLeft, Dimension extent) {
88 this.topLeft = topLeft;
89 this.extent = extent;
90 }
91
92 /**
93 * Creates a window geometry from the position and the size of a window.
94 *
95 * @param window the window
96 */
97 public WindowGeometry(Window window) {
98 this(window.getLocationOnScreen(), window.getSize());
99 }
100
101 protected int parseField(String preferenceKey, String preferenceValue, String field) throws WindowGeometryException {
102 String v = "";
103 try {
104 Pattern p = Pattern.compile(field + "=(\\d+)",Pattern.CASE_INSENSITIVE);
105 Matcher m = p.matcher(preferenceValue);
106 if (!m.find())
107 throw new WindowGeometryException(tr("Preference with key ''{0}'' does not include ''{1}''. Cannot restore window geometry from preferences.", preferenceKey, field));
108 v = m.group(1);
109 return Integer.parseInt(v);
110 } catch(WindowGeometryException e) {
111 throw e;
112 } catch(NumberFormatException e) {
113 throw new WindowGeometryException(tr("Preference with key ''{0}'' does not provide an int value for ''{1}''. Got {2}. Cannot restore window geometry from preferences.", preferenceKey, field, v));
114 } catch(Exception e) {
115 throw new WindowGeometryException(tr("Failed to parse field ''{1}'' in preference with key ''{0}''. Exception was: {2}. Cannot restore window geometry from preferences.", preferenceKey, field, e.toString()), e);
116 }
117 }
118
119 protected void initFromPreferences(String preferenceKey) throws WindowGeometryException {
120 String value = Main.pref.get(preferenceKey);
121 if (value == null || value.equals(""))
122 throw new WindowGeometryException(tr("Preference with key ''{0}'' does not exist. Cannot restore window geometry from preferences.", preferenceKey));
123 topLeft = new Point();
124 extent = new Dimension();
125 topLeft.x = parseField(preferenceKey, value, "x");
126 topLeft.y = parseField(preferenceKey, value, "y");
127 extent.width = parseField(preferenceKey, value, "width");
128 extent.height = parseField(preferenceKey, value, "height");
129 }
130
131 protected void initFromWindowGeometry(WindowGeometry other) {
132 this.topLeft = other.topLeft;
133 this.extent = other.extent;
134 }
135
136 /**
137 * Creates a window geometry from the values kept in the preference store under the
138 * key <code>preferenceKey</code>
139 *
140 * @param preferenceKey the preference key
141 * @throws WindowGeometryException thrown if no such key exist or if the preference value has
142 * an illegal format
143 */
144 public WindowGeometry(String preferenceKey) throws WindowGeometryException {
145 initFromPreferences(preferenceKey);
146 }
147
148 /**
149 * Creates a window geometry from the values kept in the preference store under the
150 * key <code>preferenceKey</code>. Falls back to the <code>defaultGeometry</code> if
151 * something goes wrong.
152 *
153 * @param preferenceKey the preference key
154 * @param defaultGeometry the default geometry
155 *
156 */
157 public WindowGeometry(String preferenceKey, WindowGeometry defaultGeometry) {
158 try {
159 initFromPreferences(preferenceKey);
160 } catch(WindowGeometryException e) {
161 // System.out.println(tr("Warning: Failed to restore window geometry from key ''{0}''. Falling back to default geometry. Details: {1}", preferenceKey, e.getMessage()));
162 initFromWindowGeometry(defaultGeometry);
163 }
164 }
165
166 /**
167 * Remembers a window geometry under a specific preference key
168 *
169 * @param preferenceKey the preference key
170 */
171 public void remember(String preferenceKey) {
172 StringBuffer value = new StringBuffer();
173 value.append("x=").append(topLeft.x).append(",")
174 .append("y=").append(topLeft.y).append(",")
175 .append("width=").append(extent.width).append(",")
176 .append("height=").append(extent.height);
177 Main.pref.put(preferenceKey, value.toString());
178 }
179
180 /**
181 * Replies the top left point for the geometry
182 *
183 * @return the top left point for the geometry
184 */
185 public Point getTopLeft() {
186 return topLeft;
187 }
188
189 /**
190 * Replies the size spezified by the geometry
191 *
192 * @return the size spezified by the geometry
193 */
194 public Dimension getSize() {
195 return extent;
196 }
197
198 /**
199 * Applies this geometry to a window
200 *
201 * @param window the window
202 */
203 public void apply(Window window) {
204 window.setLocation(topLeft);
205 window.setSize(extent);
206 }
207
208 /**
209 * Applies this geometry to a window. Makes sure that the window is not placed outside
210 * of the coordinate range of the current screen.
211 *
212 * @param window the window
213 */
214 public void applySafe(Window window) {
215 Point p = new Point(topLeft);
216 if (p.x > Toolkit.getDefaultToolkit().getScreenSize().width - 10) {
217 p.x = 0;
218 }
219 if (p.y > Toolkit.getDefaultToolkit().getScreenSize().height - 10) {
220 p.y = 0;
221 }
222 window.setLocation(p);
223 window.setSize(extent);
224 }
225}
Note: See TracBrowser for help on using the repository browser.