KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > deployment > impl > ui > ServersCustomizer


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.j2ee.deployment.impl.ui;
21
22 import java.awt.CardLayout JavaDoc;
23 import java.awt.Component JavaDoc;
24 import java.awt.Container JavaDoc;
25 import java.awt.Dialog JavaDoc;
26 import java.awt.Dimension JavaDoc;
27 import java.awt.GridBagConstraints JavaDoc;
28 import java.awt.GridBagLayout JavaDoc;
29 import java.awt.Image JavaDoc;
30 import java.awt.Window JavaDoc;
31 import java.beans.PropertyChangeListener JavaDoc;
32 import java.beans.PropertyChangeEvent JavaDoc;
33 import java.beans.PropertyVetoException JavaDoc;
34 import java.beans.VetoableChangeListener JavaDoc;
35 import java.util.ArrayList JavaDoc;
36 import java.util.Collection JavaDoc;
37 import java.util.Collections JavaDoc;
38 import java.util.HashMap JavaDoc;
39 import java.util.Iterator JavaDoc;
40 import java.util.List JavaDoc;
41 import java.util.Set JavaDoc;
42 import javax.swing.UIManager JavaDoc;
43 import org.netbeans.modules.j2ee.deployment.impl.ServerInstance;
44 import org.netbeans.modules.j2ee.deployment.impl.ServerRegistry;
45 import org.netbeans.modules.j2ee.deployment.impl.ui.wizard.AddServerInstanceWizard;
46 import org.netbeans.modules.j2ee.deployment.plugins.api.InstanceProperties;
47 import org.openide.nodes.Node;
48 import org.openide.explorer.ExplorerManager;
49 import org.openide.explorer.view.BeanTreeView;
50 import org.openide.DialogDisplayer;
51 import org.openide.WizardDescriptor;
52 import org.openide.filesystems.Repository;
53 import org.openide.loaders.DataFolder;
54 import org.openide.nodes.AbstractNode;
55 import org.openide.nodes.Children;
56 import org.openide.nodes.FilterNode;
57 import org.openide.util.NbBundle;
58
59
60
61 /**
62  * Servers customizer displays a list of registered server and allows to add,
63  * remove and configure them.
64  *
65  * @author Stepan Herold
66  */

67 public class ServersCustomizer extends javax.swing.JPanel JavaDoc implements PropertyChangeListener JavaDoc, VetoableChangeListener JavaDoc, ExplorerManager.Provider {
68     
69     private static final Dimension JavaDoc MINIMUM_SIZE = new Dimension JavaDoc(720, 400);
70     
71     private ServerCategoriesChildren children;
72     private ExplorerManager manager;
73     private ServerInstance initialInstance;
74     
75     /** Creates new form PlatformsCustomizer */
76     public ServersCustomizer(ServerInstance initialInstance) {
77         initComponents();
78         serverName.setColumns(30);
79         serverType.setColumns(30);
80         // set the preferred width, height is not very important here
81
servers.setPreferredSize(new Dimension JavaDoc(200,200));
82         this.initialInstance = initialInstance;
83         setPreferredSize(MINIMUM_SIZE);
84     }
85     
86     
87     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
88         if (ExplorerManager.PROP_SELECTED_NODES.equals(evt.getPropertyName())) {
89         Node[] nodes = (Node[]) evt.getNewValue();
90         if (nodes.length!=1) {
91             selectServer(null);
92         } else {
93             selectServer(nodes[0]);
94         }
95         }
96     }
97     
98     public void vetoableChange(PropertyChangeEvent JavaDoc evt) throws PropertyVetoException JavaDoc {
99         if (ExplorerManager.PROP_SELECTED_NODES.equals(evt.getPropertyName())) {
100             Node[] nodes = (Node[]) evt.getNewValue();
101             if (nodes.length>1) {
102                 throw new PropertyVetoException JavaDoc("Invalid length",evt); //NOI18N
103
}
104         }
105     }
106     
107     public synchronized ExplorerManager getExplorerManager() {
108         if (this.manager == null) {
109             this.manager = new ExplorerManager();
110             this.manager.setRootContext(new AbstractNode(getChildren()));
111             this.manager.addPropertyChangeListener(this);
112             this.manager.addVetoableChangeListener(this);
113         }
114         return manager;
115     }
116     
117     public void addNotify() {
118         super.addNotify();
119         expandServers(initialInstance);
120     }
121     
122     
123     /** This method is called from within the constructor to
124      * initialize the form.
125      * WARNING: Do NOT modify this code. The content of this method is
126      * always regenerated by the Form Editor.
127      */

