KickJava   Java API By Example, From Geeks To Geeks.

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


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.beans.*;
23 import java.awt.Component JavaDoc;
24 import java.awt.Font JavaDoc;
25 import java.awt.GridBagConstraints JavaDoc;
26 import java.awt.GridBagLayout JavaDoc;
27 import java.awt.Insets JavaDoc;
28 import java.awt.event.ActionEvent JavaDoc;
29 import java.awt.event.ActionListener JavaDoc;
30 import java.text.MessageFormat JavaDoc;
31 import java.util.*;
32 import javax.swing.ButtonGroup JavaDoc;
33 import javax.swing.JLabel JavaDoc;
34 import javax.swing.JPanel JavaDoc;
35 import javax.swing.JRadioButton JavaDoc;
36 import org.netbeans.core.UIExceptions;
37 import org.openide.explorer.propertysheet.ExPropertyEditor;
38 import org.openide.explorer.propertysheet.PropertyEnv;
39 import org.openide.util.NbBundle;
40 import org.openide.util.Lookup;
41 import org.openide.util.Utilities;
42
43 /**
44  * Defines editor for choosing of any object using lookup.
45  *
46  * @author Jaroslav Tulach
47  */

48 public final class ObjectEditor extends PropertyEditorSupport
49 implements ExPropertyEditor {
50     /** Name of the custom property that can be passed in PropertyEnv.
51      * Should contain superclass that is allowed to be
52      */

53     private static final String JavaDoc PROP_SUPERCLASS = "superClass"; // NOI18N
54
/** Name of the custom property that can be passed in PropertyEnv.
55      * Either Boolean.TRUE or a String, in such case the string represents
56      * human readable name of the value.
57      */

58     private static final String JavaDoc PROP_NULL = "nullValue"; // NOI18N
59
/** Name of the custom property that can be passed in PropertyEnv.
60      * A lookup to use to query for results.
61      */

62     private static final String JavaDoc PROP_LOOKUP = "lookup"; // NOI18N
63

64     /** custom editor */
65     private ObjectPanel customEditor;
66     
67     /** super class to search for */
68     private Lookup.Template<Object JavaDoc> template;
69     
70     /** null or name to use for null value */
71     private String JavaDoc nullValue;
72     
73     /** a special lookup to use or null */
74     private Lookup lookup;
75     
76     /** Creates new ObjectEditor */
77     public ObjectEditor() {
78     }
79
80     /**
81      * This method is called by the IDE to pass
82      * the environment to the property editor.
83      */

84     public synchronized void attachEnv(PropertyEnv env) {
85         Object JavaDoc obj = env.getFeatureDescriptor ().getValue (PROP_SUPERCLASS);
86         if (obj instanceof Class JavaDoc) {
87         @SuppressWarnings JavaDoc("unchecked") Class JavaDoc<Object JavaDoc> clz = (Class JavaDoc<Object JavaDoc>)obj;
88             template = new Lookup.Template<Object JavaDoc> (clz);
89         } else {
90             template = null;
91         }
92         
93         obj = env.getFeatureDescriptor ().getValue (PROP_NULL);
94         if (Boolean.TRUE.equals (obj)) {
95             nullValue = NbBundle.getMessage (ObjectEditor.class, "CTL_NullValue");
96         } else {
97             if (obj instanceof String JavaDoc) {
98                 nullValue = (String JavaDoc)obj;
99             } else {
100                 nullValue = null;
101             }
102         }
103         
104         obj = env.getFeatureDescriptor ().getValue (PROP_LOOKUP);
105         lookup = obj instanceof Lookup ? (Lookup)obj : null;
106         //Don't allow editing in the case only one item and tags are null
107
if (getTags()==null || getTags().length <= 1) {
108             env.getFeatureDescriptor().setValue("canEditAsText",Boolean.FALSE); //NOI18N
109
}
110     }
111     
112     /** A lookup to work on.
113      * @return a lookup.
114      */

115     protected Lookup lookup () {
116         Lookup l = lookup;
117         return l == null ? Lookup.getDefault () : l;
118     }
119     
120     /** A template to use.
121      */

122     protected Lookup.Template<Object JavaDoc> template () {
123         if (template == null) {
124             template = new Lookup.Template<Object JavaDoc> (Object JavaDoc.class);
125         }
126          
127         return template;
128     }
129     
130     public String JavaDoc getAsText() {
131         Object JavaDoc value = getValue ();
132         if (value == null) {
133             return nullValue == null ?
134                 NbBundle.getMessage (ObjectEditor.class, "CTL_NullValue")
135             :
136                 nullValue;
137         }
138         
139         Lookup.Template<Object JavaDoc> t = new Lookup.Template<Object JavaDoc> (
140             template ().getType (),
141             template ().getId (),
142             value // instance to search for
143
);
144         Lookup.Item item = lookup ().lookupItem (t);
145         
146         if (item == null) {
147             return NbBundle.getMessage (ObjectEditor.class, "CTL_NullItem");
148         }
149         
150         return item.getDisplayName();
151     }
152     
153     /** Searches between items whether there is one with the same display name.
154      * @param str item name
155      */

156     public void setAsText(java.lang.String JavaDoc str) throws java.lang.IllegalArgumentException JavaDoc {
157         if (nullValue != null && nullValue.equals (str)) {
158             setValue (null);
159             return;
160         }
161         
162         
163         Collection allItems = lookup ().lookup (template ()).allItems ();
164         
165         Iterator it = allItems.iterator ();
166         while (it.hasNext ()) {
167             Lookup.Item item = (Lookup.Item)it.next ();
168             
169             if (item.getDisplayName().equals (str)) {
170                 setValue (item.getInstance ());
171                 firePropertyChange();
172                 return;
173             }
174         }
175         IllegalArgumentException JavaDoc iae = new IllegalArgumentException JavaDoc (str);
176         String JavaDoc msg = MessageFormat.format(
177             NbBundle.getMessage(
178                 ObjectEditor.class, "FMT_EXC_GENERIC_BAD_VALUE"), //NOI18N
179
new Object JavaDoc[] {str});
180         UIExceptions.annotateUser(iae, str, msg, null, new Date());
181         throw iae;
182     }
183     
184     /** List of all display names for items.
185      * @return array of strings
186      */

187     public java.lang.String JavaDoc[] getTags() {
188         Collection<? extends Lookup.Item<Object JavaDoc>> allItems = lookup ().lookup (template ()).allItems ();
189         if (allItems.size() <= 1) {
190             return null;
191         }
192    
193         ArrayList<String JavaDoc> list = new ArrayList<String JavaDoc> (allItems.size () + 1);
194         if (nullValue != null) {
195             list.add (nullValue);
196         }
197         
198     for (Lookup.Item<Object JavaDoc> item: allItems) {
199             list.add (item.getDisplayName());
200         }
201         
202         String JavaDoc[] retValue = new String JavaDoc[list.size()];
203         list.toArray(retValue);
204         return retValue;
205     }
206
207     /** Yes we have custom editor.
208      */

209     public boolean supportsCustomEditor() {
210         //Don't allow custom editor if there will be nothing to show
211
return getTags()!= null && getTags().length > 1;
212     }
213     
214     public synchronized Component JavaDoc getCustomEditor () {
215         if (!supportsCustomEditor()) {
216             return null;
217         }
218         if (customEditor != null) {
219             return customEditor;
220         }
221         Lookup.Result<Object JavaDoc> contents = lookup().lookup(template());
222         ObjectPanel panel = new ObjectPanel(contents);
223         return customEditor = panel;
224     }
225
226     private class ObjectPanel extends JPanel JavaDoc implements ActionListener JavaDoc {
227         public ObjectPanel(Lookup.Result<Object JavaDoc> res) {
228             getAccessibleContext().setAccessibleName(
229                 NbBundle.getMessage(ObjectEditor.class,
230                 "ACSN_ObjectTree")); //NOI18N
231
getAccessibleContext().setAccessibleDescription(
232                 NbBundle.getMessage(ObjectEditor.class, "ACSD_ObjectTree")); //NOI18N
233

234             setLayout (new GridBagLayout JavaDoc());
235             GridBagConstraints JavaDoc gbc = new GridBagConstraints JavaDoc();
236             int row = 0;
237             ButtonGroup JavaDoc bg = new ButtonGroup JavaDoc();
238             Font JavaDoc bold;
239             Font JavaDoc plain;
240             if (Utilities.isMac()) {
241                 // don't use deriveFont() - see #49973 for details
242
bold = new Font JavaDoc(getFont().getName(), Font.BOLD, getFont().getSize());
243                 //For default metal L&F where labels are by default bold
244
// don't use deriveFont() - see #49973 for details
245
plain = new Font JavaDoc(getFont().getName(), Font.PLAIN, getFont().getSize());
246             } else {
247                 bold = getFont().deriveFont(Font.BOLD);
248                 plain = getFont().deriveFont(Font.PLAIN);
249             }
250             
251             Collection<? extends Lookup.Item<Object JavaDoc>> c = res.allItems();
252             Lookup.Item[] items = new Lookup.Item[c.size()];
253             items = (Lookup.Item[]) c.toArray(items);
254
255             int BASE_LEFT_INSET=7;
256             for (int i=0; i < items.length; i++) {
257                 JRadioButton JavaDoc rb = new ItemRadioButton(items[i], bold);
258                 if (items[i].getInstance().equals(getValue())) {
259                     rb.setSelected(true);
260                 }
261                 rb.addActionListener(this);
262                 bg.add(rb);
263                 String JavaDoc description = getDescription(items[i]);
264                 
265                 gbc.gridx=0;
266                 gbc.gridy=row;
267                 gbc.insets = new Insets JavaDoc(i==0 ? 7 : 0, BASE_LEFT_INSET,
268                     description != null ? 1 : i==items.length-1 ? 7: 4, BASE_LEFT_INSET);
269                 gbc.fill=gbc.HORIZONTAL;
270                 add(rb, gbc);
271                 row++;
272                 if (description != null) {
273                     JLabel JavaDoc lbl = new JLabel JavaDoc(description);
274                     lbl.setLabelFor(rb);
275                     lbl.setFont(plain);
276                     int left = rb.getIcon() != null ? rb.getIcon().getIconWidth() : 20;
277                     gbc.insets = new Insets JavaDoc(0, BASE_LEFT_INSET +
278                         left, 4, BASE_LEFT_INSET + left);
279                     gbc.gridx=0;
280                     gbc.gridy=row;
281                     add(lbl, gbc);
282                     row++;
283                }
284             }
285         }
286         
287         private String JavaDoc getDescription (Lookup.Item item) {
288             String JavaDoc id = item.getId ();
289             String JavaDoc result = null;
290             try {
291                 result = Introspector.getBeanInfo(item.getInstance().getClass()).getBeanDescriptor().getShortDescription();
292             } catch (IntrospectionException ie) {
293                 //do nothing
294
}
295             String JavaDoc toCheck = item.getInstance().getClass().getName();
296             toCheck = toCheck.lastIndexOf('.')!=-1 ?
297                 toCheck.substring(toCheck.lastIndexOf('.')+1) : toCheck; //NOI18N
298
if (toCheck.equals(result)) {
299                 result = null;
300             }
301             return result;
302         }
303         
304         public void actionPerformed(ActionEvent JavaDoc ae) {
305             Lookup.Item item = ((ItemRadioButton) ae.getSource()).item;
306             Object JavaDoc o = item.getInstance();
307             setValue (item.getInstance());
308             ObjectEditor.this.firePropertyChange();
309         }
310     }
311     
312     private static class ItemRadioButton extends JRadioButton JavaDoc {
313         Lookup.Item item;
314         public ItemRadioButton(Lookup.Item item, Font JavaDoc font) {
315             this.item = item;
316             setName(item.getId());
317             setText(item.getDisplayName());
318             setFont(font);
319             getAccessibleContext().setAccessibleName(getName());
320             getAccessibleContext().setAccessibleDescription(
321             getText());
322         }
323     }
324 }
325
326
Popular Tags