1 19 20 package org.netbeans.modules.loadgenerator.nodes; 21 22 import java.awt.Image ; 23 import java.util.ArrayList ; 24 import java.util.Arrays ; 25 import java.util.Collection ; 26 import java.util.Collections ; 27 import java.util.HashSet ; 28 import java.util.Iterator ; 29 import java.util.Set ; 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 47 public class ManagerNode extends AbstractNode implements LookupListener { 48 private Lookup.Result lookup; 49 50 private static ManagerNode instance = null; 51 52 55 private ManagerNode(final Children children) { 56 super(children); 57 } 58 59 synchronized public static final ManagerNode getInstance() { 60 if (instance == null) { 78 ManagerChildren children = new ManagerChildren(); 79 children.setEngines(new ArrayList <Engine>()); 80 instance = new ManagerNode(children); 81 } 82 return instance; 83 } 84 85 @Override 86 public String getName() { 87 return NbBundle.getMessage(ManagerNode.class, "ManagerNode_ID"); } 89 90 @Override 91 public String getDisplayName() { 92 return NbBundle.getMessage(ManagerNode.class, "ManagerNode_Title"); } 94 95 @Override 96 public Image getOpenedIcon(int i) { 97 return getIcon(i); 98 } 99 100 @Override 101 public synchronized Image getIcon(int i) { 102 return Utilities.loadImage(NbBundle.getMessage(this.getClass(), "ManagerNode_Icon")); } 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 <Engine> removedEngines = new HashSet <Engine>(); 133 Set <Engine> addedEngines = new HashSet <Engine>(); 134 135 Collection <Node> currentNodes = new ArrayList <Node>(Arrays.asList(getChildren().getNodes())); 136 Collection <Engine> currentEngines = new ArrayList <Engine>(currentNodes.size()); 137 for(Node node : currentNodes) { 138 currentEngines.add(((EngineNode)node).getProvider()); 139 } 140 141 Collection <? extends Lookup.Item<Engine>> newItems = result.allItems(); 142 Collection <Engine> newEngines = new ArrayList <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 <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 |