KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > explorer > propertysheet > NodePropertyModel


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  * NodePropertyModel.java
21  *
22  * Created on April 22, 2003, 5:09 PM
23  */

24 package org.openide.explorer.propertysheet;
25
26 import org.openide.nodes.Node;
27
28 import java.beans.FeatureDescriptor JavaDoc;
29 import java.beans.PropertyChangeListener JavaDoc;
30 import java.beans.PropertyChangeSupport JavaDoc;
31 import java.beans.PropertyEditor JavaDoc;
32
33 import java.lang.reflect.InvocationTargetException JavaDoc;
34
35
36 /** Implementation of the <code>PropertyModel</code> interface keeping
37  * a <code>Node.Property</code>. Refactored from PropertyPanel.SimpleModel
38  * as part of the property sheet rewrite. */

39 class NodePropertyModel implements ExPropertyModel {
40     //This class was originally PropertyPanel.SimpleModel up to
41
//PropertyPanel 1.123
42

43     /** Property to work with. */
44     private Node.Property prop;
45
46     /** Array of beans(nodes) to which belong the property. */
47     private Object JavaDoc[] beans;
48
49     /** Property change support. */
50     private PropertyChangeSupport JavaDoc sup = new PropertyChangeSupport JavaDoc(this);
51     String JavaDoc beanName = null;
52
53     /** Construct simple model instance.
54      * @param property proeprty to work with
55      * @param beans array of beans(nodes) to which belong the property
56      */

57     public NodePropertyModel(Node.Property property, Object JavaDoc[] beans) {
58         this.prop = property;
59         this.beans = beans;
60     }
61
62     String JavaDoc getBeanName() {
63         if (beans != null) {
64             if ((beans.length == 1) && beans[0] instanceof Node.Property) {
65                 return ((Node.Property) beans[0]).getDisplayName();
66             }
67         }
68
69         return null;
70     }
71
72     /** Implements <code>PropertyModel</code> interface. */
73     public Object JavaDoc getValue() throws InvocationTargetException JavaDoc {
74         try {
75             return prop.getValue();
76         } catch (IllegalAccessException JavaDoc iae) {
77             throw annotateException(iae);
78         } catch (InvocationTargetException JavaDoc ite) {
79             throw annotateException(ite);
80         } catch (ProxyNode.DifferentValuesException dve) {
81             return null;
82         }
83     }
84
85     /** Implements <code>PropertyModel</code> interface. */
86     public void setValue(Object JavaDoc v) throws InvocationTargetException JavaDoc {
87         try {
88             prop.setValue(v);
89             sup.firePropertyChange(PropertyModel.PROP_VALUE, null, null);
90         } catch (IllegalAccessException JavaDoc iae) {
91             throw annotateException(iae);
92         } catch (IllegalArgumentException JavaDoc iaae) {
93             throw annotateException(iaae);
94         } catch (InvocationTargetException JavaDoc ite) {
95             throw annotateException(ite);
96         }
97     }
98
99     /** Annotates specified exception. Helper method.
100      * @param exception original exception to annotate
101      * @return <code>IvocationTargetException</code> which annotates the
102      * original exception
103      */

104     private InvocationTargetException JavaDoc annotateException(Exception JavaDoc exception) {
105         if (exception instanceof InvocationTargetException JavaDoc) {
106             return (InvocationTargetException JavaDoc) exception;
107         } else {
108             return new InvocationTargetException JavaDoc(exception);
109         }
110     }
111
112     /** Implements <code>PropertyModel</code> interface. */
113     public Class JavaDoc getPropertyType() {
114         return prop.getValueType();
115     }
116
117     /** Implements <code>PropertyModel</code> interface. */
118     public Class JavaDoc getPropertyEditorClass() {
119         Object JavaDoc ed = prop.getPropertyEditor();
120
121         if (ed != null) {
122             return ed.getClass();
123         }
124
125         return null;
126     }
127
128     /** Mainly a hack to avoid gratuitous calls to fetch property editors.
129      * @since 1.123.2.1 - branch propsheet_issue_29447
130      */

131     public PropertyEditor JavaDoc getPropertyEditor() {
132         return PropUtils.getPropertyEditor(prop);
133     }
134
135     /** Implements <code>PropertyModel</code> interface. */
136     public void addPropertyChangeListener(PropertyChangeListener JavaDoc l) {
137         sup.addPropertyChangeListener(l);
138     }
139
140     /** Implements <code>PropertyModel</code> interface. */
141     public void removePropertyChangeListener(PropertyChangeListener JavaDoc l) {
142         sup.removePropertyChangeListener(l);
143     }
144
145     /** Implements <code>ExPropertyModel</code> interface. */
146     public Object JavaDoc[] getBeans() {
147         return beans;
148     }
149
150     /** Implements <code>ExPropertyModel</code> interface. */
151     public FeatureDescriptor JavaDoc getFeatureDescriptor() {
152         return prop;
153     }
154
155     void fireValueChanged() {
156         sup.firePropertyChange(PropertyModel.PROP_VALUE, null, null);
157     }
158
159     /** Package private method to return the property, so error handling
160      * can use the display name in the dialog for the user if the user
161      * enters an invalid value */

162     Node.Property getProperty() {
163         return prop;
164     }
165 }
166
Popular Tags