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