1 19 20 package org.openide.src.nodes; 21 22 import java.util.ResourceBundle ; 23 24 import org.openide.src.*; 25 import org.openide.options.SystemOption; 26 import org.openide.util.HelpCtx; 27 import org.openide.util.NbBundle; 28 29 35 44 public final class SourceOptions extends SystemOption { 45 private static final int lastCompatibleVersionTag = 1; 46 private static final int currentVersionTag = 1; 47 48 49 private static ResourceBundle bundle; 50 51 52 private static final byte T_INITIALIZER = 0; 53 private static final byte T_FIELD = 1; 54 private static final byte T_CONSTRUCTOR = 2; 55 private static final byte T_METHOD = 3; 56 private static final byte T_CLASS = 4; 57 private static final byte T_INTERFACE = 5; 58 59 60 static final String [] PROP_NAMES = { 61 "initializerElementFormat", "fieldElementFormat", "constructorElementFormat", "methodElementFormat", "classElementFormat", "interfaceElementFormat" }; 65 66 static Element[] TEST_ELEMENTS; 67 68 69 private static final ElementFormat[] DEFAULT_FORMATS_SHORT = new ElementFormat[6]; 70 71 72 private static final ElementFormat[] DEFAULT_FORMATS_LONG = new ElementFormat[6]; 73 74 78 private ElementFormat[] formats = new ElementFormat[6]; 79 80 83 private int version; 84 85 private static synchronized void loadBundle() { 86 if (bundle != null) 87 return; 88 bundle = NbBundle.getBundle(SourceOptions.class); 89 } 90 91 private static void loadDefaultFormats() { 92 if (DEFAULT_FORMATS_SHORT[0] != null) 93 return; 94 synchronized (SourceOptions.class) { 95 if (DEFAULT_FORMATS_SHORT[0] != null) 96 return; 97 loadBundle(); 98 for (int i = 0; i < 6; i++) { 99 DEFAULT_FORMATS_SHORT[i] = new ElementFormat(bundle.getString("SHORT_"+PROP_NAMES[i])); 100 DEFAULT_FORMATS_LONG[i] = new ElementFormat(bundle.getString("LONG_"+PROP_NAMES[i])); 101 } 102 } 103 } 104 105 108 private void clearElementFormats() { 109 formats = new ElementFormat[6]; 110 } 111 112 113 public static final String PROP_INITIALIZER_FORMAT = PROP_NAMES[T_INITIALIZER]; 114 115 116 public static final String PROP_FIELD_FORMAT = PROP_NAMES[T_FIELD]; 117 118 119 public static final String PROP_CONSTRUCTOR_FORMAT = PROP_NAMES[T_CONSTRUCTOR]; 120 121 122 public static final String PROP_METHOD_FORMAT = PROP_NAMES[T_METHOD]; 123 124 125 public static final String PROP_CLASS_FORMAT = PROP_NAMES[T_CLASS]; 126 127 128 public static final String PROP_INTERFACE_FORMAT = PROP_NAMES[T_INTERFACE]; 129 130 131 public static final String PROP_CATEGORIES_USAGE = "categoriesUsage"; 133 134 private static boolean categories = true; 135 136 static final long serialVersionUID =-2120623049071035434L; 137 138 140 public String displayName () { 141 loadBundle(); 142 return bundle.getString("MSG_sourceOptions"); 143 } 144 145 public HelpCtx getHelpCtx () { 146 return new HelpCtx (SourceOptions.class); 147 } 148 149 151 154 public void setInitializerElementFormat(ElementFormat format) { 155 setElementFormat(T_INITIALIZER, format); 156 } 157 158 161 public ElementFormat getInitializerElementFormat() { 162 return getElementFormat(T_INITIALIZER); 163 } 164 165 168 public void setFieldElementFormat(ElementFormat format) { 169 setElementFormat(T_FIELD, format); 170 } 171 172 private ElementFormat getElementFormat(int type) { 173 synchronized (this) { 174 if (formats[type] != null) 175 return formats[type]; 176 if (isWriteExternal()) 178 return null; 179 } 180 loadDefaultFormats(); 181 return DEFAULT_FORMATS_SHORT[type]; 182 } 183 184 187 public ElementFormat getFieldElementFormat() { 188 return getElementFormat(T_FIELD); 189 } 190 191 194 public void setConstructorElementFormat(ElementFormat format) { 195 setElementFormat(T_CONSTRUCTOR, format); 196 } 197 198 201 public ElementFormat getConstructorElementFormat() { 202 return getElementFormat(T_CONSTRUCTOR); 203 } 204 205 208 public void setMethodElementFormat(ElementFormat format) { 209 setElementFormat(T_METHOD, format); 210 } 211 212 215 public ElementFormat getMethodElementFormat() { 216 return getElementFormat(T_METHOD); 217 } 218 219 222 public void setClassElementFormat(ElementFormat format) { 223 setElementFormat(T_CLASS, format); 224 } 225 226 229 public ElementFormat getClassElementFormat() { 230 return getElementFormat(T_CLASS); 231 } 232 233 236 public void setInterfaceElementFormat(ElementFormat format) { 237 setElementFormat(T_INTERFACE, format); 238 } 239 240 243 public ElementFormat getInterfaceElementFormat() { 244 return getElementFormat(T_INTERFACE); 245 } 246 247 249 252 public ElementFormat getInitializerElementLongFormat() { 253 loadDefaultFormats(); 254 return DEFAULT_FORMATS_LONG[T_INITIALIZER]; 255 } 256 257 260 public ElementFormat getFieldElementLongFormat() { 261 loadDefaultFormats(); 262 return DEFAULT_FORMATS_LONG[T_FIELD]; 263 } 264 265 268 public ElementFormat getConstructorElementLongFormat() { 269 loadDefaultFormats(); 270 return DEFAULT_FORMATS_LONG[T_CONSTRUCTOR]; 271 } 272 273 276 public ElementFormat getMethodElementLongFormat() { 277 loadDefaultFormats(); 278 return DEFAULT_FORMATS_LONG[T_METHOD]; 279 } 280 281 284 public ElementFormat getClassElementLongFormat() { 285 loadDefaultFormats(); 286 return DEFAULT_FORMATS_LONG[T_CLASS]; 287 } 288 289 292 public ElementFormat getInterfaceElementLongFormat() { 293 loadDefaultFormats(); 294 return DEFAULT_FORMATS_LONG[T_INTERFACE]; 295 } 296 297 299 304 public void setCategoriesUsage(boolean cat) { 305 categories = cat; 306 } 307 308 313 public boolean getCategoriesUsage() { 314 return categories; 315 } 316 317 319 private synchronized static Element getTestElement(int index) { 320 if (TEST_ELEMENTS == null) { 321 Element[] els = new Element[6]; 322 323 try { 324 els[T_INITIALIZER] = new InitializerElement(); 325 FieldElement f = new FieldElement(); 326 Identifier id = Identifier.create("foo"); f.setName(id); f.setType(Type.INT); 329 els[T_FIELD] = f; 330 331 MethodElement m = new MethodElement(); 332 m.setName(id); 333 m.setReturn(Type.VOID); 334 els[T_METHOD] = m; 335 els[T_CONSTRUCTOR] = new ConstructorElement(); 336 337 ClassElement c = new ClassElement(); 338 c.setName(id); 339 els[T_CLASS] = els[T_INTERFACE] = c; 340 TEST_ELEMENTS = els; 341 } catch (SourceException ex) { 342 } 344 } 345 return TEST_ELEMENTS[index]; 346 } 347 348 352 private void setElementFormat(byte index, ElementFormat format) { 353 ElementFormat old = formats[index]; 354 if (format != null) { 355 Element el = getTestElement(index); 357 try { 358 format.format(el); 359 } catch (IllegalArgumentException iae) { 360 throw (IllegalArgumentException ) 361 org.openide.ErrorManager.getDefault().annotate( 362 iae, org.openide.ErrorManager.USER, null, 363 bundle.getString("MSG_IllegalElementFormat"), null, null); 365 } 366 } 367 formats[index] = format; 368 firePropertyChange (PROP_NAMES[index], old, formats[index]); 369 } 370 371 public void writeExternal(java.io.ObjectOutput out) throws java.io.IOException { 372 super.writeExternal(out); 373 out.writeInt(version); 374 } 375 376 public void readExternal (java.io.ObjectInput in) 377 throws java.io.IOException , ClassNotFoundException { 378 super.readExternal(in); 379 if (in.available() > 0) { 380 version = in.readInt(); 381 } 382 if (version < lastCompatibleVersionTag) { 383 clearElementFormats(); 384 version = currentVersionTag; 385 } 386 } 387 } 388 | Popular Tags |