KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * AnnotationsPanel1.java
3  *
4  * Created on January 17, 2006, 4:27 PM
5  */

6
7 package org.netbeans.modules.options.colors;
8
9 import java.awt.Color JavaDoc;
10 import java.awt.Component JavaDoc;
11 import java.awt.event.ActionEvent JavaDoc;
12 import java.awt.event.ActionListener JavaDoc;
13 import java.beans.PropertyChangeEvent JavaDoc;
14 import java.beans.PropertyChangeListener JavaDoc;
15 import java.util.ArrayList JavaDoc;
16 import java.util.Collection JavaDoc;
17 import java.util.Collections JavaDoc;
18 import java.util.HashMap JavaDoc;
19 import java.util.HashSet JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.Set JavaDoc;
24 import java.util.Vector JavaDoc;
25 import javax.swing.AbstractButton JavaDoc;
26 import javax.swing.JComponent JavaDoc;
27 import javax.swing.JLabel JavaDoc;
28 import javax.swing.JPanel JavaDoc;
29 import javax.swing.ListSelectionModel JavaDoc;
30 import javax.swing.event.ListSelectionEvent JavaDoc;
31 import javax.swing.event.ListSelectionListener JavaDoc;
32 import javax.swing.text.AttributeSet JavaDoc;
33 import javax.swing.text.SimpleAttributeSet JavaDoc;
34 import javax.swing.text.StyleConstants JavaDoc;
35 import org.netbeans.api.editor.settings.EditorStyleConstants;
36 import org.openide.awt.Mnemonics;
37 import org.openide.util.NbBundle;
38
39 /**
40  *
41  * @author Jan Jancura
42  */

43 public class AnnotationsPanel extends JPanel JavaDoc implements ActionListener JavaDoc,
44 PropertyChangeListener JavaDoc {
45     
46     private ColorModel colorModel;
47     private boolean listen = false;
48     private String JavaDoc currentScheme;
49     private Map JavaDoc schemes = new HashMap JavaDoc ();
50     private Set JavaDoc toBeSaved = new HashSet JavaDoc ();
51     private boolean changed = false;
52     
53     
54     /** Creates new form AnnotationsPanel1 */
55     public AnnotationsPanel() {
56         initComponents();
57         
58         // 1) init components
59
cbForeground.getAccessibleContext ().setAccessibleName (loc ("AN_Foreground_Chooser"));
60         cbForeground.getAccessibleContext ().setAccessibleDescription (loc ("AD_Foreground_Chooser"));
61         cbBackground.getAccessibleContext ().setAccessibleName (loc ("AN_Background_Chooser"));
62         cbBackground.getAccessibleContext ().setAccessibleDescription (loc ("AD_Background_Chooser"));
63         cbWaveUnderlined.getAccessibleContext ().setAccessibleName (loc ("AN_Wave_Underlined"));
64         cbWaveUnderlined.getAccessibleContext ().setAccessibleDescription (loc ("AD_Wave_Underlined"));
65         lCategories.getAccessibleContext ().setAccessibleName (loc ("AN_Categories"));
66         lCategories.getAccessibleContext ().setAccessibleDescription (loc ("AD_Categories"));
67         ColorComboBox.init (cbForeground);
68         ColorComboBox.init (cbBackground);
69         ColorComboBox.init (cbWaveUnderlined);
70         lCategories.setSelectionMode (ListSelectionModel.SINGLE_SELECTION);
71         lCategories.setVisibleRowCount (3);
72         lCategories.addListSelectionListener (new ListSelectionListener JavaDoc () {
73             public void valueChanged (ListSelectionEvent JavaDoc e) {
74                 if (!listen) return;
75                 refreshUI ();
76             }
77         });
78     lCategories.setCellRenderer (new CategoryRenderer ());
79         cbForeground.addPropertyChangeListener (this);
80         ((JComponent JavaDoc)cbForeground.getEditor()).addPropertyChangeListener (this);
81         cbBackground.addPropertyChangeListener (this);
82         ((JComponent JavaDoc)cbBackground.getEditor()).addPropertyChangeListener (this);
83         cbWaveUnderlined.addPropertyChangeListener (this);
84         ((JComponent JavaDoc)cbWaveUnderlined.getEditor()).addPropertyChangeListener (this);
85         JLabel JavaDoc lCategory = new JLabel JavaDoc ();
86         loc (lCategory, "CTL_Category");
87         lCategory.setLabelFor (lCategories);
88     }
89     
90     /** This method is called from within the constructor to
91      * initialize the form.
92      * WARNING: Do NOT modify this code. The content of this method is
93      * always regenerated by the Form Editor.
94      */

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

163     
164     // Variables declaration - do not modify//GEN-BEGIN:variables
165
private javax.swing.JComboBox JavaDoc cbBackground;
166     private javax.swing.JComboBox JavaDoc cbForeground;
167     private javax.swing.JComboBox JavaDoc cbWaveUnderlined;
168     private javax.swing.JScrollPane JavaDoc cpCategories;
169     private javax.swing.JList JavaDoc lCategories;
170     private javax.swing.JLabel JavaDoc lCategory;
171     private javax.swing.JLabel JavaDoc lForeground;
172     private javax.swing.JLabel JavaDoc lWaveUnderlined;
173     private javax.swing.JLabel JavaDoc lbackground;
174     // End of variables declaration//GEN-END:variables
175

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

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