128     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
129
private void initComponents() {
130         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
131
132         jPanel3 = new javax.swing.JPanel JavaDoc();
133         servers = new PlatformsView ();
134         addButton = new javax.swing.JButton JavaDoc();
135         removeButton = new javax.swing.JButton JavaDoc();
136         cards = new javax.swing.JPanel JavaDoc();
137         messageArea = new javax.swing.JPanel JavaDoc();
138         jPanel1 = new javax.swing.JPanel JavaDoc();
139         jLabel1 = new javax.swing.JLabel JavaDoc();
140         serverName = new javax.swing.JTextField JavaDoc();
141         jLabel2 = new javax.swing.JLabel JavaDoc();
142         serverType = new javax.swing.JTextField JavaDoc();
143         clientArea = new javax.swing.JPanel JavaDoc();
144         serversLabel = new javax.swing.JLabel JavaDoc();
145
146         setLayout(new java.awt.GridBagLayout JavaDoc());
147
148         servers.setBorder(null);
149         servers.setPreferredSize(new java.awt.Dimension JavaDoc(220, 400));
150         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
151         gridBagConstraints.gridx = 0;
152         gridBagConstraints.gridy = 1;
153         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.RELATIVE;
154         gridBagConstraints.gridheight = java.awt.GridBagConstraints.RELATIVE;
155         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
156         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
157         gridBagConstraints.weighty = 1.0;
158         gridBagConstraints.insets = new java.awt.Insets JavaDoc(3, 12, 6, 6);
159         add(servers, gridBagConstraints);
160         servers.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(ServersCustomizer.class, "ASCN_InstalledServers")); // NOI18N
161
servers.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(ServersCustomizer.class, "ASCD_InstalledServers")); // NOI18N
162

163         org.openide.awt.Mnemonics.setLocalizedText(addButton, NbBundle.getMessage(ServersCustomizer.class, "CTL_AddServer")); // NOI18N
164
addButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
165             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
166                 addServer(evt);
167             }
168         });
169         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
170         gridBagConstraints.gridx = 0;
171         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
172         gridBagConstraints.insets = new java.awt.Insets JavaDoc(6, 12, 12, 0);
173         add(addButton, gridBagConstraints);
174         addButton.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(ServersCustomizer.class, "ASCN_AddServer")); // NOI18N
175
addButton.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(ServersCustomizer.class, "ASCD_AddServer")); // NOI18N
176

177         org.openide.awt.Mnemonics.setLocalizedText(removeButton, NbBundle.getMessage(ServersCustomizer.class, "CTL_Remove")); // NOI18N
178
removeButton.setEnabled(false);
179         removeButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
180             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
181                 removeServer(evt);
182             }
183         });
184         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
185         gridBagConstraints.gridx = 1;
186         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
187         gridBagConstraints.insets = new java.awt.Insets JavaDoc(6, 6, 12, 6);
188         add(removeButton, gridBagConstraints);
189         removeButton.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(ServersCustomizer.class, "ASCN_Remove")); // NOI18N
190
removeButton.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(ServersCustomizer.class, "ASCD_Remove")); // NOI18N
191

192         cards.setLayout(new java.awt.CardLayout JavaDoc());
193
194         messageArea.setLayout(new java.awt.GridBagLayout JavaDoc());
195         cards.add(messageArea, "card3");
196
197         jPanel1.setLayout(new java.awt.GridBagLayout JavaDoc());
198
199         jLabel1.setLabelFor(serverName);
200         org.openide.awt.Mnemonics.setLocalizedText(jLabel1, NbBundle.getMessage(ServersCustomizer.class, "CTL_ServerName")); // NOI18N
201
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
202         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
203         jPanel1.add(jLabel1, gridBagConstraints);
204         jLabel1.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(ServersCustomizer.class, "ASCN_ServerName")); // NOI18N
205
jLabel1.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(ServersCustomizer.class, "ASCD_ServerName")); // NOI18N
206

