~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to openide/explorer/src/org/openide/explorer/propertysheet/IconPanel.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
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]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
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.
 
29
 *
 
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.
 
40
 */
 
41
 
 
42
package org.openide.explorer.propertysheet;
 
43
 
 
44
import org.openide.util.Utilities;
 
45
 
 
46
import java.awt.Color;
 
47
import java.awt.Component;
 
48
import java.awt.Container;
 
49
import java.awt.Dimension;
 
50
import java.awt.Font;
 
51
import java.awt.Graphics;
 
52
import java.awt.Insets;
 
53
import java.awt.event.FocusListener;
 
54
 
 
55
import java.beans.FeatureDescriptor;
 
56
 
 
57
import javax.swing.Icon;
 
58
import javax.swing.ImageIcon;
 
59
import javax.swing.JComponent;
 
60
 
 
61
 
 
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.
 
66
 *
 
67
 * @author  Tim Boudreau
 
68
 */
 
69
class IconPanel extends JComponent implements InplaceEditor {
 
70
    private InplaceEditor inplaceEditor;
 
71
    private Icon icon;
 
72
    private boolean needLayout = true;
 
73
    private PropertyEnv env = null;
 
74
    private Component comp;
 
75
 
 
76
    /** Creates a new instance of IconValuePanel */
 
77
    public IconPanel() {
 
78
        setOpaque(true);
 
79
    }
 
80
 
 
81
    /**
 
82
     * Setter for property inplaceEditor.
 
83
     * @param inplaceEditor New value of property inplaceEditor.
 
84
     */
 
85
    public void setInplaceEditor(InplaceEditor inplaceEditor) {
 
86
        this.inplaceEditor = inplaceEditor;
 
87
        setComponent(inplaceEditor.getComponent());
 
88
    }
 
89
 
 
90
    public InplaceEditor getInplaceEditor() {
 
91
        return inplaceEditor;
 
92
    }
 
93
 
 
94
    public void setEnabled(boolean val) {
 
95
        if (comp != null) {
 
96
            //Can be called from setUI in superclass constructor
 
97
            comp.setEnabled(val);
 
98
        }
 
99
 
 
100
        super.setEnabled(val);
 
101
    }
 
102
 
 
103
    public void setBackground(Color c) {
 
104
        if (comp != null) {
 
105
            //Can be called from setUI in superclass constructor
 
106
            comp.setBackground(c);
 
107
        }
 
108
 
 
109
        super.setBackground(c);
 
110
    }
 
111
 
 
112
    public void setForeground(Color c) {
 
113
        if (comp != null) {
 
114
            //Can be called from setUI in superclass constructor
 
115
            comp.setForeground(c);
 
116
        }
 
117
 
 
118
        super.setForeground(c);
 
119
    }
 
120
 
 
121
    public void setFont(Font f) {
 
122
        if (comp != null) {
 
123
            comp.setFont(f);
 
124
        }
 
125
 
 
126
        super.setFont(f);
 
127
    }
 
128
 
 
129
    /** Set the inner component that will actually display the property */
 
130
    private void setComponent(Component c) {
 
131
        if (comp != null) {
 
132
            remove(comp);
 
133
        }
 
134
 
 
135
        if (c != null) {
 
136
            add(c);
 
137
        }
 
138
 
 
139
        comp = c;
 
140
        needLayout = true;
 
141
    }
 
142
 
 
143
    /** Set the icon that will be used. */
 
144
    public void setIcon(Icon i) {
 
145
        this.icon = i;
 
146
        needLayout = true;
 
147
    }
 
148
 
 
149
    /** Overridden to paint the icon */
 
150
    public void paintComponent(Graphics g) {
 
151
        if (needLayout) {
 
152
            doLayout();
 
153
        }
 
154
 
 
155
        if (icon != null) {
 
156
            Color c = g.getColor();
 
157
 
 
158
            try {
 
159
                g.setColor(getBackground());
 
160
 
 
161
                int right = (comp != null) ? (comp.getLocation().x + icon.getIconWidth()) : (icon.getIconWidth() + 2);
 
162
                g.fillRect(0, 0, right, getHeight());
 
163
 
 
164
                Insets ins = getInsets();
 
165
                int x = ins.left;
 
166
                int y = ins.top + Math.max((getHeight() / 2) - (icon.getIconHeight() / 2), 0);
 
167
                icon.paintIcon(this, g, x, y);
 
168
            } finally {
 
169
                g.setColor(c);
 
170
            }
 
171
        }
 
172
 
 
173
        super.paintComponent(g);
 
174
    }
 
175
 
 
176
    /** Proxies the embedded inplace editor */
 
177
    public void addActionListener(java.awt.event.ActionListener al) {
 
178
        inplaceEditor.addActionListener(al);
 
179
    }
 
180
 
 
181
    /** Proxies the embedded inplace editor */
 
182
    public void clear() {
 
183
        inplaceEditor.clear();
 
184
        setIcon(null);
 
185
        setComponent(null);
 
186
        env = null;
 
187
    }
 
188
 
 
189
    /** Proxies the embedded inplace editor */
 
190
    public void connect(java.beans.PropertyEditor pe, PropertyEnv env) {
 
191
        inplaceEditor.connect(pe, env);
 
192
        this.env = env;
 
193
        updateIcon();
 
194
    }
 
195
 
 
196
    private void updateIcon() {
 
197
        if (env != null) {
 
198
            Icon ic = null;
 
199
            FeatureDescriptor fd = env.getFeatureDescriptor();
 
200
 
 
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
 
205
            }
 
206
 
 
207
            setIcon(ic);
 
208
            needLayout = true;
 
209
        }
 
210
    }
 
