KickJava   Java API By Example, From Geeks To Geeks.

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


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.catalog;
20
21 import java.awt.event.*;
22 import java.awt.*;
23 import java.beans.*;
24 import java.util.*;
25 import java.io.*;
26 import java.net.*;
27
28 import javax.swing.event.*;
29
30 import org.openide.*;
31 import org.openide.nodes.*;
32 import org.openide.util.actions.*;
33 import org.openide.util.datatransfer.*;
34 import org.openide.actions.*;
35
36 import org.netbeans.modules.xml.catalog.spi.*;
37 import org.netbeans.modules.xml.catalog.impl.*;
38 import org.netbeans.modules.xml.catalog.settings.CatalogSettings;
39 import org.openide.util.HelpCtx;
40 import java.awt.event.ActionEvent JavaDoc;
41 import javax.swing.*;
42
43 /**
44  * Node representing catalog root in the Runtime tab. It retrieves all
45  * mounted catalogs from current project settings.
46  *
47  * To be registered in manifest file as:
48  * <pre>
49  * Name: org.netbeans.modules.xml.catalog.CatalogNode.class
50  * OpenIDE-Module-Class: Environment
51  * </pre>
52  *
53  * <p><b>Implementation Note:</b>
54  * <p>The node has session lifetime but its model has project lifetime, so there
55  * is implemented a logic for model instance changing (see children).
56  *
57  * @author Petr Kuzel
58  * @version 1.0
59  */