207         serverName.setEditable(false);
208         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
209         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
210         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
211         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
212         gridBagConstraints.weightx = 1.0;
213         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 6, 0, 0);
214         jPanel1.add(serverName, gridBagConstraints);
215         serverName.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(ServersCustomizer.class, "ASCN_ServerName")); // NOI18N
216
serverName.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(ServersCustomizer.class, "ASCD_ServerName")); // NOI18N
217

218         jLabel2.setLabelFor(serverType);
219         org.openide.awt.Mnemonics.setLocalizedText(jLabel2, NbBundle.getMessage(ServersCustomizer.class, "CTL_ServerType")); // NOI18N
220
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
221         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
222         gridBagConstraints.insets = new java.awt.Insets JavaDoc(6, 0, 12, 0);
223         jPanel1.add(jLabel2, gridBagConstraints);
224         jLabel2.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(ServersCustomizer.class, "ASCN_ServerType")); // NOI18N
225
jLabel2.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(ServersCustomizer.class, "ASCD_Server Type")); // NOI18N
226

227         serverType.setEditable(false);
228         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
229         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
230         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
231         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
232         gridBagConstraints.weightx = 1.0;
233         gridBagConstraints.insets = new java.awt.Insets JavaDoc(6, 6, 12, 0);
234         jPanel1.add(serverType, gridBagConstraints);
235         serverType.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(ServersCustomizer.class, "ASCN_ServerType")); // NOI18N
236
serverType.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(ServersCustomizer.class, "ASCD_Server Type")); // NOI18N
237

238         clientArea.setLayout(new java.awt.GridBagLayout JavaDoc());
239         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
240         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
241         gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
242         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
243         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
244         gridBagConstraints.weightx = 1.0;
245         gridBagConstraints.weighty = 1.0;
246         jPanel1.add(clientArea, gridBagConstraints);
247
248         cards.add(jPanel1, "card2");
249
250         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
251         gridBagConstraints.gridx = 2;
252         gridBagConstraints.gridy = 1;
253         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
254         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
255         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
256         gridBagConstraints.weightx = 1.0;
257         gridBagConstraints.weighty = 1.0;
258         gridBagConstraints.insets = new java.awt.Insets JavaDoc(6, 6, 6, 12);
259         add(cards, gridBagConstraints);
260
261         serversLabel.setLabelFor(servers);
262         org.openide.awt.Mnemonics.setLocalizedText(serversLabel, org.openide.util.NbBundle.getMessage(ServersCustomizer.class, "CTL_Servers")); // NOI18N
263
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
264         gridBagConstraints.gridx = 0;
265         gridBagConstraints.gridy = 0;
266         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
267         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 12, 0, 0);
268         add(serversLabel, gridBagConstraints);
269         serversLabel.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(ServersCustomizer.class, "LBL_Servers")); // NOI18N
270
java.util.ResourceBundle JavaDoc bundle = java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/deployment/impl/ui/Bundle"); // NOI18N
271
serversLabel.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_ServerList")); // NOI18N
272

273         getAccessibleContext().setAccessibleName(bundle.getString("ASCN_ServerManager")); // NOI18N
274
getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(ServersCustomizer.class, "ASCD_ServerCustomizer")); // NOI18N
275
}// </editor-fold>//GEN-END:initComponents
276

277     private void removeServer(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_removeServer
278
Node[] nodes = getExplorerManager().getSelectedNodes();
279         if (nodes.length!=1) {
280             assert false : "Illegal number of selected nodes"; //NOI18N
281
return;
282         }
283         if (nodes[0] instanceof ServerNode) {
284             ServerInstance serverInstance = ((ServerNode)nodes[0]).getServerInstance();
285             if (!serverInstance.isRemoveForbidden()) {
286                 ServerRegistry.getInstance().removeServerInstance(serverInstance.getUrl());
287                 getChildren().refreshServers();
288                 expandServers(null);
289             }
290         }
291     }//GEN-LAST:event_removeServer
292