211
 
 
212
    public void setOpaque(boolean val) {
 
213
        if (getInplaceEditor() != null) {
 
214
            getInplaceEditor().getComponent().setOpaque(true);
 
215
        }
 
216
    }
 
217
 
 
218
    /** Proxies the embedded inplace editor */
 
219
    public javax.swing.JComponent getComponent() {
 
220
        return this;
 
221
    }
 
222
 
 
223
    /** Proxies the embedded inplace editor */
 
224
    public javax.swing.KeyStroke[] getKeyStrokes() {
 
225
        return inplaceEditor.getKeyStrokes();
 
226
    }
 
227
 
 
228
    /** Proxies the embedded inplace editor */
 
229
    public java.beans.PropertyEditor getPropertyEditor() {
 
230
        return inplaceEditor.getPropertyEditor();
 
231
    }
 
232
 
 
233
    /** Proxies the embedded inplace editor */
 
234
    public PropertyModel getPropertyModel() {
 
235
        return inplaceEditor.getPropertyModel();
 
236
    }
 
237
 
 
238
    /** Proxies the embedded inplace editor */
 
239
    public Object getValue() {
 
240
        return inplaceEditor.getValue();
 
241
    }
 
242
 
 
243
    /** Proxies the embedded inplace editor */
 
244
    public boolean isKnownComponent(java.awt.Component c) {
 
245
        return ((c == this) || inplaceEditor.isKnownComponent(c));
 
246
    }
 
247
 
 
248
    /** Proxies the embedded inplace editor */
 
249
    public void removeActionListener(java.awt.event.ActionListener al) {
 
250
        inplaceEditor.removeActionListener(al);
 
251
    }
 
252
 
 
253
    /** Proxies the embedded inplace editor */
 
254
    public void reset() {
 
255
        inplaceEditor.reset();
 
256
        updateIcon();
 
257
    }
 
258
 
 
259
    /** Proxies the embedded inplace editor */
 
260
    public void setPropertyModel(PropertyModel pm) {
 
261
        inplaceEditor.setPropertyModel(pm);
 
262
    }
 
263
 
 
264
    /** Proxies the embedded inplace editor */
 
265
    public void setValue(Object o) {
 
266
        inplaceEditor.setValue(o);
 
267
    }
 
268
 
 
269
    /** Proxies the embedded inplace editor */
 
270
    public boolean supportsTextEntry() {
 
271
        return inplaceEditor.supportsTextEntry();
 
272
    }
 
273
 
 
274
    public void requestFocus() {
 
275
        comp.requestFocus();
 
276
    }
 
277
 
 
278
    public boolean requestFocusInWindow() {
 
279
        return comp.requestFocusInWindow();
 
280
    }
 
281
 
 
282
    public void addFocusListener(FocusListener fl) {
 
283
        if (comp != null) {
 
284
            comp.addFocusListener(fl);
 
285
        } else {
 
286
            super.addFocusListener(fl);
 
287
        }
 
288
    }
 
289
 
 
290
    public void removeFocusListener(FocusListener fl) {
 
291
        if (comp != null) {
 
292
            comp.removeFocusListener(fl);
 
293
        } else {
 
294
            super.removeFocusListener(fl);
 
295
        }
 
296
    }
 
297
 
 
298
    @SuppressWarnings("deprecation")
 
299
    public void layout() {
 
300
        Insets ins = getInsets();
 
301
 
 
302
        //use a minimum size so typical icons won't cause resizing of the
 
303
        //component
 
304
        int iconWidth = Math.max(icon.getIconWidth() + PropUtils.getTextMargin(), 18);
 
305
 
 
306
        int x = (icon == null) ? ins.left : (ins.left + iconWidth);
 
307
        int y = ins.top;
 
308
 
 
309
        synchronized (getTreeLock()) {
 
310
            Component c = comp;
 
311
 
 
312
            if (c == null) {
 
313
                return;
 
314
            }
 
315
 
 
316
            c.setBounds(x, y, getWidth() - (x + ins.right), getHeight() - ins.bottom);
 
317
 
 
318
            if (c instanceof Container) {
 
319
                ((Container) c).doLayout();
 
320
            }
 
321
        }
 
322
    }
 
323
 
 
324
    public Dimension getPreferredSize() {
 
325
        Insets ins = getInsets();
 
326
        Component c = comp;
 
327
        Dimension result = new Dimension(0, 0);
 
328
 
 
329
        if (icon != null) {
 
330
            result.width = icon.getIconWidth() + PropUtils.getTextMargin();
 
331
            result.height = icon.getIconHeight();
 
332
        }
 
333
 
 
334
        if (c != null) {
 
335
            Dimension ps = c.getPreferredSize();
 
336
            result.width += ps.width;
 
337
            result.height = Math.max(ps.height, result.height);
 
338
        }
 
339
 
 
340
        result.width += (ins.left + ins.right);
 
341
        result.height += (ins.top + ins.bottom);
 
342
 
 
343
        return result;
 
344
    }
 
345
 
 
346
    public Dimension getMinimumSize() {
 
347
        return getPreferredSize();
 
348
    }
 
349
}