KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > catalog > CatalogNode


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.modules.xml.catalog;
21
22 import java.awt.Image JavaDoc;
23 import java.beans.IntrospectionException JavaDoc;
24 import java.beans.PropertyChangeEvent JavaDoc;
25 import java.beans.PropertyChangeListener JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.ObjectInputStream JavaDoc;
28 import java.io.ObjectOutputStream JavaDoc;
29 import java.util.HashSet JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.Set JavaDoc;
32 import java.util.TreeSet JavaDoc;
33 import javax.swing.Action JavaDoc;
34 import org.netbeans.modules.xml.catalog.settings.CatalogSettings;
35 import org.netbeans.modules.xml.catalog.spi.CatalogDescriptor;
36 import org.netbeans.modules.xml.catalog.spi.CatalogListener;
37 import org.netbeans.modules.xml.catalog.spi.CatalogReader;
38 import org.netbeans.modules.xml.catalog.spi.CatalogWriter;
39 import org.openide.actions.PropertiesAction;
40 import org.openide.nodes.BeanNode;
41 import org.openide.nodes.Children;
42 import org.openide.nodes.Node;
43 import org.openide.util.HelpCtx;
44 import org.openide.util.WeakListeners;
45 import org.openide.util.actions.NodeAction;
46 import org.openide.util.actions.SystemAction;
47
48 /**
49  * Node representing a catalog.
50  * Every catalog reader is considered to be a bean.
51  * Information about catalog instance are obtained using CatalogDescriptor interface
52  * if passed instance implements it.
53  *
54  * @author Petr Kuzel
55  * @version 1.0
56  */

