KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > options > colors > SyntaxColoringPanel


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.options.colors;
21
22 import java.awt.Color JavaDoc;
23 import java.awt.Component JavaDoc;
24 import java.awt.Font JavaDoc;
25 import java.awt.Insets JavaDoc;
26 import java.awt.event.ActionEvent JavaDoc;
27 import java.awt.event.ActionListener JavaDoc;
28 import java.beans.PropertyChangeEvent JavaDoc;
29 import java.beans.PropertyChangeListener JavaDoc;
30 import java.beans.PropertyEditor JavaDoc;
31 import java.beans.PropertyEditorManager JavaDoc;
32 import java.util.ArrayList JavaDoc;
33 import java.util.Collection JavaDoc;
34 import java.util.Collections JavaDoc;
35 import java.util.Comparator JavaDoc;
36 import java.util.HashMap JavaDoc;
37 import java.util.HashSet JavaDoc;
38 import java.util.Iterator JavaDoc;
39 import java.util.List JavaDoc;
40 import java.util.Map JavaDoc;
41 import java.util.Set JavaDoc;
42 import java.util.Vector JavaDoc;
43 import javax.swing.AbstractButton JavaDoc;
44 import javax.swing.JComboBox JavaDoc;
45 import javax.swing.JComponent JavaDoc;
46 import javax.swing.JLabel JavaDoc;
47 import javax.swing.JPanel JavaDoc;
48 import javax.swing.ListSelectionModel JavaDoc;
49 import javax.swing.UIManager JavaDoc;
50 import javax.swing.event.ListSelectionEvent JavaDoc;
51 import javax.swing.event.ListSelectionListener JavaDoc;
52 import javax.swing.text.AttributeSet JavaDoc;
53 import javax.swing.text.SimpleAttributeSet JavaDoc;
54 import javax.swing.text.StyleConstants JavaDoc;
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 /**
65  *
66  * @author Jan Jancura
67  */

68 public class SyntaxColoringPanel extends JPanel JavaDoc implements ActionListener JavaDoc,
69 PropertyChangeListener JavaDoc {
70     
71     
72     private Preview preview;
73     private Task selectTask;
74     private FontAndColorsPanel fontAndColorsPanel;
75     private ColorModel colorModel = null;
76     private String JavaDoc currentLanguage;
77     private String JavaDoc currentProfile;
78     /** cache Map (String (profile name) > Map (String (language name) > Vector (AttributeSet))). */
79     private Map JavaDoc profiles = new HashMap JavaDoc ();
80     /** Map (String (profile name) > Set (String (language name))) of names of changed languages. */
81     private Map JavaDoc toBeSaved = new HashMap JavaDoc ();
82     private boolean listen = false;
83     
84     /** Creates new form SyntaxColoringPanel1 */
85     public SyntaxColoringPanel (FontAndColorsPanel fontAndColorsPanel) {
86         initComponents ();
87         this.fontAndColorsPanel = fontAndColorsPanel;
88         
89         // 1) init components
90
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 // bForeground.getAccessibleContext ().setAccessibleName (loc ("AN_Foreground"));
99
// bForeground.getAccessibleContext ().setAccessibleDescription (loc ("AD_Foreground"));
100
cbBackground.getAccessibleContext ().setAccessibleName (loc ("AN_Background_Chooser"));
101         cbBackground.getAccessibleContext ().setAccessibleDescription (loc ("AD_Background_Chooser"));
102 // bBackground.getAccessibleContext ().setAccessibleName (loc ("AN_Background"));
103
// bBackground.getAccessibleContext ().setAccessibleDescription (loc ("AD_Background"));
104
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 JavaDoc () {
116             public void valueChanged (ListSelectionEvent JavaDoc e) {
117                 if (!listen) return;
118                 selectTask.schedule (200);
119             }
120         });
121     tfFont.setEditable (false);
122         bFont.addActionListener (this);
123         bFont.setMargin (new Insets JavaDoc (0, 0, 0, 0));
124 // bForeground.addActionListener (this);
125
// bForeground.setMargin (new Insets (0, 0, 0, 0));
126
// bBackground.addActionListener (this);
127
// bBackground.setMargin (new Insets (0, 0, 0, 0));
128
cbForeground.addActionListener (this);
129         ((JComponent JavaDoc)cbForeground.getEditor()).addPropertyChangeListener (this);
130
131         cbBackground.addActionListener (this);
132         ((JComponent JavaDoc)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 JavaDoc () {
147                 public void run () {
148                     refreshUI ();
149                     if (!blink) return;
150                     startBlinking ();
151                 }
152             }
153         );
154     }
155     
156     /** This method is called from within the constructor to
157      * initialize the form.
158      * WARNING: Do NOT modify this code. The content of this method is
159      * always regenerated by the Form Editor.
160      */

