KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > options > ColoringArrayEditorPanel


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.editor.options;
21
22 import java.awt.*;
23 import java.beans.*;
24 import java.util.Set JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.lang.reflect.InvocationTargetException JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Arrays JavaDoc;
30 import java.util.Collections JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Vector JavaDoc;
33 import javax.swing.SwingUtilities JavaDoc;
34
35 import org.openide.explorer.propertysheet.PropertyPanel;
36 import org.openide.explorer.propertysheet.PropertyModel;
37
38 import org.netbeans.editor.Coloring;
39 import org.netbeans.editor.LocaleSupport;
40 import org.netbeans.editor.SettingsNames;
41 import org.openide.util.Lookup;
42 import org.openide.util.NbBundle;
43
44 /**
45  * ColoringArrayEditorPanel is custom property editor operating over HashMap
46  * containing (String)name:(Coloring)value pairs. Special name=null is used
47  * to identify default coloring.
48  *
49  * @author Petr Nejedly
50  * @version 1.0
51  */

52 public class ColoringArrayEditorPanel extends javax.swing.JPanel JavaDoc {
53
54     /** Editor interface for visual editing of single coloring */
55     PropertyModel coloringModel;
56
57     /** Index of Coloring actually edited/displayed by coloringModel */
58     int actValueIndex;
59     // Bug #18539 temporary index value
60
int newValueIndex;
61
62
63     /** Name of the the type of the kit, which coloring we're editing */
64     private String JavaDoc typeName;
65
66     /** Names of coloring obtained from HashMap, names[0] is 'default' in given locale */
67     private String JavaDoc names[];
68
69     /** Table of all edited colorings, colorings[0] is default coloring */
70     // private Coloring[] colorings;
71

72     /** HashMap hodling our value. Changes are made by modifying this HashMap */
73     private HashMap JavaDoc value;
74
75     /** Creates new form ColoringArrayEditorPanel */
76     public ColoringArrayEditorPanel() {
77         typeName = BaseOptions.BASE;
78
79         value = new HashMap JavaDoc();
80         names = new String JavaDoc[] { SettingsNames.DEFAULT_COLORING };
81
82         actValueIndex = 0;
83         value.put(names[0], new Coloring( Font.decode( null ), Color.red, Color.blue ) );
84
85         initComponents ();
86
87         getAccessibleContext().setAccessibleDescription(getBundleString("ACSD_CAEP_Panel")); // NOI18N
88
syntaxLabel.setDisplayedMnemonic (getBundleString("CAEP_SyntaxLabel_Mnemonic").charAt (0)); // NOI18N
89
syntaxList.getAccessibleContext().setAccessibleDescription(getBundleString("ACSD_CAEP_Syntax")); // NOI18N
90

91         coloringModel = new PropertyModelSupport( ColoringBean.class, ColoringEditor.class);
92         coloringModel.addPropertyChangeListener( new PropertyChangeListener() {
93                     public void propertyChange( PropertyChangeEvent evt ) {
94                         try {
95                             Coloring newColoring = ((ColoringBean)coloringModel.getValue()).coloring;
96                             if( ! newColoring.equals( value.get( names[actValueIndex] ) ) ) {
97                                 //System.err.println("updating coloring[" + actValueIndex + "] from " + value.get( names[actValueIndex] ) + " to " + newColoring ); // NOI18N
98
//Need to recreate value here (because of equals(), then set!
99
value = (HashMap JavaDoc)value.clone();
100                                 value.put( names[actValueIndex], newColoring );
101                                 // Bug #18539 Hack to prevent changing selected index by firePropertyChange fired below
102
actValueIndex = newValueIndex;
103                                 ColoringArrayEditorPanel.this.firePropertyChange( "value", null, null ); // NOI18N
104
}
105                         } catch( InvocationTargetException JavaDoc e ) {
106                             if( Boolean.getBoolean( "org.netbeans.exceptions" ) ) e.printStackTrace(); // NOI18N
107
}
108                     }
109                 });
110
111
112         syntaxList.setSelectedIndex( actValueIndex );
113         // setEditorValue( actValueIndex );
114
PropertyPanel editorPanel = new PropertyPanel( coloringModel, PropertyPanel.PREF_CUSTOM_EDITOR );
115         detailPanel.add( editorPanel, BorderLayout.CENTER );
116     }
117
118     private String JavaDoc getBundleString(String JavaDoc s) {
119         return NbBundle.getMessage(ColoringArrayEditorPanel.class, s);
120     }
121
122     public HashMap JavaDoc getValue() {
123         return value;
124     }
125
126     public void setValue( HashMap JavaDoc map ) {
127         if( map == null ) return;
128
129         int oldIndex = actValueIndex;
130
131         value = map;
132
133         // Obtain name of the kits type
134
try {
135             ClassLoader JavaDoc l = (ClassLoader JavaDoc)Lookup.getDefault().lookup(ClassLoader JavaDoc.class);
136             Class JavaDoc kitClass = Class.forName( (String JavaDoc)map.get( null ), true, l );
137             typeName = OptionSupport.getTypeName( kitClass );
138         } catch( ClassNotFoundException JavaDoc e ) {
139             org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.INFORMATIONAL, e);
140             return;
141         }
142
143         Set JavaDoc keySet = map.keySet();
144         HashMap JavaDoc tempMap = new HashMap JavaDoc(keySet.size() - 1);
145         String JavaDoc[] names = new String JavaDoc[keySet.size() - 1];
146
147         Iterator JavaDoc iter = keySet.iterator();
148         String JavaDoc defaultName = null;
149         while (iter.hasNext()){
150             String JavaDoc name = (String JavaDoc) iter.next();
151             if (name == null) continue;
152             String JavaDoc visualName = LocaleSupport.getString( "NAME_coloring_" + name ); // NOI18N
153
if( visualName == null )
154                 visualName = LocaleSupport.getString("NAME_coloring_" + BaseOptions.BASE + "-" + name, name ); // NOI18N
155
if (name == SettingsNames.DEFAULT_COLORING) defaultName = visualName;
156             tempMap.put(visualName, name);
157         }
158         
159         List JavaDoc visualNamesList = new ArrayList JavaDoc(tempMap.keySet());
160         
161         Collections.sort(visualNamesList);
162
163         if (defaultName!=null){
164             boolean removed = visualNamesList.remove(defaultName);
165             if (removed){
166                 visualNamesList.add(0, defaultName);
167             }
168         }
169         
170         for (int i = 0; i<visualNamesList.size(); i++){
171             names[i] = (String JavaDoc)tempMap.get(visualNamesList.get(i));
172         }
173         
174         this.names = names;
175         
176         syntaxList.setListData(new Vector JavaDoc(visualNamesList));
177         if( oldIndex < visualNamesList.size() ) actValueIndex = oldIndex;
178         else actValueIndex = 0;
179         syntaxList.setSelectedIndex( actValueIndex );
180     }
181
182     void setEditorValue( int index ) {
183         if( index < 0 ) return;
184
185         String JavaDoc example = LocaleSupport.getString( "EXAMPLE_coloring_" /*+ typeName + "_" */ + names[index] ); // NOI18N
186
if( example == null )
187             example = LocaleSupport.getString("EXAMPLE_coloring_" + BaseOptions.BASE + "-" + names[index], names[index] ); // NOI18N
188

189         Coloring c = (Coloring)value.get(names[index]);
190         ColoringBean bean =
191             new ColoringBean( (Coloring)value.get(names[index]), example, (Coloring)value.get(names[0]), index == 0 );
192
193         try {
194             coloringModel.setValue( bean );
195         } catch( java.lang.reflect.InvocationTargetException JavaDoc e ) {
196             if( Boolean.getBoolean( "org.netbeans.exceptions" ) ) e.printStackTrace(); // NOI18N
197
}
198     }
199
200
201     /** This method is called from within the constructor to initialize the form. */
202     private void initComponents() {//GEN-BEGIN:initComponents
203
java.awt.GridBagConstraints JavaDoc gridBagConstraints;
204
205         detailPanel = new javax.swing.JPanel JavaDoc();
206         masterPanel = new javax.swing.JPanel JavaDoc();
207         syntaxLabel = new javax.swing.JLabel JavaDoc();
208         syntaxScroll = new javax.swing.JScrollPane JavaDoc();
209         syntaxList = new javax.swing.JList JavaDoc();
210
211         setLayout(new java.awt.GridBagLayout JavaDoc());
212
213         detailPanel.setLayout(new java.awt.GridLayout JavaDoc(1, 1));
214
215         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
216         gridBagConstraints.gridx = 1;
217         gridBagConstraints.gridy = 0;
218         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
219         gridBagConstraints.weightx = 1.0;
220         gridBagConstraints.weighty = 1.0;
221         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 0, 11, 11);
222         add(detailPanel, gridBagConstraints);
223
224         masterPanel.setLayout(new java.awt.GridBagLayout JavaDoc());
225
226         syntaxLabel.setLabelFor(syntaxList);
227         syntaxLabel.setText(getBundleString("CAEP_SyntaxLabel"));
228         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
229         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
230         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 2, 0);
231         masterPanel.add(syntaxLabel, gridBagConstraints);
232
233         syntaxList.addListSelectionListener(new javax.swing.event.ListSelectionListener JavaDoc() {
234             public void valueChanged(javax.swing.event.ListSelectionEvent JavaDoc evt) {
235                 syntaxListValueChanged(evt);
236             }
237         });
238
239         syntaxScroll.setViewportView(syntaxList);
240
241         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
242         gridBagConstraints.gridx = 0;
243         gridBagConstraints.gridy = 1;
244         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
245         gridBagConstraints.weightx = 1.0;
246         gridBagConstraints.weighty = 1.0;
247         masterPanel.add(syntaxScroll, gridBagConstraints);
248
249         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
250         gridBagConstraints.gridx = 0;
251         gridBagConstraints.gridy = 0;
252         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
253         gridBagConstraints.weightx = 1.0;
254         gridBagConstraints.weighty = 1.0;
255         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 12, 11, 12);
256         add(masterPanel, gridBagConstraints);
257
258     }//GEN-END:initComponents
259

