KickJava   Java API By Example, From Geeks To Geeks.

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


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  * RendererFactory.java
21  *
22  * Created on 28 September 2003, 17:29
23  */

24 package org.openide.explorer.propertysheet;
25
26 import java.awt.Color JavaDoc;
27 import java.awt.Component JavaDoc;
28 import java.awt.Dimension JavaDoc;
29 import java.awt.Font JavaDoc;
30 import java.awt.Graphics JavaDoc;
31 import java.awt.Image JavaDoc;
32 import java.awt.Rectangle JavaDoc;
33 import java.awt.Toolkit JavaDoc;
34 import java.awt.event.ActionEvent JavaDoc;
35 import java.awt.event.ActionListener JavaDoc;
36 import java.awt.event.ComponentListener JavaDoc;
37 import java.awt.event.ContainerListener JavaDoc;
38 import java.awt.event.FocusEvent JavaDoc;
39 import java.awt.event.HierarchyBoundsListener JavaDoc;
40 import java.awt.event.HierarchyListener JavaDoc;
41 import java.awt.event.MouseEvent JavaDoc;
42 import java.beans.PropertyChangeEvent JavaDoc;
43 import java.beans.PropertyChangeListener JavaDoc;
44 import java.beans.PropertyEditor JavaDoc;
45 import java.lang.reflect.InvocationTargetException JavaDoc;
46 import java.util.logging.Level JavaDoc;
47 import java.util.logging.Logger JavaDoc;
48 import javax.swing.BorderFactory JavaDoc;
49 import javax.swing.Icon JavaDoc;
50 import javax.swing.ImageIcon JavaDoc;
51 import javax.swing.JCheckBox JavaDoc;
52 import javax.swing.JComboBox JavaDoc;
53 import javax.swing.JComponent JavaDoc;
54 import javax.swing.JLabel JavaDoc;
55 import javax.swing.KeyStroke JavaDoc;
56 import javax.swing.border.BevelBorder JavaDoc;
57 import javax.swing.border.Border JavaDoc;
58 import javax.swing.event.ChangeListener JavaDoc;
59 import org.openide.awt.HtmlRenderer;
60 import org.openide.nodes.Node.Property;
61 import org.openide.util.Utilities;
62
63 /**
64  * Factory for renderers which can display properties. With the exception
65  * of the string renderer (which, if tableUI is passed to the constructor,
66  * is a simple JLabel), the renderers are subclasses of the various
67  * InplaceEditor implementations in this package, which are subclassed in
68  * order to suppress all property change events and to call <code>clear()</code>
69  * after any call to <code>paint()</code>. What this means is that once a
70  * renderer is fetched to paint a property, paint may be called <strong>
71  * exactly once</strong>, after which it will have unconfigured itself (this
72  * is to avoid memory leaks due to held references). Whenever it is necessary
73  * to repaint, the user of this factory class <strong>must</strong> once
74  * again fetch a renderer (this also ensures that the renderer used will
75  * always reflect the current state of the property - no caching of possibly
76  * stale state information is possible).
77  *
78  * @author Tim Boudreau
79  */

