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

Last change on this file since 2667 was 2512, checked in by stoecker, 14 years ago

i18n updated, fixed files to reduce problems when applying patches, fix #4017

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