2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
42
package org.openide.explorer.propertysheet;
44
import org.openide.util.Utilities;
46
import java.awt.Color;
47
import java.awt.Component;
48
import java.awt.Container;
49
import java.awt.Dimension;
51
import java.awt.Graphics;
52
import java.awt.Insets;
53
import java.awt.event.FocusListener;
55
import java.beans.FeatureDescriptor;
57
import javax.swing.Icon;
58
import javax.swing.ImageIcon;
59
import javax.swing.JComponent;
62
/** A panel which embeds and displays an inplace editor and which can
63
* show property marking for components that are not JLabels but should
64
* show an icon either because of hinting or because the state is
65
* PropertyEnv.STATE_INVALID.
67
* @author Tim Boudreau
69
class IconPanel extends JComponent implements InplaceEditor {
70
private InplaceEditor inplaceEditor;
72
private boolean needLayout = true;
73
private PropertyEnv env = null;
74
private Component comp;
76
/** Creates a new instance of IconValuePanel */
82
* Setter for property inplaceEditor.
83
* @param inplaceEditor New value of property inplaceEditor.
85
public void setInplaceEditor(InplaceEditor inplaceEditor) {
86
this.inplaceEditor = inplaceEditor;
87
setComponent(inplaceEditor.getComponent());
90
public InplaceEditor getInplaceEditor() {
94
public void setEnabled(boolean val) {
96
//Can be called from setUI in superclass constructor
100
super.setEnabled(val);
103
public void setBackground(Color c) {
105
//Can be called from setUI in superclass constructor
106
comp.setBackground(c);
109
super.setBackground(c);
112
public void setForeground(Color c) {
114
//Can be called from setUI in superclass constructor
115
comp.setForeground(c);
118
super.setForeground(c);
121
public void setFont(Font f) {
129
/** Set the inner component that will actually display the property */
130
private void setComponent(Component c) {
143
/** Set the icon that will be used. */
144
public void setIcon(Icon i) {
149
/** Overridden to paint the icon */
150
public void paintComponent(Graphics g) {
156
Color c = g.getColor();
159
g.setColor(getBackground());
161
int right = (comp != null) ? (comp.getLocation().x + icon.getIconWidth()) : (icon.getIconWidth() + 2);
162
g.fillRect(0, 0, right, getHeight());
164
Insets ins = getInsets();
166
int y = ins.top + Math.max((getHeight() / 2) - (icon.getIconHeight() / 2), 0);
167
icon.paintIcon(this, g, x, y);
173
super.paintComponent(g);
176
/** Proxies the embedded inplace editor */
177
public void addActionListener(java.awt.event.ActionListener al) {
178
inplaceEditor.addActionListener(al);
181
/** Proxies the embedded inplace editor */
182
public void clear() {
183
inplaceEditor.clear();
189
/** Proxies the embedded inplace editor */
190
public void connect(java.beans.PropertyEditor pe, PropertyEnv env) {
191
inplaceEditor.connect(pe, env);
196
private void updateIcon() {
199
FeatureDescriptor fd = env.getFeatureDescriptor();
201
if (env.getState() == env.STATE_INVALID) {
202
ic = new ImageIcon(Utilities.loadImage("org/openide/resources/propertysheet/invalid.gif")); //NOI18N
203
} else if (fd != null) {
204
ic = (Icon) fd.getValue("valueIcon"); //NOI18N
212
public void setOpaque(boolean val) {
213
if (getInplaceEditor() != null) {
214
getInplaceEditor().getComponent().setOpaque(true);
218
/** Proxies the embedded inplace editor */
219
public javax.swing.JComponent getComponent() {
223
/** Proxies the embedded inplace editor */
224
public javax.swing.KeyStroke[] getKeyStrokes() {
225
return inplaceEditor.getKeyStrokes();
228
/** Proxies the embedded inplace editor */
229
public java.beans.PropertyEditor getPropertyEditor() {
230
return inplaceEditor.getPropertyEditor();
233
/** Proxies the embedded inplace editor */
234
public PropertyModel getPropertyModel() {
235
return inplaceEditor.getPropertyModel();
238
/** Proxies the embedded inplace editor */
239
public Object getValue() {
240
return inplaceEditor.getValue();
243
/** Proxies the embedded inplace editor */
244
public boolean isKnownComponent(java.awt.Component c) {
245
return ((c == this) || inplaceEditor.isKnownComponent(c));
248
/** Proxies the embedded inplace editor */
249
public void removeActionListener(java.awt.event.ActionListener al) {
250
inplaceEditor.removeActionListener(al);
253
/** Proxies the embedded inplace editor */
254
public void reset() {
255
inplaceEditor.reset();
259
/** Proxies the embedded inplace editor */
260
public void setPropertyModel(PropertyModel pm) {
261
inplaceEditor.setPropertyModel(pm);
264
/** Proxies the embedded inplace editor */
265
public void setValue(Object o) {
266
inplaceEditor.setValue(o);
269
/** Proxies the embedded inplace editor */
270
public boolean supportsTextEntry() {
271
return inplaceEditor.supportsTextEntry();
274
public void requestFocus() {
278
public boolean requestFocusInWindow() {
279
return comp.requestFocusInWindow();
282
public void addFocusListener(FocusListener fl) {
284
comp.addFocusListener(fl);
286
super.addFocusListener(fl);
290
public void removeFocusListener(FocusListener fl) {
292
comp.removeFocusListener(fl);
294
super.removeFocusListener(fl);
298
@SuppressWarnings("deprecation")
299
public void layout() {
300
Insets ins = getInsets();
302
//use a minimum size so typical icons won't cause resizing of the
304
int iconWidth = Math.max(icon.getIconWidth() + PropUtils.getTextMargin(), 18);
306
int x = (icon == null) ? ins.left : (ins.left + iconWidth);
309
synchronized (getTreeLock()) {
316
c.setBounds(x, y, getWidth() - (x + ins.right), getHeight() - ins.bottom);
318
if (c instanceof Container) {
319
((Container) c).doLayout();
324
public Dimension getPreferredSize() {
325
Insets ins = getInsets();
327
Dimension result = new Dimension(0, 0);
330
result.width = icon.getIconWidth() + PropUtils.getTextMargin();
331
result.height = icon.getIconHeight();
335
Dimension ps = c.getPreferredSize();
336
result.width += ps.width;
337
result.height = Math.max(ps.height, result.height);
340
result.width += (ins.left + ins.right);
341
result.height += (ins.top + ins.bottom);
346
public Dimension getMinimumSize() {
347
return getPreferredSize();