80 final class RendererFactory {
81
82     private StringRenderer stringRenderer;
83     private CheckboxRenderer checkboxRenderer;
84     private ComboboxRenderer comboboxRenderer;
85     private RadioRenderer radioRenderer;
86     private TextFieldRenderer textFieldRenderer;
87     private ButtonPanel buttonPanel;
88     private IconPanel iconPanel;
89     private ReusablePropertyModel mdl;
90     private ReusablePropertyEnv env;
91     private boolean tableUI;
92     private boolean suppressButton;
93     private int radioButtonMax = -1;
94     private boolean useRadioBoolean = PropUtils.forceRadioButtons;
95     private boolean useLabels;
96
97     /** Creates a new instance of RendererFactory */
98     public RendererFactory(boolean tableUI, ReusablePropertyEnv env, ReusablePropertyModel mdl) {
99         this.tableUI = tableUI;
100         this.env = env;
101         this.mdl = mdl;
102
103         
104         //reset renderers when windows theme is changing (classic <-> xp)
105
Toolkit.getDefaultToolkit().addPropertyChangeListener( "win.xpstyle.themeActive", new PropertyChangeListener JavaDoc() { //NOI18N
106
public void propertyChange(PropertyChangeEvent JavaDoc evt) {
107                 stringRenderer = null;
108                 checkboxRenderer = null;
109                 comboboxRenderer = null;
110                 radioRenderer = null;
111                 textFieldRenderer = null;
112                 buttonPanel = null;
113                 iconPanel = null;
114             }
115         });
116     }
117
118     public void setRadioButtonMax(int i) {
119         radioButtonMax = i;
120     }
121
122     public void setSuppressButton(boolean val) {
123         suppressButton = val;
124     }
125
126     void setUseRadioBoolean(boolean val) {
127         useRadioBoolean = val;
128     }
129
130     /** Set whether or not radio and checkbox editors should show the property
131      * name */

132     void setUseLabels(boolean val) {
133         useLabels = val;
134     }
135
136     /** Get a renderer component appropriate to a given property */
137     public JComponent JavaDoc getRenderer(Property prop) {
138         mdl.setProperty(prop);
139         env.reset();
140
141         PropertyEditor JavaDoc editor = preparePropertyEditor(mdl, env);
142
143         if (editor instanceof ExceptionPropertyEditor) {
144             return getExceptionRenderer((Exception JavaDoc) editor.getValue());
145         }
146
147         JComponent JavaDoc result = null;
148
149         try {
150             if (editor.isPaintable()) {
151                 result = prepareString(editor, env);
152             } else {
153                 Class JavaDoc c = mdl.getPropertyType();
154
155                 if ((c == Boolean JavaDoc.class) || (c == boolean.class)) {
156                     //Special handling for hinting for org.netbeans.beaninfo.BoolEditor
157
boolean useRadioRenderer = useRadioBoolean ||
158                         (env.getFeatureDescriptor().getValue("stringValues") != null); //NOI18N
159

160                     if (useRadioRenderer) {
161                         result = prepareRadioButtons(editor, env);
162                     } else {
163                         result = prepareCheckbox(editor, env);
164                     }
165                 } else if (editor.getTags() != null) {
166                     String JavaDoc[] s = editor.getTags();
167                     boolean editAsText = Boolean.TRUE.equals(prop.getValue("canEditAsText"));
168
169                     if ((s.length <= radioButtonMax) && !editAsText) {
170                         result = prepareRadioButtons(editor, env);
171                     } else {
172                         result = prepareCombobox(editor, env);
173                     }
174                 } else {
175                     result = prepareString(editor, env);
176                 }
177             }
178
179             if ((result != radioRenderer) && (result != textFieldRenderer)) {
180                 if ((result != checkboxRenderer) && tableUI && !(result instanceof JComboBox JavaDoc)) {
181                     result.setBorder(BorderFactory.createEmptyBorder(0, 3, 0, 0));
182                 } else if ((result instanceof JComboBox JavaDoc) && tableUI) {
183                     result.setBorder(BorderFactory.createEmptyBorder());
184                 } else if (!(result instanceof JComboBox JavaDoc) && (!(result instanceof JCheckBox JavaDoc))) {
185                     result.setBorder(BorderFactory.createEmptyBorder(0, 2, 0, 0));
186                 }
187             }
188         } catch (Exception JavaDoc e) {
189             result = getExceptionRenderer(e);
190             Logger.getLogger(RendererFactory.class.getName()).log(Level.WARNING, null, e);
191         }
192
193         result.setEnabled(prop.canWrite());
194
195         boolean propRequestsSuppressButton = Boolean.TRUE.equals(prop.getValue("suppressCustomEditor")); //NOI18N
196

197         if (
198             !(result instanceof JLabel JavaDoc) &&
199                 ((env.getState() == env.STATE_INVALID) || (prop.getValue("valueIcon") != null))
200         ) { //NOI18N
201
result = prepareIconPanel(editor, env, (InplaceEditor) result);
202         }
203
204         /* If we need a custom editor button, embed the resulting component in
205          an instance of ButtonPanel and return that */

206         if (
207             editor.supportsCustomEditor() && !PropUtils.noCustomButtons && !suppressButton &&
208                 !propRequestsSuppressButton
209         ) {
210             ButtonPanel bp = buttonPanel();
211             bp.setInplaceEditor((InplaceEditor) result);
212             result = bp;
213         }
214
215         return result;
216     }
217
218     private IconPanel prepareIconPanel(PropertyEditor JavaDoc ed, PropertyEnv env, InplaceEditor inner) {
219         IconPanel icp = iconPanel();
220         icp.setInplaceEditor(inner);
221         icp.connect(ed, env);
222
223         return icp;
224     }
225
226     private PropertyEditor JavaDoc preparePropertyEditor(PropertyModel pm, PropertyEnv env) {
227         PropertyEditor JavaDoc result;
228
229         try {
230             if (pm instanceof NodePropertyModel) {
231                 result = ((NodePropertyModel) pm).getPropertyEditor();
232             } else if (pm instanceof ReusablePropertyModel) {
233                 result = ((ReusablePropertyModel) pm).getPropertyEditor();
234             } else {
235                 Class JavaDoc c = pm.getPropertyEditorClass();
236
237                 if (c != null) {
238                     try {
239                         result = (PropertyEditor JavaDoc) c.newInstance();
240
241                         //Check the values first
242
Object JavaDoc mdlValue = pm.getValue();
243                         Object JavaDoc edValue = result.getValue();
244
245                         if (edValue != mdlValue) {
246                             result.setValue(pm.getValue());
247                         }
248                     } catch (Exception JavaDoc e) {
249                         result = new ExceptionPropertyEditor(e);
250                     }
251                 } else {
252                     result = PropUtils.getPropertyEditor(pm.getPropertyType());
253
254                     try {
255                         result.setValue(pm.getValue());
256                     } catch (InvocationTargetException JavaDoc ite) {
257                         result = new ExceptionPropertyEditor(ite);
258                     }
259                 }
260             }
261         } catch (Exception JavaDoc e) {
262             result = new ExceptionPropertyEditor(e);
263         }
264
265         if (result instanceof ExPropertyEditor) {
266             ((ExPropertyEditor) result).attachEnv(env);
267         }
268
269         return result;
270     }
271
272     private JComponent JavaDoc getExceptionRenderer(Exception JavaDoc e) {
273         //Anything may have gone wrong, don't rely on other infrastructure
274
ExceptionRenderer lbl = new ExceptionRenderer();
275         lbl.setForeground(PropUtils.getErrorColor());
276         lbl.setText(e.getMessage());
277
278         return lbl;
279     }
280
281     public JComponent JavaDoc getStringRenderer() {
282         StringRenderer result = stringRenderer();
283         result.clear();
284         result.setEnabled(true);
285
286         return result;
287     }
288
289     private JComponent JavaDoc prepareRadioButtons(PropertyEditor JavaDoc editor, PropertyEnv env) {
290         RadioRenderer ren = radioRenderer();
291         ren.clear();
292         ren.setUseTitle(useLabels);
293         ren.connect(editor, env);
294
295         return ren.getComponent();
296     }
297
298     private JComponent JavaDoc prepareCombobox(PropertyEditor JavaDoc editor, PropertyEnv env) {
299         ComboboxRenderer ren = comboboxRenderer();
300         ren.clear();
301         ren.setEnabled(true);
302         ren.connect(editor, env);
303
304         return ren.getComponent();
305     }
306
307     private JComponent JavaDoc prepareString(PropertyEditor JavaDoc editor, PropertyEnv env) {
308         InplaceEditor ren = (tableUI || editor.isPaintable()) ? (InplaceEditor) stringRenderer()
309                                                               : (InplaceEditor) textFieldRenderer();
310         ren.clear();
311         ren.getComponent().setEnabled(true);
312         ren.connect(editor, env);
313
314         return ren.getComponent();
315     }
316
317     private JComponent JavaDoc prepareCheckbox(PropertyEditor JavaDoc editor, PropertyEnv env) {
318         CheckboxRenderer ren = checkboxRenderer();
319         ren.setUseTitle(useLabels);
320         ren.clear();
321         ren.setEnabled(true);
322         ren.connect(editor, env);
323
324         return ren.getComponent();
325     }
326
327     private ButtonPanel buttonPanel() {
328         if (buttonPanel == null) {
329             buttonPanel = new ButtonPanel();
330         }
331
332         buttonPanel.setEnabled(true);
333
334         return buttonPanel;
335     }
336
337     private IconPanel iconPanel() {
338         if (iconPanel == null) {
339             iconPanel = new IconPanel();
340         }
341
342         iconPanel.setEnabled(true);
343
344         return iconPanel;
345     }
346
347     /**
348      * Lazily creates a combo box renderer
349      */

350     private ComboboxRenderer comboboxRenderer() {
351         if (comboboxRenderer == null) {
352             comboboxRenderer = new ComboboxRenderer(tableUI);
353
354             //Mainly for debugging
355
((JComponent JavaDoc) comboboxRenderer).setName(
356                 "ComboboxRenderer for " + getClass().getName() + "@" + System.identityHashCode(this)
357             ); //NOI18N
358
}
359
360         return comboboxRenderer;
361     }
362
363     /**
364      * Lazily creates a string renderer
365      */

366     private StringRenderer stringRenderer() {
367         if (stringRenderer == null) {
368             stringRenderer = new StringRenderer(tableUI);
369
370             //Mainly for debugging
371
((JComponent JavaDoc) stringRenderer).setName(
372                 "StringRenderer for " + getClass().getName() + "@" + System.identityHashCode(this)
373             ); //NOI18N
374
}
375
376         return stringRenderer;
377     }
378
379     /**
380      * Lazily creates a checkbox renderer
381      */

382     private CheckboxRenderer checkboxRenderer() {
383         if (checkboxRenderer == null) {
384             checkboxRenderer = new CheckboxRenderer();
385
386             //Mainly for debugging
387
((JComponent JavaDoc) checkboxRenderer).setName(
388                 "CheckboxRenderer for " + getClass().getName() + "@" + System.identityHashCode(this)
389             ); //NOI18N
390
}
391
392         return checkboxRenderer;
393     }
394
395     /**
396      * Lazily creates a radio button renderer
397      */

398     private RadioRenderer radioRenderer() {
399         if (radioRenderer == null) {
400             radioRenderer = new RadioRenderer(tableUI);
401
402             //Mainly for debugging
403
((JComponent JavaDoc) radioRenderer).setName(
404                 "RadioRenderer for " + getClass().getName() + "@" + System.identityHashCode(this)
405             ); //NOI18N
406
}
407
408         return radioRenderer;
409     }
410
411     /**
412      * Lazily creates a text field renderer
413      */

414     private TextFieldRenderer textFieldRenderer() {
415         if (textFieldRenderer == null) {
416             textFieldRenderer = new TextFieldRenderer();
417         }
418
419         return textFieldRenderer;
420     }
421
422     /**
423      * Makes the given String displayble. Probably there doesn't exists
424      * perfect solution for all situation. (someone prefer display those
425      * squares for undisplayable chars, someone unicode placeholders). So lets
426      * try do the best compromise.
427      */

428     private static String JavaDoc makeDisplayble(String JavaDoc str, Font JavaDoc f) {
429         if (null == str) {
430             return str;
431         }
432
433         if (null == f) {
434             f = new JLabel JavaDoc().getFont();
435         }
436
437         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(str.length() * 6); // x -> \u1234
438
char[] chars = str.toCharArray();
439
440         for (int i = 0; i < chars.length; i++) {
441             char c = chars[i];
442
443             switch (c) {
444             // label doesn't interpret tab correctly
445
case '\t':
446                 buf.append(" "); // NOI18N
447
break;
448
449             case '\n':
450                 break;
451
452             case '\r':
453                 break;
454
455             case '\b':
456                 buf.append("\\b");
457
458                 break; // NOI18N
459

460             case '\f':
461                 buf.append("\\f");
462
463                 break; // NOI18N
464

465             default:
466
467                 if ((null == f) || f.canDisplay(c)) {
468                     buf.append(c);
469                 } else {
470                     buf.append("\\u"); // NOI18N
471

472                     String JavaDoc hex = Integer.toHexString(c);
473
474                     for (int j = 0; j < (4 - hex.length()); j++)
475                         buf.append('0');
476
477                     buf.append(hex);
478                 }
479             }
480         }
481
482         return buf.toString();
483     }
484
485     static final class ComboboxRenderer extends ComboInplaceEditor {
486         private Object JavaDoc item = null;
487         boolean editable = false;
488
489         public ComboboxRenderer(boolean tableUI) {
490             super(tableUI);
491         }
492
493         public boolean isEditable() {
494             return false;
495         }
496
497         /** Overridden to clear state after painting once */
498         public void paintComponent(Graphics JavaDoc g) {
499             setEnabled(isEnabled() && env.isEditable());
500
501             //We may paint without a parent in PropertyPanel, so do a layout to
502
//ensure there's something to paint
503
doLayout(); //just in case some L&F will render directly
504
super.paintComponent(g);
505
506             //Clear cached values
507
clear();
508         }
509
510         public void clear() {
511             super.clear();
512             item = null;
513         }
514
515         public void setSelectedItem(Object JavaDoc o) {
516             item = o;
517
518             if ((item == null) && (editor != null) && (editor.getTags().length > 0)) {
519                 item = editor.getTags()[0];
520             }
521
522             if (editable) {
523                 getEditor().setItem(getSelectedItem());
524             }
525         }
526
527         public Object JavaDoc getSelectedItem() {
528             return item;
529         }
530
531         public void installAncestorListener() {
532             //do nothing
533
}
534
535         /** Overridden to block code in ComboInplaceEditor */
536         public void processFocusEvent(FocusEvent JavaDoc fe) {
537             //do nothing
538
}
539
540         public void processMouseEvent(MouseEvent JavaDoc me) {
541             //do nothing
542
}
543
544         /** Overridden to do nothing */
545         public void addActionListener(ActionListener JavaDoc ae) {
546         }
547
548         /** Overridden to do nothing */
549         protected void fireActionPerformed(ActionEvent JavaDoc ae) {
550         }
551
552         /** Overridden to do nothing */
553         protected void fireStateChanged() {
554         }
555
556         /** Overridden only fire those properties needed */
557         protected void firePropertyChange(String JavaDoc name, Object JavaDoc old, Object JavaDoc nue) {
558             //firing all changes for now - breaks text painting on OS-X
559
super.firePropertyChange(name, old, nue);
560         }
561     }
562
563     private static final class CheckboxRenderer extends CheckboxInplaceEditor {
564         /** Overridden to clear state after painting once */
565         public void paintComponent(Graphics JavaDoc g) {
566             setEnabled(PropUtils.checkEnabled(this, editor, env));
567             super.paintComponent(g);
568             clear();
569         }
570
571         /** Overridden to do nothing */
572         protected void fireActionPerformed(ActionEvent JavaDoc ae) {
573         }
574
575         /** Overridden to do nothing */
576         protected void fireStateChanged() {
577         }
578
579         /** Overridden only fire those properties needed */
580         protected void firePropertyChange(String JavaDoc name, Object JavaDoc old, Object JavaDoc nue) {
581             //gtk L&F needs these, although bg and fg don't work on it in 1.4.2
582
if ("foreground".equals(name) || "background".equals(name) || "font".equals(name)) { //NOI18N
583
super.firePropertyChange(name, old, nue);
584             }
585         }
586
587         /** Overridden to do nothing */
588         public void firePropertyChange(String JavaDoc name, boolean old, boolean nue) {
589         }
590
591         /** Overridden to do nothing */
592         public void firePropertyChange(String JavaDoc name, int old, int nue) {
593         }
594
595         /** Overridden to do nothing */
596         public void firePropertyChange(String JavaDoc name, byte old, byte nue) {
597         }
598
599         /** Overridden to do nothing */
600         public void firePropertyChange(String JavaDoc name, char old, char nue) {
601         }
602
603         /** Overridden to do nothing */
604         public void firePropertyChange(String JavaDoc name, double old, double nue) {
605         }
606
607         /** Overridden to do nothing */
608         public void firePropertyChange(String JavaDoc name, float old, float nue) {
609         }
610
611         /** Overridden to do nothing */
612         public void firePropertyChange(String JavaDoc name, short old, short nue) {
613         }
614     }
615
616     /** A renderer for string properties, which can also delegate to the
617      * property editor's <code>paint()</code>method if possible. */

618     private static final class StringRenderer extends JLabel JavaDoc implements InplaceEditor {
619         private PropertyEditor JavaDoc editor = null;
620         private PropertyEnv env = null;
621         private boolean tableUI = false;
622         private boolean enabled = true;
623         private JLabel JavaDoc htmlLabel = HtmlRenderer.createLabel();
624         private JLabel JavaDoc noHtmlLabel = new JLabel JavaDoc();
625         private Object JavaDoc value = null;
626
627         public StringRenderer(boolean tableUI) {
628             this.tableUI = tableUI;
629             setOpaque(true);
630             ((HtmlRenderer.Renderer) htmlLabel).setRenderStyle(HtmlRenderer.STYLE_TRUNCATE);
631         }
632
633         /** OptimizeIt shows about 12Ms overhead calling back to Component.enable(),
634          * so overriding */

635         public void setEnabled(boolean val) {
636             enabled = val;
637         }
638
639         public void setText(String JavaDoc s) {
640             if (s != null) {
641                 if (s.length() > 512) {
642                     //IZ 44152 - Debugger producing 512K long strings, etc.
643
super.setText(makeDisplayble(s.substring(0, 512), getFont()));
644                 } else {
645                     super.setText(makeDisplayble(s, getFont()));
646                 }
647             } else {
648                 super.setText(""); //NOI18N
649
}
650         }
651
652         /** OptimizeIt shows about 12Ms overhead calling back to Component.enable(),
653          * so overriding */

654         public boolean isEnabled() {
655             return enabled;
656         }
657
658         /** Overridden to do nothing */
659         protected void firePropertyChange(String JavaDoc name, Object JavaDoc old, Object JavaDoc nue) {
660             //do nothing
661
}
662
663         public void validate() {
664             //do nothing
665
}
666
667         public void invalidate() {
668             //do nothing
669
}
670
671         public void revalidate() {
672             //do nothing
673
}
674
675         public void repaint() {
676             //do nothing
677
}
678
679         public void repaint(long tm, int x, int y, int w, int h) {
680             //do nothing
681
}
682
683         public Dimension JavaDoc getPreferredSize() {
684             if (getText().length() > 1024) {
685                 //IZ 44152, avoid excessive calculations when debugger
686
//returns its 512K+ strings
687
return new Dimension JavaDoc(4196, PropUtils.getMinimumPropPanelHeight());
688             }
689
690             Dimension JavaDoc result = super.getPreferredSize();
691             result.width = Math.max(result.width, PropUtils.getMinimumPropPanelWidth());
692
693             result.height = Math.max(result.height, PropUtils.getMinimumPropPanelHeight());
694
695             return result;
696         }
697
698         public void paint(Graphics JavaDoc g) {
699             if (editor != null) {
700                 setEnabled(PropUtils.checkEnabled(this, editor, env));
701             }
702
703             if (editor instanceof ExceptionPropertyEditor) {
704                 setForeground(PropUtils.getErrorColor());
705             }
706
707             if ((editor != null) && editor.isPaintable()) {
708                 delegatedPaint(g);
709             } else {
710                 String JavaDoc htmlDisplayValue = (env == null) ? null :
711                     (String JavaDoc) env.getFeatureDescriptor().getValue("htmlDisplayValue"); // NOI18N
712
boolean htmlValueUsed = htmlDisplayValue != null;
713
714                 JLabel JavaDoc lbl = htmlValueUsed ? htmlLabel : noHtmlLabel;
715                 String JavaDoc text = htmlValueUsed ? htmlDisplayValue : getText();
716
717                 if (text == null) {
718                     text = ""; // NOI18N
719
} else {
720                     text = makeDisplayble( text, getFont() );
721                 }
722
723                 if (htmlValueUsed) {
724                     // > 512 = huge strings - don't try to support this as html
725
((HtmlRenderer.Renderer) lbl).setHtml(text.length() < 512);
726                 }
727
728                 lbl.setFont(getFont());
729                 lbl.setEnabled(isEnabled());
730                 lbl.setText(text); //NOI18N
731

732                 if (!htmlValueUsed) {
733                     lbl.putClientProperty("html", null); // NOI18N
734
}
735
736                 lbl.setIcon(getIcon());
737                 lbl.setIconTextGap(getIconTextGap());
738                 lbl.setBounds(getBounds());
739                 lbl.setOpaque(true);
740                 lbl.setBackground(getBackground());
741                 lbl.setForeground(getForeground());
742                 lbl.setBorder( getBorder() );
743                 lbl.paint(g);
744             }
745
746             clear();
747         }
748
749         private void delegatedPaint(Graphics JavaDoc g) {
750             Color JavaDoc c = g.getColor();
751
752             try {
753                 g.setColor(getBackground());
754                 g.fillRect(0, 0, getWidth(), getHeight());
755                 g.setColor(getForeground());
756
757                 if (!tableUI) {
758                     //in the panel, give self-painting editors a lowered
759
//border so they look like something
760
Border JavaDoc b = BorderFactory.createBevelBorder(BevelBorder.LOWERED);
761                     b.paintBorder(this, g, 0, 0, getWidth(), getHeight());
762                 }
763
764                 Rectangle JavaDoc r = getBounds();
765
766                 //XXX May be the source of Rochelle's multiple rows of error
767
//marking misalignment problem...(I do not jest)
768
r.x = (getWidth() > 16) ? ((editor instanceof Boolean3WayEditor) ? 0 : 3) : 0; //align text with other renderers
769
r.width -= ((getWidth() > 16) ? ((editor instanceof Boolean3WayEditor) ? 0 : 3) : 0); //align text with other renderers
770
r.y = 0;
771                 editor.paintValue(g, r);
772             } finally {
773                 g.setColor(c);
774             }
775         }
776
777         public void clear() {
778             editor = null;
779             env = null;
780             setIcon(null);
781             setOpaque(true);
782         }
783
784         public void setValue(Object JavaDoc o) {
785             value = o;
786             setText((value instanceof String JavaDoc) ? (String JavaDoc) value : ((value != null) ? value.toString() : null));
787         }
788
789         public void connect(PropertyEditor JavaDoc p, PropertyEnv env) {
790             editor = p;
791             this.env = env;
792             reset();
793         }
794
795         public JComponent JavaDoc getComponent() {
796             return this;
797         }
798
799         public KeyStroke JavaDoc[] getKeyStrokes() {
800             return null;
801         }
802
803         public PropertyEditor JavaDoc getPropertyEditor() {
804             return editor;
805         }
806
807         public PropertyModel getPropertyModel() {
808             return null;
809         }
810
811         public Object JavaDoc getValue() {
812             return getText();
813         }
814
815         public void handleInitialInputEvent(java.awt.event.InputEvent JavaDoc e) {
816             //do nothing
817
}
818
819         public boolean isKnownComponent(Component JavaDoc c) {
820             return false;
821         }
822
823         public void removeActionListener(ActionListener JavaDoc al) {
824             //do nothing
825
}
826
827         public void reset() {
828             setText(editor.getAsText());
829
830             Image JavaDoc i = null;
831
832             if (env != null) {
833                 if (env.getState() == env.STATE_INVALID) {
834                     setForeground(PropUtils.getErrorColor());
835                     i = Utilities.loadImage("org/openide/resources/propertysheet/invalid.gif"); //NOI18N
836
} else {
837                     Object JavaDoc o = env.getFeatureDescriptor().getValue("valueIcon"); //NOI18N
838

839                     if (o instanceof Icon JavaDoc) {
840                         setIcon((Icon JavaDoc) o);
841                     } else if (o instanceof Image JavaDoc) {
842                         i = (Image JavaDoc) o;
843                     }
844                 }
845             }
846
847             if (i != null) {
848                 setIcon(new ImageIcon JavaDoc(i));
849             }
850         }
851
852         public void setPropertyModel(PropertyModel pm) {
853             //do nothing
854
}
855
856         public boolean supportsTextEntry() {
857             return false;
858         }
859
860         /** Overridden to do nothing */
861         protected void fireActionPerformed(ActionEvent JavaDoc ae) {
862         }
863
864         /** Overridden to do nothing */
865         protected void fireStateChanged() {
866         }
867
868         public void addActionListener(ActionListener JavaDoc al) {
869             //do nothing
870
}
871     }
872
873     /** A JTextField renderer - the property sheet does not use this, but
874      * the property panel does */

875     private static final class TextFieldRenderer extends StringInplaceEditor {
876         public void paintComponent(Graphics JavaDoc g) {
877             setEnabled(PropUtils.checkEnabled(this, editor, env));
878             super.paintComponent(g);
879             clear();
880         }
881
882         /** Overridden to do nothing */
883         protected void fireActionPerformed(ActionEvent JavaDoc ae) {
884         }
885
886         /** Overridden to do nothing */
887         protected void fireStateChanged() {
888         }
889
890         /** Overridden to do nothing */
891         protected void firePropertyChange(String JavaDoc name, Object JavaDoc old, Object JavaDoc nue) {
892             //two changes we need to fire in order to be able to paint properly
893
boolean fire = ("locale".equals(name)) || ("document".equals(name)); //NOI18N
894

895             if (fire) {
896                 super.firePropertyChange(name, old, nue);
897             }
898         }
899     }
900
901     private static final class RadioRenderer extends RadioInplaceEditor {
902         private boolean needLayout = true;
903
904         public RadioRenderer(boolean tableUI) {
905             super(tableUI);
906         }
907
908         public void connect(PropertyEditor JavaDoc pe, PropertyEnv env) {
909             super.connect(pe, env);
910             needLayout = true;
911         }
912
913         public void paint(Graphics JavaDoc g) {
914             if (needLayout) {
915                 getLayout().layoutContainer(this);
916                 needLayout = false;
917             }
918
919             // setEnabled(PropUtils.checkEnabled (this, editor, env));
920
super.paint(g);
921             clear();
922         }
923
924         /** Renderer version overrides this to create a subclass that won't
925          * fire changes */

926         protected InvRadioButton createButton() {
927             return new NoEventsInvRadioButton();
928         }
929
930         /** Renderer version overrides this */
931         protected void configureButton(InvRadioButton ire, String JavaDoc txt) {
932             if (editor.getTags().length == 1) {
933                 ire.setEnabled(false);
934             } else {
935                 ire.setEnabled(isEnabled());
936             }
937
938             ire.setText(txt);
939             ire.setForeground(getForeground());
940             ire.setBackground(getBackground());
941             ire.setFont(getFont());
942
943             if (txt.equals(editor.getAsText())) {
944                 ire.setSelected(true);
945             } else {
946                 ire.setSelected(false);
947             }
948         }
949
950         /** Overridden to do nothing */
951         public void addContainerListener(ContainerListener JavaDoc cl) {
952         }
953
954         /** Overridden to do nothing */
955         public void addChangeListener(ChangeListener JavaDoc cl) {
956         }
957
958         /** Overridden to do nothing */
959         public void addComponentListener(ComponentListener JavaDoc l) {
960         }
961
962         /** Overridden to do nothing */
963         public void addHierarchyBoundsListener(HierarchyBoundsListener JavaDoc hbl) {
964         }
965
966         /** Overridden to do nothing */
967         public void addHierarchyListener(HierarchyListener JavaDoc hl) {
968         }
969
970         /** Overridden to do nothing */
971         protected void firePropertyChange(String JavaDoc name, Object JavaDoc old, Object JavaDoc nue) {
972         }
973
974         /** Overridden to do nothing */
975         public void firePropertyChange(String JavaDoc name, boolean old, boolean nue) {
976         }
977
978         /** Overridden to do nothing */
979         public void firePropertyChange(String JavaDoc name, int old, int nue) {
980         }
981
982         /** Overridden to do nothing */
983         public void firePropertyChange(String JavaDoc name, byte old, byte nue) {
984         }
985
986         /** Overridden to do nothing */
987         public void firePropertyChange(String JavaDoc name, char old, char nue) {
988         }
989
990         /** Overridden to do nothing */
991         public void firePropertyChange(String JavaDoc name, double old, double nue) {
992         }
993
994         /** Overridden to do nothing */
995         public void firePropertyChange(String JavaDoc name, float old, float nue) {
996         }
997
998         /** Overridden to do nothing */
999         public void firePropertyChange(String JavaDoc name, short old, short nue) {
1000        }
1001
1002        private class NoEventsInvRadioButton extends InvRadioButton {
1003            /** Overridden to do nothing */
1004            public void addActionListener(ActionListener JavaDoc al) {
1005            }
1006
1007            /** Overridden to do nothing */
1008            public void addContainerListener(ContainerListener JavaDoc cl) {
1009            }
1010
1011            /** Overridden to do nothing */
1012            public void addChangeListener(ChangeListener JavaDoc cl) {
1013            }
1014
1015            /** Overridden to do nothing */
1016            public void addComponentListener(ComponentListener JavaDoc l) {
1017            }
1018
1019            /** Overridden to do nothing */
1020            public void addHierarchyBoundsListener(HierarchyBoundsListener JavaDoc hbl) {
1021            }
1022
1023            /** Overridden to do nothing */
1024            public void addHierarchyListener(HierarchyListener JavaDoc hl) {
1025            }
1026
1027            /** Overridden to do nothing */
1028            protected void fireActionPerformed(ActionEvent JavaDoc ae) {
1029            }
1030
1031            /** Overridden to do nothing */
1032            protected void fireStateChanged() {
1033            }
1034
1035            /** Overridden to do nothing */
1036            protected void firePropertyChange(String JavaDoc name, Object JavaDoc old, Object JavaDoc nue) {
1037            }
1038
1039            /** Overridden to do nothing */
1040            public void firePropertyChange(String JavaDoc name, boolean old, boolean nue) {
1041            }
1042
1043            /** Overridden to do nothing */
1044            public void firePropertyChange(String JavaDoc name, int old, int nue) {
1045            }
1046
1047            /** Overridden to do nothing */
1048            public void firePropertyChange(String JavaDoc name, byte old, byte nue) {
1049            }
1050
1051            /** Overridden to do nothing */
1052            public void firePropertyChange(String JavaDoc name, char old, char nue) {
1053            }
1054
1055            /** Overridden to do nothing */
1056            public void firePropertyChange(String JavaDoc name, double old, double nue) {
1057            }
1058
1059            /** Overridden to do nothing */
1060            public void firePropertyChange(String JavaDoc name, float old, float nue) {
1061            }
1062
1063            /** Overridden to do nothing */
1064            public void firePropertyChange(String JavaDoc name, short old, short nue) {
1065            }
1066        }
1067    }
1068
1069    private static final class ExceptionPropertyEditor implements PropertyEditor JavaDoc {
1070        Exception JavaDoc e;
1071
1072        public ExceptionPropertyEditor(Exception JavaDoc e) {
1073            this.e = e;
1074        }
1075
1076        public void addPropertyChangeListener(java.beans.PropertyChangeListener JavaDoc listener) {
1077            //do nothing
1078
}
1079
1080        public String JavaDoc getAsText() {
1081            return e.getMessage();
1082        }
1083
1084        public java.awt.Component JavaDoc getCustomEditor() {
1085            return null;
1086        }
1087
1088        public String JavaDoc getJavaInitializationString() {
1089            return null;
1090        }
1091
1092        public String JavaDoc[] getTags() {
1093            return null;
1094        }
1095
1096        public Object JavaDoc getValue() {
1097            return e;
1098        }
1099
1100        public boolean isPaintable() {
1101            return false;
1102        }
1103
1104        public void paintValue(java.awt.Graphics JavaDoc gfx, java.awt.Rectangle JavaDoc box) {
1105            //do nothing
1106
}
1107
1108        public void removePropertyChangeListener(java.beans.PropertyChangeListener JavaDoc listener) {
1109            //do nothing
1110
}
1111
1112        public void setAsText(String JavaDoc text) throws java.lang.IllegalArgumentException JavaDoc {
1113            //do nothing
1114
}
1115
1116        public void setValue(Object JavaDoc value) {
1117            //do nothing
1118
}
1119
1120        public boolean supportsCustomEditor() {
1121            return false;
1122        }
1123    }
1124
1125    /** A JLabel that implements InplaceEditor so consumers can safely cast
1126     * to InplaceEditor even in the case of problems */

1127    private class ExceptionRenderer extends JLabel JavaDoc implements InplaceEditor {
1128        public void addActionListener(ActionListener JavaDoc al) {
1129            //do nothing
1130
}
1131
1132        public Color JavaDoc getForeground() {
1133            return PropUtils.getErrorColor();
1134        }
1135
1136        public void clear() {
1137            //do nothing
1138
}
1139
1140        public void connect(PropertyEditor JavaDoc pe, org.openide.explorer.propertysheet.PropertyEnv env) {
1141            //do nothing
1142
}
1143
1144        public JComponent JavaDoc getComponent() {
1145            return this;
1146        }
1147
1148        public KeyStroke JavaDoc[] getKeyStrokes() {
1149            return null;
1150        }
1151
1152        public PropertyEditor JavaDoc getPropertyEditor() {
1153            return null;
1154        }
1155
1156        public org.openide.explorer.propertysheet.PropertyModel getPropertyModel() {
1157            return null;
1158        }
1159
1160        public Object JavaDoc getValue() {
1161            return getText();
1162        }
1163
1164        public boolean isKnownComponent(Component JavaDoc c) {
1165            return c == this;
1166        }
1167
1168        public void removeActionListener(ActionListener JavaDoc al) {
1169            //do nothing
1170
}
1171
1172        public void reset() {
1173            //do nothing
1174
}
1175
1176        public void setPropertyModel(org.openide.explorer.propertysheet.PropertyModel pm) {
1177            //do nothing
1178
}
1179
1180        public void setValue(Object JavaDoc o) {
1181            //do nothing
1182
}
1183
1184        public boolean supportsTextEntry() {
1185            return false;
1186        }
1187    }
1188}
1189
Popular Tags