293     private void addServer(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_addServer
294
AddServerInstanceWizard wizard = new AddServerInstanceWizard();
295         final Dialog JavaDoc dialog = DialogDisplayer.getDefault().createDialog(wizard);
296         dialog.getAccessibleContext().setAccessibleDescription(
297                 NbBundle.getMessage(ServersCustomizer.class, "ACSD_Add_Server_Instance"));
298         dialog.setVisible(true);
299         if (wizard.getValue() == WizardDescriptor.FINISH_OPTION) {
300             getChildren().refreshServers();
301             ServerInstance servInst = null;
302             // PENDING : AddServerInstanceWizard.getInstantiatedObjects is expected to
303
// return set of InstanceProperties instances - this should be ensured
304
Set JavaDoc result = wizard.getInstantiatedObjects();
305             if (result != null) {
306                 for (Iterator JavaDoc i = result.iterator(); i.hasNext();) {
307                     Object JavaDoc instObj = i.next();
308                     if (instObj instanceof InstanceProperties) {
309                         InstanceProperties ip = (InstanceProperties)instObj;
310                         String JavaDoc url = ip.getProperty(InstanceProperties.URL_ATTR);
311                         servInst = ServerRegistry.getInstance().getServerInstance(url);
312                     }
313                 }
314             }
315             expandServers(servInst);
316         }
317     }//GEN-LAST:event_addServer
318

319     
320         private synchronized ServerCategoriesChildren getChildren() {
321             if (this.children == null) {
322                 this.children = new ServerCategoriesChildren();
323             }
324             return this.children;
325         }
326     
327         private void selectServer(Node aNode) {
328             clientArea.removeAll();
329
330             if (aNode instanceof ServerNode) {
331                 ServerInstance serverInstance = ((ServerNode)aNode).getServerInstance();
332                 serverName.setText(serverInstance.getDisplayName());
333                 serverType.setText(serverInstance.getServer().getDisplayName());
334                 if (serverInstance.isRemoveForbidden()) {
335                     removeButton.setEnabled(false);
336                 } else {
337                     removeButton.setEnabled(true);
338                 }
339             } else {
340                 removeButton.setEnabled(false);
341                 ((CardLayout JavaDoc)cards.getLayout()).first(cards);
342                 return;
343             }
344             
345             if (aNode.hasCustomizer()) {
346                 Component JavaDoc component = aNode.getCustomizer();
347                 if (component != null) {
348                     addComponent(clientArea, component);
349                 }
350             }
351             // handle the correct window size
352
int height = getHeight();
353             int width = getWidth();
354             // reset the preferred size so that it can be computed during revalidation
355
setPreferredSize(null);
356             revalidate();
357             // now we have the new computed preferred size
358
Dimension JavaDoc prefSize = getPreferredSize();
359             int prefWidth = (int)(prefSize.getWidth() > MINIMUM_SIZE.getWidth() ? prefSize.getWidth() : MINIMUM_SIZE.getWidth());
360             int prefHeight = (int)(prefSize.getHeight() > MINIMUM_SIZE.getHeight() ? prefSize.getHeight() : MINIMUM_SIZE.getHeight());
361             // do we need to resize the manager window?
362
if (prefHeight > height || prefWidth > width) {
363                 setPreferredSize(new Dimension JavaDoc(prefWidth > width ? prefWidth : width,
364                                                prefHeight > height ? prefHeight : height));
365                 // repack the parent window
366
for (Container JavaDoc parent = getParent(); parent != null; parent = parent.getParent()) {
367                     if (parent instanceof Window JavaDoc) {
368                        ((Window JavaDoc)parent).pack();
369                        break;
370                     }
371                 }
372             }
373             CardLayout JavaDoc cl = (CardLayout JavaDoc)cards.getLayout();
374             cl.last(cards);
375         }
376     
377     private static void addComponent(Container JavaDoc container, Component JavaDoc component) {
378         GridBagConstraints JavaDoc c = new GridBagConstraints JavaDoc();
379         c.gridx = c.gridy = GridBagConstraints.RELATIVE;
380         c.gridheight = c.gridwidth = GridBagConstraints.REMAINDER;
381         c.fill = GridBagConstraints.BOTH;
382         c.anchor = GridBagConstraints.NORTHWEST;
383         c.weightx = c.weighty = 1.0;
384         ((GridBagLayout JavaDoc)container.getLayout()).setConstraints(component,c);
385         container.add(component);
386     }
387     
388     private void expandServers(ServerInstance servInst) {
389         ExplorerManager mgr = this.getExplorerManager();
390         Node node = mgr.getRootContext();
391         expandAllNodes(servers, node, mgr, servInst);
392     }
393     
394     private static void expandAllNodes(BeanTreeView btv, Node node, ExplorerManager mgr, ServerInstance servInst) {
395         btv.expandNode(node);
396         Children ch = node.getChildren();
397         
398         // preselect node for the specified server instance
399
if (servInst != null && ch == Children.LEAF && node instanceof ServerNode) {
400             try {
401                 if (((ServerNode)node).getServerInstance() == servInst) {
402                     mgr.setSelectedNodes(new Node[] {node});
403                 }
404             } catch (PropertyVetoException JavaDoc e) {
405                 //Ignore it
406
}
407         }
408         
409         // preselect first server
410
if (servInst == null && ch == Children.LEAF && mgr.getSelectedNodes().length == 0) {
411             try {
412                 mgr.setSelectedNodes(new Node[] {node});
413             } catch (PropertyVetoException JavaDoc e) {
414                 //Ignore it
415
}
416         }
417         Node nodes[] = ch.getNodes( true );
418         for ( int i = 0; i < nodes.length; i++ ) {
419             expandAllNodes( btv, nodes[i], mgr, servInst);
420         }
421     }
422     
423     
424     // Variables declaration - do not modify//GEN-BEGIN:variables
425
private javax.swing.JButton JavaDoc addButton;
426     private javax.swing.JPanel JavaDoc cards;
427     private javax.swing.JPanel JavaDoc clientArea;
428     private javax.swing.JLabel JavaDoc jLabel1;
429     private javax.swing.JLabel JavaDoc jLabel2;
430     private javax.swing.JPanel JavaDoc jPanel1;
431     private javax.swing.JPanel JavaDoc jPanel3;
432     private javax.swing.JPanel JavaDoc messageArea;
433     private javax.swing.JButton JavaDoc removeButton;
434     private javax.swing.JTextField JavaDoc serverName;
435     private javax.swing.JTextField JavaDoc serverType;
436     private org.openide.explorer.view.BeanTreeView servers;
437     private javax.swing.JLabel JavaDoc serversLabel;
438     // End of variables declaration//GEN-END:variables
439

