KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > explorer > propertysheet > CheckboxInplaceEditor


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.openide.explorer.propertysheet;
21
22 import org.openide.nodes.Node.*;
23 import org.openide.util.NbBundle;
24
25 import java.awt.Dimension JavaDoc;
26 import java.awt.FontMetrics JavaDoc;
27 import java.awt.Graphics JavaDoc;
28 import java.awt.Insets JavaDoc;
29 import java.awt.event.*;
30
31 import java.beans.*;
32
33 import java.text.MessageFormat JavaDoc;
34
35 import java.util.*;
36
37 import javax.swing.*;
38 import javax.swing.event.*;
39
40
41 /** A basic property-editor-aware JCheckbox that fires changes
42  * appropriately. Note that the property sheet
43  * implementation never instantiates an inplace editor for
44  * booleans, but toggles their state on the editing trigger.
45  * For the property renderer/property panel use case, this
46  * editor is used, since a focusable component is needed.
47  *
48  * @author Tim Boudreau
49  */

50 class CheckboxInplaceEditor extends JCheckBox implements InplaceEditor {
51     protected PropertyEditor editor = null;
52     protected PropertyEnv env = null;
53     private boolean useTitle = false;
54     private String JavaDoc text = null;
55     private PropertyModel pm = null;
56
57     public CheckboxInplaceEditor() {
58         setActionCommand(COMMAND_SUCCESS);
59     }
60
61     public void setUseTitle(boolean val) {
62         if (useTitle != val) {
63             useTitle = val;
64             text = null;
65
66             if (env != null) {
67                 setText(env.getFeatureDescriptor().getDisplayName());
68             }
69         }
70     }
71
72     public void setSelected(boolean val) {
73         boolean fire = val == isSelected();
74         String JavaDoc s = getText();
75         super.setSelected(val);
76
77         if (fire) {
78             firePropertyChange("text", s, getText()); //NOI18N
79
}
80     }
81
82     public void connect(PropertyEditor p, PropertyEnv env) {
83         text = null;
84
85         if (editor instanceof PropUtils.NoPropertyEditorEditor) {
86             //only happens in platform use case
87
setSelected(false);
88
89             return;
90         }
91
92         if (editor == p) {
93             return;
94         }
95
96         editor = p;
97         setSelected(Boolean.TRUE.equals(p.getValue()));
98         reset();
99         this.env = env;
100
101         if (env != null) {
102             if (useTitle) {
103                 setText(env.getFeatureDescriptor().getDisplayName());
104             }
105         }
106     }
107
108     public void clear() {
109         editor = null;
110         pm = null;
111         env = null;
112
113         //setText("");
114
text = null;
115         getModel().setRollover(false);
116     }
117
118     public JComponent getComponent() {
119         return this;
120     }
121
122     public Object JavaDoc getValue() {
123         return isSelected() ? Boolean.TRUE : Boolean.FALSE;
124     }
125
126     public void reset() {
127         if (editor instanceof PropUtils.NoPropertyEditorEditor) {
128             //only happens in platform use case
129
return;
130         }
131
132         if (editor != null) {
133             Boolean JavaDoc value = (Boolean JavaDoc) editor.getValue();
134
135             if (value == null) {
136                 getModel().setArmed(true);
137             } else {
138                 setSelected(value.booleanValue());
139             }
140         }
141     }
142
143     public String JavaDoc getText() {
144         //OptimizeIt shows 1% of drawing time can be spent in re-fetching
145
//text, so cache it as a microoptimization
146
if (text == null) {
147             if (useTitle || (editor == null) || (editor.getTags() == null)) {
148                 //Initialization call in superclass constructor or we do not
149
//have the standard NetBeans boolean editor with tags
150
text = super.getText();
151             } else if (PropUtils.noCheckboxCaption) {
152                 text = ""; //NOI18N
153
} else {
154                 String JavaDoc prepend = NbBundle.getMessage(CheckboxInplaceEditor.class, "BOOLEAN_PREPEND"); //NOI18N
155
String JavaDoc append = NbBundle.getMessage(CheckboxInplaceEditor.class, "BOOLEAN_APPEND"); //NOI18N
156
java.text.MessageFormat JavaDoc mf = new MessageFormat JavaDoc(
157                         NbBundle.getMessage(CheckboxInplaceEditor.class, "FMT_BOOLEAN")
158                     ); //NOI18N
159

160                 String JavaDoc s;
161                 Boolean JavaDoc sel = isSelected() ? Boolean.TRUE : Boolean.FALSE;
162
163                 if (sel.equals(editor.getValue())) {
164                     s = editor.getAsText();
165                 } else {
166                     String JavaDoc[] tags = editor.getTags();
167
168                     if (tags[0].equals(editor.getAsText())) {
169                         s = tags[1];
170                     } else {
171                         s = tags[0];
172                     }
173                 }
174
175                 text = mf.format(new String JavaDoc[] { prepend, s, append });
176             }
177         }
178
179         return text;
180     }
181
182     public KeyStroke[] getKeyStrokes() {
183         return null;
184     }
185
186     public PropertyEditor getPropertyEditor() {
187         return editor;
188     }
189
190     public void handleInitialInputEvent(InputEvent e) {
191         boolean toggle = false;
192
193         if (e instanceof MouseEvent) {
194             toggle = true;
195         } else if (e instanceof KeyEvent) {
196             if (((KeyEvent) e).getKeyCode() == KeyEvent.VK_SPACE) {
197                 toggle = true;
198             }
199         }
200
201         if (toggle) {
202             setSelected(!isSelected());
203             fireActionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, COMMAND_SUCCESS));
204             getModel().setPressed(false); //Mainly an issue for unit tests, but can't hurt
205
}
206     }
207
208     public void setValue(Object JavaDoc o) {
209         if (o == null) {
210             //platform use case
211
setSelected(false);
212         }
213
214         if (Boolean.TRUE.equals(o)) {
215             setSelected(true);
216         } else if (Boolean.FALSE.equals(o)) {
217             setSelected(false);
218         }
219     }
220
221     public boolean supportsTextEntry() {
222         return false;
223     }
224
225     public PropertyModel getPropertyModel() {
226         return pm;
227     }
228
229     public void setPropertyModel(PropertyModel pm) {
230         this.pm = pm;
231     }
232
233     public boolean isKnownComponent(java.awt.Component JavaDoc c) {
234         return false;
235     }
236
237     /** Overridden to be able to calculate the preferred size without having
238      * to be added to the AWT hierarchy */

239     public Dimension JavaDoc getPreferredSize() {
240         if (isShowing()) {
241             return super.getPreferredSize();
242         }
243
244         Dimension JavaDoc result = PropUtils.getMinimumPanelSize();
245         Graphics JavaDoc g = PropUtils.getScratchGraphics(this);
246         g.setFont(getFont());
247
248         String JavaDoc txt = getText();
249         Icon i = getIcon();
250         FontMetrics JavaDoc fm = g.getFontMetrics(getFont());
251         int w = fm.stringWidth(txt);
252         int h = fm.getHeight();
253
254         if (i != null) {
255             w += (i.getIconWidth() + getIconTextGap());
256             h = Math.max(h, result.height);
257         }
258
259         Insets JavaDoc ins = getInsets();
260
261         if (ins != null) {
262             w += (ins.left + ins.right);
263             h += (ins.top + ins.bottom);
264         }
265
266         w += 22; //A small fudge factor to avoid truncated text
267

268         result.width = Math.max(result.width, w);
269         result.height = Math.max(result.height, h);
270
271         return result;
272     }
273 }
274
Popular Tags