57 final class CatalogNode extends BeanNode implements Refreshable, PropertyChangeListener JavaDoc, Node.Cookie {
58     
59     private CatalogReader catalog;
60     /** Creates new CatalogNode */
61     public CatalogNode(CatalogReader catalog) throws IntrospectionException JavaDoc {
62         super(catalog, new CatalogChildren(catalog));
63         this.catalog=catalog;
64         getCookieSet().add(this);
65         
66         if (catalog instanceof CatalogDescriptor) {
67             
68             // set node properties acording to descriptor
69

70             CatalogDescriptor desc = (CatalogDescriptor) catalog;
71             setSynchronizeName(false);
72             setName(desc.getDisplayName());
73             String JavaDoc bundleString = catalog instanceof CatalogWriter ?"LBL_catalogReadWrite":"LBL_catalogReadOnly"; //NOI18N
74
setDisplayName(Util.THIS.getString(bundleString, desc.getDisplayName()));
75             setShortDescription(desc.getShortDescription());
76             fireIconChange();
77
78             // listen on it
79

80             desc.addPropertyChangeListener(WeakListeners.propertyChange(this, desc));
81         }
82     }
83     
84     CatalogReader getCatalogReader() {
85         return catalog;
86     }
87
88     public Action JavaDoc[] getActions(boolean context) {
89         if (catalog instanceof CatalogWriter)
90             return new Action JavaDoc[] {
91                 SystemAction.get(AddCatalogEntryAction.class),
92                 SystemAction.get(RefreshAction.class),
93                 SystemAction.get(CatalogNode.UnmountAction.class),
94                 null,
95                 //??? #24349 CustimizeAction sometimes added by BeanNode here
96
SystemAction.get(PropertiesAction.class)
97             };
98         else
99             return new Action JavaDoc[] {
100                 SystemAction.get(RefreshAction.class),
101                 SystemAction.get(CatalogNode.UnmountAction.class),
102                 null,
103                 //??? #24349 CustimizeAction sometimes added by BeanNode here
104
SystemAction.get(PropertiesAction.class)
105             };
106     }
107
108     /**
109      * @return icon regurned by CatalogDescriptor if instance of it
110      */

111     public Image JavaDoc getIcon(int type) {
112         if (catalog instanceof CatalogDescriptor) {
113             Image JavaDoc icon = ((CatalogDescriptor)catalog).getIcon(type);
114             if (icon != null) return icon;
115         }
116         
117         return super.getIcon(type);
118     }
119     
120     public HelpCtx getHelpCtx() {
121         //return new HelpCtx(CatalogNode.class);
122
return HelpCtx.DEFAULT_HELP;
123     }
124     
125     /**
126      * Refresh catalog provider and then refresh children.
127      */

128     public void refresh() {
129         catalog.refresh();
130         ((CatalogChildren)getChildren()).reload(); // may be double reload
131
}
132
133     /** This node cannot be destroyed, just unmount.
134      * @return always <CODE>false</CODE>
135      */

136     public boolean canDestroy () {
137         return false;
138     }
139     
140     public boolean canRename() {
141         return false;
142     }
143
144     /**
145      * Remove itseld from CatalogSettings,
146      */

147     public void destroy() throws IOException JavaDoc {
148         CatalogSettings mounted = CatalogSettings.getDefault();
149         mounted.removeCatalog(catalog);
150         super.destroy();
151     }
152
153     /**
154      * The node listens on some changes
155      */

156     public void propertyChange(PropertyChangeEvent JavaDoc e) {
157         if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug(e.toString());
158         if (CatalogDescriptor.PROP_CATALOG_NAME.equals(e.getPropertyName())) {
159             if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug(" Setting name: " + (String JavaDoc) e.getNewValue()); // NOI18N
160

161             setName((String JavaDoc) e.getNewValue());
162             setDisplayName((String JavaDoc) e.getNewValue());
163         } else if (CatalogDescriptor.PROP_CATALOG_DESC.equals(e.getPropertyName())) {
164             if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug(" Setting desc: " + (String JavaDoc) e.getNewValue()); // NOI18N
165

166             setShortDescription((String JavaDoc) e.getNewValue());
167         } else if (CatalogDescriptor.PROP_CATALOG_ICON.equals(e.getPropertyName())) {
168             if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug(" Updating icon"); // NOI18N
169

170             fireIconChange();
171         }
172     }
173     
174     
175     // ~~~~~~~~~~~~~~~~~~~~~~ Serialization stuff ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
176

177     private void readObject(ObjectInputStream JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
178         if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug("Reading Catalog node " + this); // NOI18N
179

180         in.defaultReadObject();
181     }
182     
183     private void writeObject(ObjectOutputStream JavaDoc out) throws IOException JavaDoc {
184         if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug("Writing " + this); // NOI18N
185

186         out.defaultWriteObject();
187     }
188
189     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
190

191     /**
192      * Kids have to listen at Catalog
193      */

194     public static class CatalogChildren extends Children.Keys {
195         
196         private CatalogReader peer;
197         private CatalogListener catalogListener;
198         
199         public CatalogChildren(CatalogReader catalog) {
200             peer = catalog;
201             
202         }
203                 
204         /** Contains public ID (String) instances. */
205         private final TreeSet JavaDoc keys = new TreeSet JavaDoc();
206         
207         public void addNotify() {
208             catalogListener = new Lis();
209             try {
210                 peer.addCatalogListener(catalogListener);
211             } catch (UnsupportedOperationException JavaDoc ex) {
212                 // User must use explicit refresh
213
}
214             reload();
215         }
216
217         public void removeNotify() {
218             try {
219                 peer.removeCatalogListener(catalogListener);
220             } catch (UnsupportedOperationException JavaDoc ex) {
221                 // does not matter
222
}
223             keys.clear();
224             setKeys(keys);
225         }
226         
227         public Node[] createNodes(Object JavaDoc key) {
228             try {
229                 CatalogEntry catalogEntry = new CatalogEntry((String JavaDoc) key, peer);
230                 return new Node[] {
231                     new CatalogEntryNode(catalogEntry)
232                 };
233             } catch (IntrospectionException JavaDoc ex) {
234                 return null;
235             }
236         }
237
238         /**
239           * Reloads catalog content
240           */

241         public void reload() {
242             if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug(" Reloading kids of " + peer + "..."); // NOI18N
243

244             Set JavaDoc previous = new HashSet JavaDoc(keys);
245             keys.clear();
246             Iterator JavaDoc it = peer.getPublicIDs();
247             if (it != null) {
248                 while (it.hasNext()) {
249                     String JavaDoc publicID = (String JavaDoc) it.next();
250                     keys.add(publicID);
251                     if (previous.contains(publicID)) {
252                         refreshKey(publicID); // recreate node, the systemId may have changed
253
}
254                 }
255             }
256             setKeys(keys);
257         }
258         
259         private class Lis implements CatalogListener {
260             
261             /** Given public ID has changed - created. */
262             public void notifyNew(String JavaDoc publicID) {
263                 keys.add(publicID);
264                 setKeys(keys);
265             }
266             
267             /** Given public ID has changed - disappeared. */
268             public void notifyRemoved(String JavaDoc publicID) {
269                 keys.remove(publicID);
270                 setKeys(keys);
271             }
272             
273             /** Given public ID has changed. */
274             public void notifyUpdate(String JavaDoc publicID) {
275                 refreshKey(publicID);
276             }
277             
278             /** All entries are invalidated. */
279             public void notifyInvalidate() {
280                 reload();
281             }
282             
283         }
284         
285     }
286
287     /**
288      * Give to the action your own name
289      */

290     private static final class UnmountAction extends NodeAction {
291         /** Serial Version UID */
292         private static final long serialVersionUID = 3556006276357785484L;
293         
294         public UnmountAction() {
295         }
296         
297         public String JavaDoc getName() {
298             return Util.THIS.getString("LBL_unmount");
299         }
300         
301         public HelpCtx getHelpCtx() {
302             return new HelpCtx(UnmountAction.class);
303         }
304         
305         protected boolean enable(Node[] activatedNodes) {
306             if (activatedNodes.length > 0) {
307                 for (int i = 0; i<activatedNodes.length; i++) {
308                     Node me = activatedNodes[i];
309                     Object JavaDoc self = me.getCookie(CatalogNode.class);
310                     if (self instanceof CatalogNode) {
311                         CatalogReader reader = (CatalogReader) ((CatalogNode)self).getBean();
312                         if (CatalogSettings.getDefault().isRemovable(reader)) {
313                             return true;
314                         }
315                     }
316                 }
317             }
318             return false;
319         }
320         
321         protected void performAction(Node[] activatedNodes) {
322             if (enable(activatedNodes) == false) return;
323             for (int i = 0; i<activatedNodes.length; i++) {
324                 try {
325                     Node me = activatedNodes[i];
326                     CatalogNode self = (CatalogNode) me.getCookie(CatalogNode.class);
327                     self.destroy();
328                 } catch (IOException JavaDoc ex) {
329                     Util.THIS.debug("Cannot unmount XML entity catalog!", ex);
330                 }
331             }
332         }
333         
334         protected boolean asynchronous() {
335             return false;
336         }
337         
338     }
339     
340 }
341
Popular Tags