1 19 20 package org.netbeans.modules.options.colors; 21 22 import java.awt.BorderLayout ; 23 import java.awt.Color ; 24 import java.awt.Component ; 25 import java.awt.Cursor ; 26 import java.awt.Image ; 27 import java.awt.Toolkit ; 28 import java.io.BufferedReader ; 29 import java.io.FileNotFoundException ; 30 import java.io.IOException ; 31 import java.io.InputStream ; 32 import java.io.InputStreamReader ; 33 import java.net.URL ; 34 import java.util.ArrayList ; 35 import java.util.Collection ; 36 import java.util.HashMap ; 37 import java.util.HashSet ; 38 import java.util.Iterator ; 39 import java.util.List ; 40 import java.util.Map ; 41 import java.util.Set ; 42 import javax.swing.ImageIcon ; 43 import javax.swing.JEditorPane ; 44 import javax.swing.JPanel ; 45 import javax.swing.SwingUtilities ; 46 import javax.swing.event.CaretEvent ; 47 import javax.swing.event.CaretListener ; 48 import javax.swing.text.AttributeSet ; 49 import javax.swing.text.BadLocationException ; 50 import javax.swing.text.Document ; 51 import javax.swing.text.SimpleAttributeSet ; 52 import javax.swing.text.StyleConstants ; 53 import org.netbeans.api.editor.settings.EditorStyleConstants; 54 import org.netbeans.editor.AnnotationType; 55 import org.netbeans.editor.AnnotationTypes; 56 import org.netbeans.editor.EditorUI; 57 import org.netbeans.editor.SyntaxSupport; 58 import org.netbeans.editor.TokenItem; 59 import org.netbeans.editor.Utilities; 60 import org.netbeans.editor.ext.ExtSyntaxSupport; 61 import org.netbeans.modules.editor.settings.storage.api.EditorSettings; 62 import org.netbeans.modules.editor.settings.storage.api.FontColorSettingsFactory; 63 import org.openide.filesystems.FileObject; 64 import org.openide.filesystems.FileSystem; 65 import org.openide.filesystems.Repository; 66 import org.openide.text.CloneableEditorSupport; 67 import org.openide.util.NbBundle; 68 69 public final class ColorModel { 70 71 static final String ALL_LANGUAGES = NbBundle.getMessage(ColorModel.class, "CTL_All_Languages"); private static final String HIGHLIGHTING_LANGUAGE = "Highlighting"; private static final String [] EMPTY_MIMEPATH = new String [0]; 74 75 private EditorSettings editorSettings = EditorSettings.getDefault (); 76 77 78 79 81 public Set getProfiles () { 82 return editorSettings.getFontColorProfiles (); 83 } 84 85 public String getCurrentProfile () { 86 return editorSettings.getCurrentFontColorProfile (); 87 } 88 89 public boolean isCustomProfile (String profile) { 90 return editorSettings.isCustomFontColorProfile (profile); 91 } 92 93 public void setCurrentProfile (String profile) { 94 editorSettings.setCurrentFontColorProfile (profile); 95 } 96 97 98 100 public Collection getAnnotations (String profile) { 101 Collection annotations = new ArrayList (); 102 for(Iterator it = AnnotationTypes.getTypes().getAnnotationTypeNames(); it.hasNext(); ) { 103 String name = (String ) it.next (); 104 105 AnnotationType annotationType = AnnotationTypes.getTypes().getType(name); 106 if (!annotationType.isVisible()) { 107 continue; 108 } 109 110 String description = annotationType.getDescription(); 111 if (description == null) { 112 continue; 113 } 114 115 SimpleAttributeSet category = new SimpleAttributeSet (); 116 category.addAttribute(EditorStyleConstants.DisplayName, description); 117 category.addAttribute(StyleConstants.NameAttribute, description); 118 119 URL iconURL = annotationType.getGlyph (); 120 Image image = null; 121 if (iconURL.getProtocol ().equals ("nbresloc")) { image = org.openide.util.Utilities.loadImage(iconURL.getPath().substring(1)); 123 } else { 124 image = Toolkit.getDefaultToolkit ().getImage (iconURL); 125 } 126 if (image != null) { 127 category.addAttribute("icon", new ImageIcon (image)); } 129 130 Color bgColor = annotationType.getHighlight(); 131 if (annotationType.isUseHighlightColor() && bgColor != null) { 132 category.addAttribute(StyleConstants.Background, bgColor); 133 } 134 135 Color fgColor = annotationType.getForegroundColor(); 136 if (!annotationType.isInheritForegroundColor() && fgColor != null) { 137 category.addAttribute(StyleConstants.Foreground, fgColor); 138 } 139 140 Color underColor = annotationType.getWaveUnderlineColor(); 141 if (annotationType.isUseWaveUnderlineColor() && underColor != null) { 142 category.addAttribute(EditorStyleConstants.WaveUnderlineColor, underColor); 143 } 144 145 category.addAttribute("annotationType", annotationType); annotations.add(category); 147 } 148 149 return annotations; 150 } 151 152 public void setAnnotations ( 153 String profile, 154 Collection annotations 155 ) { 156 Iterator it = annotations.iterator (); 157 while (it.hasNext ()) { 159 AttributeSet category = (AttributeSet ) it.next (); 160 AnnotationType annotationType = (AnnotationType) 161 category.getAttribute ("annotationType"); 162 163 if (category.isDefined (StyleConstants.Background)) { 164 annotationType.setUseHighlightColor (true); 165 annotationType.setHighlight ( 166 (Color ) category.getAttribute (StyleConstants.Background) 167 ); 168 } else 169 annotationType.setUseHighlightColor (false); 170 if (category.isDefined (StyleConstants.Foreground)) { 171 annotationType.setInheritForegroundColor (false); 172 annotationType.setForegroundColor ( 173 (Color ) category.getAttribute (StyleConstants.Foreground) 174 ); 175 } else 176 annotationType.setInheritForegroundColor (true); 177 if (category.isDefined (EditorStyleConstants.WaveUnderlineColor)) { 178 annotationType.setUseWaveUnderlineColor (true); 179 annotationType.setWaveUnderlineColor ( 180 (Color ) category.getAttribute (EditorStyleConstants.WaveUnderlineColor) 181 ); 182 } else 183 annotationType.setUseWaveUnderlineColor (false); 184 } 186 } 187 188 189 191 198 public Collection getHighlightings (String profile) { 199 Map m = editorSettings.getHighlightings(profile); 200 if (m == null) { 201 return null; 202 } 203 return hideDummyCategories(m.values()); 204 } 205 206 public Collection getHighlightingDefaults (String profile) { 207 Collection r = editorSettings.getHighlightingDefaults (profile).values (); 208 if (r == null) return null; 209 return hideDummyCategories (r); 210 } 211 212 public void setHighlightings ( 213 String profile, 214 Collection highlihgtings 215 ) { 216 editorSettings.setHighlightings ( 217 profile, 218 toMap (highlihgtings) 219 ); 220 } 221 222 223 225 public Set getLanguages () { 226 return getLanguageToMimeTypeMap ().keySet (); 227 } 228 229 public Collection getCategories ( 230 String profile, 231 String language 232 ) { 233 if (language.equals(ALL_LANGUAGES)) { 234 return editorSettings.getFontColorSettings(EMPTY_MIMEPATH).getAllFontColors(profile); 235 } 236 237 String mimeType = getMimeType (language); 238 FontColorSettingsFactory fcs = EditorSettings.getDefault (). 239 getFontColorSettings (new String [] {mimeType}); 240 return fcs.getAllFontColors (profile); 241 } 242 243 public Collection getDefaults ( 244 String profile, 245 String language 246 ) { 247 if (language.equals(ALL_LANGUAGES)) { 248 return editorSettings.getFontColorSettings(EMPTY_MIMEPATH).getAllFontColorDefaults(profile); 249 } 250 251 String mimeType = getMimeType (language); 252 FontColorSettingsFactory fcs = EditorSettings.getDefault (). 253 getFontColorSettings (new String [] {mimeType}); 254 return fcs.getAllFontColorDefaults (profile); 255 } 256 257 public void setCategories ( 258 String profile, 259 String language, 260 Collection categories 261 ) { 262 if (language.equals (ALL_LANGUAGES)) { 263 editorSettings.getFontColorSettings(EMPTY_MIMEPATH).setAllFontColors(profile, categories); 264 return; 265 } 266 267 String mimeType = getMimeType (language); 268 if (mimeType == null) { 269 if (System.getProperty ("org.netbeans.optionsDialog") != null) 270 System.out.println("ColorModelImpl.setCategories - unknown language " + language); 271 return; 272 } 273 FontColorSettingsFactory fcs = EditorSettings.getDefault (). 274 getFontColorSettings (new String [] {mimeType}); 275 fcs.setAllFontColors ( 276 profile, 277 categories 278 ); 279 } 280 281 public Component getEditorPreviewComponent () { 282 return new Preview (HIGHLIGHTING_LANGUAGE); 283 } 284 285 public Component getSyntaxColoringPreviewComponent ( 286 String language 287 ) { 288 return new Preview (language); 289 } 290 291 class Preview extends JPanel { 292 293 static final String PROP_CURRENT_ELEMENT = "currentAElement"; 294 private JEditorPane editorPane; 295 private FontColorSettingsFactory fontColorSettings; 296 297 298 Preview ( 299 final String language 300 ) { 301 super (new BorderLayout ()); 302 305 SwingUtilities.invokeLater (new Runnable () { 306 public void run () { 307 editorPane = new JEditorPane (); 308 updateMimeType (language); 309 if (language == HIGHLIGHTING_LANGUAGE) { 310 EditorUI editorUI = Utilities.getEditorUI (editorPane); 311 if (editorUI != null) { 312 editorUI.setLineNumberEnabled (true); 313 editorUI.getExtComponent (); 314 add (editorUI.getExtComponent (), BorderLayout.CENTER); 315 return; 316 } 317 } 319 add (editorPane, BorderLayout.CENTER); 320 } 321 }); 322 setCursor (Cursor.getPredefinedCursor (Cursor.HAND_CURSOR)); 323 } 324 325 private String currentLanguage; 326 327 public void setParameters ( 328 final String language, 329 final Collection defaults, 330 final Collection highlightings, 331 final Collection syntaxColorings 332 ) { 333 SwingUtilities.invokeLater (new Runnable () { 334 public void run () { 335 String internalMimeType = null; 336 if (!language.equals (currentLanguage)) { 337 updateMimeType (language); 338 currentLanguage = language; 339 internalMimeType = languageToInternalMimeType(language, false); 340 fontColorSettings = EditorSettings.getDefault (). 341 getFontColorSettings (new String [] {internalMimeType}); 342 } 343 344 if (internalMimeType == null) { 345 internalMimeType = languageToInternalMimeType(language, false); 346 } 347 if (defaults != null) { 348 editorSettings.getFontColorSettings(EMPTY_MIMEPATH).setAllFontColors( 349 "test" + ColorModel.this.hashCode(), 350 defaults 351 ); 352 } 353 if (highlightings != null) 354 editorSettings.setHighlightings ( 355 "test" + ColorModel.this.hashCode (), 356 toMap (highlightings) 357 ); 358 if (syntaxColorings != null) 359 fontColorSettings.setAllFontColors ( 360 "test" + ColorModel.this.hashCode (), 361 syntaxColorings 362 ); 363 } 364 }); 365 } 366 367 370 private void updateMimeType (String language) { 371 String internalMimeType = languageToInternalMimeType(language, true); 372 373 383 Document document = editorPane.getDocument (); 384 document.putProperty ("mimeType", internalMimeType); 385 editorPane.setEditorKit (CloneableEditorSupport.getEditorKit(internalMimeType)); 386 document = editorPane.getDocument (); 387 document.putProperty ("mimeType", internalMimeType); 388 editorPane.firePropertyChange(null, 0, 1); 389 390 editorPane.addCaretListener (new CaretListener () { 391 public void caretUpdate (CaretEvent e) { 392 int position = e.getDot (); 393 EditorUI editorUI = Utilities.getEditorUI (editorPane); 394 if (editorUI == null) return; 395 SyntaxSupport ss = Utilities.getSyntaxSupport 396 (editorUI.getComponent ()); 397 if (!(ss instanceof ExtSyntaxSupport)) return; 398 try { 399 TokenItem tokenItem = ((ExtSyntaxSupport) ss). 400 getTokenChain (position, position + 1); 401 if (tokenItem == null) return; 402 String elementName = tokenItem.getTokenContextPath (). 403 getNamePrefix (); 404 if (tokenItem.getTokenID ().getCategory () != null) 405 elementName += tokenItem.getTokenID (). 406 getCategory ().getName (); 407 else 408 elementName += tokenItem.getTokenID ().getName (); 409 firePropertyChange (PROP_CURRENT_ELEMENT, null, elementName); 410 } catch (BadLocationException ex) { 411 ex.printStackTrace (); 412 } 413 } 414 }); 415 editorPane.setEnabled (false); 416 InputStream is = loadPreviewExample (language); 417 if (is == null) { 418 assert true : 419 "Example for " + language + " language not found."; 420 is = loadPreviewExample ("Java"); 421 } 422 BufferedReader r = new BufferedReader (new InputStreamReader (is)); 423 StringBuffer sb = new StringBuffer (); 424 try { 425 String line = r.readLine (); 426 while (line != null) { 427 sb.append (line).append ('\n'); 428 line = r.readLine (); 429 } 430 editorPane.setText (new String (sb)); 431 } catch (IOException ex) { 432 ex.printStackTrace (); 433 } 434 } 435 436 private InputStream loadPreviewExample (String language) { 437 String mimeType = getMimeType (language); 438 FileSystem fs = Repository.getDefault ().getDefaultFileSystem (); 439 FileObject exampleFile = fs.findResource 440 ("OptionsDialog/PreviewExamples/" + mimeType); 441 try { 442 return exampleFile != null ? 443 exampleFile.getInputStream () : null; 444 } catch (FileNotFoundException fnfe) { 445 return null; 446 } 447 } 448 449 private String languageToInternalMimeType (String language, boolean encodeTestProfileName) { 450 String mimeType = ( 451 language == HIGHLIGHTING_LANGUAGE || 452 language == ALL_LANGUAGES 453 ) ? 454 "text/x-java" : getMimeType (language); 456 457 if (encodeTestProfileName) { 458 return "test" + ColorModel.this.hashCode () + "_" + mimeType; } else { 460 return mimeType; 461 } 462 } 463 } 464 465 466 468 private String getMimeType (String language) { 469 return (String ) getLanguageToMimeTypeMap ().get (language); 470 } 471 472 private Map languageToMimeType; 473 private Map getLanguageToMimeTypeMap () { 474 if (languageToMimeType == null) { 475 languageToMimeType = new HashMap (); 476 Set mimeTypes = editorSettings.getMimeTypes (); 477 Iterator it = mimeTypes.iterator (); 478 while (it.hasNext ()) { 479 String mimeType = (String ) it.next (); 480 languageToMimeType.put ( 481 editorSettings.getLanguageName (mimeType), 482 mimeType 483 ); 484 } 485 languageToMimeType.put ( 486 ALL_LANGUAGES, 487 "Defaults" 488 ); 489 } 490 return languageToMimeType; 491 } 492 493 private Set hiddenCategories = new HashSet (); 494 { 495 } 498 499 private Collection hideDummyCategories ( 500 Collection categories 501 ) { 502 List result = new ArrayList (); 503 Iterator it = categories.iterator (); 504 while (it.hasNext ()) { 505 AttributeSet as = (AttributeSet ) it.next (); 506 if (hiddenCategories.contains ( 507 as.getAttribute (StyleConstants.NameAttribute) 508 )) continue; 509 result.add (as); 510 } 511 return result; 512 } 513 514 private static Map toMap (Collection categories) { 515 if (categories == null) return null; 516 Map result = new HashMap (); 517 Iterator it = categories.iterator (); 518 while (it.hasNext ()) { 519 AttributeSet as = (AttributeSet ) it.next (); 520 result.put ( 521 as.getAttribute (StyleConstants.NameAttribute), 522 as 523 ); 524 } 525 return result; 526 } 527 } 528 | Popular Tags |