1 11 package org.eclipse.jface.preference; 12 13 import java.util.Arrays ; 14 import java.util.StringTokenizer ; 15 16 import org.eclipse.jface.resource.JFaceResources; 17 import org.eclipse.jface.resource.StringConverter; 18 import org.eclipse.swt.SWTException; 19 import org.eclipse.swt.graphics.FontData; 20 import org.eclipse.swt.graphics.Point; 21 import org.eclipse.swt.graphics.RGB; 22 import org.eclipse.swt.graphics.Rectangle; 23 import org.eclipse.swt.widgets.Display; 24 25 45 public class PreferenceConverter { 46 47 51 public static final Point POINT_DEFAULT_DEFAULT = new Point(0, 0); 52 53 57 public static final Rectangle RECTANGLE_DEFAULT_DEFAULT = new Rectangle(0, 58 0, 0, 0); 59 60 64 public static final RGB COLOR_DEFAULT_DEFAULT = new RGB(0, 0, 0); 65 66 private static final String ENTRY_SEPARATOR = ";"; 68 71 public static final FontData[] FONTDATA_ARRAY_DEFAULT_DEFAULT; 72 73 76 public static final FontData FONTDATA_DEFAULT_DEFAULT; 77 static { 78 Display display = Display.getCurrent(); 79 if (display == null) { 80 display = Display.getDefault (); 81 } 82 83 FONTDATA_ARRAY_DEFAULT_DEFAULT = display.getSystemFont().getFontData(); 84 89 90 FONTDATA_DEFAULT_DEFAULT = FONTDATA_ARRAY_DEFAULT_DEFAULT[0]; 91 } 92 93 96 private PreferenceConverter() { 97 } 99 100 105 private static RGB basicGetColor(String value) { 106 107 if (IPreferenceStore.STRING_DEFAULT_DEFAULT.equals(value)) { 108 return COLOR_DEFAULT_DEFAULT; 109 } 110 111 RGB color = StringConverter.asRGB(value, null); 112 if (color == null) { 113 return COLOR_DEFAULT_DEFAULT; 114 } 115 return color; 116 } 117 118 127 public static FontData[] basicGetFontData(String value) { 128 if (IPreferenceStore.STRING_DEFAULT_DEFAULT.equals(value)) { 129 return FONTDATA_ARRAY_DEFAULT_DEFAULT; 130 } 131 132 StringTokenizer tokenizer = new StringTokenizer (value, ENTRY_SEPARATOR); 134 int numTokens = tokenizer.countTokens(); 135 FontData[] fontData = new FontData[numTokens]; 136 137 for (int i = 0; i < numTokens; i++) { 138 try { 139 fontData[i] = new FontData(tokenizer.nextToken()); 140 } catch (SWTException error) { 141 return FONTDATA_ARRAY_DEFAULT_DEFAULT; 142 } catch (IllegalArgumentException error) { 143 return FONTDATA_ARRAY_DEFAULT_DEFAULT; 144 } 145 } 146 return fontData; 147 } 148 149 157 public static FontData[] readFontData(String fontDataValue) { 158 return basicGetFontData(fontDataValue); 159 } 160 161 166 private static Point basicGetPoint(String value) { 167 Point dp = new Point(POINT_DEFAULT_DEFAULT.x, POINT_DEFAULT_DEFAULT.y); 168 if (IPreferenceStore.STRING_DEFAULT_DEFAULT.equals(value)) { 169 return dp; 170 } 171 return StringConverter.asPoint(value, dp); 172 } 173 174 179 private static Rectangle basicGetRectangle(String value) { 180 Rectangle dr = new Rectangle(RECTANGLE_DEFAULT_DEFAULT.x, 183 RECTANGLE_DEFAULT_DEFAULT.y, RECTANGLE_DEFAULT_DEFAULT.width, 184 RECTANGLE_DEFAULT_DEFAULT.height); 185 186 if (IPreferenceStore.STRING_DEFAULT_DEFAULT.equals(value)) { 187 return dr; 188 } 189 return StringConverter.asRectangle(value, dr); 190 } 191 192 203 public static RGB getColor(IPreferenceStore store, String name) { 204 return basicGetColor(store.getString(name)); 205 } 206 207 218 public static RGB getDefaultColor(IPreferenceStore store, String name) { 219 return basicGetColor(store.getDefaultString(name)); 220 } 221 222 233 public static FontData[] getDefaultFontDataArray(IPreferenceStore store, 234 String name) { 235 return basicGetFontData(store.getDefaultString(name)); 236 } 237 238 252 public static FontData getDefaultFontData(IPreferenceStore store, 253 String name) { 254 return getDefaultFontDataArray(store, name)[0]; 255 } 256 257 268 public static Point getDefaultPoint(IPreferenceStore store, String name) { 269 return basicGetPoint(store.getDefaultString(name)); 270 } 271 272 283 public static Rectangle getDefaultRectangle(IPreferenceStore store, 284 String name) { 285 return basicGetRectangle(store.getDefaultString(name)); 286 } 287 288 299 public static FontData[] getFontDataArray(IPreferenceStore store, 300 String name) { 301 return basicGetFontData(store.getString(name)); 302 } 303 304 318 public static FontData getFontData(IPreferenceStore store, String name) { 319 return getFontDataArray(store, name)[0]; 320 } 321 322 333 public static Point getPoint(IPreferenceStore store, String name) { 334 return basicGetPoint(store.getString(name)); 335 } 336 337 348 public static Rectangle getRectangle(IPreferenceStore store, String name) { 349 return basicGetRectangle(store.getString(name)); 350 } 351 352 363 public static void setDefault(IPreferenceStore store, String name, 364 FontData value) { 365 FontData[] fontDatas = new FontData[1]; 366 fontDatas[0] = value; 367 setDefault(store, name, fontDatas); 368 } 369 370 378 public static void setDefault(IPreferenceStore store, String name, 379 FontData[] value) { 380 store.setDefault(name, getStoredRepresentation(value)); 381 } 382 383 391 public static void setDefault(IPreferenceStore store, String name, 392 Point value) { 393 store.setDefault(name, StringConverter.asString(value)); 394 } 395 396 404 public static void setDefault(IPreferenceStore store, String name, 405 Rectangle value) { 406 store.setDefault(name, StringConverter.asString(value)); 407 } 408 409 417 public static void setDefault(IPreferenceStore store, String name, RGB value) { 418 store.setDefault(name, StringConverter.asString(value)); 419 } 420 421 433 public static void setValue(IPreferenceStore store, String name, 434 FontData value) { 435 setValue(store, name, new FontData[] { value }); 436 } 437 438 455 public static void setValue(IPreferenceStore store, String name, 456 FontData[] value) { 457 FontData[] oldValue = getFontDataArray(store, name); 458 if (!Arrays.equals(oldValue, value)) { 460 store.putValue(name, getStoredRepresentation(value)); 461 JFaceResources.getFontRegistry().put(name, value); 462 store.firePropertyChangeEvent(name, oldValue, value); 463 } 464 } 465 466 477 public static void putValue(IPreferenceStore store, String name, 478 FontData[] value) { 479 FontData[] oldValue = getFontDataArray(store, name); 480 if (!Arrays.equals(oldValue, value)) { 482 store.putValue(name, getStoredRepresentation(value)); 483 } 484 } 485 486 495 public static String getStoredRepresentation(FontData[] fontData) { 496 StringBuffer buffer = new StringBuffer (); 497 for (int i = 0; i < fontData.length; i++) { 498 if (fontData[i] != null) { 499 buffer.append(fontData[i].toString()); 500 buffer.append(ENTRY_SEPARATOR); 501 } 502 } 503 return buffer.toString(); 504 } 505 506 514 public static void setValue(IPreferenceStore store, String name, Point value) { 515 Point oldValue = getPoint(store, name); 516 if (oldValue == null || !oldValue.equals(value)) { 517 store.putValue(name, StringConverter.asString(value)); 518 store.firePropertyChangeEvent(name, oldValue, value); 519 } 520 } 521 522 530 public static void setValue(IPreferenceStore store, String name, 531 Rectangle value) { 532 Rectangle oldValue = getRectangle(store, name); 533 if (oldValue == null || !oldValue.equals(value)) { 534 store.putValue(name, StringConverter.asString(value)); 535 store.firePropertyChangeEvent(name, oldValue, value); 536 } 537 } 538 539 547 public static void setValue(IPreferenceStore store, String name, RGB value) { 548 RGB oldValue = getColor(store, name); 549 if (oldValue == null || !oldValue.equals(value)) { 550 store.putValue(name, StringConverter.asString(value)); 551 store.firePropertyChangeEvent(name, oldValue, value); 552 } 553 } 554 } 555 | Popular Tags |