KickJava   Java API By Example, From Geeks To Geeks.

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


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  * RendererPropertyDisplayer_java
21  *
22  * Created on 18 October 2003, 11:52
23  */

24 package org.openide.explorer.propertysheet;
25
26 import org.openide.nodes.Node.Property;
27
28 import java.awt.Component JavaDoc;
29 import java.awt.Dimension JavaDoc;
30 import java.awt.Graphics JavaDoc;
31
32 import java.beans.PropertyEditor JavaDoc;
33
34 import javax.swing.BorderFactory JavaDoc;
35 import javax.swing.JCheckBox JavaDoc;
36 import javax.swing.JComboBox JavaDoc;
37 import javax.swing.JComponent JavaDoc;
38 import javax.swing.SwingUtilities JavaDoc;
39
40
41 /** A component which displays a property but cannot edit it.
42  *
43  * @author Tim Boudreau
44  */

45 final class RendererPropertyDisplayer extends JComponent JavaDoc implements PropertyDisplayer_Mutable, PropertyDisplayer_Inline {
46     private boolean showCustomEditorButton = true;
47     private boolean tableUI = false;
48     private int radioButtonMax = 0;
49     private boolean useLabels = true;
50     private Property prop;
51     private boolean radioBoolean;
52     private ReusablePropertyEnv reusableEnv = new ReusablePropertyEnv(); //XXX pass from PP?
53
private ReusablePropertyModel reusableModel = new ReusablePropertyModel(reusableEnv);
54     boolean inGetRenderer = false;
55     private Dimension JavaDoc prefSize = null;
56     private RendererFactory rendererFactory1 = null;
57     private RendererFactory rendererFactory2 = null;
58
59     public RendererPropertyDisplayer(Property p) {
60         this.prop = p;
61     }
62
63     public Component JavaDoc getComponent() {
64         return this;
65     }
66
67     public Property getProperty() {
68         return prop;
69     }
70
71     public void refresh() {
72         repaint();
73     }
74
75     public void validate() {
76         if (!tableUI) {
77             super.validate();
78         }
79     }
80
81     public boolean isValid() {
82         if (tableUI) {
83             return true;
84         } else {
85             return super.isValid();
86         }
87     }
88
89     public boolean isShowing() {
90         if (tableUI) {
91             return true;
92         } else {
93             return super.isShowing();
94         }
95     }
96
97     public void setProperty(Property prop) {
98         if (prop == null) {
99             throw new NullPointerException JavaDoc("Property cannot be null"); //NOI18N
100
}
101
102         if (prop != this.prop) {
103             this.prop = prop;
104             prefSize = null;
105
106             if (isShowing()) {
107                 firePropertyChange("preferredSize", null, null); //NOI18N
108
}
109
110             repaint();
111         }
112     }
113
114     public final boolean isShowCustomEditorButton() {
115         boolean result = showCustomEditorButton;
116
117         if (getProperty() != null) {
118             Boolean JavaDoc explicit = (Boolean JavaDoc) getProperty().getValue("suppressCustomEditor"); //NOI18N
119

120             if (explicit != null) {
121                 result = !explicit.booleanValue();
122             }
123         }
124
125         return result;
126     }
127
128     public boolean isTableUI() {
129         return tableUI;
130     }
131
132     public boolean isUseLabels() {
133         return useLabels;
134     }
135
136     public void setUseLabels(boolean useLabels) {
137         if (useLabels != this.useLabels) {
138             Dimension JavaDoc oldPreferredSize = null;
139
140             if (isShowing()) {
141                 JComponent JavaDoc innermost = findInnermostRenderer(getRenderer(this));
142
143                 if (innermost instanceof RadioInplaceEditor || innermost instanceof JCheckBox JavaDoc) {
144                     oldPreferredSize = getPreferredSize();
145                 }
146             }
147
148             this.useLabels = useLabels;
149
150             if (oldPreferredSize != null) {
151                 firePropertyChange("preferredSize", oldPreferredSize, getPreferredSize()); //NOI18N
152
}
153         }
154     }
155
156     public void setShowCustomEditorButton(boolean val) {
157         //A property saying it doesn't want the custom editor button should
158
//always override anything set on the property panel.
159
if (getProperty() != null) {
160             Boolean JavaDoc explicit = (Boolean JavaDoc) getProperty().getValue("suppressCustomEditor"); //NOI18N
161

162             if (explicit != null) {
163                 val = explicit.booleanValue();
164             }
165         }
166
167         if (showCustomEditorButton != val) {
168             prefSize = null;
169
170             Dimension JavaDoc oldPreferredSize = null;
171
172             if (isShowing()) {
173                 oldPreferredSize = getPreferredSize();
174             }
175
176             showCustomEditorButton = val;
177
178             if (oldPreferredSize != null) {
179                 firePropertyChange("preferredSize", oldPreferredSize, getPreferredSize()); //NOI18N
180
repaint();
181             }
182         }
183     }
184
185     public void setTableUI(boolean val) {
186         if (val != tableUI) {
187             tableUI = val;
188             repaint();
189         }
190     }
191
192     public int getRadioButtonMax() {
193         return radioButtonMax;
194     }
195
196     public void setRadioButtonMax(int i) {
197         if (i != radioButtonMax) {
198             Dimension JavaDoc oldPreferredSize = null;
199
200             if (isShowing()) {
201                 oldPreferredSize = getPreferredSize();
202             }
203
204             int old = radioButtonMax;
205             radioButtonMax = i;
206
207             if (oldPreferredSize != null) {
208                 //see if the change will affect anything
209
PropertyEditor JavaDoc ed = PropUtils.getPropertyEditor(prop);
210                 String JavaDoc[] tags = ed.getTags();
211
212                 if (tags != null) {
213                     if ((tags.length >= i) != (tags.length >= old)) {
214                         firePropertyChange("preferredSize", oldPreferredSize, getPreferredSize()); //NOI18N
215
}
216                 }
217             }
218         }
219     }
220
221     public String JavaDoc toString() {
222         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("Inline editor for property "); //NOI18N
223
sb.append(getProperty().getDisplayName());
224         sb.append(" = "); //NOI18N
225
sb.append(getProperty());
226         sb.append(" inplace editor="); //NOI18N
227

228         if (!inGetRenderer) {
229             try {
230                 sb.append(getRenderer(this));
231             } catch (Exception JavaDoc e) {
232                 sb.append(e);
233             }
234         }
235
236         return sb.toString();
237     }
238
239     public void paintComponent(Graphics JavaDoc g) {
240         //Hack for issue 38132 - Beans are set via TTVBridge as a package
241
//private property on the parent PropertyPanel (if there is one).
242
//FindBeans() will locate the beans either in the model or as a
243
//property of the PropertyPanel (if an esoteric undocumented client property
244
//is set on the PropertyPanel). RendererFactory will set the env
245
//value (if there is an env) to the value of ReusablePropertyEnv.NODE
246
//(a performance hack to avoid creating 1 property env for each property
247
//painted each time we paint). Cool, huh?
248
reusableEnv.setNode(EditorPropertyDisplayer.findBeans(this));
249
250         JComponent JavaDoc comp = getRenderer(this);
251         prepareRenderer(comp);
252         comp.setBounds(0, 0, getWidth(), getHeight());
253
254         if (comp instanceof InplaceEditor) {
255             Component JavaDoc inner = findInnermostRenderer(comp);
256             SwingUtilities.paintComponent(g, comp, this, 0, 0, getWidth(), getHeight());
257
258             return;
259         }
260
261         comp.paint(g);
262     }
263
264     protected void superPaintComponent(Graphics JavaDoc g) {
265         super.paintComponent(g);
266     }
267
268     JComponent JavaDoc getRenderer(PropertyDisplayer_Inline inline) {
269         inGetRenderer = true;
270
271         JComponent JavaDoc result = rfactory(inline).getRenderer(inline.getProperty());
272
273         if (inline.isTableUI()) {
274             //Actually want an empty border, not null - some components treat
275
//a null border as an invitation to improvise
276
result.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
277         }
278
279         inGetRenderer = false;
280
281         return result;
282     }
283
284     protected void prepareRenderer(JComponent JavaDoc comp) {
285         comp.setBackground(getBackground());
286         comp.setForeground(getForeground());
287         comp.setBounds(0, 0, getWidth(), getHeight());
288
289         JComponent JavaDoc innermost;
290
291         if ((innermost = findInnermostRenderer(comp)) instanceof JComboBox JavaDoc) {
292             if (comp.getLayout() != null) {
293                 comp.getLayout().layoutContainer(comp);
294             }
295         }
296
297         if (!isTableUI() && ((InplaceEditor) comp).supportsTextEntry()) {
298             innermost.setBackground(PropUtils.getTextFieldBackground());
299             innermost.setForeground(PropUtils.getTextFieldForeground());
300         }
301     }
302
303     public boolean isTitleDisplayed() {
304         if (!useLabels) {
305             return false;
306         }
307
308         JComponent JavaDoc jc = getRenderer(this);
309
310         if (jc instanceof InplaceEditor) {
311             InplaceEditor innermost = PropUtils.findInnermostInplaceEditor((InplaceEditor) jc);
312
313             return innermost instanceof CheckboxInplaceEditor || innermost instanceof RadioInplaceEditor;
314         }
315
316         return false;
317     }
318
319     static JComponent JavaDoc findInnermostRenderer(JComponent JavaDoc comp) {
320         if (comp instanceof InplaceEditor) {
321             InplaceEditor ine = (InplaceEditor) comp;
322
323             return PropUtils.findInnermostInplaceEditor(ine).getComponent();
324         } else {
325             return comp;
326         }
327     }
328
329     public Dimension JavaDoc getPreferredSize() {
330         //Optimize it shows 16% of painting time is in this call. In some
331
//cases it will be called more than once, so cache the return value
332
if (prefSize == null) {
333             JComponent JavaDoc jc = getRenderer(this);
334             prefSize = jc.getPreferredSize();
335         }
336
337         return prefSize;
338     }
339
340     RendererFactory rfactory(final PropertyDisplayer_Inline inline) {
341         RendererFactory factory;
342
343         if (inline.isTableUI()) {
344             if (rendererFactory1 == null) {
345                 rendererFactory1 = new RendererFactory(
346                         inline.isTableUI(), inline.getReusablePropertyEnv(),
347                         inline.getReusablePropertyEnv().getReusablePropertyModel()
348                     );
349             }
350
351             factory = rendererFactory1;
352         } else {
353             if (rendererFactory2 == null) {
354                 rendererFactory2 = new RendererFactory(
355                         inline.isTableUI(), inline.getReusablePropertyEnv(),
356                         inline.getReusablePropertyEnv().getReusablePropertyModel()
357                     );
358             }
359
360             factory = rendererFactory2;
361         }
362
363         factory.setUseRadioBoolean(inline.isRadioBoolean());
364         factory.setUseLabels(inline.isUseLabels());
365         factory.setRadioButtonMax(inline.getRadioButtonMax());
366
367         return factory;
368     }
369
370     public boolean isRadioBoolean() {
371         return radioBoolean;
372     }
373
374     public void setRadioBoolean(boolean b) {
375         radioBoolean = b;
376     }
377
378     public ReusablePropertyEnv getReusablePropertyEnv() {
379         return reusableEnv;
380     }
381
382     public void firePropertyChange(String JavaDoc name, Object JavaDoc old, Object JavaDoc nue) {
383         //do nothing
384
}
385 }
386
Popular Tags