KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > src > nodes > ElementBeanModel


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.src.nodes;
21
22 import java.lang.reflect.InvocationTargetException JavaDoc;
23 import java.beans.*;
24
25 import org.openide.src.*;
26 import org.openide.nodes.PropertySupport;
27 import org.openide.ErrorManager;
28 import org.openide.explorer.propertysheet.PropertyPanel;
29 import org.openide.explorer.propertysheet.editors.ModifierEditor;
30
31 /** The class calls Node.Property.setValue in runAtomicAsUser
32  * to provide guarded block cheking.
33  *
34  * @author sdedic
35  * @version
36  */

37 class ElementBeanModel extends PropertySupport.Reflection {
38     private Element bean;
39     private PropertyEditor editor;
40
41     public ElementBeanModel(Element bean, String JavaDoc propertyName) throws NoSuchMethodException JavaDoc {
42         this(bean, findInfo(bean, propertyName));
43     }
44     
45     public ElementBeanModel(Element bean, PropertyDescriptor desc) throws NoSuchMethodException JavaDoc {
46         super(bean, desc.getPropertyType(), desc.getName());
47         setPropertyEditorClass(desc.getPropertyEditorClass());
48         this.bean = bean;
49     }
50
51     public PropertyEditor getPropertyEditor() {
52         if (editor == null) {
53             editor = super.getPropertyEditor();
54         }
55         return editor;
56     }
57
58     /** indicates if it is running inside the atomic section */
59     private boolean isRunningAtomic = false;
60     
61     public void setValue(final Object JavaDoc val) throws
62             IllegalAccessException JavaDoc, IllegalArgumentException JavaDoc, InvocationTargetException JavaDoc {
63         
64         if (isRunningAtomic) {
65             super.setValue(val);
66             return;
67         }
68         
69         SourceElement srcel = SourceEditSupport.findSource(bean);
70         
71         if (srcel == null) {
72             super.setValue(val);
73             return;
74         }
75         
76         final InvocationTargetException JavaDoc[] ex = new InvocationTargetException JavaDoc[1];
77         final IllegalAccessException JavaDoc[] ex2 = new IllegalAccessException JavaDoc[1];
78         try {
79             isRunningAtomic = true;
80             srcel.runAtomicAsUser(new Runnable JavaDoc() {
81                 public void run() {
82                     try {
83                         setValue(val);
84                     } catch (InvocationTargetException JavaDoc e) {
85                         ex[0] = e;
86                         ErrorManager.getDefault().annotate(
87                             e, e.getTargetException()
88                         );
89                     } catch (IllegalAccessException JavaDoc e) {
90                         ex2[0] = e;
91                     }
92                 }
93             });
94         } catch (SourceException e) {
95             ex[0] = new InvocationTargetException JavaDoc(e);
96         ErrorManager.getDefault().annotate(ex[0], e);
97         } finally {
98             isRunningAtomic = false;
99         }
100         if (ex[0] != null) throw ex[0];
101         if (ex2[0] != null) throw ex2[0];
102         
103     }
104     
105     /** Finds property descriptor for a bean.
106      * @param bean the bean
107      * @param name name of the property to find
108      * @return the descriptor
109      * @exception IllegalArgumentException if the method is not found
110      */

111     private static PropertyDescriptor findInfo (Object JavaDoc bean, String JavaDoc name)
112     throws IllegalArgumentException JavaDoc {
113         try {
114             BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass());
115             PropertyDescriptor[] descr = beanInfo.getPropertyDescriptors();
116             for (int i = 0; i < descr.length; i++) {
117                 if (descr[i].getName().equals(name)) {
118                     return descr[i];
119                 }
120             }
121             throw new IllegalArgumentException JavaDoc (
122                 "No property named " + name + " in class " + bean.getClass () // NOI18N
123
);
124         } catch (IntrospectionException e) {
125             IllegalArgumentException JavaDoc newEx = new IllegalArgumentException JavaDoc();
126             ErrorManager.getDefault().annotate (newEx, e);
127             throw newEx;
128         }
129     }
130
131     public static PropertyPanel createPropertyPanel(MemberElement element, String JavaDoc propertyName) {
132         try {
133             PropertyPanel panel = new PropertyPanel (
134                                             new ElementBeanModel(element, propertyName),
135                                             PropertyPanel.PREF_CUSTOM_EDITOR
136                                         );
137             
138             return panel;
139         } catch (NoSuchMethodException JavaDoc e) {
140             IllegalStateException JavaDoc ise = new IllegalStateException JavaDoc("Corrupted code"); // NOI18N
141
ise.initCause(e);
142             throw ise;
143         }
144     }
145     
146     public static PropertyPanel createModifiersPanel(MemberElement element) {
147         PropertyPanel modifPanel = createPropertyPanel(element, ElementProperties.PROP_MODIFIERS);
148         PropertyEditor propEdit = modifPanel.getProperty().getPropertyEditor();
149         if (propEdit instanceof ModifierEditor) {
150             ((ModifierEditor) propEdit).setMask(element.getModifiersMask());
151         }
152         return modifPanel;
153     }
154
155 }
156
Popular Tags