KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > beaninfo > editors > ListImageEditor


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.netbeans.beaninfo.editors;
21
22 import java.awt.Image JavaDoc;
23 import java.beans.PropertyEditorSupport JavaDoc;
24 import java.beans.FeatureDescriptor JavaDoc;
25 import org.netbeans.core.UIExceptions;
26
27 import org.openide.explorer.propertysheet.PropertyEnv;
28 import org.openide.explorer.propertysheet.ExPropertyEditor;
29 import org.openide.nodes.Node;
30 import org.openide.util.NbBundle;
31
32
33 /** Editor for property of enumerated integers, each integer should
34  * have associated image displayed as a property value. It's possible
35  * to associate descriptions for each value which is then shown in combobox
36  * when property is edited.
37  *
38  * @author Vitezslav Stejskal
39  */

40 public class ListImageEditor extends PropertyEditorSupport JavaDoc implements ExPropertyEditor {
41
42     public static final String JavaDoc PROP_IMAGES = "images"; //NOI18N
43
public static final String JavaDoc PROP_VALUES = "values"; //NOI18N
44
public static final String JavaDoc PROP_DESCRIPTIONS = "descriptions"; //NOI18N
45

46     private boolean canWrite = true;
47
48     private Image JavaDoc [] images = null;
49     private Integer JavaDoc [] values = null;
50     private String JavaDoc [] descriptions = null;
51
52     /** Creates new ListEditor */
53     public ListImageEditor () {
54         super ();
55     }
56
57     public void attachEnv (PropertyEnv env) {
58         FeatureDescriptor JavaDoc d = env.getFeatureDescriptor ();
59         if (d instanceof Node.Property) {
60             canWrite = ((Node.Property)d).canWrite ();
61         }
62         
63         Object JavaDoc o;
64         Image JavaDoc imgs [] = null;
65         Integer JavaDoc vals [] = null;
66         String JavaDoc descs [] = null;
67         
68         o = d.getValue (PROP_IMAGES);
69         if (o instanceof Image JavaDoc []) {
70             imgs = (Image JavaDoc [])o;
71         }
72         o = d.getValue (PROP_VALUES);
73         if (o instanceof Integer JavaDoc []) {
74             vals = (Integer JavaDoc [])o;
75         }
76         o = d.getValue (PROP_DESCRIPTIONS);
77         if (o instanceof String JavaDoc []) {
78             descs = (String JavaDoc [])o;
79         }
80         
81         if (imgs != null && vals != null) {
82             int length = length = imgs.length;
83
84             if(vals.length < length) {
85                 length = vals.length;
86             }
87
88             if (descs != null && descs.length < length) {
89                 length = descs.length;
90             }
91
92             images = new Image JavaDoc [length];
93             values = new Integer JavaDoc [length];
94             descriptions = new String JavaDoc [length];
95
96             for (int i = 0; i < length; i++) {
97                 images [i] = imgs [i];
98                 values [i] = vals [i];
99                 descriptions [i] = descs == null ? vals [i].toString () : descs [i];
100             }
101         }
102     }
103     
104     public boolean isEditable () {
105         return canWrite;
106     }
107     
108     public String JavaDoc getAsText () {
109         int i = findIndex (values, getValue ());
110         return (String JavaDoc) findObject (descriptions, i);
111     }
112     
113     public void setAsText (String JavaDoc str) throws java.lang.IllegalArgumentException JavaDoc {
114         int i = findIndex (descriptions, str);
115         if (i == -1) {
116             IllegalArgumentException JavaDoc iae = new IllegalArgumentException JavaDoc (
117                 "negative: " + str); //NOI18N
118
String JavaDoc msg = NbBundle.getMessage(ListImageEditor.class,
119                 "CTL_NegativeSize"); //NOI18N
120
UIExceptions.annotateUser(iae, iae.getMessage(), msg, null,
121                                      new java.util.Date JavaDoc());
122             throw iae;
123         }
124         setValue (findObject (values, i));
125     }
126     
127     public String JavaDoc[] getTags () {
128         return descriptions;
129     }
130
131     public boolean isPaintable () {
132         return true;
133     }
134     
135     public void paintValue (java.awt.Graphics JavaDoc g, java.awt.Rectangle JavaDoc rectangle) {
136         Image JavaDoc img = (Image JavaDoc) findObject (images, findIndex (values, getValue ()));
137     
138         if (img != null) {
139             g.drawImage (img,
140                 rectangle.x + (rectangle.width - img.getWidth (null))/ 2,
141                 rectangle.y + (rectangle.height - img.getHeight (null))/ 2,
142                 img.getWidth (null),
143                 img.getHeight (null),
144                 null);
145         }
146     }
147     
148     public String JavaDoc getJavaInitializationString () {
149         return "new Integer(" + getValue () + ")"; // NOI18N
150
}
151     
152     private Object JavaDoc findObject (Object JavaDoc [] objs, int i) {
153         if (objs == null || i < 0 || i >= objs.length)
154             return null;
155         
156         return objs[i];
157     }
158     
159     private int findIndex (Object JavaDoc [] objs, Object JavaDoc obj) {
160         if (objs != null) {
161             for ( int i = 0; i < objs.length; i++) {
162                 if (objs[i].equals (obj))
163                     return i;
164             }
165         }
166         return -1;
167     }
168 }
169
Popular Tags