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