34
|
1 /*******************************************************************************
|
90
|
2 * Copyright (c) 2000, 2008 IBM Corporation and others.
|
34
|
3 * All rights reserved. This program and the accompanying materials
|
|
4 * are made available under the terms of the Eclipse Public License v1.0
|
|
5 * which accompanies this distribution, and is available at
|
|
6 * http://www.eclipse.org/legal/epl-v10.html
|
|
7 *
|
|
8 * Contributors:
|
|
9 * IBM Corporation - initial API and implementation
|
|
10 * Port to the D programming language:
|
|
11 * Frank Benoit <benoit@tionex.de>
|
|
12 *******************************************************************************/
|
|
13 module dwtx.jface.preference.PreferenceConverter;
|
|
14
|
|
15 import dwtx.jface.preference.IPreferenceStore;
|
|
16
|
|
17 // import java.util.Arrays;
|
|
18 // import java.util.StringTokenizer;
|
|
19
|
|
20 import dwt.DWTException;
|
|
21 import dwt.graphics.FontData;
|
|
22 import dwt.graphics.Point;
|
|
23 import dwt.graphics.RGB;
|
|
24 import dwt.graphics.Rectangle;
|
|
25 import dwt.widgets.Display;
|
|
26 import dwtx.jface.resource.JFaceResources;
|
|
27 import dwtx.jface.resource.StringConverter;
|
|
28
|
|
29 import dwt.dwthelper.utils;
|
|
30
|
|
31 /**
|
|
32 * A utility class for dealing with preferences whose values are
|
|
33 * common DWT objects (color, points, rectangles, and font data).
|
|
34 * The static methods on this class handle the conversion between
|
|
35 * the DWT objects and their string representations.
|
|
36 * <p>
|
|
37 * Usage:
|
|
38 * <pre>
|
|
39 * IPreferenceStore store = ...;
|
|
40 * PreferenceConverter.setValue(store, "bg", new RGB(127,127,127));
|
|
41 * ...
|
|
42 * RBG bgColor = PreferenceConverter.getValue(store, "bg");
|
|
43 * </pre>
|
|
44 * </p>
|
|
45 * <p>
|
|
46 * This class contains static methods and fields only and cannot
|
|
47 * be instantiated.
|
|
48 * </p>
|
|
49 * Note: touching this class has the side effect of creating a display (static initializer).
|
90
|
50 * @noinstantiate This class is not intended to be instantiated by clients.
|
34
|
51 */
|
|
52 public class PreferenceConverter {
|
|
53
|
|
54 /**
|
|
55 * The default-default value for point preferences
|
|
56 * (the origin, <code>(0,0)</code>).
|
|
57 */
|
|
58 public static const Point POINT_DEFAULT_DEFAULT;
|
|
59
|
|
60 /**
|
|
61 * The default-default value for rectangle preferences
|
|
62 * (the empty rectangle <code>(0,0,0,0)</code>).
|
|
63 */
|
|
64 public static const Rectangle RECTANGLE_DEFAULT_DEFAULT;
|
|
65
|
|
66 /**
|
|
67 * The default-default value for color preferences
|
|
68 * (black, <code>RGB(0,0,0)</code>).
|
|
69 */
|
|
70 public static const RGB COLOR_DEFAULT_DEFAULT;
|
|
71
|
|
72 private static const String ENTRY_SEPARATOR = ";"; //$NON-NLS-1$
|
|
73
|
|
74 /**
|
|
75 * The default-default value for <code>FontData[]</code> preferences.
|
|
76 */
|
|
77 public static const FontData[] FONTDATA_ARRAY_DEFAULT_DEFAULT;
|
|
78
|
|
79 /**
|
|
80 * The default-default value for <code>FontData</code> preferences.
|
|
81 */
|
|
82 public static const FontData FONTDATA_DEFAULT_DEFAULT;
|
|
83 static this() {
|
|
84 POINT_DEFAULT_DEFAULT = new Point(0, 0);
|
|
85 RECTANGLE_DEFAULT_DEFAULT = new Rectangle(0,
|
|
86 0, 0, 0);
|
|
87 COLOR_DEFAULT_DEFAULT = new RGB(0, 0, 0);
|
|
88 Display display = Display.getCurrent();
|
|
89 if (display is null) {
|
|
90 display = Display.getDefault ();
|
|
91 }
|
|
92
|
|
93 FONTDATA_ARRAY_DEFAULT_DEFAULT = display.getSystemFont().getFontData();
|
|
94 /**
|
|
95 * The default-default value for <code>FontData</code> preferences.
|
|
96 * This is left in for compatibility purposes. It is recommended that
|
|
97 * FONTDATA_ARRAY_DEFAULT_DEFAULT is actually used.
|
|
98 */
|
|
99
|
|
100 FONTDATA_DEFAULT_DEFAULT = FONTDATA_ARRAY_DEFAULT_DEFAULT[0];
|
|
101 }
|
|
102
|
|
103 /* (non-Javadoc)
|
|
104 * private constructor to prevent instantiation.
|
|
105 */
|
|
106 private this() {
|
|
107 //no-op
|
|
108 }
|
|
109
|
|
110 /**
|
|
111 * Helper method to construct a color from the given string.
|
|
112 * @param value the indentifier for the color
|
|
113 * @return RGB
|
|
114 */
|
|
115 private static RGB basicGetColor(String value) {
|
|
116
|
|
117 if (IPreferenceStore.STRING_DEFAULT_DEFAULT.equals(value)) {
|
|
118 return COLOR_DEFAULT_DEFAULT;
|
|
119 }
|
|
120
|
|
121 RGB color = StringConverter.asRGB(value, null);
|
|
122 if (color is null) {
|
|
123 return COLOR_DEFAULT_DEFAULT;
|
|
124 }
|
|
125 return color;
|
|
126 }
|
|
127
|
|
128 /**
|
|
129 * Helper method to construct a <code>FontData</code> from the given string.
|
|
130 * String is in the form FontData;FontData; in order that
|
|
131 * multiple FontDatas can be defined.
|
|
132 * @param value the identifier for the font
|
|
133 * @return FontData[]
|
|
134 *
|
|
135 * @since 3.0
|
|
136 */
|
|
137 public static FontData[] basicGetFontData(String value) {
|
|
138 if (IPreferenceStore.STRING_DEFAULT_DEFAULT.equals(value)) {
|
|
139 return FONTDATA_ARRAY_DEFAULT_DEFAULT;
|
|
140 }
|
|
141
|
|
142 //Read in all of them to get the value
|
|
143 auto tokens = tango.text.Util.delimit( value, ENTRY_SEPARATOR);
|
|
144 int numTokens = tokens.length;
|
|
145 FontData[] fontData = new FontData[numTokens];
|
|
146
|
|
147 for (int i = 0; i < numTokens; i++) {
|
|
148 try {
|
|
149 fontData[i] = new FontData(tokens[i]);
|
|
150 } catch (DWTException error) {
|
|
151 return FONTDATA_ARRAY_DEFAULT_DEFAULT;
|
|
152 } catch (IllegalArgumentException error) {
|
|
153 return FONTDATA_ARRAY_DEFAULT_DEFAULT;
|
|
154 }
|
|
155 }
|
|
156 return fontData;
|
|
157 }
|
|
158
|
|
159 /**
|
|
160 * Reads the supplied string and returns its corresponding
|
|
161 * FontData. If it cannot be read then the default FontData
|
|
162 * will be returned.
|
|
163 *
|
|
164 * @param fontDataValue the string value for the font data
|
|
165 * @return the font data
|
|
166 */
|
|
167 public static FontData[] readFontData(String fontDataValue) {
|
|
168 return basicGetFontData(fontDataValue);
|
|
169 }
|
|
170
|
|
171 /**
|
|
172 * Helper method to construct a point from the given string.
|
|
173 * @param value
|
|
174 * @return Point
|
|
175 */
|
|
176 private static Point basicGetPoint(String value) {
|
|
177 Point dp = new Point(POINT_DEFAULT_DEFAULT.x, POINT_DEFAULT_DEFAULT.y);
|
|
178 if (IPreferenceStore.STRING_DEFAULT_DEFAULT.equals(value)) {
|
|
179 return dp;
|
|
180 }
|
|
181 return StringConverter.asPoint(value, dp);
|
|
182 }
|
|
183
|
|
184 /**
|
|
185 * Helper method to construct a rectangle from the given string.
|
|
186 * @param value
|
|
187 * @return Rectangle
|
|
188 */
|
|
189 private static Rectangle basicGetRectangle(String value) {
|
|
190 // We can't just return RECTANGLE_DEFAULT_DEFAULT because
|
|
191 // a rectangle object doesn't have value semantik.
|
|
192 Rectangle dr = new Rectangle(RECTANGLE_DEFAULT_DEFAULT.x,
|
|
193 RECTANGLE_DEFAULT_DEFAULT.y, RECTANGLE_DEFAULT_DEFAULT.width,
|
|
194 RECTANGLE_DEFAULT_DEFAULT.height);
|
|
195
|
|
196 if (IPreferenceStore.STRING_DEFAULT_DEFAULT.equals(value)) {
|
|
197 return dr;
|
|
198 }
|
|
199 return StringConverter.asRectangle(value, dr);
|
|
200 }
|
|
201
|
|
202 /**
|
|
203 * Returns the current value of the color-valued preference with the
|
|
204 * given name in the given preference store.
|
|
205 * Returns the default-default value (<code>COLOR_DEFAULT_DEFAULT</code>)
|
|
206 * if there is no preference with the given name, or if the current value
|
|
207 * cannot be treated as a color.
|
|
208 *
|
|
209 * @param store the preference store
|
|
210 * @param name the name of the preference
|
|
211 * @return the color-valued preference
|
|
212 */
|
|
213 public static RGB getColor(IPreferenceStore store, String name) {
|
|
214 return basicGetColor(store.getString(name));
|
|
215 }
|
|
216
|
|
217 /**
|
|
218 * Returns the default value for the color-valued preference
|
|
219 * with the given name in the given preference store.
|
|
220 * Returns the default-default value (<code>COLOR_DEFAULT_DEFAULT</code>)
|
|
221 * is no default preference with the given name, or if the default
|
|
222 * value cannot be treated as a color.
|
|
223 *
|
|
224 * @param store the preference store
|
|
225 * @param name the name of the preference
|
|
226 * @return the default value of the preference
|
|
227 */
|
|
228 public static RGB getDefaultColor(IPreferenceStore store, String name) {
|
|
229 return basicGetColor(store.getDefaultString(name));
|
|
230 }
|
|
231
|
|
232 /**
|
|
233 * Returns the default value array for the font-valued preference
|
|
234 * with the given name in the given preference store.
|
|
235 * Returns the default-default value (<code>FONTDATA_ARRAY_DEFAULT_DEFAULT</code>)
|
|
236 * is no default preference with the given name, or if the default
|
|
237 * value cannot be treated as font data.
|
|
238 *
|
|
239 * @param store the preference store
|
|
240 * @param name the name of the preference
|
|
241 * @return the default value of the preference
|
|
242 */
|
|
243 public static FontData[] getDefaultFontDataArray(IPreferenceStore store,
|
|
244 String name) {
|
|
245 return basicGetFontData(store.getDefaultString(name));
|
|
246 }
|
|
247
|
|
248 /**
|
|
249 * Returns a single default value for the font-valued preference
|
|
250 * with the given name in the given preference store.
|
|
251 * Returns the default-default value (<code>FONTDATA_DEFAULT_DEFAULT</code>)
|
|
252 * is no default preference with the given name, or if the default
|
|
253 * value cannot be treated as font data.
|
|
254 * This method is provided for backwards compatibility. It is
|
|
255 * recommended that <code>getDefaultFontDataArray</code> is
|
|
256 * used instead.
|
|
257 *
|
|
258 * @param store the preference store
|
|
259 * @param name the name of the preference
|
|
260 * @return the default value of the preference
|
|
261 */
|
|
262 public static FontData getDefaultFontData(IPreferenceStore store,
|
|
263 String name) {
|
|
264 return getDefaultFontDataArray(store, name)[0];
|
|
265 }
|
|
266
|
|
267 /**
|
|
268 * Returns the default value for the point-valued preference
|
|
269 * with the given name in the given preference store.
|
|
270 * Returns the default-default value (<code>POINT_DEFAULT_DEFAULT</code>)
|
|
271 * is no default preference with the given name, or if the default
|
|
272 * value cannot be treated as a point.
|
|
273 *
|
|
274 * @param store the preference store
|
|
275 * @param name the name of the preference
|
|
276 * @return the default value of the preference
|
|
277 */
|
|
278 public static Point getDefaultPoint(IPreferenceStore store, String name) {
|
|
279 return basicGetPoint(store.getDefaultString(name));
|
|
280 }
|
|
281
|
|
282 /**
|
|
283 * Returns the default value for the rectangle-valued preference
|
|
284 * with the given name in the given preference store.
|
|
285 * Returns the default-default value (<code>RECTANGLE_DEFAULT_DEFAULT</code>)
|
|
286 * is no default preference with the given name, or if the default
|
|
287 * value cannot be treated as a rectangle.
|
|
288 *
|
|
289 * @param store the preference store
|
|
290 * @param name the name of the preference
|
|
291 * @return the default value of the preference
|
|
292 */
|
|
293 public static Rectangle getDefaultRectangle(IPreferenceStore store,
|
|
294 String name) {
|
|
295 return basicGetRectangle(store.getDefaultString(name));
|
|
296 }
|
|
297
|
|
298 /**
|
|
299 * Returns the current value of the font-valued preference with the
|
|
300 * given name in the given preference store.
|
|
301 * Returns the default-default value (<code>FONTDATA_ARRAY_DEFAULT_DEFAULT</code>)
|
|
302 * if there is no preference with the given name, or if the current value
|
|
303 * cannot be treated as font data.
|
|
304 *
|
|
305 * @param store the preference store
|
|
306 * @param name the name of the preference
|
|
307 * @return the font-valued preference
|
|
308 */
|
|
309 public static FontData[] getFontDataArray(IPreferenceStore store,
|
|
310 String name) {
|
|
311 return basicGetFontData(store.getString(name));
|
|
312 }
|
|
313
|
|
314 /**
|
|
315 * Returns the current value of the first entry of the
|
|
316 * font-valued preference with the
|
|
317 * given name in the given preference store.
|
|
318 * Returns the default-default value (<code>FONTDATA_ARRAY_DEFAULT_DEFAULT</code>)
|
|
319 * if there is no preference with the given name, or if the current value
|
|
320 * cannot be treated as font data.
|
|
321 * This API is provided for backwards compatibility. It is
|
|
322 * recommended that <code>getFontDataArray</code> is used instead.
|
|
323 *
|
|
324 * @param store the preference store
|
|
325 * @param name the name of the preference
|
|
326 * @return the font-valued preference
|
|
327 */
|
|
328 public static FontData getFontData(IPreferenceStore store, String name) {
|
|
329 return getFontDataArray(store, name)[0];
|
|
330 }
|
|
331
|
|
332 /**
|
|
333 * Returns the current value of the point-valued preference with the
|
|
334 * given name in the given preference store.
|
|
335 * Returns the default-default value (<code>POINT_DEFAULT_DEFAULT</code>)
|
|
336 * if there is no preference with the given name, or if the current value
|
|
337 * cannot be treated as a point.
|
|
338 *
|
|
339 * @param store the preference store
|
|
340 * @param name the name of the preference
|
|
341 * @return the point-valued preference
|
|
342 */
|
|
343 public static Point getPoint(IPreferenceStore store, String name) {
|
|
344 return basicGetPoint(store.getString(name));
|
|
345 }
|
|
346
|
|
347 /**
|
|
348 * Returns the current value of the rectangle-valued preference with the
|
|
349 * given name in the given preference store.
|
|
350 * Returns the default-default value (<code>RECTANGLE_DEFAULT_DEFAULT</code>)
|
|
351 * if there is no preference with the given name, or if the current value
|
|
352 * cannot be treated as a rectangle.
|
|
353 *
|
|
354 * @param store the preference store
|
|
355 * @param name the name of the preference
|
|
356 * @return the rectangle-valued preference
|
|
357 */
|
|
358 public static Rectangle getRectangle(IPreferenceStore store, String name) {
|
|
359 return basicGetRectangle(store.getString(name));
|
|
360 }
|
|
361
|
|
362 /**
|
|
363 * Sets the default value of the preference with the given name
|
|
364 * in the given preference store. As FontDatas are stored as
|
|
365 * arrays this method is only provided for backwards compatibility.
|
|
366 * Use <code>setDefault(IPreferenceStore, String, FontData[])</code>
|
|
367 * instead.
|
|
368 *
|
|
369 * @param store the preference store
|
|
370 * @param name the name of the preference
|
|
371 * @param value the new default value of the preference
|
|
372 */
|
|
373 public static void setDefault(IPreferenceStore store, String name,
|
|
374 FontData value) {
|
|
375 FontData[] fontDatas = new FontData[1];
|
|
376 fontDatas[0] = value;
|
|
377 setDefault(store, name, fontDatas);
|
|
378 }
|
|
379
|
|
380 /**
|
|
381 * Sets the default value of the preference with the given name
|
|
382 * in the given preference store.
|
|
383 *
|
|
384 * @param store the preference store
|
|
385 * @param name the name of the preference
|
|
386 * @param value the new default value of the preference
|
|
387 */
|
|
388 public static void setDefault(IPreferenceStore store, String name,
|
|
389 FontData[] value) {
|
|
390 store.setDefault(name, getStoredRepresentation(value));
|
|
391 }
|
|
392
|
|
393 /**
|
|
394 * Sets the default value of the preference with the given name
|
|
395 * in the given preference store.
|
|
396 *
|
|
397 * @param store the preference store
|
|
398 * @param name the name of the preference
|
|
399 * @param value the new default value of the preference
|
|
400 */
|
|
401 public static void setDefault(IPreferenceStore store, String name,
|
|
402 Point value) {
|
|
403 store.setDefault(name, StringConverter.asString(value));
|
|
404 }
|
|
405
|
|
406 /**
|
|
407 * Sets the default value of the preference with the given name
|
|
408 * in the given preference store.
|
|
409 *
|
|
410 * @param store the preference store
|
|
411 * @param name the name of the preference
|
|
412 * @param value the new default value of the preference
|
|
413 */
|
|
414 public static void setDefault(IPreferenceStore store, String name,
|
|
415 Rectangle value) {
|
|
416 store.setDefault(name, StringConverter.asString(value));
|
|
417 }
|
|
418
|
|
419 /**
|
|
420 * Sets the default value of the preference with the given name
|
|
421 * in the given preference store.
|
|
422 *
|
|
423 * @param store the preference store
|
|
424 * @param name the name of the preference
|
|
425 * @param value the new default value of the preference
|
|
426 */
|
|
427 public static void setDefault(IPreferenceStore store, String name, RGB value) {
|
|
428 store.setDefault(name, StringConverter.asString(value));
|
|
429 }
|
|
430
|
|
431 /**
|
|
432 * Sets the current value of the preference with the given name
|
|
433 * in the given preference store.
|
|
434 * <p>
|
|
435 * Included for backwards compatibility. This method is equivalent to
|
|
436 * </code>setValue(store, name, new FontData[]{value})</code>.
|
|
437 * </p>
|
|
438 *
|
|
439 * @param store the preference store
|
|
440 * @param name the name of the preference
|
|
441 * @param value the new current value of the preference
|
|
442 */
|
|
443 public static void setValue(IPreferenceStore store, String name,
|
|
444 FontData value) {
|
|
445 setValue(store, name, [ value ]);
|
|
446 }
|
|
447
|
|
448 /**
|
|
449 * Sets the current value of the preference with the given name
|
|
450 * in the given preference store. This method also sets the corresponding
|
|
451 * key in the JFace font registry to the value and fires a
|
|
452 * property change event to listeners on the preference store.
|
|
453 *
|
|
454 * <p>
|
|
455 * Note that this API does not update any other settings that may
|
|
456 * be dependant upon it. Only the value in the preference store
|
|
457 * and in the font registry is updated.
|
|
458 * </p>
|
|
459 * @param store the preference store
|
|
460 * @param name the name of the preference
|
|
461 * @param value the new current value of the preference
|
|
462 *
|
|
463 * @see #putValue(IPreferenceStore, String, FontData[])
|
|
464 */
|
|
465 public static void setValue(IPreferenceStore store, String name,
|
|
466 FontData[] value) {
|
|
467 FontData[] oldValue = getFontDataArray(store, name);
|
|
468 // see if the font has changed
|
|
469 if (!Arrays.equals(oldValue, value)) {
|
|
470 store.putValue(name, getStoredRepresentation(value));
|
|
471 JFaceResources.getFontRegistry().put(name, value);
|
|
472 store.firePropertyChangeEvent(name, new ArrayWrapperT!(FontData)(oldValue), new ArrayWrapperT!(FontData)(value));
|
|
473 }
|
|
474 }
|
|
475
|
|
476 /**
|
|
477 * Sets the current value of the preference with the given name
|
|
478 * in the given preference store. This method does not update
|
|
479 * the font registry or fire a property change event.
|
|
480 *
|
|
481 * @param store the preference store
|
|
482 * @param name the name of the preference
|
|
483 * @param value the new current value of the preference
|
|
484 *
|
|
485 * @see PreferenceConverter#setValue(IPreferenceStore, String, FontData[])
|
|
486 */
|
|
487 public static void putValue(IPreferenceStore store, String name,
|
|
488 FontData[] value) {
|
|
489 FontData[] oldValue = getFontDataArray(store, name);
|
|
490 // see if the font has changed
|
|
491 if (!Arrays.equals(oldValue, value)) {
|
|
492 store.putValue(name, getStoredRepresentation(value));
|
|
493 }
|
|
494 }
|
|
495
|
|
496 /**
|
|
497 * Returns the stored representation of the given array of FontData objects.
|
|
498 * The stored representation has the form FontData;FontData;
|
|
499 * Only includes the non-null entries.
|
|
500 *
|
|
501 * @param fontData the array of FontData objects
|
|
502 * @return the stored representation of the FontData objects
|
|
503 * @since 3.0
|
|
504 */
|
|
505 public static String getStoredRepresentation(FontData[] fontData) {
|
|
506 StringBuffer buffer = new StringBuffer();
|
|
507 for (int i = 0; i < fontData.length; i++) {
|
|
508 if (fontData[i] !is null) {
|
|
509 buffer.append(fontData[i].toString());
|
|
510 buffer.append(ENTRY_SEPARATOR);
|
|
511 }
|
|
512 }
|
|
513 return buffer.toString();
|
|
514 }
|
|
515
|
|
516 /**
|
|
517 * Sets the current value of the preference with the given name
|
|
518 * in the given preference store.
|
|
519 *
|
|
520 * @param store the preference store
|
|
521 * @param name the name of the preference
|
|
522 * @param value the new current value of the preference
|
|
523 */
|
|
524 public static void setValue(IPreferenceStore store, String name, Point value) {
|
|
525 Point oldValue = getPoint(store, name);
|
|
526 if (oldValue is null || !oldValue.opEquals(value)) {
|
|
527 store.putValue(name, StringConverter.asString(value));
|
|
528 store.firePropertyChangeEvent(name, oldValue, value);
|
|
529 }
|
|
530 }
|
|
531
|
|
532 /**
|
|
533 * Sets the current value of the preference with the given name
|
|
534 * in the given preference store.
|
|
535 *
|
|
536 * @param store the preference store
|
|
537 * @param name the name of the preference
|
|
538 * @param value the new current value of the preference
|
|
539 */
|
|
540 public static void setValue(IPreferenceStore store, String name,
|
|
541 Rectangle value) {
|
|
542 Rectangle oldValue = getRectangle(store, name);
|
|
543 if (oldValue is null || !oldValue.opEquals(value)) {
|
|
544 store.putValue(name, StringConverter.asString(value));
|
|
545 store.firePropertyChangeEvent(name, oldValue, value);
|
|
546 }
|
|
547 }
|
|
548
|
|
549 /**
|
|
550 * Sets the current value of the preference with the given name
|
|
551 * in the given preference store.
|
|
552 *
|
|
553 * @param store the preference store
|
|
554 * @param name the name of the preference
|
|
555 * @param value the new current value of the preference
|
|
556 */
|
|
557 public static void setValue(IPreferenceStore store, String name, RGB value) {
|
|
558 RGB oldValue = getColor(store, name);
|
|
559 if (oldValue is null || !oldValue.opEquals(value)) {
|
|
560 store.putValue(name, StringConverter.asString(value));
|
|
561 store.firePropertyChangeEvent(name, oldValue, value);
|
|
562 }
|
|
563 }
|
|
564 }
|