1 19 20 21 package org.netbeans.editor; 22 23 import java.net.URL ; 24 import java.util.Map ; 25 import java.util.Iterator ; 26 import java.util.HashMap ; 27 import java.net.MalformedURLException ; 28 import org.netbeans.editor.Settings; 29 import org.netbeans.editor.WeakPropertyChangeSupport; 30 import javax.swing.SwingUtilities ; 31 import java.util.Set ; 32 import java.util.HashSet ; 33 34 42 43 public class AnnotationTypes { 44 45 46 public static final String PROP_BACKGROUND_DRAWING = "backgroundDrawing"; 48 49 public static final String PROP_BACKGROUND_GLYPH_ALPHA = "backgroundGlyphAlpha"; 51 52 public static final String PROP_COMBINE_GLYPHS = "combineGlyphs"; 54 55 public static final String PROP_GLYPHS_OVER_LINE_NUMBERS = "glyphsOverLineNumbers"; 57 60 public static final String PROP_SHOW_GLYPH_GUTTER = "showGlyphGutter"; 62 63 public static final String PROP_ANNOTATION_TYPES = "annotationTypes"; 65 66 private Map properties; 67 68 69 private WeakPropertyChangeSupport support; 70 71 72 private Map allTypes = null; 73 74 75 private boolean loadedTypes = false; 76 77 78 private boolean loadedSettings = false; 79 80 81 private boolean loadingInProgress = false; 82 83 84 private Loader loader = null; 85 86 87 private static URL defaultGlyphIcon = null; 88 89 90 private static AnnotationTypes annoTypes = null; 91 92 private AnnotationTypes() { 93 properties = new HashMap (5*4/3); 94 support = new WeakPropertyChangeSupport(); 95 } 96 97 98 public static AnnotationTypes getTypes() { 99 if (annoTypes == null) { 100 annoTypes = new AnnotationTypes(); 101 } 102 return annoTypes; 103 } 104 105 107 public static URL getDefaultGlyphURL() { 108 if (defaultGlyphIcon == null) { 109 try { 110 defaultGlyphIcon = new URL ("nbresloc:/org/netbeans/editor/resources/defaultglyph.gif"); } catch (MalformedURLException ex) { 113 Utilities.annotateLoggable(ex); 114 } 115 } 116 117 return defaultGlyphIcon; 118 } 119 120 122 public Boolean isBackgroundDrawing() { 123 loadSettings(); 124 125 Boolean b = (Boolean )getProp(PROP_BACKGROUND_DRAWING); 126 if (b == null) 127 return Boolean.FALSE; 128 return b; 129 } 130 131 133 public void setBackgroundDrawing(Boolean drawing) { 134 if (!isBackgroundDrawing().equals(drawing)) { 135 putProp(PROP_BACKGROUND_DRAWING, drawing); 136 firePropertyChange(PROP_BACKGROUND_DRAWING, null, null); 137 Settings.touchValue(null, null); 139 saveSetting(PROP_BACKGROUND_DRAWING, drawing); 140 } 141 } 142 143 145 public Boolean isCombineGlyphs() { 146 loadSettings(); 147 148 Boolean b = (Boolean )getProp(PROP_COMBINE_GLYPHS); 149 if (b == null) 150 return Boolean.TRUE; 151 return b; 152 } 153 154 156 public void setCombineGlyphs(Boolean combine) { 157 if (!isCombineGlyphs().equals(combine)) { 158 putProp(PROP_COMBINE_GLYPHS, combine); 159 firePropertyChange(PROP_COMBINE_GLYPHS, null, null); 160 Settings.touchValue(null, null); 162 saveSetting(PROP_COMBINE_GLYPHS, combine); 163 } 164 } 165 166 168 public Integer getBackgroundGlyphAlpha() { 169 loadSettings(); 170 171 if (getProp(PROP_BACKGROUND_GLYPH_ALPHA) == null) 172 return new Integer (40); 173 return (Integer )getProp(PROP_BACKGROUND_GLYPH_ALPHA); 174 } 175 176 178 public void setBackgroundGlyphAlpha(int alpha) { 179 if (alpha < 0 || alpha > 100) { 180 return; 181 } 182 Integer i = new Integer (alpha); 183 putProp(PROP_BACKGROUND_GLYPH_ALPHA, i); 184 firePropertyChange(PROP_BACKGROUND_GLYPH_ALPHA, null, null); 185 Settings.touchValue(null, null); 187 saveSetting(PROP_BACKGROUND_GLYPH_ALPHA, i); 188 } 189 190 192 public Boolean isGlyphsOverLineNumbers() { 193 loadSettings(); 194 195 Boolean b = (Boolean )getProp(PROP_GLYPHS_OVER_LINE_NUMBERS); 196 if (b == null) 197 return Boolean.TRUE; 198 return b; 199 } 200 201 203 public void setGlyphsOverLineNumbers(Boolean over) { 204 if (!isGlyphsOverLineNumbers().equals(over)) { 205 putProp(PROP_GLYPHS_OVER_LINE_NUMBERS, over); 206 firePropertyChange(PROP_GLYPHS_OVER_LINE_NUMBERS, null, null); 207 saveSetting(PROP_GLYPHS_OVER_LINE_NUMBERS, over); 208 } 209 } 210 211 213 public Boolean isShowGlyphGutter() { 214 loadSettings(); 215 216 Boolean b = (Boolean )getProp(PROP_SHOW_GLYPH_GUTTER); 217 if (b == null) 218 return Boolean.TRUE; 219 return b; 220 } 221 222 224 public void setShowGlyphGutter(Boolean gutter) { 225 if (!isShowGlyphGutter().equals(gutter)) { 226 putProp(PROP_SHOW_GLYPH_GUTTER, gutter); 227 firePropertyChange(PROP_SHOW_GLYPH_GUTTER, null, null); 228 saveSetting(PROP_SHOW_GLYPH_GUTTER, gutter); 229 } 230 } 231 232 233 private Object getProp(String prop){ 234 return properties.get(prop); 235 } 236 237 238 private void putProp(Object key, Object value){ 239 if (value == null) { 240 properties.remove(key); 241 return; 242 } 243 properties.put(key,value); 244 } 245 246 247 249 final public void addPropertyChangeListener(java.beans.PropertyChangeListener l) { 250 support.addPropertyChangeListener (l); 251 } 252 253 255 final public void removePropertyChangeListener(java.beans.PropertyChangeListener l) { 256 support.removePropertyChangeListener (l); 257 } 258 259 260 final protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) { 261 support.firePropertyChange(this, propertyName, oldValue, newValue); 262 } 263 264 266 public final void setTypes(Map map) { 267 if (allTypes != null) { 268 allTypes = map; 269 SwingUtilities.invokeLater(new FirePropertyChange()); 273 } else { 274 allTypes = map; 275 } 276 } 277 278 public final void removeType(String name) { 279 allTypes.remove(name); 280 SwingUtilities.invokeLater(new FirePropertyChange()); 281 } 282 283 286 public final AnnotationType getType(String name) { 287 loadTypes(); 288 289 AnnotationType ret = null; 290 if (allTypes != null){ 291 ret = (AnnotationType)allTypes.get(name); 292 } 293 294 if (ret == null){ 295 Utilities.annotateLoggable(new NullPointerException ("null AnnotationType for:"+name)); } 297 298 return ret; 299 } 300 301 302 public Iterator getAnnotationTypeNames() { 303 loadTypes(); 304 Set temp = new HashSet (); 305 if (allTypes != null) 306 temp.addAll(allTypes.keySet()); 307 return temp.iterator(); 308 } 309 310 311 public int getAnnotationTypeNamesCount() { 312 loadTypes(); 313 314 return allTypes.keySet().size(); 315 } 316 317 318 public int getVisibleAnnotationTypeNamesCount() { 319 loadTypes(); 320 321 Iterator i = getAnnotationTypeNames(); 322 int count = 0; 323 for (; i.hasNext(); ) { 324 AnnotationType type = getType((String )i.next()); 325 if (type == null) 326 continue; 327 if (type.isVisible()) 328 count++; 329 } 330 return count; 331 } 332 333 334 public void registerLoader(Loader l) { 335 loader = l; 336 loadedTypes = false; 337 loadedSettings = false; 338 } 339 340 341 private void loadTypes() { 342 if (loadedTypes || loader == null) 343 return; 344 345 loader.loadTypes(); 346 347 loadedTypes = true; 348 } 349 350 351 public void saveType(AnnotationType type) { 352 if (!loadedTypes || loader == null) 353 return; 354 355 loader.saveType(type); 356 } 357 358 359 private void loadSettings() { 360 if (loadedSettings || loader == null || loadingInProgress) 361 return; 362 363 loadingInProgress = true; 364 loader.loadSettings(); 365 loadingInProgress = false; 366 367 loadedSettings = true; 368 } 369 370 371 public void saveSetting(String settingName, Object value) { 372 if (!loadedSettings || loader == null) 373 return; 374 375 loader.saveSetting(settingName, value); 376 } 377 378 380 public interface Loader { 381 382 383 public void loadTypes(); 384 385 386 public void loadSettings(); 387 388 389 public void saveType(AnnotationType type); 390 391 392 public void saveSetting(String settingName, Object value); 393 } 394 395 396 private class FirePropertyChange implements Runnable { 397 FirePropertyChange() { 398 } 399 public void run() { 400 firePropertyChange(PROP_ANNOTATION_TYPES, null, null); 401 } 402 } 403 404 } 405 | Popular Tags |