KickJava   Java API By Example, From Geeks To Geeks.

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


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.event.ActionEvent JavaDoc;
25 import java.awt.event.ActionListener JavaDoc;
26 import java.beans.PropertyChangeEvent JavaDoc;
27 import java.beans.PropertyChangeListener JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Collection JavaDoc;
30 import java.util.Collections JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.HashSet JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.List JavaDoc;
35 import java.util.Map JavaDoc;
36 import java.util.Set JavaDoc;
37 import java.util.Vector JavaDoc;
38 import javax.swing.AbstractButton JavaDoc;
39 import javax.swing.JComponent JavaDoc;
40 import javax.swing.JLabel JavaDoc;
41 import javax.swing.JPanel JavaDoc;
42 import javax.swing.ListSelectionModel JavaDoc;
43 import javax.swing.event.ListSelectionEvent JavaDoc;
44 import javax.swing.event.ListSelectionListener JavaDoc;
45 import javax.swing.text.AttributeSet JavaDoc;
46 import javax.swing.text.SimpleAttributeSet JavaDoc;
47 import javax.swing.text.StyleConstants JavaDoc;
48 import org.openide.awt.Mnemonics;
49 import org.openide.util.NbBundle;
50
51 /**
52  *
53  * @author Jan Jancura
54  */

55 public class HighlightingPanel extends JPanel JavaDoc implements ActionListener JavaDoc, PropertyChangeListener JavaDoc {
56     
57     private ColorModel colorModel = null;
58     private boolean listen = false;
59     private String JavaDoc currentProfile;
60     /** cache Map (String (profile name) > Vector (AttributeSet)). */
61     private Map JavaDoc profileToCategories = new HashMap JavaDoc ();
62     /** Set (String (profile name)) of changed profile names. */
63     private Set JavaDoc toBeSaved = new HashSet JavaDoc ();
64     private boolean changed = false;
65
66     
67     /** Creates new form Highlightingpanel1 */
68     public HighlightingPanel () {
69         initComponents ();
70
71         // 1) init components
72
lCategories.getAccessibleContext ().setAccessibleName (loc ("AN_Categories"));
73         lCategories.getAccessibleContext ().setAccessibleDescription (loc ("AD_Categories"));
74         cbForeground.getAccessibleContext ().setAccessibleName (loc ("AN_Foreground_Chooser"));
75         cbForeground.getAccessibleContext ().setAccessibleDescription (loc ("AD_Foreground_Chooser"));
76         cbBackground.getAccessibleContext ().setAccessibleName (loc ("AN_Background_Chooser"));
77         cbBackground.getAccessibleContext ().setAccessibleDescription (loc ("AD_Background_Chooser"));
78         ColorComboBox.init (cbForeground);
79         ColorComboBox.init (cbBackground);
80         lCategories.setSelectionMode (ListSelectionModel.SINGLE_SELECTION);
81         lCategories.setVisibleRowCount (3);
82         lCategories.addListSelectionListener (new ListSelectionListener JavaDoc () {
83             public void valueChanged (ListSelectionEvent JavaDoc e) {
84                 if (!listen) return;
85                 refreshUI ();
86             }
87         });
88         lCategories.setCellRenderer (new CategoryRenderer ());
89         cbForeground.addActionListener (this);
90         ((JComponent JavaDoc)cbForeground.getEditor()).addPropertyChangeListener (this);
91         cbBackground.addActionListener (this);
92         ((JComponent JavaDoc)cbBackground.getEditor()).addPropertyChangeListener (this);
93         JLabel JavaDoc lCategory = new JLabel JavaDoc ();
94         loc (lCategory, "CTL_Category");
95         lCategory.setLabelFor (lCategories);
96     }
97     
98     /** This method is called from within the constructor to
99      * initialize the form.
100      * WARNING: Do NOT modify this code. The content of this method is
101      * always regenerated by the Form Editor.
102      */

103     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
104
private void initComponents() {
105         lCategory = new javax.swing.JLabel JavaDoc();
106         cpCategories = new javax.swing.JScrollPane JavaDoc();
107         lCategories = new javax.swing.JList JavaDoc();
108         lForeground = new javax.swing.JLabel JavaDoc();
109         lBackground = new javax.swing.JLabel JavaDoc();
110         cbBackground = new javax.swing.JComboBox JavaDoc();
111         cbForeground = new javax.swing.JComboBox JavaDoc();
112
113         lCategory.setText("Category:");
114
115         cpCategories.setViewportView(lCategories);
116
117         lForeground.setText("Foreground:");
118
119         lBackground.setText("Background:");
120
121         org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
122         this.setLayout(layout);
123         layout.setHorizontalGroup(
124             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
125             .add(layout.createSequentialGroup()
126                 .addContainerGap()
127                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
128                     .add(layout.createSequentialGroup()
129                         .add(cpCategories, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 211, Short.MAX_VALUE)
130                         .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
131                         .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
132                             .add(lBackground)
133                             .add(lForeground))
134                         .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
135                         .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
136                             .add(org.jdesktop.layout.GroupLayout.TRAILING, cbBackground, 0, 53, Short.MAX_VALUE)
137                             .add(org.jdesktop.layout.GroupLayout.TRAILING, cbForeground, 0, 53, Short.MAX_VALUE)))
138                     .add(lCategory))
139                 .addContainerGap())
140         );
141         layout.setVerticalGroup(
142             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
143             .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
144                 .addContainerGap()
145                 .add(lCategory)
146                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
147                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
148                     .add(layout.createSequentialGroup()
149                         .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
150                             .add(lForeground)
151                             .add(cbForeground, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
152                         .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
153                         .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
154                             .add(lBackground)
155                             .add(cbBackground, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
156                     .add(cpCategories, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 189, Short.MAX_VALUE))
157                 .addContainerGap())
158         );
159     }// </editor-fold>//GEN-END:initComponents
160

