KickJava   Java API By Example, From Geeks To Geeks.

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


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  * ReusablePropertyModel.java
21  *
22  * Created on February 6, 2003, 5:12 PM
23  */

24 package org.openide.explorer.propertysheet;
25
26 import org.openide.nodes.Node;
27 import org.openide.nodes.Node.Property;
28
29 import java.beans.PropertyEditor JavaDoc;
30
31 import javax.swing.SwingUtilities JavaDoc;
32 import org.openide.util.Exceptions;
33
34
35 /** A reconfigurable property model for use by the rendering
36  * infrastructure, to avoid allocating memory while painting.
37  * Contains two static fields, PROPERTY and NODE which
38  * set the node and property this model acts as an interface to.<P>
39  * This class is <i>not thread safe</i>. It assumes that it will
40  * only be called from the AWT thread, since it is used in painting
41  * infrastructure. If property misrendering occurs, run NetBeans
42  * with the argument <code>-J-Dnetbeans.reusable.strictthreads=true</code>
43  * and exceptions will be thrown if any method is called from off the
44  * AWT thread.
45  *
46  * @author Tim Boudreau
47  */

48 class ReusablePropertyModel implements ExPropertyModel {
49     static final boolean DEBUG = Boolean.getBoolean("netbeans.reusable.strictthreads");
50     private transient Property PROPERTY = null;
51     private final ReusablePropertyEnv env;
52
53     /** Creates a new instance of ReusablePropertyModel */
54     public ReusablePropertyModel(ReusablePropertyEnv env) {
55         this.env = env;
56         env.setReusablePropertyModel(this);
57     }
58
59     void clear() {
60         PROPERTY = null;
61     }
62
63     /** Does nothing - if a property changes, the sheet will get notification
64      * and the model will be reconfigured with the new value and re-rendered */

65     public void addPropertyChangeListener(java.beans.PropertyChangeListener JavaDoc l) {
66     }
67
68     /** Does nothing - if a property changes, the sheet will get notification
69      * and the model will be reconfigured with the new value and re-rendered */

70     public void removePropertyChangeListener(java.beans.PropertyChangeListener JavaDoc l) {
71     }
72
73     public PropertyEditor JavaDoc getPropertyEditor() {
74         Node.Property p = getProperty();
75
76         // #52179: PropUtils.isExternallyEdited(p) - don't affect just
77
// externally edited properties or their current changes will be lost
78
// due to the firing PropertyChangeEvents to theirs UI counterpart
79
return PropUtils.getPropertyEditor(p, !PropUtils.isExternallyEdited(p));
80     }
81
82     public Class JavaDoc getPropertyEditorClass() {
83         if (DEBUG) {
84             checkThread();
85         }
86
87         return getPropertyEditor().getClass();
88     }
89
90     public Class JavaDoc getPropertyType() {
91         if (DEBUG) {
92             checkThread();
93         }
94
95         return getProperty().getValueType();
96     }
97
98     public Object JavaDoc getValue() throws java.lang.reflect.InvocationTargetException JavaDoc {
99         if (DEBUG) {
100             checkThread();
101         }
102
103         try {
104             return getProperty().getValue();
105         } catch (IllegalAccessException JavaDoc iae) {
106             Exceptions.printStackTrace(iae);
107         }
108
109         return null;
110     }
111
112     public void setValue(Object JavaDoc v) throws java.lang.reflect.InvocationTargetException JavaDoc {
113         if (DEBUG) {
114             checkThread();
115         }
116
117         try {
118             getProperty().setValue(v);
119         } catch (IllegalAccessException JavaDoc iae) {
120             Exceptions.printStackTrace(iae);
121         }
122     }
123
124     public Object JavaDoc[] getBeans() {
125         if (DEBUG) {
126             checkThread();
127         }
128
129         if (env.getNode() instanceof ProxyNode) {
130             return ((ProxyNode) env.getNode()).getOriginalNodes();
131         } else {
132             return new Object JavaDoc[] { env.getNode() };
133         }
134     }
135
136     public java.beans.FeatureDescriptor JavaDoc getFeatureDescriptor() {
137         if (DEBUG) {
138             checkThread();
139         }
140
141         return getProperty();
142     }
143
144     /** Ensure we're really running on the AWT thread, otherwise bad things can
145      * happen. */

146     static void checkThread() {
147         if (SwingUtilities.isEventDispatchThread() == false) {
148             throw new IllegalStateException JavaDoc("Reusable property model accessed from off the AWT thread.");
149         }
150     }
151
152     public Node.Property getProperty() {
153         return PROPERTY;
154     }
155
156     public void setProperty(Node.Property PROPERTY) {
157         this.PROPERTY = PROPERTY;
158     }
159 }
160
Popular Tags