KickJava   Java API By Example, From Geeks To Geeks.

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


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 * SheetCellRenderer.java
21 *
22 * Created on April 22, 2003, 5:35 PM
23 */

24 package org.openide.explorer.propertysheet;
25
26 import org.openide.awt.HtmlRenderer;
27 import org.openide.nodes.Node.*;
28
29 import java.awt.*;
30
31 import java.beans.FeatureDescriptor JavaDoc;
32
33 import javax.swing.*;
34 import javax.swing.table.TableCellRenderer JavaDoc;
35
36
37 /** An implementation of SheetCellRenderer that wraps custom InplaceEditors
38  * to efficiently render properties.
39  *
40  * @author Tim Boudreau
41  */

42 final class SheetCellRenderer implements TableCellRenderer JavaDoc {
43     private RendererFactory factory = null;
44     private boolean tableUI;
45     boolean includeMargin = false;
46     private ReusablePropertyEnv reusableEnv;
47     private ReusablePropertyModel reusableModel;
48     boolean suppressButton = false;
49     int rbMax = 0;
50     private JLabel htmlLabel = HtmlRenderer.createLabel();
51
52     SheetCellRenderer(boolean tableUI, ReusablePropertyEnv env, ReusablePropertyModel reusableModel) {
53         this.tableUI = tableUI;
54         this.reusableEnv = env;
55         this.reusableModel = reusableModel;
56     }
57
58     void setIncludeMargin(boolean val) {
59         includeMargin = val;
60     }
61
62     void setSuppressButton(boolean val) {
63         suppressButton = val;
64     }
65
66     void setRadioButtonMax(int i) {
67         rbMax = i;
68     }
69
70     public Component getTableCellRendererComponent(
71         JTable table, Object JavaDoc value, boolean selected, boolean hasFocus, int row, int column
72     ) {
73         FeatureDescriptor JavaDoc fd = (FeatureDescriptor JavaDoc) value;
74         Component result;
75
76         //Use selection color for both columns
77
selected |= (hasFocus && (table.getSelectedRow() == row));
78
79         if (fd instanceof PropertySet) {
80             return null;
81         } else {
82             if (column == 0) {
83                 String JavaDoc txt = ((Property) fd).getHtmlDisplayName();
84                 boolean isHtml = txt != null;
85
86                 if (!isHtml) {
87                     txt = fd.getDisplayName();
88                 }
89
90                 JLabel lbl = htmlLabel;
91
92                 HtmlRenderer.Renderer ren = (HtmlRenderer.Renderer) lbl;
93
94                 ren.setHtml(isHtml);
95
96                 lbl.setText(txt);
97
98                 if (selected) {
99                     lbl.setBackground(table.getSelectionBackground());
100                     lbl.setForeground(table.getSelectionForeground());
101                 } else {
102                     lbl.setBackground(table.getBackground());
103                     lbl.setForeground(table.getForeground());
104                 }
105
106                 lbl.setOpaque(selected);
107
108                 if (includeMargin) {
109                     lbl.setBorder(
110                         BorderFactory.createMatteBorder(0, PropUtils.getMarginWidth() + 2, 0, 0, lbl.getBackground())
111                     );
112                 } else {
113                     lbl.setBorder(
114                         BorderFactory.createMatteBorder(0, PropUtils.getTextMargin(), 0, 0, lbl.getBackground())
115                     );
116                 }
117
118                 //Support for name marking with icon requested by form editor
119
Object JavaDoc o = fd.getValue("nameIcon"); //NOI18N
120

121                 if (o instanceof Icon) {
122                     lbl.setIcon((Icon) o);
123                 } else if (o instanceof Image) {
124                     lbl.setIcon(new ImageIcon((Image) o));
125                 }
126
127                 result = lbl;
128             } else {
129                 result = factory().getRenderer((Property) fd);
130
131                 if (selected) {
132                     result.setBackground(table.getSelectionBackground());
133                     result.setForeground(table.getSelectionForeground());
134                 } else {
135                     result.setBackground(table.getBackground());
136                     result.setForeground(table.getForeground());
137                 }
138
139                 ((JComponent) result).setOpaque(selected);
140             }
141         }
142
143         return result;
144     }
145
146     RendererFactory factory() {
147         if (factory == null) {
148             factory = new RendererFactory(true, reusableEnv, reusableModel);
149         }
150
151         return factory;
152     }
153 }
154
Popular Tags