1 19 20 package org.netbeans.modules.options.colors; 21 22 import java.awt.Color ; 23 import java.awt.Component ; 24 import java.awt.Font ; 25 import java.awt.Insets ; 26 import java.awt.event.ActionEvent ; 27 import java.awt.event.ActionListener ; 28 import java.beans.PropertyChangeEvent ; 29 import java.beans.PropertyChangeListener ; 30 import java.beans.PropertyEditor ; 31 import java.beans.PropertyEditorManager ; 32 import java.util.ArrayList ; 33 import java.util.Collection ; 34 import java.util.Collections ; 35 import java.util.Comparator ; 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 java.util.Vector ; 43 import javax.swing.AbstractButton ; 44 import javax.swing.JComboBox ; 45 import javax.swing.JComponent ; 46 import javax.swing.JLabel ; 47 import javax.swing.JPanel ; 48 import javax.swing.ListSelectionModel ; 49 import javax.swing.UIManager ; 50 import javax.swing.event.ListSelectionEvent ; 51 import javax.swing.event.ListSelectionListener ; 52 import javax.swing.text.AttributeSet ; 53 import javax.swing.text.SimpleAttributeSet ; 54 import javax.swing.text.StyleConstants ; 55 import org.netbeans.api.editor.settings.EditorStyleConstants; 56 import org.netbeans.modules.options.colors.ColorModel.Preview; 57 import org.openide.DialogDescriptor; 58 import org.openide.DialogDisplayer; 59 import org.openide.awt.Mnemonics; 60 import org.openide.util.NbBundle; 61 import org.openide.util.RequestProcessor; 62 import org.openide.util.RequestProcessor.Task; 63 64 68 public class SyntaxColoringPanel extends JPanel implements ActionListener , 69 PropertyChangeListener { 70 71 72 private Preview preview; 73 private Task selectTask; 74 private FontAndColorsPanel fontAndColorsPanel; 75 private ColorModel colorModel = null; 76 private String currentLanguage; 77 private String currentProfile; 78 79 private Map profiles = new HashMap (); 80 81 private Map toBeSaved = new HashMap (); 82 private boolean listen = false; 83 84 85 public SyntaxColoringPanel (FontAndColorsPanel fontAndColorsPanel) { 86 initComponents (); 87 this.fontAndColorsPanel = fontAndColorsPanel; 88 89 cbLanguage.getAccessibleContext ().setAccessibleName (loc ("AN_Languages")); 91 cbLanguage.getAccessibleContext ().setAccessibleDescription (loc ("AD_Languages")); 92 lCategories.getAccessibleContext ().setAccessibleName (loc ("AN_Categories")); 93 lCategories.getAccessibleContext ().setAccessibleDescription (loc ("AD_Categories")); 94 bFont.getAccessibleContext ().setAccessibleName (loc ("AN_Font")); 95 bFont.getAccessibleContext ().setAccessibleDescription (loc ("AD_Font")); 96 cbForeground.getAccessibleContext ().setAccessibleName (loc ("AN_Foreground_Chooser")); 97 cbForeground.getAccessibleContext ().setAccessibleDescription (loc ("AD_Foreground_Chooser")); 98 cbBackground.getAccessibleContext ().setAccessibleName (loc ("AN_Background_Chooser")); 101 cbBackground.getAccessibleContext ().setAccessibleDescription (loc ("AD_Background_Chooser")); 102 cbEffects.getAccessibleContext ().setAccessibleName (loc ("AN_Efects_Color_Chooser")); 105 cbEffects.getAccessibleContext ().setAccessibleDescription (loc ("AD_Efects_Color_Chooser")); 106 cbEffectColor.getAccessibleContext ().setAccessibleName (loc ("AN_Efects_Color")); 107 cbEffectColor.getAccessibleContext ().setAccessibleDescription (loc ("AD_Efects_Color")); 108 ColorComboBox.init (cbBackground); 109 ColorComboBox.init (cbForeground); 110 ColorComboBox.init (cbEffectColor); 111 cbLanguage.addActionListener (this); 112 lCategories.setSelectionMode (ListSelectionModel.SINGLE_SELECTION); 113 lCategories.setVisibleRowCount (3); 114 lCategories.setCellRenderer (new CategoryRenderer ()); 115 lCategories.addListSelectionListener (new ListSelectionListener () { 116 public void valueChanged (ListSelectionEvent e) { 117 if (!listen) return; 118 selectTask.schedule (200); 119 } 120 }); 121 tfFont.setEditable (false); 122 bFont.addActionListener (this); 123 bFont.setMargin (new Insets (0, 0, 0, 0)); 124 cbForeground.addActionListener (this); 129 ((JComponent )cbForeground.getEditor()).addPropertyChangeListener (this); 130 131 cbBackground.addActionListener (this); 132 ((JComponent )cbBackground.getEditor()).addPropertyChangeListener (this); 133 134 cbEffects.addItem (loc ("CTL_Effects_None")); 135 cbEffects.addItem (loc ("CTL_Effects_Underlined")); 136 cbEffects.addItem (loc ("CTL_Effects_Wave_Underlined")); 137 cbEffects.addItem (loc ("CTL_Effects_Strike_Through")); 138 cbEffects.getAccessibleContext ().setAccessibleName (loc ("AN_Effects")); 139 cbEffects.getAccessibleContext ().setAccessibleDescription (loc ("AD_Effects")); 140 cbEffects.addActionListener (this); 141 cbEffectColor.addActionListener (this); 142 loc (lCategory, "CTL_Category"); 143 loc (lFont, "CTL_Font"); 144 145 selectTask = new RequestProcessor ("SyntaxColoringPanel1").create ( 146 new Runnable () { 147 public void run () { 148 refreshUI (); 149 if (!blink) return; 150 startBlinking (); 151 } 152 } 153 ); 154 } 155 156 161 private void initComponents() { 163 164 jButton1 = new javax.swing.JButton (); 165 lLanguage = new javax.swing.JLabel (); 166 cbLanguage = new javax.swing.JComboBox (); 167 lCategory = new javax.swing.JLabel (); 168 spCategories = new javax.swing.JScrollPane (); 169 lCategories = new javax.swing.JList (); 170 lPreview = new javax.swing.JLabel (); 171 spPreview = new javax.swing.JScrollPane (); 172 pPreview = new javax.swing.JPanel (); 173 lFont = new javax.swing.JLabel (); 174 lForeground = new javax.swing.JLabel (); 175 lBackground = new javax.swing.JLabel (); 176 lEffects = new javax.swing.JLabel (); 177 lEffectColor = new javax.swing.JLabel (); 178 cbForeground = new javax.swing.JComboBox (); 179 cbBackground = new javax.swing.JComboBox (); 180 cbEffects = new javax.swing.JComboBox (); 181 cbEffectColor = new javax.swing.JComboBox (); 182 tfFont = new javax.swing.JTextField (); 183 bFont = new javax.swing.JButton (); 184 185 jButton1.setText("jButton1"); 186 187 lLanguage.setLabelFor(cbLanguage); 188 lLanguage.setText("Language:"); 189 190 lCategory.setLabelFor(lCategories); 191 lCategory.setText("Category:"); 192 193 spCategories.setViewportView(lCategories); 194 195 lPreview.setText("Preview:"); 196 197 spPreview.setBorder(javax.swing.BorderFactory.createEtchedBorder()); 198 spPreview.setAutoscrolls(true); 199 200 pPreview.setAutoscrolls(true); 201 pPreview.setLayout(new java.awt.BorderLayout ()); 202 spPreview.setViewportView(pPreview); 203 204 lFont.setLabelFor(bFont); 205 lFont.setText("Font:"); 206 207 lForeground.setLabelFor(cbForeground); 208 lForeground.setText("Foreground:"); 209 210 lBackground.setLabelFor(cbBackground); 211 lBackground.setText("Background:"); 212 213 lEffects.setLabelFor(cbEffects); 214 lEffects.setText("Effects:"); 215 216 lEffectColor.setLabelFor(cbEffectColor); 217 lEffectColor.setText("Effect Color:"); 218 219 bFont.setText("..."); 220 bFont.setMargin(new java.awt.Insets (2, 2, 2, 2)); 221 222 org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this); 223 this.setLayout(layout); 224 layout.setHorizontalGroup( 225 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 226 .add(layout.createSequentialGroup() 227 .addContainerGap() 228 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 229 .add(spPreview, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 340, Short.MAX_VALUE) 230 .add(layout.createSequentialGroup() 231 .add(lLanguage) 232 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 233 .add(cbLanguage, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) 234 .add(lCategory) 235 .add(layout.createSequentialGroup() 236 .add(spCategories, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 201, Short.MAX_VALUE) 237 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 238 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 239 .add(lEffectColor) 240 .add(lForeground) 241 .add(lFont) 242 .add(lEffects) 243 .add(lBackground)) 244 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 245 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 246 .add(layout.createSequentialGroup() 247 .add(tfFont, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 13, Short.MAX_VALUE) 248 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 249 .add(bFont)) 250 .add(cbForeground, 0, 38, Short.MAX_VALUE) 251 .add(cbBackground, 0, 38, Short.MAX_VALUE) 252 .add(cbEffects, 0, 38, Short.MAX_VALUE) 253 .add(cbEffectColor, 0, 38, Short.MAX_VALUE)) 254 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)) 255 .add(lPreview)) 256 .addContainerGap()) 257 ); 258 layout.setVerticalGroup( 259 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 260 .add(layout.createSequentialGroup() 261 .addContainerGap() 262 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 263 .add(lLanguage) 264 .add(cbLanguage, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) 265 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 266 .add(lCategory) 267 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 268 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 269 .add(layout.createSequentialGroup() 270 .add(spCategories, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 130, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) 271 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 272 .add(lPreview)) 273 .add(layout.createSequentialGroup() 274 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 275 .add(lFont) 276 .add(tfFont, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) 277 .add(bFont)) 278 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 279 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 280 .add(lForeground) 281 .add(cbForeground, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) 282 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 283 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 284 .add(lBackground) 285 .add(cbBackground, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) 286 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 287 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 288 .add(lEffects) 289 .add(cbEffects, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) 290 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 291 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 292 .add(lEffectColor) 293 .add(cbEffectColor, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))) 294 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 295 .add(spPreview, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 153, Short.MAX_VALUE) 296 .addContainerGap()) 297 ); 298 } 300 301 private javax.swing.JButton bFont; 303 private javax.swing.JComboBox cbBackground; 304 private javax.swing.JComboBox cbEffectColor; 305 private javax.swing.JComboBox cbEffects; 306 private javax.swing.JComboBox cbForeground; 307 private javax.swing.JComboBox cbLanguage; 308 private javax.swing.JButton jButton1; 309 private javax.swing.JLabel lBackground; 310 private javax.swing.JList lCategories; 311 private javax.swing.JLabel lCategory; 312 private javax.swing.JLabel lEffectColor; 313 private javax.swing.JLabel lEffects; 314 private javax.swing.JLabel lFont; 315 private javax.swing.JLabel lForeground; 316 private javax.swing.JLabel lLanguage; 317 private javax.swing.JLabel lPreview; 318 private javax.swing.JPanel pPreview; 319 private javax.swing.JScrollPane spCategories; 320 private javax.swing.JScrollPane spPreview; 321 private javax.swing.JTextField tfFont; 322 324 325 public void actionPerformed (ActionEvent evt) { 326 if (!listen) return; 327 if (evt.getSource () == cbEffects) { 328 cbEffectColor.setEnabled (cbEffects.getSelectedIndex () > 0); 329 if (cbEffects.getSelectedIndex () == 0) 330 ColorComboBox.setColor (cbEffectColor, null); 331 updateData (); 332 } else 333 if (evt.getSource () == cbLanguage) { 334 setCurrentLanguage ((String ) cbLanguage.getSelectedItem ()); 335 } else 336 if (evt.getSource () == bFont) { 337 PropertyEditor pe = PropertyEditorManager.findEditor (Font .class); 338 AttributeSet category = getCurrentCategory (); 339 Font f = getFont (category); 340 pe.setValue (f); 341 DialogDescriptor dd = new DialogDescriptor ( 342 pe.getCustomEditor (), 343 loc ("CTL_Font_Chooser") ); 345 DialogDisplayer.getDefault ().createDialog (dd).setVisible (true); 346 if (dd.getValue () == DialogDescriptor.OK_OPTION) { 347 f = (Font ) pe.getValue (); 348 category = modifyFont (category, f); 349 replaceCurrrentCategory (category); 350 setToBeSaved (currentProfile, currentLanguage); 351 refreshUI (); } 353 } else 354 if (evt.getSource () instanceof JComboBox ) { 355 updateData (); 356 } 357 } 358 359 public void propertyChange (PropertyChangeEvent evt) { 360 if (!listen) return; 361 if (evt.getPropertyName () == Preview.PROP_CURRENT_ELEMENT) { 362 String currentCategory = (String ) evt.getNewValue (); 363 Vector categories = getCategories (currentProfile, currentLanguage); 364 if (currentLanguage.equals (ColorModel.ALL_LANGUAGES)) 365 currentCategory = (String ) convertALC.get (currentCategory); 366 int i, k = categories.size (); 367 for (i = 0; i < k; i++) { 368 AttributeSet as = (AttributeSet ) categories.get (i); 369 if (!currentCategory.equals ( 370 as.getAttribute (StyleConstants.NameAttribute) 371 )) continue; 372 blink = false; 373 lCategories.setSelectedIndex (i); 374 lCategories.ensureIndexIsVisible (i); 375 blink = true; 376 return; 377 } 378 } else if (evt.getPropertyName () == ColorComboBox.PROP_COLOR) { 379 updateData (); 380 } 381 } 382 383 void update (ColorModel colorModel) { 384 this.colorModel = colorModel; 385 currentProfile = colorModel.getCurrentProfile (); 386 currentLanguage = (String ) colorModel.getLanguages (). 387 iterator ().next (); 388 if (preview != null) 389 preview.removePropertyChangeListener 390 (Preview.PROP_CURRENT_ELEMENT, this); 391 Component component = colorModel.getSyntaxColoringPreviewComponent 392 (currentLanguage); 393 preview = (Preview) component; 394 pPreview.removeAll (); 395 pPreview.add ("Center", component); 396 preview.addPropertyChangeListener 397 (Preview.PROP_CURRENT_ELEMENT, this); 398 listen = false; 399 List languages = new ArrayList 400 (colorModel.getLanguages ()); 401 Collections.sort (languages, new LanguagesComparator ()); 402 Iterator it = languages.iterator (); 403 cbLanguage.removeAllItems (); 404 while (it.hasNext ()) 405 cbLanguage.addItem (it.next ()); 406 listen = true; 407 cbLanguage.setSelectedIndex (0); 408 } 409 410 void cancel () { 411 toBeSaved = new HashMap (); 412 profiles = new HashMap (); 413 } 414 415 void applyChanges () { 416 if (colorModel == null) return; 417 Iterator it = toBeSaved.keySet ().iterator (); 418 while (it.hasNext ()) { 419 String profile = (String ) it.next (); 420 Set toBeSavedLanguages = (Set ) toBeSaved.get (profile); 421 Map schemeMap = (Map ) profiles.get (profile); 422 Iterator it2 = toBeSavedLanguages.iterator (); 423 while (it2.hasNext ()) { 424 String languageName = (String ) it2.next (); 425 colorModel.setCategories ( 426 profile, 427 languageName, 428 (Vector ) schemeMap.get (languageName) 429 ); 430 } 431 } 432 toBeSaved = new HashMap (); 433 profiles = new HashMap (); 434 } 435 436 boolean isChanged () { 437 return !toBeSaved.isEmpty (); 438 } 439 440 public void setCurrentProfile (String currentProfile) { 441 String oldProfile = this.currentProfile; 442 this.currentProfile = currentProfile; 443 if (!colorModel.getProfiles ().contains (currentProfile)) 444 cloneScheme (oldProfile, currentProfile); 445 Vector categories = getCategories (currentProfile, currentLanguage); 446 lCategories.setListData (categories); 447 blink = false; 448 lCategories.setSelectedIndex (0); 449 blink = true; 450 refreshUI (); 451 } 452 453 void deleteProfile (String profile) { 454 Iterator it = colorModel.getLanguages ().iterator (); 455 Map m = new HashMap (); 456 boolean custom = colorModel.isCustomProfile (profile); 457 while (it.hasNext ()) { 458 String language = (String ) it.next (); 459 if (custom) 460 m.put (language, null); 461 else 462 m.put (language, getDefaults (profile, language)); 463 } 464 profiles.put (profile, m); 465 toBeSaved.put (profile, new HashSet (colorModel.getLanguages ())); 466 if (!custom) 467 refreshUI (); 468 } 469 470 471 473 private void cloneScheme (String oldScheme, String newScheme) { 474 Map m = new HashMap (); 475 Iterator it = colorModel.getLanguages ().iterator (); 476 while (it.hasNext ()) { 477 String language = (String ) it.next (); 478 Vector v = getCategories (oldScheme, language); 479 m.put (language, new Vector (v)); 480 setToBeSaved (newScheme, language); 481 } 482 profiles.put (newScheme, m); 483 } 484 485 Collection getAllLanguages () { 486 return getCategories (currentProfile, ColorModel.ALL_LANGUAGES); 487 } 488 489 Collection getSyntaxColorings () { 490 return getCategories (currentProfile, currentLanguage); 491 } 492 493 private void setCurrentLanguage (String language) { 494 currentLanguage = language; 495 496 blink = false; 498 lCategories.setListData (getCategories (currentProfile, currentLanguage)); 499 lCategories.setSelectedIndex (0); 500 blink = true; 501 refreshUI (); 502 } 503 504 private static String loc (String key) { 505 return NbBundle.getMessage (SyntaxColoringPanel.class, key); 506 } 507 508 private static void loc (Component c, String key) { 509 if (c instanceof AbstractButton ) 510 Mnemonics.setLocalizedText ( 511 (AbstractButton ) c, 512 loc (key) 513 ); 514 else 515 Mnemonics.setLocalizedText ( 516 (JLabel ) c, 517 loc (key) 518 ); 519 } 520 521 525 private void updateData () { 526 int i = lCategories.getSelectedIndex (); 527 if (i < 0) return; 528 529 AttributeSet category = getCurrentCategory (); 530 Color underline = null, 531 wave = null, 532 strikethrough = null; 533 if (cbEffects.getSelectedIndex () == 1) 534 underline = getRealColor(cbEffectColor); 535 if (cbEffects.getSelectedIndex () == 2) 536 wave = getRealColor(cbEffectColor); 537 if (cbEffects.getSelectedIndex () == 3) 538 strikethrough = getRealColor(cbEffectColor); 539 540 SimpleAttributeSet c = new SimpleAttributeSet (category); 541 542 Color color = getRealColor(cbBackground); 543 if (color != null) { 544 c.addAttribute(StyleConstants.Background, color); 545 } else { 546 c.removeAttribute(StyleConstants.Background); 547 } 548 549 color = getRealColor(cbForeground); 550 if (color != null) { 551 c.addAttribute(StyleConstants.Foreground, color); 552 } else { 553 c.removeAttribute(StyleConstants.Foreground); 554 } 555 556 if (underline != null) { 557 c.addAttribute(StyleConstants.Underline, underline); 558 } else { 559 c.removeAttribute(StyleConstants.Underline); 560 } 561 562 if (strikethrough != null) { 563 c.addAttribute(StyleConstants.StrikeThrough, strikethrough); 564 } else { 565 c.removeAttribute(StyleConstants.StrikeThrough); 566 } 567 568 if (wave != null) { 569 c.addAttribute(EditorStyleConstants.WaveUnderlineColor, wave); 570 } else { 571 c.removeAttribute(EditorStyleConstants.WaveUnderlineColor); 572 } 573 574 replaceCurrrentCategory(c); 575 setToBeSaved(currentProfile, currentLanguage); 576 updatePreview(); 577 } 578 579 private Color getRealColor(JComboBox comboBox) { 580 if (comboBox.getSelectedIndex() < comboBox.getItemCount() - 1) { 582 return ((ColorValue) comboBox.getSelectedItem()).color; 583 } else { 584 return null; 585 } 586 } 587 588 private boolean blink = true; 589 private int blinkSequence = 0; 590 private RequestProcessor.Task task = new RequestProcessor 591 ("SyntaxColoringPanel").create (new Runnable () { 592 public void run () { 593 updatePreview (); 594 if (blinkSequence == 0) return; 595 blinkSequence --; 596 task.schedule (250); 597 } 598 }); 599 600 private void startBlinking () { 601 blinkSequence = 5; 602 task.schedule (0); 603 } 604 605 private void updatePreview () { 606 Collection syntaxColorings = getSyntaxColorings (); 607 Collection allLanguages = getAllLanguages (); 608 if ((blinkSequence % 2) == 1) { 609 if (currentLanguage == ColorModel.ALL_LANGUAGES) 610 allLanguages = invertCategory (allLanguages, getCurrentCategory ()); 611 else 612 syntaxColorings = invertCategory (syntaxColorings, getCurrentCategory ()); 613 } 614 preview.setParameters ( 615 currentLanguage, 616 allLanguages, 617 fontAndColorsPanel.getHighlights (), 618 syntaxColorings 619 ); 620 } 621 622 private Collection invertCategory (Collection c, AttributeSet category) { 623 if (category == null) return c; 624 ArrayList result = new ArrayList (c); 625 int i = result.indexOf (category); 626 SimpleAttributeSet as = new SimpleAttributeSet (category); 627 Color highlight = (Color ) getValue (currentLanguage, category, StyleConstants.Background); 628 if (highlight == null) return result; 629 Color newColor = new Color ( 630 255 - highlight.getRed (), 631 255 - highlight.getGreen (), 632 255 - highlight.getBlue () 633 ); 634 as.addAttribute ( 635 StyleConstants.Underline, 636 newColor 637 ); 638 result.set (i, as); 639 return result; 640 } 641 642 646 private void refreshUI () { 647 listen = false; 648 AttributeSet category = getCurrentCategory (); 649 if (category == null) { 650 tfFont.setText (""); 652 bFont.setEnabled (false); 653 cbEffects.setEnabled (false); 654 cbForeground.setEnabled (false); 655 cbForeground.setSelectedItem (new ColorValue (null, null)); 656 cbBackground.setEnabled (false); 657 cbBackground.setSelectedItem (new ColorValue (null, null)); 658 cbEffectColor.setEnabled (false); 659 cbEffectColor.setSelectedItem (new ColorValue (null, null)); 660 updatePreview (); 661 return; 662 } 663 bFont.setEnabled (true); 664 cbEffects.setEnabled (true); 665 cbForeground.setEnabled (true); 666 cbBackground.setEnabled (true); 667 668 Color inheritedForeground = (Color ) getDefault 670 (currentLanguage, category, StyleConstants.Foreground); 671 if (inheritedForeground == null) inheritedForeground = Color.black; 672 ColorComboBox.setInheritedColor (cbForeground, inheritedForeground); 673 Color inheritedBackground = (Color ) getDefault 674 (currentLanguage, category, StyleConstants.Background); 675 if (inheritedBackground == null) inheritedBackground = Color.white; 676 ColorComboBox.setInheritedColor (cbBackground, inheritedBackground); 677 678 String font = fontToString (category); 679 tfFont.setText (font); 680 ColorComboBox.setColor ( 681 cbForeground, 682 (Color ) category.getAttribute (StyleConstants.Foreground) 683 ); 684 ColorComboBox.setColor ( 685 cbBackground, 686 (Color ) category.getAttribute (StyleConstants.Background) 687 ); 688 689 if (category.getAttribute (StyleConstants.Underline) != null) { 690 cbEffects.setSelectedIndex (1); 691 cbEffectColor.setEnabled (true); 692 ColorComboBox.setColor ( 693 cbEffectColor, 694 (Color ) category.getAttribute (StyleConstants.Underline) 695 ); 696 } else 697 if (category.getAttribute (EditorStyleConstants.WaveUnderlineColor) != null) { 698 cbEffects.setSelectedIndex (2); 699 cbEffectColor.setEnabled (true); 700 ColorComboBox.setColor ( 701 cbEffectColor, 702 (Color ) category.getAttribute (EditorStyleConstants.WaveUnderlineColor) 703 ); 704 } else 705 if (category.getAttribute (StyleConstants.StrikeThrough) != null) { 706 cbEffects.setSelectedIndex (3); 707 cbEffectColor.setEnabled (true); 708 ColorComboBox.setColor ( 709 cbEffectColor, 710 (Color ) category.getAttribute (StyleConstants.StrikeThrough) 711 ); 712 } else { 713 cbEffects.setSelectedIndex (0); 714 cbEffectColor.setEnabled (false); 715 cbEffectColor.setSelectedItem (new ColorValue (null, null)); 716 } 717 updatePreview (); 718 listen = true; 719 } 720 721 private void setToBeSaved (String currentProfile, String currentLanguage) { 722 Set s = (Set ) toBeSaved.get (currentProfile); 723 if (s == null) { 724 s = new HashSet (); 725 toBeSaved.put (currentProfile, s); 726 } 727 s.add (currentLanguage); 728 } 729 730 private Vector getCategories (String profile, String language) { 731 if (colorModel == null) return null; 732 Map m = (Map ) profiles.get (profile); 733 if (m == null) { 734 m = new HashMap (); 735 profiles.put (profile, m); 736 } 737 Vector v = (Vector ) m.get (language); 738 if (v == null) { 739 Collection c = colorModel.getCategories (profile, language); 740 if (c == null) { 741 c = Collections.EMPTY_SET; } 743 List l = new ArrayList (c); 744 Collections.sort (l, new CategoryComparator ()); 745 v = new Vector (l); 746 m.put (language, v); 747 } 748 return v; 749 } 750 751 private Map defaults = new HashMap (); 752 755 private Vector getDefaults (String profile, String language) { 756 Map m = (Map ) defaults.get (profile); 757 if (m == null) { 758 m = new HashMap (); 759 defaults.put (profile, m); 760 } 761 Vector v = (Vector ) m.get (language); 762 if (v == null) { 763 Collection c = colorModel.getDefaults (profile, language); 764 List l = new ArrayList (c); 765 Collections.sort (l, new CategoryComparator ()); 766 v = new Vector (l); 767 m.put (language, v); 768 } 769 return new Vector (v); 770 } 771 772 private AttributeSet getCurrentCategory () { 773 int i = lCategories.getSelectedIndex (); 774 if (i < 0) return null; 775 return (AttributeSet ) getCategories (currentProfile, currentLanguage).get (i); 776 } 777 778 private void replaceCurrrentCategory (AttributeSet newValues) { 779 int i = lCategories.getSelectedIndex (); 780 getCategories (currentProfile, currentLanguage).set (i, newValues); 781 } 782 783 private AttributeSet getCategory ( 784 String profile, 785 String language, 786 String name 787 ) { 788 Vector v = getCategories (profile, language); 789 Iterator it = v.iterator (); 790 while (it.hasNext ()) { 791 AttributeSet c = (AttributeSet ) it.next (); 792 if (c.getAttribute (StyleConstants.NameAttribute).equals (name)) 793 return c; 794 } 795 return null; 796 } 797 798 private Object getValue (String language, AttributeSet category, Object key) { 799 if (category.isDefined (key)) 800 return category.getAttribute (key); 801 return getDefault (language, category, key); 802 } 803 804 private Object getDefault (String language, AttributeSet category, Object key) { 805 String name = (String ) category.getAttribute (EditorStyleConstants.Default); 806 if (name == null) name = "default"; 807 808 if (!name.equals (category.getAttribute (StyleConstants.NameAttribute)) 810 ) { 811 AttributeSet defaultAS = getCategory 812 (currentProfile, language, name); 813 if (defaultAS != null) 814 return getValue (language, defaultAS, key); 815 } 816 817 if (!language.equals (ColorModel.ALL_LANGUAGES)) { 819 AttributeSet defaultAS = getCategory 820 (currentProfile, ColorModel.ALL_LANGUAGES, name); 821 if (defaultAS != null) 822 return getValue (ColorModel.ALL_LANGUAGES, defaultAS, key); 823 } 824 825 if (key == StyleConstants.FontFamily) return "Monospaced"; if (key == StyleConstants.FontSize) return getDefaultFontSize (); 827 return null; 828 } 829 830 private Font getFont (AttributeSet category) { 831 String name = (String ) getValue (currentLanguage, category, StyleConstants.FontFamily); 832 if (name == null) name = "Monospaced"; Integer size = (Integer ) getValue (currentLanguage, category, StyleConstants.FontSize); 834 if (size == null) 835 size = getDefaultFontSize (); 836 Boolean bold = (Boolean ) getValue (currentLanguage, category, StyleConstants.Bold); 837 if (bold == null) bold = Boolean.FALSE; 838 Boolean italic = (Boolean ) getValue (currentLanguage, category, StyleConstants.Italic); 839 if (italic == null) italic = Boolean.FALSE; 840 int style = bold.booleanValue () ? Font.BOLD : Font.PLAIN; 841 if (italic.booleanValue ()) style += Font.ITALIC; 842 return new Font (name, style, size.intValue ()); 843 } 844 845 private AttributeSet modifyFont (AttributeSet category, Font f) { 846 String fontName = f.getName (); 847 Integer fontSize = new Integer (f.getSize ()); 848 Boolean bold = Boolean.valueOf (f.isBold ()); 849 Boolean italic = Boolean.valueOf (f.isItalic ()); 850 boolean isDefault = "default".equals ( 851 category.getAttribute (StyleConstants.NameAttribute) 852 ); 853 if (fontName.equals ( 854 getDefault (currentLanguage, category, StyleConstants.FontFamily) 855 ) && !isDefault) 856 fontName = null; 857 if (fontSize.equals ( 858 getDefault (currentLanguage, category, StyleConstants.FontSize) 859 ) && !isDefault) 860 fontSize = null; 861 if (bold.equals (getDefault (currentLanguage, category, StyleConstants.Bold)) 862 ) 863 bold = null; 864 else 865 if (bold.equals (Boolean.FALSE) && 866 getDefault (currentLanguage, category, StyleConstants.Bold) == null 867 ) 868 bold = null; 869 if (italic.equals (getDefault (currentLanguage, category, StyleConstants.Italic)) 870 ) 871 italic = null; 872 else 873 if (italic.equals (Boolean.FALSE) && 874 getDefault (currentLanguage, category, StyleConstants.Italic) == null 875 ) 876 italic = null; 877 SimpleAttributeSet c = new SimpleAttributeSet (category); 878 if (fontName != null) 879 c.addAttribute ( 880 StyleConstants.FontFamily, 881 fontName 882 ); 883 else 884 c.removeAttribute (StyleConstants.FontFamily); 885 if (fontSize != null) 886 c.addAttribute ( 887 StyleConstants.FontSize, 888 fontSize 889 ); 890 else 891 c.removeAttribute (StyleConstants.FontSize); 892 if (bold != null) 893 c.addAttribute ( 894 StyleConstants.Bold, 895 bold 896 ); 897 else 898 c.removeAttribute (StyleConstants.Bold); 899 if (italic != null) 900 c.addAttribute ( 901 StyleConstants.Italic, 902 italic 903 ); 904 else 905 c.removeAttribute (StyleConstants.Italic); 906 907 return c; 908 } 909 910 private String fontToString (AttributeSet category) { 911 if ("default".equals ( 912 category.getAttribute (StyleConstants.NameAttribute) 913 )) { 914 StringBuffer sb = new StringBuffer (); 915 sb.append (getValue (currentLanguage, category, StyleConstants.FontFamily)); 916 sb.append (' '); 917 sb.append (getValue (currentLanguage, category, StyleConstants.FontSize)); 918 Boolean bold = (Boolean ) getValue (currentLanguage, category, StyleConstants.Bold); 919 if (bold != null && bold.booleanValue ()) 920 sb.append (' ').append (loc ("CTL_Bold")); Boolean italic = (Boolean ) getValue (currentLanguage, category, StyleConstants.Italic); 922 if (italic != null && italic.booleanValue ()) 923 sb.append (' ').append (loc ("CTL_Italic")); return sb.toString (); 925 } 926 boolean def = false; 927 StringBuffer sb = new StringBuffer (); 928 if (category.getAttribute (StyleConstants.FontFamily) != null) 929 sb.append ('+').append (category.getAttribute (StyleConstants.FontFamily)); 930 else 931 def = true; 932 if (category.getAttribute (StyleConstants.FontSize) != null) 933 sb.append ('+').append (category.getAttribute (StyleConstants.FontSize)); 934 else 935 def = true; 936 if (Boolean.TRUE.equals (category.getAttribute (StyleConstants.Bold))) 937 sb.append ('+').append (loc ("CTL_Bold")); if (Boolean.FALSE.equals (category.getAttribute (StyleConstants.Bold))) 939 sb.append ('-').append (loc ("CTL_Bold")); if (Boolean.TRUE.equals (category.getAttribute (StyleConstants.Italic))) 941 sb.append ('+').append (loc ("CTL_Italic")); if (Boolean.FALSE.equals (category.getAttribute (StyleConstants.Italic))) 943 sb.append ('-').append (loc ("CTL_Italic")); 945 if (def) { 946 sb.insert (0, loc ("CTL_Inherited")); return sb.toString (); 948 } else { 949 String result = sb.toString (); 950 return result.replace ('+', ' '); 951 } 952 } 953 954 private static Map convertALC = new HashMap (); 955 956 static { 957 convertALC.put ("java-block-comment", "comment"); 958 convertALC.put ("java-keywords", "keyword"); 959 convertALC.put ("java-line-comment", "comment"); 960 convertALC.put ("java-dentifier", "identifier"); 961 convertALC.put ("java-numeric-literals", "number"); 962 convertALC.put ("java-operators", "operator"); 963 convertALC.put ("java-char-literal", "char"); 964 convertALC.put ("java-string-literal", "string"); 965 convertALC.put ("java-whitespace", "whitespace"); 966 convertALC.put ("java-identifier", "identifier"); 967 convertALC.put ("java-error", "error"); 968 } 969 970 private static Integer defaultFontSize; 971 private static Integer getDefaultFontSize () { 972 if (defaultFontSize == null) { 973 defaultFontSize = (Integer ) UIManager.get 974 ("customFontSize"); if (defaultFontSize == null) { 976 int s = UIManager.getFont ("TextField.font").getSize (); if (s < 12) s = 12; 978 defaultFontSize = new Integer (s); 979 } 980 } 981 return defaultFontSize; 982 } 983 984 private static class LanguagesComparator implements Comparator { 985 public int compare (Object o1, Object o2) { 986 if (o1.equals (ColorModel.ALL_LANGUAGES)) 987 return o2.equals (ColorModel.ALL_LANGUAGES) ? 0 : -1; 988 return ((String ) o1).compareTo ((String ) o2); 989 } 990 } 991 } 992 | Popular Tags |