161     
162     // Variables declaration - do not modify//GEN-BEGIN:variables
163
private javax.swing.JComboBox JavaDoc cbBackground;
164     private javax.swing.JComboBox JavaDoc cbForeground;
165     private javax.swing.JScrollPane JavaDoc cpCategories;
166     private javax.swing.JLabel JavaDoc lBackground;
167     private javax.swing.JList JavaDoc lCategories;
168     private javax.swing.JLabel JavaDoc lCategory;
169     private javax.swing.JLabel JavaDoc lForeground;
170     // End of variables declaration//GEN-END:variables
171

172  
173     public void actionPerformed (ActionEvent JavaDoc evt) {
174         if (!listen) return;
175         updateData ();
176         changed = true;
177     }
178     
179     public void propertyChange (PropertyChangeEvent JavaDoc evt) {
180         if (!listen) return;
181         if (evt.getPropertyName () == ColorComboBox.PROP_COLOR) {
182             updateData ();
183             changed = true;
184         }
185     }
186     
187     void update (ColorModel colorModel) {
188         this.colorModel = colorModel;
189         currentProfile = colorModel.getCurrentProfile ();
190         listen = false;
191         setCurrentProfile (currentProfile);
192         lCategories.setListData (getCategories (currentProfile));
193         lCategories.setSelectedIndex (0);
194         refreshUI ();
195         listen = true;
196         changed = false;
197     }
198     
199     void cancel () {
200         toBeSaved = new HashSet JavaDoc ();
201         profileToCategories = new HashMap JavaDoc ();
202         changed = false;
203     }
204     
205     public void applyChanges () {
206         if (colorModel == null) return;
207         Iterator JavaDoc it = toBeSaved.iterator ();
208         while (it.hasNext ()) {
209             String JavaDoc profile = (String JavaDoc) it.next ();
210             colorModel.setHighlightings (profile, getCategories (profile));
211         }
212         toBeSaved = new HashSet JavaDoc ();
213         profileToCategories = new HashMap JavaDoc ();
214     }
215     
216     boolean isChanged () {
217         return changed;
218     }
219     
220     void setCurrentProfile (String JavaDoc currentProfile) {
221         String JavaDoc oldScheme = this.currentProfile;
222         this.currentProfile = currentProfile;
223         if (!colorModel.getProfiles ().contains (currentProfile)) {
224             // clone profile
225
Vector JavaDoc categories = getCategories (oldScheme);
226             profileToCategories.put (currentProfile, new Vector JavaDoc (categories));
227             toBeSaved.add (currentProfile);
228         }
229         refreshUI ();
230     }
231
232     void deleteProfile (String JavaDoc profile) {
233         if (colorModel.isCustomProfile (profile))
234             profileToCategories.put (profile, null);
235         else {
236             profileToCategories.put (profile, getDefaults (profile));
237             refreshUI ();
238         }
239         toBeSaved.add (profile);
240     }
241     
242     // other methods ...........................................................
243