260     private void syntaxListValueChanged (javax.swing.event.ListSelectionEvent JavaDoc evt) {//GEN-FIRST:event_syntaxListValueChanged
261
// Bug #18539 invoking List value change after property sheet changes the property value
262
if( syntaxList.getSelectedIndex() < 0 )
263             return;
264         if( actValueIndex != syntaxList.getSelectedIndex()) {
265             newValueIndex = syntaxList.getSelectedIndex();
266             SwingUtilities.invokeLater(new Runnable JavaDoc() {
267                 public void run() {
268                     actValueIndex = newValueIndex;
269                     setEditorValue( actValueIndex );
270                 }
271             });
272         }else{
273             actValueIndex = syntaxList.getSelectedIndex();
274             setEditorValue( actValueIndex );
275         }
276     }//GEN-LAST:event_syntaxListValueChanged
277

278
279     // Variables declaration - do not modify//GEN-BEGIN:variables
280
private javax.swing.JPanel JavaDoc masterPanel;
281     private javax.swing.JLabel JavaDoc syntaxLabel;
282     private javax.swing.JScrollPane JavaDoc syntaxScroll;
283     private javax.swing.JList JavaDoc syntaxList;
284     private javax.swing.JPanel JavaDoc detailPanel;
285     // End of variables declaration//GEN-END:variables
286

