KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > tax > beans > customizer > AbstractTreeCustomizer


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 package org.netbeans.modules.xml.tax.beans.customizer;
20
21 import java.awt.event.KeyEvent JavaDoc;
22 import javax.swing.JPanel JavaDoc;
23 import javax.swing.SwingUtilities JavaDoc;
24 import java.beans.Customizer JavaDoc;
25 import java.beans.PropertyChangeEvent JavaDoc;
26 import java.beans.PropertyChangeListener JavaDoc;
27
28 import org.netbeans.tax.TreeObject;
29
30 /**
31  *
32  * @author Libor Kramolis
33  * @version 0.1
34  */

35 public abstract class AbstractTreeCustomizer extends JPanel JavaDoc implements Customizer JavaDoc, PropertyChangeListener JavaDoc {
36
37     /** Serial Version UID */
38     private static final long serialVersionUID =7141277140374364170L;
39     
40     /** */
41     private static final String JavaDoc TEXT_DEFAULT = Util.THIS.getString ("TEXT_DEFAULT"); // NOI18N
42

43     /** */
44     public static final String JavaDoc MIME_XML = "text/xml"; // NOI18N
45

46     /** */
47     public static final String JavaDoc MIME_DTD = "text/x-dtd"; // NOI18N
48

49     /** */
50     public static final String JavaDoc MIME_TXT = "text/plain"; // NOI18N
51

52     
53     /** Used to disable propertu changes etc. during initilizing. */
54     protected boolean initializing;
55     
56     /** */
57     private TreeObject treeObject;
58     
59     /** Does this registered itself listeners as TreeNode? */
60     private boolean treeListening = false;
61     
62     
63     //
64
// init
65
//
66

67     /** We call virtual method from constructor. Use initializing to check stage. */
68     public AbstractTreeCustomizer () {
69         super ();
70         
71         treeObject = null;
72         initializing = false;
73     }
74     
75     
76     //
77
// from Customizer
78
//
79

80     /** Set the object to be customized.
81      * @param bean The object to be customized.
82      */

83     public final void setObject (Object JavaDoc bean) throws IllegalArgumentException JavaDoc {
84         try {
85             initializing = true;
86             
87             if (! (bean instanceof TreeObject))
88                 throw new IllegalArgumentException JavaDoc (bean + Util.THIS.getString ("PROP__invalid_instance")); //!!!
89

90             treeObject = (TreeObject)bean;
91             
92             ownInitComponents ();
93             
94             initValues ();
95         } finally {
96             initializing = false;
97         }
98     }
99     
100     
101     //
102
// itself
103
//
104

105     /**
106      */

107     protected final TreeObject getTreeObject () {
108         return treeObject;
109     }
110     
111     /**
112      */

113     private final void initValues () {
114         initComponentValues ();
115         updateReadOnlyStatus ();
116         initListeners ();
117     }
118     
119     /**
120      */

121     abstract protected void initComponentValues ();
122     
123     /**
124      */

125     protected void ownInitComponents () {
126     }
127     
128     /**
129      */

130     private void updateReadOnlyStatus () {
131         updateReadOnlyStatus (!!! getTreeObject ().isReadOnly ());
132     }
133     
134     /**
135      */

136     abstract protected void updateReadOnlyStatus (boolean editable);
137     
138     
139     //
140
// events
141
//
142

143     /**
144      */

145     private void initListeners () {
146         if (!treeListening) {
147             treeObject.addPropertyChangeListener (org.openide.util.WeakListeners.propertyChange (this, treeObject));
148             treeListening = true;
149         }
150     }
151     
152     /**
153      * It will be called from AWT thread and it will never be caller during init stage.
154      */

155     protected void safePropertyChange (PropertyChangeEvent JavaDoc pche) {
156         if (pche.getPropertyName ().equals (TreeObject.PROP_READ_ONLY)) {
157             updateReadOnlyStatus ();
158         }
159     }
160     
161     /**
162      * Filter out notifications during selfinitialization stage and
163      * pass others in AWT thread.
164      */

165     public final void propertyChange (final PropertyChangeEvent JavaDoc e) {
166         if (initializing)
167             return;
168         
169         if (SwingUtilities.isEventDispatchThread ()) {
170             safePropertyChange (e);
171         } else {
172             SwingUtilities.invokeLater (new Runnable JavaDoc () {
173                 public void run () {
174                     AbstractTreeCustomizer.this.safePropertyChange (e);
175                 }
176             });
177         }
178     }
179     
180     
181     //
182
// Utils
183
//
184

185     protected static String JavaDoc text2null (String JavaDoc text) {
186         if ( text.equals (TEXT_DEFAULT) )
187             return null;
188         if ( text.length () == 0 )
189             return null;
190         if ( text.trim ().length () == 0 )
191             return null;
192         return text;
193     }
194     
195     protected static String JavaDoc null2text (String JavaDoc maybeNull) {
196         if ( maybeNull == null )
197             return TEXT_DEFAULT;
198         return maybeNull;
199     }
200     
201     protected static boolean applyKeyPressed (KeyEvent JavaDoc evt) {
202         return (evt.isControlDown () && (evt.getKeyCode () == KeyEvent.VK_ENTER));
203     }
204     
205 }
206
Popular Tags