161     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
162
private void initComponents() {
163
164         jButton1 = new javax.swing.JButton JavaDoc();
165         lLanguage = new javax.swing.JLabel JavaDoc();
166         cbLanguage = new javax.swing.JComboBox JavaDoc();
167         lCategory = new javax.swing.JLabel JavaDoc();
168         spCategories = new javax.swing.JScrollPane JavaDoc();
169         lCategories = new javax.swing.JList JavaDoc();
170         lPreview = new javax.swing.JLabel JavaDoc();
171         spPreview = new javax.swing.JScrollPane JavaDoc();
172         pPreview = new javax.swing.JPanel JavaDoc();
173         lFont = new javax.swing.JLabel JavaDoc();
174         lForeground = new javax.swing.JLabel JavaDoc();
175         lBackground = new javax.swing.JLabel JavaDoc();
176         lEffects = new javax.swing.JLabel JavaDoc();
177         lEffectColor = new javax.swing.JLabel JavaDoc();
178         cbForeground = new javax.swing.JComboBox JavaDoc();
179         cbBackground = new javax.swing.JComboBox JavaDoc();
180         cbEffects = new javax.swing.JComboBox JavaDoc();
181         cbEffectColor = new javax.swing.JComboBox JavaDoc();
182         tfFont = new javax.swing.JTextField JavaDoc();
183         bFont = new javax.swing.JButton JavaDoc();
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 JavaDoc());
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 JavaDoc(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     }// </editor-fold>//GEN-END:initComponents
299

300     
301     // Variables declaration - do not modify//GEN-BEGIN:variables
302
private javax.swing.JButton JavaDoc bFont;
303     private javax.swing.JComboBox JavaDoc cbBackground;
304     private javax.swing.JComboBox JavaDoc cbEffectColor;
305     private javax.swing.JComboBox JavaDoc cbEffects;
306     private javax.swing.JComboBox JavaDoc cbForeground;
307     private javax.swing.JComboBox JavaDoc cbLanguage;
308     private javax.swing.JButton JavaDoc jButton1;
309     private javax.swing.JLabel JavaDoc lBackground;
310     private javax.swing.JList JavaDoc lCategories;
311     private javax.swing.JLabel JavaDoc lCategory;
312     private javax.swing.JLabel JavaDoc lEffectColor;
313     private javax.swing.JLabel JavaDoc lEffects;
314     private javax.swing.JLabel JavaDoc lFont;
315     private javax.swing.JLabel JavaDoc lForeground;
316     private javax.swing.JLabel JavaDoc lLanguage;
317     private javax.swing.JLabel JavaDoc lPreview;
318     private javax.swing.JPanel JavaDoc pPreview;
319     private javax.swing.JScrollPane JavaDoc spCategories;
320     private javax.swing.JScrollPane JavaDoc spPreview;
321     private javax.swing.JTextField JavaDoc tfFont;
322     // End of variables declaration//GEN-END:variables
323