60 public final class CatalogRootNode extends AbstractNode implements Node.Cookie {
61
62     /** Creates new CatalogNode */
63     public CatalogRootNode() {
64         super(new RootChildren());
65         setName("XML-CATALOG"); // NOI18N
66
setDisplayName (Util.THIS.getString("TEXT_catalog_root")); // NOI18N
67
setIconBaseWithExtension("org/netbeans/modules/xml/catalog/resources/catalog-root.gif"); // NOI18N
68
setShortDescription(Util.THIS.getString("PROP_catalog_root_desc"));
69         getCookieSet().add(this);
70     }
71     
72     protected SystemAction[] createActions() {
73         return new SystemAction[] {
74             SystemAction.get(CatalogRootNode.MountAction.class),
75             null,
76             SystemAction.get(PropertiesAction.class)
77         };
78     }
79
80     /** We can mount entity catalogs. */
81 // public NewType[] getNewTypes() {
82
// return new NewType[] {new CatalogMounter()};
83
// }
84

85     /**
86      * Mounts new catalalog as specified by user.
87      */

88     class CatalogMounter extends NewType implements ActionListener {
89
90         CatalogMounterModel model = null;
91         Dialog myDialog = null;
92         
93         public void create() throws IOException {
94             
95             Iterator it = ProvidersRegistry.getProviderClasses(new Class JavaDoc[] {CatalogReader.class});
96             
97             model = new CatalogMounterModel(it);
98             Object JavaDoc rpanel = new CatalogMounterPanel(model);
99             DialogDescriptor dd = new DialogDescriptor(rpanel,
100                                   Util.THIS.getString ("PROP_Mount_Catalog"), true, this);
101             dd.setHelpCtx(new HelpCtx(CatalogMounterPanel.class));
102             myDialog = DialogDisplayer.getDefault().createDialog(dd);
103
104             // set dialog size to 60x10 characters
105

106             JTextArea template = new JTextArea();
107             template.setColumns(60);
108             template.setRows(8 + 2); // 8 lines, 2 bottom line with buttons
109
Dimension dimension = template.getPreferredSize();
110             
111             //#33996 this is insets size as prescribed by UI guidelines
112
final int insets = 12;
113             
114             // small fonts have problems that insets are times more important
115
// then font size, include also insets
116
int heightInsets = dimension.height + 10 * insets; // 10 lines * 12 inset size
117
int widthInsets = dimension.width + 4 * insets;
118             Dimension fullDimension = new Dimension(widthInsets, heightInsets);
119             myDialog.setSize(fullDimension); //^ packing never creates bigger window :-(
120

121             myDialog.setVisible(true);
122         }
123
124         public void actionPerformed(ActionEvent JavaDoc ae) {
125             if (ae.getSource() == DialogDescriptor.OK_OPTION) {
126                 
127                 Object JavaDoc catalog = model.getCatalog();
128                 if (catalog == null) return;
129                 CatalogSettings mounted = CatalogSettings.getDefault();
130                 mounted.addCatalog((CatalogReader)catalog);
131                 
132             }
133             if (myDialog != null) {
134                 myDialog.dispose();
135                 myDialog = null;
136             }
137         }
138         
139         public String JavaDoc getName() {
140             return Util.THIS.getString ("LBL_mount"); // NOI18N
141
}
142     }
143     
144
145     // ~~~~~~~~~~~~~~~~~~~~~~ Serialization stuff ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
146

147     private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException JavaDoc {
148         if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug("Reading CatalogRoot node " + this); // NOI18N
149

150         in.defaultReadObject();
151     }
152     
153     private void writeObject(ObjectOutputStream out) throws IOException {
154         if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug("Writing " + this); // NOI18N
155

156         out.defaultWriteObject();
157     }
158     
159     public HelpCtx getHelpCtx() {
160         //return new HelpCtx(CatalogRootNode.class);
161
return HelpCtx.DEFAULT_HELP;
162     }
163     
164     // ~~~~~~~~~~~~~~~~~~~~~~ NODE KIDS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
165

166
167     /**
168      * Kids driven by CatalogSettings. Only one instance may be used
169      * since redefined equals() method.
170      */

171     private static class RootChildren extends Children.Keys implements Comparator, PropertyChangeListener {
172         
173         /** Contains CatalogReader instances. */
174         private final TreeSet keys = new TreeSet(this);
175         
176         /**
177           * Create new keys, register itself as listener.
178           */

179         public synchronized void addNotify() {
180             CatalogSettings mounted = CatalogSettings.getDefault();
181             mounted.addPropertyChangeListener(this);
182             createKeys(mounted);
183         }
184
185         /**
186           * Remove listener and keys.
187           */

188         public synchronized void removeNotify() {
189             CatalogSettings mounted = CatalogSettings.getDefault();
190             if (mounted != null) mounted.removePropertyChangeListener(this);
191             keys.clear();
192             setKeys(keys);
193         }
194
195         /**
196           * Create CatalogNodes initialized by provider instance.
197           */

198         public Node[] createNodes(Object JavaDoc key) {
199             try {
200                 return new Node[] { new CatalogNode((CatalogReader)key) };
201             } catch (IntrospectionException ex) {
202                 return new Node[] {};
203             }
204         }
205
206         /**
207           * The only instance (see equals) listens on ProvidersRegistry
208           * for its state changes.
209           */

210         public synchronized void propertyChange(PropertyChangeEvent e) {
211             if (CatalogSettings.PROP_MOUNTED_CATALOGS.equals(e.getPropertyName())) {
212                 createKeys((CatalogSettings)e.getSource());
213             } else if (CatalogSettings.PROP_PRJ_INSTANCE.equals(e.getPropertyName())) {
214                 
215                 //??? switch model instances it is an ugly hack
216

217                 CatalogSettings mounted = (CatalogSettings) e.getOldValue();
218                 if (mounted != null) {
219                     mounted.removePropertyChangeListener(this);
220                 }
221                 mounted = (CatalogSettings) e.getNewValue();
222                 if (mounted != null) mounted.addPropertyChangeListener(this);
223                 createKeys(mounted);
224             }
225         }
226         
227         /**
228           * Creates new keys according to CatalogSettings.
229           */

230         private void createKeys(CatalogSettings mounted) {
231             keys.clear();
232             if (mounted != null) {
233                 Iterator it = mounted.getCatalogs(new Class JavaDoc[] {CatalogReader.class});
234                 while (it.hasNext()) {
235                     keys.add(it.next()); //!!! use immutable key wrappers, some
236
// instances may overwrite equals() so
237
// they cannot be used as a children key
238
}
239             }
240             setKeys(keys);
241         }
242         
243         /**
244           * We are also comparators. Use class based equality.
245           */

246         public boolean equals(java.lang.Object JavaDoc peer) {
247             return peer.getClass().equals(getClass());
248         }
249         
250         /**
251          * Compare keys giving highest priority to system catalog.
252          * Other catalogs sort by display name if available.
253          */

254         public int compare(java.lang.Object JavaDoc one,java.lang.Object JavaDoc two) {
255             if (one == two) return 0;
256             if (one instanceof SystemCatalogReader) return -1;
257             if (two instanceof SystemCatalogReader) return 1;
258             if (one instanceof CatalogDescriptor && two instanceof CatalogDescriptor) {
259                 int test = (((CatalogDescriptor)one).getDisplayName()).compareTo(
260                     ((CatalogDescriptor)two).getDisplayName()
261                 );
262                 if (test != 0) return test;
263                 
264             } else {
265                 if (one instanceof CatalogDescriptor) return -1;
266                 if (two instanceof CatalogDescriptor) return 1;
267             }
268             // show all catalogs never return 0
269
return (long)one.hashCode() - (long)two.hashCode() > 0L ? 1 : -1;
270         }
271         
272     }
273
274     
275     /**
276      * Give to action your own name
277      */

278     private static final class MountAction extends NodeAction {
279         /** Serial Version UID */
280         private static final long serialVersionUID = -3608629636833099065L;
281         
282         public MountAction() {
283         }
284         
285         public String JavaDoc getName() {
286             return Util.THIS.getString("LBL_mount");
287         }
288         
289         public HelpCtx getHelpCtx() {
290             return new HelpCtx(MountAction.class);
291         }
292         
293         protected synchronized boolean enable(Node[] activatedNodes) {
294             return activatedNodes.length > 0;
295         }
296         
297         protected boolean asynchronous() {
298             return false;
299         }
300         
301         protected synchronized void performAction(Node[] activatedNodes) {
302             if (enable(activatedNodes) == false) return;
303             try {
304                 Node current = activatedNodes[0];
305                 CatalogRootNode me = (CatalogRootNode) current.getCookie(CatalogRootNode.class);
306                 CatalogMounter newType = me.new CatalogMounter();
307                 newType.create();
308             } catch (IOException ex) {
309                 Util.THIS.debug(ex);
310             } finally {
311             }
312         }
313     }
314     
315 }
316
Popular Tags