KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Boolean3WayEditor.java
21  *
22  * Created on April 16, 2003, 7:05 PM
23  */

24 package org.openide.explorer.propertysheet;
25
26 import org.openide.util.*;
27
28 import java.awt.Component JavaDoc;
29 import java.awt.Graphics JavaDoc;
30 import java.awt.Rectangle JavaDoc;
31 import java.awt.event.*;
32
33 import java.beans.*;
34
35 import java.util.ArrayList JavaDoc;
36 import java.util.List JavaDoc;
37
38 import javax.swing.*;
39
40
41 /** A property editor for Boolean values which can also be null to
42  * indicate the editor represents multiple conflicting values.
43  *
44  * @author Tim Boudreau
45  */

46 final class Boolean3WayEditor implements ExPropertyEditor, InplaceEditor.Factory {
47     Boolean JavaDoc v = null;
48
49     /** Utility field holding list of PropertyChangeListeners. */
50     private transient List JavaDoc<PropertyChangeListener> propertyChangeListenerList;
51     private Boolean3Inplace renderer = null;
52
53     public Boolean3WayEditor() {
54     }
55
56     public String JavaDoc getAsText() {
57         if (v == null) {
58             return NbBundle.getMessage(Boolean3WayEditor.class, "CTL_Different_Values");
59         } else if (Boolean.TRUE.equals(v)) {
60             return Boolean.TRUE.toString(); //XXX use hinting
61
} else {
62             return Boolean.FALSE.toString(); //XXX use hinting
63
}
64     }
65
66     public java.awt.Component JavaDoc getCustomEditor() {
67         return null;
68     }
69
70     public String JavaDoc getJavaInitializationString() {
71         if (v == null) {
72             return "null"; //NOI18N
73
} else if (Boolean.TRUE.equals(v)) {
74             return "Boolean.TRUE"; //NOI18N
75
} else {
76             return "Boolean.FALSE"; //NOI18N
77
}
78     }
79
80     public String JavaDoc[] getTags() {
81         return null;
82     }
83
84     public Object JavaDoc getValue() {
85         return v;
86     }
87
88     public boolean isPaintable() {
89         return true;
90     }
91
92     public void paintValue(Graphics JavaDoc gfx, Rectangle JavaDoc box) {
93         if (renderer == null) {
94             renderer = new Boolean3Inplace();
95         }
96
97         renderer.setSize(box.width, box.height);
98         renderer.doLayout();
99
100         Graphics JavaDoc g = gfx.create(box.x, box.y, box.width, box.height);
101         renderer.setOpaque(false);
102         renderer.paint(g);
103         g.dispose();
104     }
105
106     public void setAsText(String JavaDoc text) {
107         if (Boolean.TRUE.toString().compareToIgnoreCase(text) == 0) {
108             setValue(Boolean.TRUE);
109         } else {
110             setValue(Boolean.FALSE);
111         }
112     }
113
114     public void setValue(Object JavaDoc value) {
115         if (v != value) {
116             v = (Boolean JavaDoc) value;
117             firePropertyChange();
118         }
119     }
120
121     public boolean supportsCustomEditor() {
122         return false;
123     }
124
125     public void attachEnv(PropertyEnv env) {
126         env.registerInplaceEditorFactory(this);
127     }
128
129     /** Registers PropertyChangeListener to receive events.
130      * @param listener The listener to register.
131      *
132      */

133     public synchronized void addPropertyChangeListener(PropertyChangeListener listener) {
134         if (propertyChangeListenerList == null) {
135             propertyChangeListenerList = new java.util.ArrayList JavaDoc<PropertyChangeListener>();
136         }
137
138         propertyChangeListenerList.add(listener);
139     }
140
141     /** Removes PropertyChangeListener from the list of listeners.
142      * @param listener The listener to remove.
143      *
144      */

145     public synchronized void removePropertyChangeListener(PropertyChangeListener listener) {
146         if (propertyChangeListenerList != null) {
147             propertyChangeListenerList.remove(listener);
148         }
149     }
150
151     /** Notifies all registered listeners about the event.
152      *
153      * @param event The event to be fired
154      *
155      */

156     private void firePropertyChange() {
157         List JavaDoc list;
158
159         synchronized (this) {
160             if (propertyChangeListenerList == null) {
161                 return;
162             }
163
164             list = (List JavaDoc) ((ArrayList JavaDoc) propertyChangeListenerList).clone();
165         }
166
167         PropertyChangeEvent event = new PropertyChangeEvent(this, null, null, null);
168
169         for (int i = 0; i < list.size(); i++) {
170             ((PropertyChangeListener) list.get(i)).propertyChange(event);
171         }
172     }
173
174     /** Implementation of InplaceEditor.Factory to create an inplace editor on demand.
175      * With the current implementation, this will actually never be called, because
176      * edit requests for boolean properties automatically toggle the value. This may,
177      * however, be desirable for the reimplementation of PropertyPanel. */

178     public InplaceEditor getInplaceEditor() {
179         return new Boolean3Inplace();
180     }
181
182     class Boolean3Inplace extends JCheckBox implements InplaceEditor {
183         private PropertyModel propertyModel = null;
184
185         Boolean3Inplace() {
186             setModel(new ButtonModel3Way());
187             setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
188         }
189
190         public String JavaDoc getText() {
191             return PropUtils.noCheckboxCaption ? "" : NbBundle.getMessage(
192                 Boolean3WayEditor.class, "CTL_Different_Values"
193             ); //NOI18N
194
}
195
196         public void clear() {
197             propertyModel = null;
198         }
199
200         public void connect(PropertyEditor pe, PropertyEnv env) {
201             //do nothing
202
}
203
204         public javax.swing.JComponent JavaDoc getComponent() {
205             return this;
206         }
207
208         public javax.swing.KeyStroke JavaDoc[] getKeyStrokes() {
209             return null;
210         }
211
212         public PropertyEditor getPropertyEditor() {
213             return Boolean3WayEditor.this;
214         }
215
216         public Object JavaDoc getValue() {
217             return Boolean3WayEditor.this.getValue();
218         }
219
220         public void reset() {
221             //do nothing
222
}
223
224         public void setValue(Object JavaDoc o) {
225             //do nothing
226
}
227
228         public boolean supportsTextEntry() {
229             return false;
230         }
231
232         public void setPropertyModel(PropertyModel pm) {
233             propertyModel = pm;
234         }
235
236         public PropertyModel getPropertyModel() {
237             return propertyModel;
238         }
239
240         public boolean isKnownComponent(Component JavaDoc c) {
241             return false;
242         }
243     }
244
245     private class ButtonModel3Way extends DefaultButtonModel {
246         public boolean isPressed() {
247             return Boolean3WayEditor.this.v == null;
248         }
249
250         public boolean isArmed() {
251             return true;
252         }
253
254         public boolean isSelected() {
255             if (v == null) {
256                 return true;
257             }
258
259             return super.isSelected();
260         }
261     }
262 }
263
Popular Tags