KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Utilities;
23
24 import java.awt.Color JavaDoc;
25 import java.awt.Component JavaDoc;
26 import java.awt.Container JavaDoc;
27 import java.awt.Dimension JavaDoc;
28 import java.awt.Font JavaDoc;
29 import java.awt.Graphics JavaDoc;
30 import java.awt.Insets JavaDoc;
31 import java.awt.event.FocusListener JavaDoc;
32
33 import java.beans.FeatureDescriptor JavaDoc;
34
35 import javax.swing.Icon JavaDoc;
36 import javax.swing.ImageIcon JavaDoc;
37 import javax.swing.JComponent JavaDoc;
38
39
40 /** A panel which embeds and displays an inplace editor and which can
41  * show property marking for components that are not JLabels but should
42  * show an icon either because of hinting or because the state is
43  * PropertyEnv.STATE_INVALID.
44  *
45  * @author Tim Boudreau
46  */

47 class IconPanel extends JComponent JavaDoc implements InplaceEditor {
48     private InplaceEditor inplaceEditor;
49     private Icon JavaDoc icon;
50     private boolean needLayout = true;
51     private PropertyEnv env = null;
52     private Component JavaDoc comp;
53
54     /** Creates a new instance of IconValuePanel */
55     public IconPanel() {
56         setOpaque(true);
57     }
58
59     /**
60      * Setter for property inplaceEditor.
61      * @param inplaceEditor New value of property inplaceEditor.
62      */

63     public void setInplaceEditor(InplaceEditor inplaceEditor) {
64         this.inplaceEditor = inplaceEditor;
65         setComponent(inplaceEditor.getComponent());
66     }
67
68     public InplaceEditor getInplaceEditor() {
69         return inplaceEditor;
70     }
71
72     public void setEnabled(boolean val) {
73         if (comp != null) {
74             //Can be called from setUI in superclass constructor
75
comp.setEnabled(val);
76         }
77
78         super.setEnabled(val);
79     }
80
81     public void setBackground(Color JavaDoc c) {
82         if (comp != null) {
83             //Can be called from setUI in superclass constructor
84
comp.setBackground(c);
85         }
86
87         super.setBackground(c);
88     }
89
90     public void setForeground(Color JavaDoc c) {
91         if (comp != null) {
92             //Can be called from setUI in superclass constructor
93
comp.setForeground(c);
94         }
95
96         super.setForeground(c);
97     }
98
99     public void setFont(Font JavaDoc f) {
100         if (comp != null) {
101             comp.setFont(f);
102         }
103
104         super.setFont(f);
105     }
106
107     /** Set the inner component that will actually display the property */
108     private void setComponent(Component JavaDoc c) {
109         if (comp != null) {
110             remove(comp);
111         }
112
113         if (c != null) {
114             add(c);
115         }
116
117         comp = c;
118         needLayout = true;
119     }
120
121     /** Set the icon that will be used. */
122     public void setIcon(Icon JavaDoc i) {
123         this.icon = i;
124         needLayout = true;
125     }
126
127     /** Overridden to paint the icon */
128     public void paintComponent(Graphics JavaDoc g) {
129         if (needLayout) {
130             doLayout();
131         }
132
133         if (icon != null) {
134             Color JavaDoc c = g.getColor();
135
136             try {
137                 g.setColor(getBackground());
138
139                 int right = (comp != null) ? (comp.getLocation().x + icon.getIconWidth()) : (icon.getIconWidth() + 2);
140                 g.fillRect(0, 0, right, getHeight());
141
142                 Insets JavaDoc ins = getInsets();
143                 int x = ins.left;
144                 int y = ins.top + Math.max((getHeight() / 2) - (icon.getIconHeight() / 2), 0);
145                 icon.paintIcon(this, g, x, y);
146             } finally {
147                 g.setColor(c);
148             }
149         }
150
151         super.paintComponent(g);
152     }
153
154     /** Proxies the embedded inplace editor */
155     public void addActionListener(java.awt.event.ActionListener JavaDoc al) {
156         inplaceEditor.addActionListener(al);
157     }
158
159     /** Proxies the embedded inplace editor */
160     public void clear() {
161         inplaceEditor.clear();
162         setIcon(null);
163         setComponent(null);
164         env = null;
165     }
166
167     /** Proxies the embedded inplace editor */
168     public void connect(java.beans.PropertyEditor JavaDoc pe, PropertyEnv env) {
169         inplaceEditor.connect(pe, env);
170         this.env = env;
171         updateIcon();
172     }
173
174     private void updateIcon() {
175         if (env != null) {
176             Icon JavaDoc ic = null;
177             FeatureDescriptor JavaDoc fd = env.getFeatureDescriptor();
178
179             if (env.getState() == env.STATE_INVALID) {
180                 ic = new ImageIcon JavaDoc(Utilities.loadImage("org/openide/resources/propertysheet/invalid.gif")); //NOI18N
181
} else if (fd != null) {
182                 ic = (Icon JavaDoc) fd.getValue("valueIcon"); //NOI18N
183
}
184
185             setIcon(ic);
186             needLayout = true;
187         }
188     }
189
190     public void setOpaque(boolean val) {
191         if (getInplaceEditor() != null) {
192             getInplaceEditor().getComponent().setOpaque(true);
193         }
194     }
195
196     /** Proxies the embedded inplace editor */
197     public javax.swing.JComponent JavaDoc getComponent() {
198         return this;
199     }
200
201     /** Proxies the embedded inplace editor */
202     public javax.swing.KeyStroke JavaDoc[] getKeyStrokes() {
203         return inplaceEditor.getKeyStrokes();
204     }
205
206     /** Proxies the embedded inplace editor */
207     public java.beans.PropertyEditor JavaDoc getPropertyEditor() {
208         return inplaceEditor.getPropertyEditor();
209     }
210
211     /** Proxies the embedded inplace editor */
212     public PropertyModel getPropertyModel() {
213         return inplaceEditor.getPropertyModel();
214     }
215
216     /** Proxies the embedded inplace editor */
217     public Object JavaDoc getValue() {
218         return inplaceEditor.getValue();
219     }
220
221     /** Proxies the embedded inplace editor */
222     public boolean isKnownComponent(java.awt.Component JavaDoc c) {
223         return ((c == this) || inplaceEditor.isKnownComponent(c));
224     }
225
226     /** Proxies the embedded inplace editor */
227     public void removeActionListener(java.awt.event.ActionListener JavaDoc al) {
228         inplaceEditor.removeActionListener(al);
229     }
230
231     /** Proxies the embedded inplace editor */
232     public void reset() {
233         inplaceEditor.reset();
234         updateIcon();
235     }
236
237     /** Proxies the embedded inplace editor */
238     public void setPropertyModel(PropertyModel pm) {
239         inplaceEditor.setPropertyModel(pm);
240     }
241
242     /** Proxies the embedded inplace editor */
243     public void setValue(Object JavaDoc o) {
244         inplaceEditor.setValue(o);
245     }
246
247     /** Proxies the embedded inplace editor */
248     public boolean supportsTextEntry() {
249         return inplaceEditor.supportsTextEntry();
250     }
251
252     public void requestFocus() {
253         comp.requestFocus();
254     }
255
256     public boolean requestFocusInWindow() {
257         return comp.requestFocusInWindow();
258     }
259
260     public void addFocusListener(FocusListener JavaDoc fl) {
261         if (comp != null) {
262             comp.addFocusListener(fl);
263         } else {
264             super.addFocusListener(fl);
265         }
266     }
267
268     public void removeFocusListener(FocusListener JavaDoc fl) {
269         if (comp != null) {
270             comp.removeFocusListener(fl);
271         } else {
272             super.removeFocusListener(fl);
273         }
274     }
275
276     @SuppressWarnings JavaDoc("deprecation")
277     public void layout() {
278         Insets JavaDoc ins = getInsets();
279
280         //use a minimum size so typical icons won't cause resizing of the
281
//component
282
int iconWidth = Math.max(icon.getIconWidth() + PropUtils.getTextMargin(), 18);
283
284         int x = (icon == null) ? ins.left : (ins.left + iconWidth);
285         int y = ins.top;
286
287         synchronized (getTreeLock()) {
288             Component JavaDoc c = comp;
289
290             if (c == null) {
291                 return;
292             }
293
294             c.setBounds(x, y, getWidth() - (x + ins.right), getHeight() - ins.bottom);
295
296             if (c instanceof Container JavaDoc) {
297                 ((Container JavaDoc) c).doLayout();
298             }
299         }
300     }
301
302     public Dimension JavaDoc getPreferredSize() {
303         Insets JavaDoc ins = getInsets();
304         Component JavaDoc c = comp;
305         Dimension JavaDoc result = new Dimension JavaDoc(0, 0);
306
307         if (icon != null) {
308             result.width = icon.getIconWidth() + PropUtils.getTextMargin();
309             result.height = icon.getIconHeight();
310         }
311
312         if (c != null) {
313             Dimension JavaDoc ps = c.getPreferredSize();
314             result.width += ps.width;
315             result.height = Math.max(ps.height, result.height);
316         }
317
318         result.width += (ins.left + ins.right);
319         result.height += (ins.top + ins.bottom);
320
321         return result;
322     }
323
324     public Dimension JavaDoc getMinimumSize() {
325         return getPreferredSize();
326     }
327 }
328
Popular Tags