1 19 20 package org.netbeans.modules.loadgenerator.nodes; 21 22 import java.awt.Image ; 23 import java.beans.PropertyChangeEvent ; 24 import java.beans.PropertyChangeListener ; 25 import java.util.ArrayList ; 26 import java.util.List ; 27 import javax.swing.Action ; 28 import org.netbeans.modules.loadgenerator.actions.LoadAction; 29 import org.netbeans.modules.loadgenerator.actions.RemoveStoppedAction; 30 import org.netbeans.modules.loadgenerator.spi.Engine; 31 import org.netbeans.modules.loadgenerator.spi.ProcessInstance; 32 import org.openide.nodes.AbstractNode; 33 import org.openide.nodes.Children; 34 import org.openide.nodes.Node; 35 import org.openide.util.WeakListeners; 36 37 41 public class EngineNode extends AbstractNode { 42 private final static Action [] ACTIONARRAY = new Action []{}; 43 44 private Engine provider; 45 private List <Action > actions; 46 private String lastDisplayName = null; 47 48 private PropertyChangeListener listener = new PropertyChangeListener () { 49 public void propertyChange(PropertyChangeEvent evt) { 50 if (evt.getPropertyName().equals(Engine.INSTANCE)) { 51 refreshChildren(); 53 } else if (evt.getPropertyName().equals(Engine.STATE)) { 54 fireDisplayNameChange(lastDisplayName, getDisplayName()); 55 lastDisplayName = getDisplayName(); 56 } 57 } 58 }; 59 60 63 private EngineNode(final Children children) { 64 super(children); 65 actions = new ArrayList <Action >(); 66 } 67 68 public static final EngineNode getInstance() { 69 return new EngineNode(new Children.Array()); 70 } 71 72 public void setProvider(final Engine provider) { 73 this.provider = provider; 74 this.provider.addPropertyChangeListener(Engine.INSTANCE, WeakListeners.propertyChange(listener, provider)); 75 actions.add(new LoadAction(this.provider)); 76 actions.add(new RemoveStoppedAction(this.provider)); 77 refreshChildren(); 78 } 79 80 public Engine getProvider() { 81 return this.provider; 82 } 83 84 @Override 85 public String getDisplayName() { 86 return provider.getDisplayName(); 87 } 88 89 @Override 90 public Image getIcon(int i) { 91 Image retValue; 92 93 retValue = provider.getIcon(); 94 return retValue != null ? retValue : super.getIcon(i); 95 } 96 97 @Override 98 public Image getOpenedIcon(int i) { 99 return getIcon(i); 100 } 101 102 @Override 103 public Action [] getActions(boolean b) { 104 return actions.toArray(ACTIONARRAY); 105 } 106 107 private void refreshChildren() { 108 Node[] currentNodes = getChildren().getNodes(); 109 getChildren().remove(currentNodes); 110 111 List <Node> nodes = new ArrayList <Node>(); 112 for(ProcessInstance instance : provider.getProcesses()) { 113 ProcessNode node = ProcessNode.getInstance(); 114 node.setProvider(instance); 115 nodes.add(node); 116 } 117 getChildren().add(nodes.toArray(new Node[]{})); 118 } 119 120 public boolean equals(Object anotherObject) { 121 if (anotherObject instanceof EngineNode) return provider.equals(((EngineNode)anotherObject).provider); 122 if (anotherObject instanceof Engine) return provider.equals(anotherObject); 123 return false; 124 } 125 126 public int hashCode() { 127 return provider.hashCode(); 128 } 129 } 130 | Popular Tags |