KickJava   Java API By Example, From Geeks To Geeks.

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


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.beans.PropertyChangeEvent JavaDoc;
24 import java.beans.PropertyChangeListener JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.List JavaDoc;
27 import javax.swing.Action JavaDoc;
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 /**
38  *
39  * @author Jaroslav Bachorik
40  */

41 public class EngineNode extends AbstractNode {
42   private final static Action JavaDoc[] ACTIONARRAY = new Action JavaDoc[]{};
43   
44   private Engine provider;
45   private List JavaDoc<Action JavaDoc> actions;
46   private String JavaDoc lastDisplayName = null;
47   
48   private PropertyChangeListener JavaDoc listener = new PropertyChangeListener JavaDoc() {
49     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
50       if (evt.getPropertyName().equals(Engine.INSTANCE)) {
51 // System.out.println("LoadGeneratorNode: Received notification about a new instance being created");
52
refreshChildren();
53       } else if (evt.getPropertyName().equals(Engine.STATE)) {
54         fireDisplayNameChange(lastDisplayName, getDisplayName());
55         lastDisplayName = getDisplayName();
56       }
57     }
58   };
59   
60   /**
61    * Creates a new instance of EngineNode
62    */

63   private EngineNode(final Children children) {
64     super(children);
65     actions = new ArrayList JavaDoc<Action JavaDoc>();
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 JavaDoc
85   public String JavaDoc getDisplayName() {
86     return provider.getDisplayName();
87   }
88   
89   @Override JavaDoc
90   public Image JavaDoc getIcon(int i) {
91     Image JavaDoc retValue;
92     
93     retValue = provider.getIcon();
94     return retValue != null ? retValue : super.getIcon(i);
95   }
96   
97   @Override JavaDoc
98   public Image JavaDoc getOpenedIcon(int i) {
99     return getIcon(i);
100   }
101   
102   @Override JavaDoc
103   public Action JavaDoc[] 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 JavaDoc<Node> nodes = new ArrayList JavaDoc<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 JavaDoc 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