KickJava   Java API By Example, From Geeks To Geeks.

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


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.Component JavaDoc;
23 import java.awt.FontMetrics JavaDoc;
24 import java.awt.Graphics JavaDoc;
25 import java.awt.Rectangle JavaDoc;
26 import java.beans.PropertyChangeEvent JavaDoc;
27 import java.beans.PropertyChangeListener JavaDoc;
28 import java.beans.PropertyEditorSupport JavaDoc;
29
30 import org.netbeans.editor.Coloring;
31 import org.openide.util.HelpCtx;
32
33 /**
34  * Coloring Editor for editor settings. Operates over one ColoringBean
35  *
36  * @author Miloslav Metelka
37  * @author Petr Nejedly
38  */

39 public class ColoringEditor extends PropertyEditorSupport JavaDoc {
40
41     /** Editor for font and color components. */
42     private ColoringEditorPanel editor;
43
44     /** Construct new instance */
45     public ColoringEditor() {
46     }
47     
48     private static final String JavaDoc HELP_ID = "editing.fontsandcolors"; // !!! NOI18N
49

50     protected HelpCtx getHelpCtx () {
51         return new HelpCtx (HELP_ID);
52     }
53
54     /** Get value as text is not supported */
55     public String JavaDoc getAsText() {
56         return null;
57     }
58
59     /** Set value as text is not supported */
60     public void setAsText(String JavaDoc text) {
61         throw new IllegalArgumentException JavaDoc();
62     }
63
64     /** Set the new value into property editor */
65     public void setValue(Object JavaDoc value) {
66         super.setValue( value );
67         if (editor != null) {
68             editor.setValue( (ColoringBean)getValue() );
69         }
70     }
71
72     /** It supports custom editor */
73     public boolean supportsCustomEditor() {
74         return true;
75     }
76
77     /** Get custom editor */
78     public Component JavaDoc getCustomEditor() {
79         if (editor == null) {
80
81             // If we don't have any, create one
82
editor = new ColoringEditorPanel();
83
84             // fill it with our current value
85
editor.setValue( (ColoringBean)getValue() );
86
87             // register listener, which will propagate editor changes to our interval value with firing
88
editor.addPropertyChangeListener(new PropertyChangeListener JavaDoc() {
89                                                  public void propertyChange(PropertyChangeEvent JavaDoc evt) {
90                                                      if (evt.getPropertyName() == "value") // NOI18N
91
superSetValue( editor.getValue()); // skip updating editor
92
}
93                                              });
94             HelpCtx.setHelpIDString( editor, getHelpCtx().getHelpID() );
95         }
96
97         return editor;
98     }
99
100
101     /** when we don't need to update editor, use this */
102     void superSetValue( Object JavaDoc value ) {
103         super.setValue( value );
104     }
105
106     /** This editor is paintable */
107     public boolean isPaintable() {
108         return true;
109     }
110
111     /** Paint the current value */
112     public void paintValue(Graphics JavaDoc g, Rectangle JavaDoc box) {
113         Coloring c = getAppliedColoring();
114         if (c != null) {
115             // clear background
116
g.setColor(c.getBackColor());
117             g.fillRect(box.x, box.y, box.width - 1, box.height - 1);
118
119             // draw example text
120
g.setColor(c.getForeColor());
121             g.setFont(c.getFont());
122             String JavaDoc text = ((ColoringBean)getValue()).example;
123             FontMetrics JavaDoc fm = g.getFontMetrics();
124             int x = Math.max((box.width - fm.stringWidth(text)) / 2, 0);
125             int y = Math.max((box.height - fm.getHeight()) / 2 + fm.getAscent(), 0);
126             g.drawString(text, x, y);
127         }
128     }
129
130     private Coloring getAppliedColoring() {
131         ColoringBean value = ((ColoringBean)getValue());
132         if( value == null ) return null;
133         Coloring dc = value.defaultColoring;
134         Coloring c = value.coloring;
135         Coloring ret = null;
136         if (dc != null && c != null) {
137             ret = c.apply(dc);
138         }
139         return ret;
140     }
141
142 }
143
Popular Tags