001 /*
002 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003 *
004 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005 *
006 * The contents of this file are subject to the terms of either the GNU
007 * General Public License Version 2 only ("GPL") or the Common
008 * Development and Distribution License("CDDL") (collectively, the
009 * "License"). You may not use this file except in compliance with the
010 * License. You can obtain a copy of the License at
011 * http://www.netbeans.org/cddl-gplv2.html
012 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013 * specific language governing permissions and limitations under the
014 * License. When distributing the software, include this License Header
015 * Notice in each file and include the License file at
016 * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017 * particular file as subject to the "Classpath" exception as provided
018 * by Sun in the GPL Version 2 section of the License file that
019 * accompanied this code. If applicable, add the following below the
020 * License Header, with the fields enclosed by brackets [] replaced by
021 * your own identifying information:
022 * "Portions Copyrighted [year] [name of copyright owner]"
023 *
024 * Contributor(s):
025 *
026 * The Original Software is NetBeans. The Initial Developer of the Original
027 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028 * Microsystems, Inc. All Rights Reserved.
029 *
030 * If you wish your version of this file to be governed by only the CDDL
031 * or only the GPL Version 2, indicate your decision by adding
032 * "[Contributor] elects to include this software in this distribution
033 * under the [CDDL or GPL Version 2] license." If you do not indicate a
034 * single choice of license, a recipient has the option to distribute
035 * your version of this file under either the CDDL, the GPL Version 2 or
036 * to extend the choice of license to its licensees as provided above.
037 * However, if you add GPL Version 2 code and therefore, elected the GPL
038 * Version 2 license, then the option applies only if the new code is
039 * made subject to such option by the copyright holder.
040 */
041
042 package org.netbeans.modules.j2ee.deployment.impl.ui;
043
044 import org.netbeans.modules.j2ee.deployment.impl.ServerInstance;
045 import org.netbeans.modules.j2ee.deployment.impl.ServerTarget;
046 import org.netbeans.modules.j2ee.deployment.plugins.api.InstanceProperties;
047 import org.openide.nodes.Node;
048 import java.util.List;
049 import java.util.Arrays;
050 import java.util.ArrayList;
051 import org.openide.nodes.AbstractNode;
052 import org.openide.util.NbBundle;
053 import org.openide.util.RequestProcessor;
054
055 /**
056 * A node for an admin instance that is also a target server. Manager and target
057 * nodes are merged into one.
058 *
059 * @author nn136682
060 */
061 public class InstanceTargetXNode extends FilterXNode implements
062 ServerInstance.StateListener {
063
064 private ServerTarget instanceTarget;
065 private ServerInstance instance;
066 private InstanceTargetChildren instanceTargetChildren;
067
068 public InstanceTargetXNode(Node instanceNode,
069 ServerInstance instance) {
070 this (instanceNode, Node.EMPTY, new InstanceTargetChildren(
071 Node.EMPTY));
072 this .instance = instance;
073 instance.addStateListener(this );
074 }
075
076 private InstanceTargetXNode(Node instanceNode, Node xnode,
077 InstanceTargetChildren instanceTargetChildren) {
078 super (instanceNode, xnode, true, instanceTargetChildren);
079 this .instanceTargetChildren = instanceTargetChildren;
080 }
081
082 private ServerTarget getServerTarget() {
083 if (instanceTarget != null) {
084 return instanceTarget;
085 }
086 instanceTarget = instance.getCoTarget();
087 return instanceTarget;
088 }
089
090 public Node getDelegateTargetNode() {
091 Node xnode = getXNode();
092 if (xnode != null && xnode != Node.EMPTY)
093 return xnode;
094 ServerTarget st = getServerTarget();
095 if (st == null)
096 return xnode;
097 Node tn = instance.getServer().getNodeProvider()
098 .createTargetNode(st);
099 if (tn != null)
100 xnode = tn;
101 return xnode;
102 }
103
104 private void resetDelegateTargetNode() {
105 setXNode(null);
106 }
107
108 public javax.swing.Action[] getActions(boolean context) {
109 List actions = new ArrayList();
110 actions
111 .addAll(Arrays
112 .asList(getOriginal().getActions(context)));
113 /*Boolean isRunning = instance.checkRunning();
114 if (isRunning != null && isRunning.booleanValue()) {*/
115 if (getServerTarget() != null) {
116 actions.addAll(Arrays.asList(getDelegateTargetNode()
117 .getActions(context)));
118 }
119
120 return (javax.swing.Action[]) actions
121 .toArray(new javax.swing.Action[actions.size()]);
122 }
123
124 public PropertySet[] getPropertySets() {
125 Node delegateNode = getDelegateTargetNode();
126 if (delegateNode == null)
127 return getOriginal().getPropertySets();
128 return FilterXNode.merge(getOriginal().getPropertySets(),
129 delegateNode.getPropertySets());
130 }
131
132 public org.openide.nodes.Node.Cookie getCookie(Class type) {
133 Node tn = getDelegateTargetNode();
134 org.openide.nodes.Node.Cookie c = null;
135 if (tn != null)
136 c = tn.getCookie(type);
137 if (c == null)
138 c = super .getCookie(type);
139 return c;
140 }
141
142 // StateListener implementation -------------------------------------------
143
144 public void stateChanged(int oldState, int newState) {
145 if (newState == ServerInstance.STATE_RUNNING
146 || newState == ServerInstance.STATE_DEBUGGING
147 || newState == ServerInstance.STATE_PROFILING) {
148 if (oldState == ServerInstance.STATE_SUSPENDED) {
149 // it looks like the server is being debugged right now, show the
150 // cached nodes rather than trying to retrieve new ones
151 instanceTargetChildren.showLastNodes();
152 getChildren().getNodes(true); // this will make the nodes expand
153 } else {
154 instanceTarget = null;
155 resetDelegateTargetNode();
156 instanceTargetChildren.hideNodes();
157 setChildren(instanceTargetChildren);
158 instanceTargetChildren.updateNodes(this );
159 getChildren().getNodes(true); // this will make the nodes expand
160 }
161 } else {
162 instanceTargetChildren.hideNodes();
163 }
164 }
165
166 public static class InstanceTargetChildren extends Children {
167
168 private Node lastDelegateTargetNode;
169
170 public InstanceTargetChildren(Node original) {
171 super (original);
172 }
173
174 public void updateNodes(final InstanceTargetXNode parent) {
175 //if (isFurtherExpandable()) {
176 if (original == Node.EMPTY) {
177 changeOriginal(createWaitNode());
178 RequestProcessor.getDefault().post(new Runnable() {
179 public void run() {
180 Node newOriginal = null;
181 if (parent != null) {
182 newOriginal = parent
183 .getDelegateTargetNode();
184 lastDelegateTargetNode = newOriginal;
185 }
186 if (newOriginal != null) {
187 changeOriginal(newOriginal);
188 } else {
189 changeOriginal(Node.EMPTY);
190 }
191 }
192 });
193 }
194 // } else {
195 // changeOriginal(Node.EMPTY);
196 // }
197 }
198
199 public void hideNodes() {
200 changeOriginal(Node.EMPTY);
201 }
202
203 public void showLastNodes() {
204 Node node = lastDelegateTargetNode;
205 if (node != null) {
206 changeOriginal(node);
207 }
208 }
209
210 private Node createWaitNode() {
211 AbstractNode node = new AbstractNode(Children.LEAF);
212 node.setName(NbBundle.getMessage(InstanceTargetXNode.class,
213 "LBL_WaitNode_DisplayName"));
214 node
215 .setIconBaseWithExtension("org/netbeans/modules/j2ee/deployment/impl/ui/resources/wait.gif"); // NOI18N
216
217 Children.Array children = new Children.Array();
218 children.add(new Node[] { node });
219 return new AbstractNode(children);
220 }
221
222 // private boolean isFurtherExpandable() {
223 // ServerRegistryNode root = ServerRegistryNode.getServerRegistryNode();
224 // if (root != null) {
225 // return root.isExpandablePassTargetNode();
226 // }
227 // return true;
228 // }
229 }
230 }
|