KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > deployment > impl > ui > InstanceTargetXNode


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.j2ee.deployment.impl.ui;
21
22 import org.netbeans.modules.j2ee.deployment.impl.ServerInstance;
23 import org.netbeans.modules.j2ee.deployment.impl.ServerTarget;
24 import org.netbeans.modules.j2ee.deployment.plugins.api.InstanceProperties;
25 import org.openide.nodes.Node;
26 import java.util.List JavaDoc;
27 import java.util.Arrays JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Collections JavaDoc;
30 import org.openide.nodes.AbstractNode;
31 import org.openide.nodes.Children;
32 import org.openide.util.NbBundle;
33 import org.openide.util.RequestProcessor;
34
35
36 /**
37  * A node for an admin instance that is also a target server. Manager and target
38  * nodes are merged into one.
39  *
40  * @author nn136682
41  */

42 public class InstanceTargetXNode extends FilterXNode implements ServerInstance.StateListener {
43     
44     private ServerTarget instanceTarget;
45     private ServerInstance instance;
46     private InstanceProperties instanceProperties;
47     private InstanceTargetChildren instanceTargetChildren;
48     
49     public InstanceTargetXNode(Node instanceNode, ServerInstance instance) {
50         this(instanceNode, Node.EMPTY, new InstanceTargetChildren(Node.EMPTY));
51         this.instance = instance;
52         instanceProperties = instance.getInstanceProperties();
53         instance.addStateListener(this);
54     }
55     
56     private InstanceTargetXNode(Node instanceNode, Node xnode, InstanceTargetChildren instanceTargetChildren) {
57         super(instanceNode, xnode, true, instanceTargetChildren);
58         this.instanceTargetChildren = instanceTargetChildren;
59     }
60     
61     private ServerTarget getServerTarget() {
62         if (instanceTarget != null) {
63             return instanceTarget;
64         }
65         instanceTarget = instance.getCoTarget();
66         return instanceTarget;
67     }
68     
69     public Node getDelegateTargetNode() {
70         if (xnode != null && xnode != Node.EMPTY)
71             return xnode;
72         ServerTarget st = getServerTarget();
73         if (st == null)
74             return xnode;
75         Node tn = instance.getServer().getNodeProvider().createTargetNode(st);
76         if (tn != null)
77             xnode = tn;
78         return xnode;
79     }
80     
81     private void resetDelegateTargetNode() {
82         xnode = null;
83     }
84     
85     public javax.swing.Action JavaDoc[] getActions(boolean context) {
86         List JavaDoc actions = new ArrayList JavaDoc();
87         actions.addAll(Arrays.asList(getOriginal().getActions(context)));
88         /*Boolean isRunning = instance.checkRunning();
89         if (isRunning != null && isRunning.booleanValue()) {*/

90         if (getServerTarget() != null) {
91             actions.addAll(Arrays.asList(getDelegateTargetNode().getActions(context)));
92         }
93         
94         return (javax.swing.Action JavaDoc[]) actions.toArray(new javax.swing.Action JavaDoc[actions.size()]);
95     }
96     
97     public PropertySet[] getPropertySets() {
98         Node delegateNode = getDelegateTargetNode();
99         if (delegateNode == null)
100             return getOriginal().getPropertySets();
101         return FilterXNode.merge(getOriginal().getPropertySets(), delegateNode.getPropertySets());
102     }
103     
104     public org.openide.nodes.Node.Cookie getCookie(Class JavaDoc type) {
105         Node tn = getDelegateTargetNode();
106         org.openide.nodes.Node.Cookie c = null;
107         if (tn != null)
108             c = tn.getCookie(type);
109         if (c == null)
110             c = super.getCookie(type);
111         return c;
112     }
113     
114     // StateListener implementation -------------------------------------------
115

116     public void stateChanged(int oldState, int newState) {
117         if (newState == ServerInstance.STATE_RUNNING || newState == ServerInstance.STATE_DEBUGGING
118             || newState == ServerInstance.STATE_PROFILING) {
119             if (oldState == ServerInstance.STATE_SUSPENDED) {
120                 // it looks like the server is being debugged right now, show the
121
// cached nodes rather than trying to retrieve new ones
122
instanceTargetChildren.showLastNodes();
123                 getChildren().getNodes(true); // this will make the nodes expand
124
} else {
125                 instanceTarget = null;
126                 resetDelegateTargetNode();
127                 instanceTargetChildren.hideNodes();
128                 setChildren(instanceTargetChildren);
129                 instanceTargetChildren.updateNodes(this);
130                 getChildren().getNodes(true); // this will make the nodes expand
131
}
132         } else {
133             instanceTargetChildren.hideNodes();
134         }
135     }
136     
137     public static class InstanceTargetChildren extends Children {
138         
139         private ServerTarget target;
140         private Node lastDelegateTargetNode;
141         
142         public InstanceTargetChildren(Node original) {
143             super(original);
144         }
145         
146         public void updateNodes(final InstanceTargetXNode parent) {
147             if (isFurtherExpandable()) {
148                 if (original == Node.EMPTY) {
149                     changeOriginal(createWaitNode());
150                     RequestProcessor.getDefault().post(new Runnable JavaDoc() {
151                         public void run() {
152                             Node newOriginal = null;
153                             if (parent != null) {
154                                 newOriginal = parent.getDelegateTargetNode();
155                                 lastDelegateTargetNode = newOriginal;
156                             }
157                             if (newOriginal != null) {
158                                 changeOriginal(newOriginal);
159                             } else {
160                                 changeOriginal(Node.EMPTY);
161                             }
162                         }
163                     });
164                 }
165             } else {
166                 changeOriginal(Node.EMPTY);
167             }
168         }
169         
170         public void hideNodes() {
171             changeOriginal(Node.EMPTY);
172         }
173         
174         public void showLastNodes() {
175             Node node = lastDelegateTargetNode;
176             if (node != null) {
177                 changeOriginal(node);
178             }
179         }
180         
181         private Node createWaitNode() {
182             AbstractNode node = new AbstractNode(Children.LEAF);
183             node.setName(NbBundle.getMessage(InstanceTargetXNode.class, "LBL_WaitNode_DisplayName"));
184             node.setIconBaseWithExtension("org/netbeans/modules/j2ee/deployment/impl/ui/resources/wait.gif"); // NOI18N
185

186             Children.Array children = new Children.Array();
187             children.add(new Node[]{node});
188             return new AbstractNode(children);
189         }
190         
191         private boolean isFurtherExpandable() {
192             ServerRegistryNode root = ServerRegistryNode.getServerRegistryNode();
193             if (root != null) {
194                 return root.isExpandablePassTargetNode();
195             }
196             return true;
197         }
198     }
199 }
200
Popular Tags