001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.server.ui.node;
043:
044: import java.io.Serializable;
045: import java.util.ArrayList;
046: import java.util.Collections;
047: import java.util.Comparator;
048: import java.util.List;
049: import javax.swing.Action;
050: import javax.swing.event.ChangeEvent;
051: import javax.swing.event.ChangeListener;
052: import org.netbeans.api.server.ServerInstance;
053: import org.netbeans.modules.server.ServerRegistry;
054: import org.netbeans.spi.server.ServerInstanceProvider;
055: import org.openide.nodes.AbstractNode;
056: import org.openide.nodes.Children;
057: import org.openide.nodes.Node;
058: import org.openide.util.NbBundle;
059: import org.openide.util.WeakListeners;
060: import org.openide.util.actions.SystemAction;
061:
062: public final class RootNode extends AbstractNode {
063:
064: private static final String SERVERS_ICON = "org/netbeans/modules/server/ui/resources/servers.png"; // NOI18N
065:
066: private static RootNode node;
067:
068: private RootNode(ChildFactory factory) {
069: super (Children.create(factory, true));
070:
071: setName(""); // NOI18N
072: setDisplayName(NbBundle.getMessage(RootNode.class,
073: "Server_Registry_Node_Name"));
074: setShortDescription(NbBundle.getMessage(RootNode.class,
075: "Server_Registry_Node_Short_Description"));
076: setIconBaseWithExtension(SERVERS_ICON);
077: }
078:
079: public static synchronized RootNode getInstance() {
080: if (node == null) {
081: ChildFactory factory = new ChildFactory();
082: factory.init();
083:
084: node = new RootNode(factory);
085: }
086: return node;
087: }
088:
089: @Override
090: public Action[] getActions(boolean context) {
091: return new SystemAction[] { SystemAction
092: .get(AddServerInstanceAction.class) };
093: }
094:
095: private static class ChildFactory extends
096: org.openide.nodes.ChildFactory<ServerInstance> implements
097: ChangeListener {
098:
099: private static final Comparator<ServerInstance> COMPARATOR = new InstanceComparator();
100:
101: /** <i>GuardedBy("this")</i> */
102: private final List<ServerInstanceProvider> types = new ArrayList<ServerInstanceProvider>();
103:
104: public ChildFactory() {
105: super ();
106: }
107:
108: public synchronized void init() {
109: final ServerRegistry registry = ServerRegistry
110: .getInstance();
111:
112: registry.addChangeListener(WeakListeners.create(
113: ChangeListener.class, this , registry));
114: stateChanged(new ChangeEvent(registry));
115: }
116:
117: public synchronized void stateChanged(ChangeEvent e) {
118: if (e.getSource() instanceof ServerRegistry) {
119: for (ServerInstanceProvider type : types) {
120: type.removeChangeListener(this );
121: }
122:
123: types.clear();
124: types.addAll(((ServerRegistry) e.getSource())
125: .getProviders());
126: for (ServerInstanceProvider type : types) {
127: type.addChangeListener(this );
128: }
129: }
130: refresh();
131: }
132:
133: protected final void refresh() {
134: refresh(false);
135: }
136:
137: @Override
138: protected Node createNodeForKey(ServerInstance key) {
139: return key.getFullNode();
140: }
141:
142: @Override
143: protected boolean createKeys(List<ServerInstance> toPopulate) {
144: List<ServerInstance> fresh = new ArrayList<ServerInstance>();
145:
146: ServerRegistry registry = ServerRegistry.getInstance();
147: for (ServerInstanceProvider type : registry.getProviders()) {
148: fresh.addAll(type.getInstances());
149: }
150:
151: Collections.sort(fresh, COMPARATOR);
152:
153: toPopulate.addAll(fresh);
154: return true;
155: }
156:
157: }
158:
159: private static class InstanceComparator implements
160: Comparator<ServerInstance>, Serializable {
161:
162: public int compare(ServerInstance o1, ServerInstance o2) {
163: return o1.getDisplayName().compareTo(o2.getDisplayName());
164: }
165:
166: }
167: }
|