440     
441     private static class PlatformsView extends BeanTreeView {
442         
443         public PlatformsView() {
444             super();
445             this.setPopupAllowed(false);
446             this.setDefaultActionAllowed(false);
447             this.setRootVisible(false);
448             this.tree.setEditable(false);
449             this.tree.setShowsRootHandles(false);
450             this.setBorder(UIManager.getBorder("Nb.ScrollPane.border")); // NOI18N
451
}
452         
453     }
454     
455     private static class ServerCategoriesDescriptor implements Comparable JavaDoc {
456         private final String JavaDoc categoryName;
457         private final List JavaDoc/*<Node>*/ servers;
458
459         public ServerCategoriesDescriptor(String JavaDoc categoryName) {
460             assert categoryName != null;
461             this.categoryName = categoryName;
462             this.servers = new ArrayList JavaDoc();
463         }
464
465         public String JavaDoc getName() {
466             return categoryName;
467         }
468
469         public List JavaDoc getServers() {
470             Collections.sort(servers);
471             return Collections.unmodifiableList(servers);
472         }
473
474         public void add(Node node) {
475             servers.add(node);
476         }
477
478         public int hashCode() {
479             return categoryName.hashCode();
480         }
481
482         public boolean equals(Object JavaDoc other) {
483             if (other instanceof ServerCategoriesDescriptor) {
484                 ServerCategoriesDescriptor desc = (ServerCategoriesDescriptor) other;
485                 return categoryName.equals(desc.categoryName) &&
486                         servers.size() == desc.servers.size();
487             }
488             return false;
489         }
490
491         public int compareTo(Object JavaDoc other) {
492             if (!(other instanceof ServerCategoriesDescriptor )) {
493                 throw new IllegalArgumentException JavaDoc();
494             }
495             ServerCategoriesDescriptor desc = (ServerCategoriesDescriptor) other;
496             return categoryName.compareTo(desc.categoryName);
497         }
498
499     }
500
501     private static class ServersChildren extends Children.Keys {
502
503         private List JavaDoc servers;
504
505         public ServersChildren (List JavaDoc/*<Node>*/ servers) {
506             this.servers = servers;
507         }
508
509         protected void addNotify() {
510             super.addNotify();
511             this.setKeys (this.servers);
512         }
513
514         protected void removeNotify() {
515             super.removeNotify();
516             this.setKeys(new Object JavaDoc[0]);
517         }
518
519         protected Node[] createNodes(Object JavaDoc key) {
520             return new Node[] {(Node) key};
521         }
522     }
523     
524     private static class ServerNode extends FilterNode implements Comparable JavaDoc {
525         
526         private final ServerInstance serverInstance;
527         
528         public ServerNode(ServerInstance serverInstance) {
529             super(serverInstance.getServer().getRegistryNodeFactory().getManagerNode(RegistryNodeProvider.createLookup(serverInstance)));
530             disableDelegation(DELEGATE_GET_DISPLAY_NAME | DELEGATE_SET_DISPLAY_NAME |
531                     DELEGATE_GET_NAME | DELEGATE_SET_NAME);
532             this.serverInstance = serverInstance;
533             setChildren(Children.LEAF);
534             setDisplayName(serverInstance.getDisplayName());
535             setName(serverInstance.getUrl());
536         }
537         
538         public ServerInstance getServerInstance() {
539             return serverInstance;
540         }
541         
542         public int compareTo(Object JavaDoc other) {
543             if (!(other instanceof ServerNode)) {
544                 throw new IllegalArgumentException JavaDoc();
545             }
546             return serverInstance.compareTo(((ServerNode)other).serverInstance);
547         }
548     }
549     
550     private static class ServerCategoryNode extends AbstractNode {
551
552         private final ServerCategoriesDescriptor desc;
553         private Node iconDelegate;
554
555         public ServerCategoryNode (ServerCategoriesDescriptor desc) {
556             super (new ServersChildren (desc.getServers()));
557             this.desc = desc;
558             this.iconDelegate = DataFolder.findFolder (Repository.getDefault().getDefaultFileSystem().getRoot()).getNodeDelegate();
559         }
560
561         public String JavaDoc getDisplayName () {
562             return desc.getName();
563         }
564
565         public Image JavaDoc getIcon(int type) {
566             return iconDelegate.getIcon(type);
567         }
568
569         public Image JavaDoc getOpenedIcon(int type) {
570             return iconDelegate.getOpenedIcon(type);
571         }
572     }
573
574     private static class ServerCategoriesChildren extends Children.Keys {
575
576         protected void addNotify () {
577             super.addNotify ();
578             this.refreshServers();
579         }
580
581         protected void removeNotify () {
582             super.removeNotify ();
583         }
584
585         protected Node[] createNodes(Object JavaDoc key) {
586             if (key instanceof ServerCategoriesDescriptor) {
587                 ServerCategoriesDescriptor desc = (ServerCategoriesDescriptor) key;
588                 return new Node[] {
589                     new ServerCategoryNode (desc)
590                 };
591             }
592             else if (key instanceof Node) {
593                 return new Node[] {
594                     new FilterNode ((Node)key,Children.LEAF)
595                 };
596             }
597             else {
598                 return new Node[0];
599             }
600         }
601
602         private void refreshServers() {
603             Collection JavaDoc servInstances = ServerRegistry.getInstance().getInstances();
604             HashMap JavaDoc/*<String,ServerCategoriesDescriptor>*/ categories = new HashMap JavaDoc();
605             
606             // currently we have only j2eeServers category
607
final String JavaDoc J2EE_SERVERS_CATEGORY = NbBundle.getMessage(ServersCustomizer.class, "LBL_J2eeServersNode"); // NOI18N
608
ServerCategoriesDescriptor j2eeServers = new ServerCategoriesDescriptor(J2EE_SERVERS_CATEGORY);
609             for(Iterator JavaDoc it = servInstances.iterator(); it.hasNext();) {
610                 ServerInstance serverInstance = (ServerInstance)it.next();
611                 j2eeServers.add(new ServerNode(serverInstance));
612             }
613             categories.put(J2EE_SERVERS_CATEGORY, j2eeServers);
614             List JavaDoc keys = new ArrayList JavaDoc(categories.values());
615             Collections.sort(keys);
616             this.setKeys(keys);
617         }
618     }
619 }
620
621
Popular Tags