KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > loadgenerator > nodes > ManagerNode


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.loadgenerator.nodes;
21
22 import java.awt.Image JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Arrays JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.Collections JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.Set JavaDoc;
30 import org.netbeans.modules.loadgenerator.api.EngineManager;
31 import org.netbeans.modules.loadgenerator.spi.Engine;
32 import org.netbeans.modules.loadgenerator.spi.ProcessInstance;
33 import org.openide.nodes.AbstractNode;
34 import org.openide.nodes.Children;
35 import org.openide.nodes.Node;
36 import org.openide.util.Lookup;
37 import org.openide.util.Lookup.Result;
38 import org.openide.util.LookupEvent;
39 import org.openide.util.LookupListener;
40 import org.openide.util.NbBundle;
41 import org.openide.util.Utilities;
42
43 /**
44  *
45  * @author Jaroslav Bachorik
46  */

47 public class ManagerNode extends AbstractNode implements LookupListener {
48   private Lookup.Result lookup;
49   
50   private static ManagerNode instance = null;
51   
52   /**
53    * Creates a new instance of ManagerNode
54    */

55   private ManagerNode(final Children children) {
56     super(children);
57   }
58   
59   synchronized public static final ManagerNode getInstance() {
60     // EngineManager manager = Lookup.getDefault().lookup(EngineManager.class);
61
// Children children = new Children.Array();
62
// Collection<Node> nodeList = new ArrayList<Node>();
63
//
64
// for(Engine provider : manager.findEngines()) {
65
// EngineNode node = EngineNode.getInstance();
66
// node.setProvider((Engine)provider);
67
// nodeList.add(node);
68
//// final String providerName = provider.getDisplayName();
69
//// nodeList.add(new AbstractNode(Children.LEAF) {
70
//// public String getName() {
71
//// return providerName;
72
//// }
73
////
74
//// });
75
// }
76
// children.add(nodeList.toArray(new Node[nodeList.size()]));
77
if (instance == null) {
78       ManagerChildren children = new ManagerChildren();
79       children.setEngines(new ArrayList JavaDoc<Engine>());
80       instance = new ManagerNode(children);
81     }
82     return instance;
83   }
84   
85   @Override JavaDoc
86   public String JavaDoc getName() {
87     return NbBundle.getMessage(ManagerNode.class, "ManagerNode_ID"); // NOI18N
88
}
89   
90   @Override JavaDoc
91   public String JavaDoc getDisplayName() {
92     return NbBundle.getMessage(ManagerNode.class, "ManagerNode_Title"); // NOI18N
93
}
94   
95   @Override JavaDoc
96   public Image JavaDoc getOpenedIcon(int i) {
97     return getIcon(i);
98   }
99   
100   @Override JavaDoc
101   public synchronized Image JavaDoc getIcon(int i) {
102     return Utilities.loadImage(NbBundle.getMessage(this.getClass(), "ManagerNode_Icon")); // NOI18N
103
}
104   
105   public void setEngineLookup(Lookup.Result<Engine> lookup) {
106     if (this.lookup != null) {
107       this.lookup.removeLookupListener(this);
108     }
109     this.lookup = lookup;
110     if (this.lookup != null) {
111       this.lookup.addLookupListener(this);
112     }
113     refreshChildren(this.lookup);
114   }
115   
116   public void resultChanged(LookupEvent ev) {
117     Result<Engine> result = (Result<Engine>)ev.getSource();
118     refreshChildren(result);
119   }
120   
121   private synchronized void refreshChildren(Lookup.Result<Engine> result) {
122     if (result == null) {
123       setChildren(Children.LEAF);
124     } else {
125       ManagerChildren children = null;
126       if (getChildren() instanceof ManagerChildren) {
127         children = (ManagerChildren)getChildren();
128       } else {
129         children = new ManagerChildren();
130         setChildren(children);
131       }
132       Set JavaDoc<Engine> removedEngines = new HashSet JavaDoc<Engine>();
133       Set JavaDoc<Engine> addedEngines = new HashSet JavaDoc<Engine>();
134       
135       Collection JavaDoc<Node> currentNodes = new ArrayList JavaDoc<Node>(Arrays.asList(getChildren().getNodes()));
136       Collection JavaDoc<Engine> currentEngines = new ArrayList JavaDoc<Engine>(currentNodes.size());
137       for(Node node : currentNodes) {
138         currentEngines.add(((EngineNode)node).getProvider());
139       }
140       
141       Collection JavaDoc<? extends Lookup.Item<Engine>> newItems = result.allItems();
142       Collection JavaDoc<Engine> newEngines = new ArrayList JavaDoc<Engine>(newItems.size());
143       for(Lookup.Item<Engine> item : newItems) {
144         newEngines.add(item.getInstance());
145       }
146       
147       for(Engine engine : newEngines) {
148         if (!currentEngines.contains(engine)) {
149           currentEngines.add(engine);
150         }
151       }
152       for (Iterator JavaDoc<Engine> iter = currentEngines.iterator(); iter.hasNext();) {
153           Engine engine = iter.next();
154
155           if (!newEngines.contains(engine)) {
156               for (ProcessInstance process : engine.getProcesses()) {
157                 process.stop(true);
158               }
159               engine.cleanup();
160               iter.remove();
161           }
162       }
163       
164       children.setEngines(currentEngines);
165     }
166   }
167 }
168
Popular Tags