KickJava   Java API By Example, From Geeks To Geeks.

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


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.awt.event.*;
24 import java.beans.*;
25 import java.lang.reflect.InvocationTargetException JavaDoc;
26 import javax.swing.*;
27 import javax.swing.border.*;
28 import javax.accessibility.*;
29
30 import org.openide.explorer.propertysheet.PropertyPanel;
31 import org.openide.explorer.propertysheet.PropertyModel;
32 import org.netbeans.editor.Coloring;
33 import org.netbeans.editor.Settings;
34 import org.netbeans.editor.SettingsDefaults;
35 import java.awt.Dimension JavaDoc;
36 import java.beans.FeatureDescriptor JavaDoc;
37 import org.openide.explorer.propertysheet.ExPropertyModel;
38
39 /**
40  * ColoringEditorPanel is custom property editor operating
41  * over ColoringBean, it is interfaced only through ColoringEditor
42  * @author Petr Nejedly
43  *
44  * TODO: remove repaints as Hans will repair PropertyPanel
45  */

46 public class ColoringEditorPanel extends javax.swing.JPanel JavaDoc {
47
48     public static final String JavaDoc PROP_COLORING = "Coloring"; // NOI18N
49

50
51     /** the value we're operating over. */
52     private ColoringBean value;
53
54     /** PropertyPanels for visual editing of Coloring's properties.
55      * We need'em for enabling/disabling
56      */

57     private PropWithDefaultPanel fontPanel;
58     private PropWithDefaultPanel fgColorPanel;
59     private PropWithDefaultPanel bgColorPanel;
60
61     /** Component for preview actual coloring composed of value and defaultColoring
62      */

63     private ColoringPreview preview;
64
65     /** Creates new form ColoringEditorPanel */
66     public ColoringEditorPanel() {
67
68         value = new ColoringBean(SettingsDefaults.defaultColoring, "null", // NOI18N
69
SettingsDefaults.defaultColoring, true );
70
71         initComponents ();
72
73         GridBagConstraints gridBagConstraints1 = new java.awt.GridBagConstraints JavaDoc ();
74         gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH;
75         gridBagConstraints1.insets = new java.awt.Insets JavaDoc (0, 0, 0, 0);
76         gridBagConstraints1.weightx = 1.0;
77         // There should always be non-null property editors for Font and Color
78
Class JavaDoc fontEditorClass = PropertyEditorManager.findEditor(Font.class).getClass();
79         Class JavaDoc colorEditorClass = PropertyEditorManager.findEditor(Color.class).getClass();
80         fontPanel = new PropWithDefaultPanel( Font.class, fontEditorClass,
81                                               getBundleString("CEP_FontTitle"), // NOI18N
82
getBundleString("ACSD_CEP_Font"), // NOI18N
83
getBundleString("CEP_FontTrans"), // NOI18N
84
getBundleString("CEP_FontTrans_Mnemonic").charAt(0), // NOI18N
85
getBundleString("ACSD_CEP_FontTrans")); // NOI18N
86
fontPanel.addPropertyChangeListener( new PropertyChangeListener() {
87                                                  public void propertyChange( PropertyChangeEvent evt ) {
88                                                      if( PropWithDefaultPanel.PROP_VALUE.equals( evt.getPropertyName() ) ) {
89                                                          Font newValue = (Font)fontPanel.getValue();
90                                                          setValueImpl( Coloring.changeFont( value.coloring, newValue ) );
91                                                      }
92                                                  }
93                                              } );
94         add( fontPanel, gridBagConstraints1 );
95
96         gridBagConstraints1 = new java.awt.GridBagConstraints JavaDoc ();
97         gridBagConstraints1.gridx = 0;
98         gridBagConstraints1.gridy = 1;
99         gridBagConstraints1.fill = java.awt.GridBagConstraints.HORIZONTAL;
100         gridBagConstraints1.insets = new java.awt.Insets JavaDoc (12, 0, 0, 0);
101         gridBagConstraints1.weightx = 1.0;
102         fgColorPanel = new PropWithDefaultPanel( Color.class, colorEditorClass,
103                        getBundleString("CEP_FgTitle"), // NOI18N
104
getBundleString("ACSD_CEP_Fg"), // NOI18N
105
getBundleString("CEP_FgTrans"), // NOI18N
106
getBundleString("CEP_FgTrans_Mnemonic").charAt(0), // NOI18N
107
getBundleString("ACSD_CEP_FgTrans")); // NOI18N
108
fgColorPanel.addPropertyChangeListener( new PropertyChangeListener() {
109                                                     public void propertyChange( PropertyChangeEvent evt ) {
110                                                         if( PropWithDefaultPanel.PROP_VALUE.equals( evt.getPropertyName() ) ) {
111                                                             Color newValue = (Color)fgColorPanel.getValue();
112                                                             setValueImpl( Coloring.changeForeColor( value.coloring, newValue) );
113                                                         }
114                                                     }
115                                                 } );
116         add( fgColorPanel, gridBagConstraints1 );
117
118         gridBagConstraints1 = new java.awt.GridBagConstraints JavaDoc ();
119         gridBagConstraints1.gridx = 0;
120         gridBagConstraints1.gridy = 2;
121         gridBagConstraints1.fill = java.awt.GridBagConstraints.HORIZONTAL;
122         gridBagConstraints1.insets = new java.awt.Insets JavaDoc (12, 0, 0, 0);
123         gridBagConstraints1.weightx = 1.0;
124         bgColorPanel = new PropWithDefaultPanel( Color.class, colorEditorClass,
125                        getBundleString("CEP_BgTitle"), // NOI18N
126
getBundleString("ACSD_CEP_Bg"), // NOI18N
127
getBundleString("CEP_BgTrans"), // NOI18N
128
getBundleString("CEP_BgTrans_Mnemonic").charAt(0), // NOI18N
129
getBundleString("ACSD_CEP_BgTrans")); // NOI18N
130

131         bgColorPanel.addPropertyChangeListener( new PropertyChangeListener() {
132                                                     public void propertyChange( PropertyChangeEvent evt ) {
133                                                         if( PropWithDefaultPanel.PROP_VALUE.equals( evt.getPropertyName() ) ) {
134                                                             Color newValue = (Color)bgColorPanel.getValue();
135                                                             setValueImpl( Coloring.changeBackColor( value.coloring, newValue) );
136                                                         }
137                                                     }
138                                                 } );
139         add( bgColorPanel, gridBagConstraints1 );
140
141         gridBagConstraints1 = new java.awt.GridBagConstraints JavaDoc ();
142         gridBagConstraints1.gridx = 0;
143         gridBagConstraints1.gridy = 3;
144         gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH;
145         gridBagConstraints1.insets = new java.awt.Insets JavaDoc (12, 0, 0, 0);
146         gridBagConstraints1.weightx = 1.0;
147         gridBagConstraints1.weighty = 1.0;
148         JPanel previewPanel = new JPanel( new BorderLayout () );
149         previewPanel.setBorder( new CompoundBorder(
150                                     new TitledBorder( getBundleString("CEP_PreviewTitle" ) ), // NOI18N
151
new EmptyBorder( new Insets( 9, 12, 11, 11) )
152                                 ) );
153
154         preview = new ColoringPreview();
155         preview.getAccessibleContext().setAccessibleName(getBundleString("ACSN_CEP_Preview"));
156         preview.getAccessibleContext().setAccessibleDescription(getBundleString("ACSD_CEP_Preview"));
157         previewPanel.add( preview );
158         add( previewPanel, gridBagConstraints1 );
159
160         updateEditors();
161
162         Dimension JavaDoc small = getPreferredSize();
163         small.width *= 2;
164         small.height *= 1.4;
165         setPreferredSize( small );
166     }
167     
168     private String JavaDoc getBundleString(String JavaDoc s) {
169         return org.openide.util.NbBundle.getMessage(ColoringEditorPanel.class, s);
170     }
171
172     /**
173      * Used by underlaying ColoringEditor to query actual Coloring
174      */

175     public ColoringBean getValue() {
176         return value;
177     }
178
179     /**
180      * Used to adjust current coloring from underlaying
181      * ColoringEditor - initial setup of displayed / edited value
182      */

183     public void setValue( ColoringBean value ) {
184         if( (value == null) || (value.coloring == null) ) {
185             return;
186         }
187
188         if( this.value != value ) {
189             this.value = value;
190             updateEditors();
191             preview.setValue( value );
192
193             firePropertyChange( "value", null, null ); // NOI18N
194
}
195     }
196
197
198     private void setValueImpl( Coloring newColoring ) {
199
200         value = value.changeColoring( newColoring );
201
202         preview.setValue( value );
203         //repaint();
204

205         firePropertyChange( "value", null, null ); // NOI18N
206
}
207
208
209     private void updateEditors() {
210         if( value == null ) {
211             return;
212         }
213
214         fontPanel.setValue( value.coloring.getFont() );
215         fontPanel.setDefaultValue( value.defaultColoring.getFont() );
216         fontPanel.setDefault( value.isDefault );
217
218         fgColorPanel.setValue( value.coloring.getForeColor() );
219         fgColorPanel.setDefaultValue( value.defaultColoring.getForeColor() );
220         fgColorPanel.setDefault( value.isDefault );
221
222         bgColorPanel.setValue( value.coloring.getBackColor() );
223         bgColorPanel.setDefaultValue( value.defaultColoring.getBackColor() );
224         bgColorPanel.setDefault( value.isDefault );
225     }
226
227
228     private void initComponents() {//GEN-BEGIN:initComponents
229

230         setLayout(new java.awt.GridBagLayout JavaDoc());
231
232     }//GEN-END:initComponents
233

234
235     // Variables declaration - do not modify//GEN-BEGIN:variables
236
// End of variables declaration//GEN-END:variables
237

238
239     //----------------------------------------------------------------
240

241     private class ColoringPreview extends javax.swing.JComponent JavaDoc implements Accessible {
242         ColoringBean value;
243
244         void setValue( ColoringBean c ) {
245             value = c;
246             this.repaint();
247         }
248
249         public void paint( java.awt.Graphics JavaDoc g ) {
250             if (value != null) {
251                 Coloring coloring = value.coloring.apply( value.defaultColoring );
252
253                 java.awt.Rectangle JavaDoc box = this.getBounds();
254
255                 // clear background
256
g.setColor(coloring.getBackColor());
257                 g.fillRect(0, 0 /*box.x, box.y*/, box.width - 1, box.height - 1);
258
259                 // draw example text
260
g.setColor(coloring.getForeColor());
261                 g.setFont(coloring.getFont());
262                 FontMetrics fm = g.getFontMetrics();
263                 int x = Math.max((box.width - fm.stringWidth(value.example)) / 2, 0);
264                 int y = Math.max((box.height - fm.getHeight()) / 2 + fm.getAscent(), 0);
265                 g.drawString(value.example, x, y);
266             }
267         }
268         
269         public AccessibleContext getAccessibleContext () {
270             if (accessibleContext == null) {
271                 accessibleContext = new AccessibleJComponent() {
272                     public AccessibleRole getAccessibleRole() {
273                         return AccessibleRole.PANEL;
274                     }
275                 };
276             }
277             return accessibleContext;
278         }
279         
280     }
281
282     //-------------------------------------------------
283
private static class PropWithDefaultPanel extends JPanel {
284
285         public static final String JavaDoc PROP_VALUE = "RealValue"; // NOI18N
286

287         Object JavaDoc value;
288         Object JavaDoc defaultValue;
289         boolean isDefault;
290
291         PropertyModel model;
292         JCheckBox defaultCheckBox;
293
294         public PropWithDefaultPanel( Class JavaDoc propertyClass, Class JavaDoc propertyEditorClass, String JavaDoc title, String JavaDoc description,
295                                      String JavaDoc checkBoxTitle, char checkBoxMnemonic, String JavaDoc checkBoxDescription) {
296
297             setLayout( new java.awt.BorderLayout JavaDoc());
298             setBorder( new CompoundBorder( new TitledBorder( title ),
299                                            new EmptyBorder( new Insets( 8, 12, 6, 11) ) ) );
300
301             model = new PropertyModelSupport( propertyClass, propertyEditorClass, title );
302             model.addPropertyChangeListener( new PropertyChangeListener() {
303                                                  public void propertyChange( PropertyChangeEvent evt ) {
304                                                      if( PropertyModelSupport.PROP_MOD_VALUE.equals( evt.getPropertyName() ) ) {
305
306                                                          Object JavaDoc newValue = null;
307                                                          try {
308                                                              newValue = model.getValue();
309                                                          } catch( InvocationTargetException JavaDoc e ) {
310                                                              if( Boolean.getBoolean( "org.netbeans.exceptions" ) ) e.printStackTrace(); // NOI18N
311
}
312
313                                                          if( value == null && newValue.equals( defaultValue ) ) { // setValue( null ) or setDefaultValue( )
314
return; // void change
315
}
316
317                                                          value = newValue;
318                                                          defaultCheckBox.setSelected( false ); // uncheck default
319
firePropertyChange( PROP_VALUE, null, null );
320                                                          repaint(); // XXX - Hack for PropertyPanel not updating
321
}
322                                                  }
323                                              } );
324             PropertyPanel pp = new PropertyPanel (model, 0);
325             pp.setMinimumSize(new Dimension JavaDoc(50,22));
326             pp.getAccessibleContext().setAccessibleName(title);
327             pp.getAccessibleContext().setAccessibleDescription(description);
328             add (pp, BorderLayout.CENTER);
329             
330
331             defaultCheckBox = new JCheckBox();
332             defaultCheckBox.setText(checkBoxTitle);
333             defaultCheckBox.setMnemonic(checkBoxMnemonic);
334             defaultCheckBox.getAccessibleContext().setAccessibleDescription(checkBoxDescription);
335             defaultCheckBox.addActionListener( new ActionListener() {
336                                                    public void actionPerformed( ActionEvent evt ) {
337                                                        if( !defaultCheckBox.isSelected() ) { // unchecked - set real value
338
value = defaultValue;
339                                                            modelSetValue( defaultValue );
340                                                            firePropertyChange( PROP_VALUE, null, null );
341                                                            
342                                                        } else { // checked, provide model with default color
343
value = null;
344                                                            modelSetValue( defaultValue );
345                                                            firePropertyChange( PROP_VALUE, null, null );
346                                                        }
347                                                    }
348                                                } );
349             add( defaultCheckBox, BorderLayout.SOUTH );
350         }
351
352         public void firePropertyChange( String JavaDoc s, Object JavaDoc old, Object JavaDoc newVal ) {
353             super.firePropertyChange( s, old, newVal );
354         }
355
356
357         public void setValue( Object JavaDoc value ) {
358             this.value = value;
359             if( value == null ) {
360                 modelSetValue( defaultValue );
361                 defaultCheckBox.setSelected( true );
362             } else {
363                 modelSetValue( value );
364                 defaultCheckBox.setSelected( false );
365             }
366         }
367
368         Object JavaDoc getValue() {
369             return value;
370         }
371
372         public void setDefaultValue( Object JavaDoc def ) {
373             //System.err.println( "Got setDefaultValue( " + def + " )" );
374
defaultValue = def;
375             if( value == null ) modelSetValue( defaultValue );
376         }
377
378         public void setDefault( boolean isDefault ) {
379             this.isDefault = isDefault;
380         }
381
382         public void addNotify() {
383             super.addNotify();
384             if (isDefault) {
385                 defaultCheckBox.setEnabled(false);
386                 defaultCheckBox.setSelected(false);
387             }
388         }
389
390         private void modelSetValue( Object JavaDoc val ) {
391             try {
392                 model.setValue( val );
393             } catch( InvocationTargetException JavaDoc e ) {
394                 if( Boolean.getBoolean( "org.netbeans.exceptions" ) ) e.printStackTrace(); // NOI18N
395
}
396             repaint(); // XXX - hack for updating PropertyPanel
397
}
398
399         private class PropertyModelSupport implements ExPropertyModel {
400
401             public static final String JavaDoc PROP_MOD_VALUE = "value"; // NOI18N
402

403             /** support for the properties changes. */
404             private PropertyChangeSupport support;
405
406             Class JavaDoc type;
407             Class JavaDoc editor;
408             Object JavaDoc value;
409             String JavaDoc displayName;
410
411             public PropertyModelSupport( Class JavaDoc propertyType, Class JavaDoc propertyEditor, String JavaDoc displayName ) {
412                 support = new PropertyChangeSupport(this);
413                 this.type = propertyType;
414                 this.editor = propertyEditor;
415                 this.displayName = displayName;
416             }
417
418             public Class JavaDoc getPropertyType() {
419                 return type;
420             }
421
422             public Class JavaDoc getPropertyEditorClass() {
423                 return editor;
424             }
425
426             public Object JavaDoc getValue() {
427                 return value;
428             }
429
430             public void setValue(Object JavaDoc v) {
431                 //System.err.println(" PropModel: got setValue( " + v + " )" );
432
if( v != null && (!v.equals( value )) ) {
433                     //System.err.println(" propagating" );
434
value = v;
435                     support.firePropertyChange( PROP_MOD_VALUE, null, null );
436                 }
437             }
438
439
440             /** Adds listener to change of the value.
441              */

442             public void addPropertyChangeListener(PropertyChangeListener l) {
443                 support.addPropertyChangeListener(l);
444             }
445
446             /** Removes listener to change of the value.
447              */

448             public void removePropertyChangeListener(PropertyChangeListener l) {
449                 support.removePropertyChangeListener(l);
450             }
451             
452             public Object JavaDoc[] getBeans() {
453                 return new Object JavaDoc[0];
454             }
455             
456             public FeatureDescriptor JavaDoc getFeatureDescriptor() {
457                 FeatureDescriptor JavaDoc desc = new FeatureDescriptor JavaDoc();
458                 desc.setDisplayName(displayName);
459                 if (editor == PropertyEditorManager.findEditor(Font.class).getClass()){
460                     //#39916
461
desc.setValue("canEditAsText", Boolean.FALSE); //NOI18N
462
}
463                 return desc;
464             }
465             
466         }
467
468     }
469
470 }
471
Popular Tags