1 19 20 package org.netbeans.modules.editor.settings.storage; 21 22 import java.beans.PropertyChangeListener ; 23 import java.beans.PropertyChangeSupport ; 24 import java.io.IOException ; 25 import java.util.Collection ; 26 import java.util.Collections ; 27 import java.util.Enumeration ; 28 import java.util.HashMap ; 29 import java.util.HashSet ; 30 import java.util.Map ; 31 import java.util.Set ; 32 import java.util.logging.Level ; 33 import java.util.logging.Logger ; 34 import javax.swing.text.AttributeSet ; 35 import org.netbeans.api.editor.mimelookup.MimePath; 36 import org.netbeans.modules.editor.settings.storage.api.EditorSettings; 37 import org.netbeans.modules.editor.settings.storage.api.FontColorSettingsFactory; 38 import org.netbeans.modules.editor.settings.storage.api.KeyBindingSettingsFactory; 39 import org.openide.filesystems.FileObject; 40 import org.openide.filesystems.FileSystem; 41 import org.openide.filesystems.Repository; 42 43 49 public class EditorSettingsImpl extends EditorSettings { 50 51 private static final Logger LOG = Logger.getLogger(EditorSettingsImpl.class.getName()); 52 53 private final PropertyChangeSupport pcs = new PropertyChangeSupport (this); 54 55 56 public static final String PROP_HIGHLIGHT_COLORINGS = "editorFontColors"; 58 59 public static final String PROP_TOKEN_COLORINGS = "fontColors"; 61 62 63 public static final String DEFAULT_PROFILE = "NetBeans"; 65 private static final String FATTR_CURRENT_FONT_COLOR_PROFILE = "currentFontColorProfile"; private static final String FATTR_CURRENT_KEYMAP_PROFILE = "currentKeymap"; 69 70 private static final String EDITORS_FOLDER = "Editors"; 72 private static final String KEYMAPS_FOLDER = "Keymaps"; 74 private static EditorSettingsImpl instance = null; 75 76 public static synchronized EditorSettingsImpl getInstance() { 77 if (instance == null) { 78 instance = new EditorSettingsImpl(); 79 } 80 return instance; 81 } 82 83 public Set <String > getAllMimeTypes () { 84 FileObject editorsFo = Repository.getDefault().getDefaultFileSystem().findResource(EDITORS_FOLDER); 85 HashSet <String > mimeTypes = new HashSet <String >(); 86 87 if (editorsFo != null) { 88 for(FileObject f : editorsFo.getChildren()) { 89 if (!f.isFolder()) { 90 continue; 91 } 92 93 String firstPart = f.getNameExt(); 94 for(FileObject ff : f.getChildren()) { 95 if (!ff.isFolder()) { 96 continue; 97 } 98 99 String mimeType = firstPart + "/" + ff.getNameExt(); mimeTypes.add(mimeType); 101 } 102 } 103 } 104 105 return mimeTypes; 106 } 107 108 113 public Set <String > getMimeTypes () { 114 if (mimeTypesWithColoring == null) { 115 init (); 116 } 117 return mimeTypesWithColoring; 118 } 119 120 125 public String getLanguageName (String mimeType) { 126 FileObject fo = Repository.getDefault().getDefaultFileSystem().findResource("Editors/" + mimeType); return fo == null ? mimeType : Utils.getLocalizedName(fo, mimeType, mimeType); 128 } 129 130 131 133 void notifyTokenFontColorChange(MimePath mimePath, String profile) { 134 pcs.firePropertyChange(PROP_TOKEN_COLORINGS, mimePath, profile); 136 } 137 138 143 public Set <String > getFontColorProfiles () { 144 if (fontColorProfiles == null) { 145 init (); 146 } 147 148 Set <String > result = new HashSet <String >(); 149 for(String profile : fontColorProfiles.keySet()) { 150 if (!profile.startsWith ("test")) { 151 result.add(profile); 152 } 153 } 154 155 return result; 156 } 157 158 private Set <String > systemFontColorProfiles; 159 160 166 public boolean isCustomFontColorProfile(String profile) { 167 if (systemFontColorProfiles == null) { 168 init (); 169 } 170 171 return !systemFontColorProfiles.contains(profile); 172 } 173 174 private String currentFontColorProfile; 175 176 181 public String getCurrentFontColorProfile () { 182 if (currentFontColorProfile == null) { 183 FileSystem fs = Repository.getDefault ().getDefaultFileSystem (); 184 FileObject fo = fs.findResource (EDITORS_FOLDER); 185 if (fo != null) { 186 currentFontColorProfile = (String ) fo.getAttribute(FATTR_CURRENT_FONT_COLOR_PROFILE); 187 } 188 if (currentFontColorProfile == null) { 189 currentFontColorProfile = DEFAULT_PROFILE; 190 } 191 } 192 if (!getFontColorProfiles ().contains (currentFontColorProfile)) { 193 currentFontColorProfile = DEFAULT_PROFILE; 194 } 195 return currentFontColorProfile; 196 } 197 198 203 public void setCurrentFontColorProfile (String profile) { 204 String oldProfile = getCurrentFontColorProfile (); 205 if (oldProfile.equals (profile)) return; 206 207 currentFontColorProfile = profile; 208 209 FileSystem fs = Repository.getDefault ().getDefaultFileSystem (); 211 FileObject fo = fs.findResource (EDITORS_FOLDER); 212 if (fo != null) { 213 try { 214 fo.setAttribute (FATTR_CURRENT_FONT_COLOR_PROFILE, profile); 215 } catch (IOException ex) { 216 LOG.log(Level.WARNING, "Can't persist change in current font&colors profile.", ex); } 218 } 219 220 pcs.firePropertyChange (PROP_CURRENT_FONT_COLOR_PROFILE, oldProfile, currentFontColorProfile); 222 } 223 224 233 public Collection <AttributeSet > getDefaultFontColors(String profile) { 234 return getFontColorSettings(new String [0]).getAllFontColors(profile); 235 } 236 237 246 public Collection <AttributeSet > getDefaultFontColorDefaults(String profile) { 247 return getFontColorSettings(new String [0]).getAllFontColorDefaults(profile); 248 } 249 250 258 public void setDefaultFontColors(String profile, Collection <AttributeSet > fontColors) { 259 getFontColorSettings(new String [0]).setAllFontColors(profile, fontColors); 260 } 261 262 private Map <String , Map <String , AttributeSet >> highlightings = new HashMap <String , Map <String , AttributeSet >>(); 264 265 272 public Map <String , AttributeSet > getHighlightings ( 273 String profile 274 ) { 275 profile = getInternalFontColorProfile (profile); 277 278 if (!highlightings.containsKey (profile)) { 279 280 if (profile.startsWith ("test")) { 282 highlightings.put ( 283 profile, 284 getHighlightings (DEFAULT_PROFILE) 285 ); 286 } else { 287 288 Map <String , AttributeSet > m = ColoringStorage.loadColorings 290 (MimePath.EMPTY, profile, false, false); 291 highlightings.put (profile, m); 292 } 293 } 294 295 if (highlightings.get(profile) == null) { 296 return null; 297 } else { 298 return Collections.unmodifiableMap(highlightings.get(profile)); 299 } 300 } 301 302 private Map <String , Map <String , AttributeSet >> highlightingDefaults = new HashMap <String , Map <String , AttributeSet >>(); 304 305 312 public Map <String , AttributeSet > getHighlightingDefaults ( 313 String profile 314 ) { 315 profile = getInternalFontColorProfile (profile); 317 318 if (!highlightingDefaults.containsKey (profile)) { 320 Map <String , AttributeSet > m = ColoringStorage.loadColorings 321 (MimePath.EMPTY, profile, false, true); 322 highlightingDefaults.put (profile, m); 323 } 324 325 if (highlightingDefaults.get(profile) == null) { 326 return null; 327 } else { 328 return Collections.unmodifiableMap(highlightingDefaults.get(profile)); 329 } 330 } 331 332 338 public void setHighlightings ( 339 String profile, 340 Map <String , AttributeSet > fontColors 341 ) { 342 String internalProfile = getInternalFontColorProfile (profile); 344 345 if (fontColors == null) { 346 ColoringStorage.deleteColorings 348 (MimePath.EMPTY, internalProfile, false, false); 349 highlightings.remove (internalProfile); 350 init (); 351 } else { 352 353 if (fontColors.equals (highlightings.get (internalProfile))) return; 354 355 fontColors = Utils.immutize(fontColors); 357 highlightings.put (internalProfile, fontColors); 358 359 if (!internalProfile.startsWith ("test")) { 361 ColoringStorage.saveColorings ( 362 MimePath.EMPTY, 363 internalProfile, 364 false, 365 false, 366 fontColors.values () 367 ); 368 if (fontColorProfiles.get (profile) == null) 369 fontColorProfiles.put (profile, profile); 370 } 371 } 372 373 pcs.firePropertyChange(PROP_HIGHLIGHT_COLORINGS, MimePath.EMPTY, internalProfile); 374 } 375 376 377 379 384 public Set <String > getKeyMapProfiles () { 385 if (keyMapProfiles == null) init (); 386 return Collections.unmodifiableSet (keyMapProfiles.keySet ()); 387 } 388 389 private Set <String > systemKeymapProfiles; 390 391 397 public boolean isCustomKeymapProfile (String profile) { 398 if (systemKeymapProfiles == null) { 399 init(); 400 } 401 402 return !systemKeymapProfiles.contains (profile); 403 } 404 405 private String currentKeyMapProfile; 406 407 412 public String getCurrentKeyMapProfile () { 413 if (currentKeyMapProfile == null) { 414 FileSystem fs = Repository.getDefault ().getDefaultFileSystem (); 415 FileObject fo = fs.findResource (KEYMAPS_FOLDER); 416 currentKeyMapProfile = fo == null ? null : (String ) fo.getAttribute (FATTR_CURRENT_KEYMAP_PROFILE); 417 if (currentKeyMapProfile == null) 418 currentKeyMapProfile = DEFAULT_PROFILE; 419 } 420 return currentKeyMapProfile; 421 } 422 423 428 public void setCurrentKeyMapProfile (String keyMapName) { 429 String oldKeyMap = getCurrentKeyMapProfile (); 430 if (oldKeyMap.equals (keyMapName)) return; 431 432 currentKeyMapProfile = keyMapName; 433 434 try { 436 FileSystem fs = Repository.getDefault ().getDefaultFileSystem (); 437 FileObject fo = fs.findResource (KEYMAPS_FOLDER); 438 if (fo == null) { 439 fo = fs.getRoot ().createFolder (KEYMAPS_FOLDER); 440 } 441 fo.setAttribute (FATTR_CURRENT_KEYMAP_PROFILE, keyMapName); 442 } catch (IOException ex) { 443 LOG.log(Level.WARNING, "Can't persist change in current keybindings profile.", ex); } 445 446 pcs.firePropertyChange (PROP_CURRENT_KEY_MAP_PROFILE, oldKeyMap, currentKeyMapProfile); 448 } 449 450 455 public void addPropertyChangeListener ( 456 PropertyChangeListener l 457 ) { 458 pcs.addPropertyChangeListener (l); 459 } 460 461 466 public void removePropertyChangeListener ( 467 PropertyChangeListener l 468 ) { 469 pcs.removePropertyChangeListener (l); 470 } 471 472 478 public void addPropertyChangeListener ( 479 String propertyName, 480 PropertyChangeListener l 481 ) { 482 pcs.addPropertyChangeListener (propertyName, l); 483 } 484 485 491 public void removePropertyChangeListener ( 492 String propertyName, 493 PropertyChangeListener l 494 ) { 495 pcs.removePropertyChangeListener (propertyName, l); 496 } 497 498 499 501 private Map <String , String > fontColorProfiles; 502 private Map <String , String > keyMapProfiles; 503 private Set <String > mimeTypesWithColoring; 504 505 private EditorSettingsImpl() { 506 507 } 508 509 private void init () { 510 fontColorProfiles = new HashMap <String , String >(); 511 keyMapProfiles = new HashMap <String , String >(); 512 keyMapProfiles.put (DEFAULT_PROFILE, DEFAULT_PROFILE); 513 mimeTypesWithColoring = new HashSet <String >(); 514 systemFontColorProfiles = new HashSet <String >(); 515 systemKeymapProfiles = new HashSet <String >(); 516 FileSystem fs = Repository.getDefault ().getDefaultFileSystem (); 517 FileObject fo = fs.findResource (EDITORS_FOLDER); 518 if (fo != null) { 519 Enumeration e = fo.getFolders (false); 520 while (e.hasMoreElements()) { 521 init1 ((FileObject) e.nextElement ()); 522 } 523 } 524 525 mimeTypesWithColoring = Collections.unmodifiableSet(mimeTypesWithColoring); 526 } 527 528 private void init1 (FileObject fo) { 529 Enumeration e = fo.getChildren (false); 530 while (e.hasMoreElements ()) 531 init2 ((FileObject) e.nextElement ()); 532 } 533 534 private void init2 (FileObject fo) { 535 if (fo.getNameExt ().equals (ColoringStorage.DEFAULTS_FOLDER) && fo.isFolder () && 536 fo.getFileObject (ColoringStorage.HIGHLIGHTING_FILE_NAME) != null 537 ) 538 addFontColorsProfile (fo, true); else 540 if (fo.getNameExt ().equals (ColoringStorage.HIGHLIGHTING_FILE_NAME)) 541 addFontColorsProfile (fo, false); else 543 if (fo.getFileObject (DEFAULT_PROFILE + "/" + ColoringStorage.DEFAULTS_FOLDER + "/" + ColoringStorage.COLORING_FILE_NAME) != null) addMimeType (fo); else 546 if (fo.getPath ().endsWith ("text/base") && fo.isFolder ()) { if (fo.getFileObject (KeyMapsStorage.DEFAULTS_FOLDER + "/" + KeyMapsStorage.KEYBINDING_FILE_NAME) != null) addKeyMapProfile (fo, true); else 550 if (fo.getFileObject (KeyMapsStorage.KEYBINDING_FILE_NAME) != null) 551 addKeyMapProfile (fo, false); Enumeration e = fo.getChildren (false); 553 while (e.hasMoreElements ()) { 554 FileObject ff = (FileObject) e.nextElement (); 555 if (!ff.getNameExt().equals(KeyMapsStorage.DEFAULTS_FOLDER)) { 556 init3 (ff); 557 } 558 } 559 } 560 } 561 562 private void init3 (FileObject fo) { 563 if (fo.getFileObject (KeyMapsStorage.DEFAULTS_FOLDER + "/" + KeyMapsStorage.KEYBINDING_FILE_NAME) != null) addKeyMapProfile (fo, true); else 566 if (fo.getFileObject (KeyMapsStorage.KEYBINDING_FILE_NAME) != null) 567 addKeyMapProfile (fo, false); } 569 570 private void addMimeType(FileObject fo) { 571 String mimeType = fo.getPath().substring(8); 572 mimeTypesWithColoring.add(mimeType); 573 } 574 575 private void addFontColorsProfile(FileObject fo, boolean systemProfile) { 576 String profile = fo.getParent().getNameExt(); 577 String displayName = Utils.getLocalizedName(fo.getParent(), profile, profile); 578 579 if (systemProfile) { 580 systemFontColorProfiles.add(displayName); 581 } 582 583 fontColorProfiles.put(displayName, profile); 584 } 585 586 private void addKeyMapProfile(FileObject fo, boolean systemProfile) { 587 String profile = fo.getNameExt(); 588 if (profile.equals("base")) { profile = DEFAULT_PROFILE; 590 } 591 592 String displayName = Utils.getLocalizedName(fo, profile, profile); 593 594 if (systemProfile) { 595 systemKeymapProfiles.add(displayName); 596 } 597 598 keyMapProfiles.put(displayName, profile); 599 } 600 601 606 String getInternalFontColorProfile(String profile) { 607 if (fontColorProfiles == null) { 608 init (); 609 } 610 611 String result = fontColorProfiles.get(profile); 612 return result != null ? result : profile; 613 } 614 615 String getInternalKeymapProfile (String profile) { 616 if (keyMapProfiles == null) { 617 init(); 618 } 619 620 String result = keyMapProfiles.get (profile); 621 if (result != null) { 622 return result; 623 } else { 624 keyMapProfiles.put(profile, profile); 625 return profile; 626 } 627 } 628 629 public KeyBindingSettingsFactory getKeyBindingSettings (String [] mimeTypes) { 630 mimeTypes = filter(mimeTypes); 631 return KeyBindingSettingsImpl.get(Utils.mimeTypes2mimePath(mimeTypes)); 632 } 633 634 public FontColorSettingsFactory getFontColorSettings (String [] mimeTypes) { 635 mimeTypes = filter(mimeTypes); 636 return FontColorSettingsImpl.get(Utils.mimeTypes2mimePath(mimeTypes)); 637 } 638 639 private String [] filter(String [] mimeTypes) { 640 if (mimeTypes.length > 0 && mimeTypes[0].startsWith("test")) { String [] filtered = new String [mimeTypes.length]; 642 System.arraycopy(mimeTypes, 0, filtered, 0, mimeTypes.length); 643 filtered[0] = mimeTypes[0].substring(mimeTypes[0].indexOf('_') + 1); 645 LOG.log(Level.INFO, "Don't use 'test' mime type to access settings through the editor/settings/storage API!", new Throwable ("Stacktrace")); 646 647 return filtered; 648 } else { 649 return mimeTypes; 650 } 651 } 652 } 653 | Popular Tags |