324
325     public void actionPerformed (ActionEvent JavaDoc 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 JavaDoc) cbLanguage.getSelectedItem ());
335     } else
336         if (evt.getSource () == bFont) {
337             PropertyEditor JavaDoc pe = PropertyEditorManager.findEditor (Font JavaDoc.class);
338             AttributeSet JavaDoc category = getCurrentCategory ();
339             Font JavaDoc f = getFont (category);
340             pe.setValue (f);
341             DialogDescriptor dd = new DialogDescriptor (
342                 pe.getCustomEditor (),
343                 loc ("CTL_Font_Chooser") // NOI18N
344
);
345             DialogDisplayer.getDefault ().createDialog (dd).setVisible (true);
346             if (dd.getValue () == DialogDescriptor.OK_OPTION) {
347                 f = (Font JavaDoc) pe.getValue ();
348                 category = modifyFont (category, f);
349                 replaceCurrrentCategory (category);
350                 setToBeSaved (currentProfile, currentLanguage);
351                 refreshUI (); // refresh font viewer
352
}
353         } else
354         if (evt.getSource () instanceof JComboBox JavaDoc) {
355             updateData ();
356         }
357     }
358     
359     public void propertyChange (PropertyChangeEvent JavaDoc evt) {
360         if (!listen) return;
361         if (evt.getPropertyName () == Preview.PROP_CURRENT_ELEMENT) {
362             String JavaDoc currentCategory = (String JavaDoc) evt.getNewValue ();
363             Vector JavaDoc categories = getCategories (currentProfile, currentLanguage);
364             if (currentLanguage.equals (ColorModel.ALL_LANGUAGES))
365                 currentCategory = (String JavaDoc) convertALC.get (currentCategory);
366             int i, k = categories.size ();
367             for (i = 0; i < k; i++) {
368                 AttributeSet JavaDoc as = (AttributeSet JavaDoc) 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 JavaDoc) colorModel.getLanguages ().
387             iterator ().next ();
388         if (preview != null)
389             preview.removePropertyChangeListener
390                 (Preview.PROP_CURRENT_ELEMENT, this);
391         Component JavaDoc 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 JavaDoc languages = new ArrayList JavaDoc
400             (colorModel.getLanguages ());
401         Collections.sort (languages, new LanguagesComparator ());
402         Iterator JavaDoc 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 JavaDoc ();
412         profiles = new HashMap JavaDoc ();
413     }
414     
415     void applyChanges () {
416         if (colorModel == null) return;
417     Iterator JavaDoc it = toBeSaved.keySet ().iterator ();
418     while (it.hasNext ()) {
419         String JavaDoc profile = (String JavaDoc) it.next ();
420             Set JavaDoc toBeSavedLanguages = (Set JavaDoc) toBeSaved.get (profile);
421             Map JavaDoc schemeMap = (Map JavaDoc) profiles.get (profile);
422             Iterator JavaDoc it2 = toBeSavedLanguages.iterator ();
423             while (it2.hasNext ()) {
424                 String JavaDoc languageName = (String JavaDoc) it2.next ();
425                 colorModel.setCategories (
426                     profile,
427                     languageName,
428                     (Vector JavaDoc) schemeMap.get (languageName)
429                 );
430             }
431     }
432         toBeSaved = new HashMap JavaDoc ();
433         profiles = new HashMap JavaDoc ();
434     }
435     
436     boolean isChanged () {
437         return !toBeSaved.isEmpty ();
438     }
439     
440     public void setCurrentProfile (String JavaDoc currentProfile) {
441         String JavaDoc oldProfile = this.currentProfile;
442         this.currentProfile = currentProfile;
443         if (!colorModel.getProfiles ().contains (currentProfile))
444             cloneScheme (oldProfile, currentProfile);
445         Vector JavaDoc 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 JavaDoc profile) {
454         Iterator JavaDoc it = colorModel.getLanguages ().iterator ();
455         Map JavaDoc m = new HashMap JavaDoc ();
456         boolean custom = colorModel.isCustomProfile (profile);
457         while (it.hasNext ()) {
458             String JavaDoc language = (String JavaDoc) 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 JavaDoc (colorModel.getLanguages ()));
466         if (!custom)
467             refreshUI ();
468     }
469     
470         
471     // other methods ...........................................................
472

473     private void cloneScheme (String JavaDoc oldScheme, String JavaDoc newScheme) {
474         Map JavaDoc m = new HashMap JavaDoc ();
475         Iterator JavaDoc it = colorModel.getLanguages ().iterator ();
476         while (it.hasNext ()) {
477             String JavaDoc language = (String JavaDoc) it.next ();
478             Vector JavaDoc v = getCategories (oldScheme, language);
479             m.put (language, new Vector JavaDoc (v));
480             setToBeSaved (newScheme, language);
481         }
482         profiles.put (newScheme, m);
483     }
484     
485     Collection JavaDoc getAllLanguages () {
486         return getCategories (currentProfile, ColorModel.ALL_LANGUAGES);
487     }
488     
489     Collection JavaDoc getSyntaxColorings () {
490         return getCategories (currentProfile, currentLanguage);
491     }
492     
493     private void setCurrentLanguage (String JavaDoc language) {
494     currentLanguage = language;
495         
496         // setup categories list
497
blink = false;
498         lCategories.setListData (getCategories (currentProfile, currentLanguage));
499         lCategories.setSelectedIndex (0);
500         blink = true;
501         refreshUI ();
502     }
503     
504     private static String JavaDoc loc (String JavaDoc key) {
505         return NbBundle.getMessage (SyntaxColoringPanel.class, key);
506     }
507     
508     private static void loc (Component JavaDoc c, String JavaDoc key) {
509         if (c instanceof AbstractButton JavaDoc)
510             Mnemonics.setLocalizedText (
511                 (AbstractButton JavaDoc) c,
512                 loc (key)
513             );
514         else
515             Mnemonics.setLocalizedText (
516                 (JLabel JavaDoc) c,
517                 loc (key)
518             );
519     }
520
521     /**
522      * Called on user change.
523      * Updates data structures and preview panel.
524      */

525     private void updateData () {
526         int i = lCategories.getSelectedIndex ();
527         if (i < 0) return;
528         
529         AttributeSet JavaDoc category = getCurrentCategory ();
530         Color JavaDoc 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 JavaDoc c = new SimpleAttributeSet JavaDoc (category);
541         
542         Color JavaDoc 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 JavaDoc getRealColor(JComboBox JavaDoc comboBox) {
580         // The last item is Inherited Color or None
581
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 JavaDoc () {
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 JavaDoc syntaxColorings = getSyntaxColorings ();
607         Collection JavaDoc 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 JavaDoc invertCategory (Collection JavaDoc c, AttributeSet JavaDoc category) {
623         if (category == null) return c;
624         ArrayList JavaDoc result = new ArrayList JavaDoc (c);
625         int i = result.indexOf (category);
626         SimpleAttributeSet JavaDoc as = new SimpleAttributeSet JavaDoc (category);
627         Color JavaDoc highlight = (Color JavaDoc) getValue (currentLanguage, category, StyleConstants.Background);
628         if (highlight == null) return result;
629         Color JavaDoc newColor = new Color JavaDoc (
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     /**
643      * Called when current category, profile or language has been changed.
644      * Updates all ui components.
645      */

646     private void refreshUI () {
647         listen = false;
648         AttributeSet JavaDoc category = getCurrentCategory ();
649         if (category == null) {
650             // no category selected > disable all elements
651
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         // set defaults
669
Color JavaDoc inheritedForeground = (Color JavaDoc) getDefault
670             (currentLanguage, category, StyleConstants.Foreground);
671         if (inheritedForeground == null) inheritedForeground = Color.black;
672         ColorComboBox.setInheritedColor (cbForeground, inheritedForeground);
673         Color JavaDoc inheritedBackground = (Color JavaDoc) getDefault
674             (currentLanguage, category, StyleConstants.Background);
675         if (inheritedBackground == null) inheritedBackground = Color.white;
676         ColorComboBox.setInheritedColor (cbBackground, inheritedBackground);
677         
678         String JavaDoc font = fontToString (category);
679         tfFont.setText (font);
680         ColorComboBox.setColor (
681             cbForeground,
682             (Color JavaDoc) category.getAttribute (StyleConstants.Foreground)
683         );
684         ColorComboBox.setColor (
685             cbBackground,
686             (Color JavaDoc) 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 JavaDoc) 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 JavaDoc) 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 JavaDoc) 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 JavaDoc currentProfile, String JavaDoc currentLanguage) {
722         Set JavaDoc s = (Set JavaDoc) toBeSaved.get (currentProfile);
723         if (s == null) {
724             s = new HashSet JavaDoc ();
725             toBeSaved.put (currentProfile, s);
726         }
727         s.add (currentLanguage);
728     }
729     
730     private Vector JavaDoc getCategories (String JavaDoc profile, String JavaDoc language) {
731         if (colorModel == null) return null;
732         Map JavaDoc m = (Map JavaDoc) profiles.get (profile);
733         if (m == null) {
734             m = new HashMap JavaDoc ();
735             profiles.put (profile, m);
736         }
737         Vector JavaDoc v = (Vector JavaDoc) m.get (language);
738         if (v == null) {
739             Collection JavaDoc c = colorModel.getCategories (profile, language);
740             if (c == null) {
741                 c = Collections.EMPTY_SET; // XXX OK?
742
}
743             List JavaDoc l = new ArrayList JavaDoc (c);
744             Collections.sort (l, new CategoryComparator ());
745             v = new Vector JavaDoc (l);
746             m.put (language, v);
747         }
748         return v;
749     }
750
751     private Map JavaDoc defaults = new HashMap JavaDoc ();
752     /**
753      * Returns original colors for given profile.
754      */

755     private Vector JavaDoc getDefaults (String JavaDoc profile, String JavaDoc language) {
756         Map JavaDoc m = (Map JavaDoc) defaults.get (profile);
757         if (m == null) {
758             m = new HashMap JavaDoc ();
759             defaults.put (profile, m);
760         }
761         Vector JavaDoc v = (Vector JavaDoc) m.get (language);
762         if (v == null) {
763             Collection JavaDoc c = colorModel.getDefaults (profile, language);
764             List JavaDoc l = new ArrayList JavaDoc (c);
765             Collections.sort (l, new CategoryComparator ());
766             v = new Vector JavaDoc (l);
767             m.put (language, v);
768         }
769         return new Vector JavaDoc (v);
770     }
771     
772     private AttributeSet JavaDoc getCurrentCategory () {
773         int i = lCategories.getSelectedIndex ();
774         if (i < 0) return null;
775         return (AttributeSet JavaDoc) getCategories (currentProfile, currentLanguage).get (i);
776     }
777     
778     private void replaceCurrrentCategory (AttributeSet JavaDoc newValues) {
779         int i = lCategories.getSelectedIndex ();
780         getCategories (currentProfile, currentLanguage).set (i, newValues);
781     }
782     
783     private AttributeSet JavaDoc getCategory (
784         String JavaDoc profile,
785         String JavaDoc language,
786         String JavaDoc name
787     ) {
788         Vector JavaDoc v = getCategories (profile, language);
789         Iterator JavaDoc it = v.iterator ();
790         while (it.hasNext ()) {
791             AttributeSet JavaDoc c = (AttributeSet JavaDoc) it.next ();
792             if (c.getAttribute (StyleConstants.NameAttribute).equals (name))
793                 return c;
794         }
795         return null;
796     }
797     
798     private Object JavaDoc getValue (String JavaDoc language, AttributeSet JavaDoc category, Object JavaDoc key) {
799         if (category.isDefined (key))
800             return category.getAttribute (key);
801         return getDefault (language, category, key);
802     }
803     
804     private Object JavaDoc getDefault (String JavaDoc language, AttributeSet JavaDoc category, Object JavaDoc key) {
805     String JavaDoc name = (String JavaDoc) category.getAttribute (EditorStyleConstants.Default);
806     if (name == null) name = "default";
807
808     // 1) search current language
809
if (!name.equals (category.getAttribute (StyleConstants.NameAttribute))
810         ) {
811             AttributeSet JavaDoc defaultAS = getCategory
812                 (currentProfile, language, name);
813             if (defaultAS != null)
814                 return getValue (language, defaultAS, key);
815     }
816     
817     // 2) search default language
818
if (!language.equals (ColorModel.ALL_LANGUAGES)) {
819             AttributeSet JavaDoc 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"; // NOI18N
826
if (key == StyleConstants.FontSize) return getDefaultFontSize ();
827         return null;
828     }
829     
830     private Font JavaDoc getFont (AttributeSet JavaDoc category) {
831         String JavaDoc name = (String JavaDoc) getValue (currentLanguage, category, StyleConstants.FontFamily);
832         if (name == null) name = "Monospaced"; // NOI18N
833
Integer JavaDoc size = (Integer JavaDoc) getValue (currentLanguage, category, StyleConstants.FontSize);
834         if (size == null)
835             size = getDefaultFontSize ();
836         Boolean JavaDoc bold = (Boolean JavaDoc) getValue (currentLanguage, category, StyleConstants.Bold);
837         if (bold == null) bold = Boolean.FALSE;
838         Boolean JavaDoc italic = (Boolean JavaDoc) 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 JavaDoc (name, style, size.intValue ());
843     }
844     
845     private AttributeSet JavaDoc modifyFont (AttributeSet JavaDoc category, Font JavaDoc f) {
846         String JavaDoc fontName = f.getName ();
847         Integer JavaDoc fontSize = new Integer JavaDoc (f.getSize ());
848         Boolean JavaDoc bold = Boolean.valueOf (f.isBold ());
849         Boolean JavaDoc 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 JavaDoc c = new SimpleAttributeSet JavaDoc (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 JavaDoc fontToString (AttributeSet JavaDoc category) {
911         if ("default".equals (
912             category.getAttribute (StyleConstants.NameAttribute)
913         )) {
914             StringBuffer JavaDoc sb = new StringBuffer JavaDoc ();
915             sb.append (getValue (currentLanguage, category, StyleConstants.FontFamily));
916             sb.append (' ');
917             sb.append (getValue (currentLanguage, category, StyleConstants.FontSize));
918             Boolean JavaDoc bold = (Boolean JavaDoc) getValue (currentLanguage, category, StyleConstants.Bold);
919             if (bold != null && bold.booleanValue ())
920                 sb.append (' ').append (loc ("CTL_Bold")); // NOI18N
921
Boolean JavaDoc italic = (Boolean JavaDoc) getValue (currentLanguage, category, StyleConstants.Italic);
922             if (italic != null && italic.booleanValue ())
923                 sb.append (' ').append (loc ("CTL_Italic")); // NOI18N
924
return sb.toString ();
925         }
926         boolean def = false;
927         StringBuffer JavaDoc sb = new StringBuffer JavaDoc ();
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")); // NOI18N
938
if (Boolean.FALSE.equals (category.getAttribute (StyleConstants.Bold)))
939             sb.append ('-').append (loc ("CTL_Bold")); // NOI18N
940
if (Boolean.TRUE.equals (category.getAttribute (StyleConstants.Italic)))
941             sb.append ('+').append (loc ("CTL_Italic")); // NOI18N
942
if (Boolean.FALSE.equals (category.getAttribute (StyleConstants.Italic)))
943             sb.append ('-').append (loc ("CTL_Italic")); // NOI18N
944

945         if (def) {
946             sb.insert (0, loc ("CTL_Inherited")); // NOI18N
947
return sb.toString ();
948         } else {
949             String JavaDoc result = sb.toString ();
950             return result.replace ('+', ' ');
951         }
952     }
953     
954     private static Map JavaDoc convertALC = new HashMap JavaDoc ();
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 JavaDoc defaultFontSize;
971     private static Integer JavaDoc getDefaultFontSize () {
972         if (defaultFontSize == null) {
973             defaultFontSize = (Integer JavaDoc) UIManager.get
974                 ("customFontSize"); // NOI18N
975
if (defaultFontSize == null) {
976                 int s = UIManager.getFont ("TextField.font").getSize (); // NOI18N
977
if (s < 12) s = 12;
978                 defaultFontSize = new Integer JavaDoc (s);
979             }
980         }
981         return defaultFontSize;
982     }
983     
984     private static class LanguagesComparator implements Comparator JavaDoc {
985     public int compare (Object JavaDoc o1, Object JavaDoc o2) {
986         if (o1.equals (ColorModel.ALL_LANGUAGES))
987         return o2.equals (ColorModel.ALL_LANGUAGES) ? 0 : -1;
988         return ((String JavaDoc) o1).compareTo ((String JavaDoc) o2);
989     }
990     }
991 }
992
Popular Tags