287     private class PropertyModelSupport implements PropertyModel {
288
289         /** support for the properties changes. */
290         private PropertyChangeSupport support;
291
292         Class JavaDoc type;
293         Class JavaDoc editor;
294         Object JavaDoc value;
295
296         public PropertyModelSupport( Class JavaDoc propertyType, Class JavaDoc propertyEditor ) {
297             support = new PropertyChangeSupport(this);
298             this.type = propertyType;
299             this.editor = propertyEditor;
300         }
301
302         public Class JavaDoc getPropertyType() {
303             return type;
304         }
305
306         public Class JavaDoc getPropertyEditorClass() {
307             return editor;
308         }
309
310         public Object JavaDoc getValue() {
311             return value;
312         }
313
314         public void setValue(Object JavaDoc v) {
315             if( v != null && (! v.equals( value ) ) ) {
316                 value = v;
317                 support.firePropertyChange( PROP_VALUE, null, null );
318             }
319         }
320
321
322         /** Adds listener to change of the value.
323          */

324         public void addPropertyChangeListener(PropertyChangeListener l) {
325             support.addPropertyChangeListener(l);
326         }
327
328         /** Removes listener to change of the value.
329          */

330         public void removePropertyChangeListener(PropertyChangeListener l) {
331             support.removePropertyChangeListener(l);
332         }
333
334     }
335 }
336
Popular Tags