244     Collection JavaDoc getHighlightings () {
245         return getCategories (currentProfile);
246     }
247     
248     private static String JavaDoc loc (String JavaDoc key) {
249         return NbBundle.getMessage (SyntaxColoringPanel.class, key);
250     }
251     
252     private static void loc (Component JavaDoc c, String JavaDoc key) {
253         if (c instanceof AbstractButton JavaDoc)
254             Mnemonics.setLocalizedText (
255                 (AbstractButton JavaDoc) c,
256                 loc (key)
257             );
258         else
259             Mnemonics.setLocalizedText (
260                 (JLabel JavaDoc) c,
261                 loc (key)
262             );
263     }
264
265     private void updateData () {
266         if (lCategories.getSelectedIndex () < 0) return;
267         Vector JavaDoc categories = getCategories (currentProfile);
268     AttributeSet JavaDoc category = (AttributeSet JavaDoc) categories.get
269         (lCategories.getSelectedIndex ());
270         Color JavaDoc underline = null,
271               wave = null,
272               strikethrough = null;
273         
274         SimpleAttributeSet JavaDoc c = new SimpleAttributeSet JavaDoc (category);
275         Color JavaDoc color = ((ColorValue) cbBackground.getSelectedItem ()).color;
276         if (color != null)
277             c.addAttribute (
278                 StyleConstants.Background,
279                 color
280             );
281         else
282             c.removeAttribute (StyleConstants.Background);
283         color = ((ColorValue) cbForeground.getSelectedItem ()).color;
284         if (color != null)
285             c.addAttribute (
286                 StyleConstants.Foreground,
287                 color
288             );
289         else
290             c.removeAttribute (StyleConstants.Foreground);
291         int i = lCategories.getSelectedIndex ();
292         categories.set (i, c);
293         
294         toBeSaved.add (currentProfile);
295     }
296     
297     private void refreshUI () {
298         int index = lCategories.getSelectedIndex ();
299         if (index < 0) {
300             cbForeground.setEnabled (false);
301             cbBackground.setEnabled (false);
302             return;
303         }
304         cbForeground.setEnabled (true);
305         cbBackground.setEnabled (true);
306         
307         Vector JavaDoc categories = getCategories (currentProfile);
308     AttributeSet JavaDoc category = (AttributeSet JavaDoc) categories.get (index);
309         
310         listen = false;
311         
312         // set defaults
313
AttributeSet JavaDoc defAs = getDefaultColoring();
314         if (defAs != null) {
315             Color JavaDoc inheritedForeground = (Color JavaDoc) defAs.getAttribute(StyleConstants.Foreground);
316             if (inheritedForeground == null) {
317                 inheritedForeground = Color.black;
318             }
319             ColorComboBox.setInheritedColor(cbForeground, inheritedForeground);
320             
321             Color JavaDoc inheritedBackground = (Color JavaDoc) defAs.getAttribute(StyleConstants.Background);
322             if (inheritedBackground == null) {
323                 inheritedBackground = Color.white;
324             }
325             ColorComboBox.setInheritedColor(cbBackground, inheritedBackground);
326         }
327         
328         // set values
329
ColorComboBox.setColor (
330             cbForeground,
331             (Color JavaDoc) category.getAttribute (StyleConstants.Foreground)
332         );
333         ColorComboBox.setColor (
334             cbBackground,
335             (Color JavaDoc) category.getAttribute (StyleConstants.Background)
336         );
337         listen = true;
338     }
339     
340     private AttributeSet JavaDoc getDefaultColoring() {
341         Collection JavaDoc/*<AttributeSet>*/ defaults = colorModel.getCategories(currentProfile, ColorModel.ALL_LANGUAGES);
342         
343         for(Iterator JavaDoc i = defaults.iterator(); i.hasNext(); ) {
344             AttributeSet JavaDoc as = (AttributeSet JavaDoc) i.next();
345             String JavaDoc name = (String JavaDoc) as.getAttribute(StyleConstants.NameAttribute);
346             if (name != null && "default".equals(name)) { //NOI18N
347
return as;
348             }
349         }
350         
351         return null;
352     }
353     
354     private Vector JavaDoc getCategories (String JavaDoc profile) {
355         if (colorModel == null) return null;
356         if (!profileToCategories.containsKey (profile)) {
357             Collection JavaDoc c = colorModel.getHighlightings (profile);
358             if (c == null) {
359                 c = Collections.EMPTY_SET; // XXX OK?
360
}
361             List JavaDoc l = new ArrayList JavaDoc (c);
362             Collections.sort (l, new CategoryComparator ());
363             profileToCategories.put (profile, new Vector JavaDoc (l));
364         }
365         return (Vector JavaDoc) profileToCategories.get (profile);
366     }
367
368     /** cache Map (String (profile name) > Vector (AttributeSet)). */
369     private Map JavaDoc profileToDefaults = new HashMap JavaDoc ();
370     
371     private Vector JavaDoc getDefaults (String JavaDoc profile) {
372         if (!profileToDefaults.containsKey (profile)) {
373             Collection JavaDoc c = colorModel.getHighlightingDefaults (profile);
374             List JavaDoc l = new ArrayList JavaDoc (c);
375             Collections.sort (l, new CategoryComparator ());
376             profileToDefaults.put (profile, new Vector JavaDoc (l));
377         }
378         return (Vector JavaDoc) profileToDefaults.get (profile);
379     }
380 }
381
Popular Tags