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-2007 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.web.monitor.client;
043:
044: import org.openide.nodes.Children;
045: import org.openide.util.actions.*;
046: import org.openide.nodes.AbstractNode;
047: import org.openide.util.actions.SystemAction;
048: import org.openide.util.Utilities;
049:
050: import java.util.StringTokenizer;
051: import java.text.DateFormat;
052: import java.util.Date;
053: import java.awt.Image;
054: import org.netbeans.modules.web.monitor.data.Constants;
055:
056: public class TransactionNode extends AbstractNode {
057:
058: String id, method, uri, name = null, timestamp = null;
059: boolean current;
060: private int statusCode;
061: static boolean showTimeStamp = true;
062:
063: public TransactionNode(String id, String method, String uri,
064: boolean current, int statusCode) {
065:
066: super (Children.LEAF);
067:
068: this .id = id;
069: this .method = method;
070: this .uri = uri;
071: this .current = current;
072: this .statusCode = statusCode;
073:
074: setProperties();
075: }
076:
077: public TransactionNode(String id, String method, String uri,
078: Children ch, boolean current, int statusCode) {
079:
080: super (ch);
081:
082: this .id = id;
083: this .method = method;
084: this .uri = uri;
085: this .current = current;
086: this .statusCode = statusCode;
087:
088: setProperties();
089: }
090:
091: // This method is incomplete, URI may need to be truncated...
092: public String getLongName() {
093:
094: StringBuffer buf = new StringBuffer(method);
095: buf.append(" "); //NOI18N
096: buf.append(uri);
097: if (timestamp == null)
098: setTimeStamp();
099: buf.append(" "); //NOI18N
100: buf.append(timestamp);
101:
102: return buf.toString();
103: }
104:
105: public Image getIcon(int type) {
106: Image base;
107: // Get icon
108: if (method.equals(Constants.Http.GET)) {
109: base = Utilities
110: .loadImage("org/netbeans/modules/web/monitor/client/icons/get.gif");
111: // Post icon
112: } else if (method.equals(Constants.Http.POST)) {
113: base = Utilities
114: .loadImage("org/netbeans/modules/web/monitor/client/icons/post.gif"); // NOI18N
115: // Other
116: } else {
117: base = Utilities
118: .loadImage("org/netbeans/modules/web/monitor/client/icons/other.gif"); // NOI18N
119: }
120:
121: Image badge;
122: if (statusCode >= 400 || statusCode < 0) {
123: badge = Utilities
124: .loadImage("org/netbeans/modules/web/monitor/client/icons/infoBadge.gif"); // NOI18N
125: } else if (statusCode >= 300) {
126: badge = Utilities
127: .loadImage("org/netbeans/modules/web/monitor/client/icons/warningBadge.gif"); // NOI18N
128: } else if (statusCode >= 200) {
129: return base;
130: } else {
131: badge = Utilities
132: .loadImage("org/netbeans/modules/web/monitor/client/icons/errorBadge.gif"); // NOI18N
133: }
134: return Utilities.mergeImages(base, badge, 0, 0);
135: }
136:
137: public Image getOpenedIcon(int type) {
138: return getIcon(type);
139: }
140:
141: public String getID() {
142: return id;
143: }
144:
145: public String getMethod() {
146: return method;
147: }
148:
149: public String getURI() {
150: return uri;
151: }
152:
153: public boolean isCurrent() {
154: return current;
155: }
156:
157: public void setCurrent(boolean b) {
158: current = b;
159: }
160:
161: /* Getter for set of actions that should be present in the
162: * popup menu of this node. This set is used in construction of
163: * menu returned from getContextMenu and specially when a menu for
164: * more nodes is constructed.
165: *
166: * @return array of system actions that should be in popup menu
167: */
168:
169: protected SystemAction[] createActions() {
170:
171: if (current) {
172: return new SystemAction[] {
173: SystemAction.get(SaveAction.class), null,
174: SystemAction.get(ReplayAction.class),
175: SystemAction.get(EditReplayAction.class), null,
176: SystemAction.get(DeleteAction.class) };
177: }
178:
179: return new SystemAction[] {
180: SystemAction.get(ReplayAction.class),
181: SystemAction.get(EditReplayAction.class), null,
182: SystemAction.get(DeleteAction.class), };
183: }
184:
185: public SystemAction[] getActions() {
186:
187: if (current) {
188: return new SystemAction[] {
189: SystemAction.get(SaveAction.class), null,
190: SystemAction.get(ReplayAction.class),
191: SystemAction.get(EditReplayAction.class), null,
192: SystemAction.get(DeleteAction.class) };
193: }
194:
195: return new SystemAction[] {
196: SystemAction.get(ReplayAction.class),
197: SystemAction.get(EditReplayAction.class), null,
198: SystemAction.get(DeleteAction.class), };
199: }
200:
201: /** Can this node be copied?
202: * @return <code>true</code> in the default implementation
203: */
204: public boolean canCopy() {
205: return true;
206: }
207:
208: /** Can this node be cut?
209: * @return <code>false</code> in the default implementation
210: */
211: public boolean canCut() {
212: return false;
213: }
214:
215: /**
216: * Set whether the timestamp is shown or not
217: */
218: public static void toggleTimeStamp() {
219: if (showTimeStamp)
220: showTimeStamp = false;
221: else
222: showTimeStamp = true;
223: }
224:
225: /**
226: * Is the timestamp showing
227: */
228: public static boolean showTimeStamp() {
229: return showTimeStamp;
230: }
231:
232: private void setProperties() {
233: setNameString();
234: setShortDescription(uri);
235: }
236:
237: public void setNameString() {
238:
239: String name = null;
240: if (uri.equals("/"))
241: name = uri; //NOI18N
242: else {
243: StringTokenizer st = new StringTokenizer(uri, "/"); //NOI18N
244: while (st.hasMoreTokens())
245: name = st.nextToken();
246: }
247:
248: StringBuilder buf = new StringBuilder(method);
249: buf.append(' '); //NOI18N
250: buf.append(name);
251: if (showTimeStamp) {
252: if (timestamp == null)
253: setTimeStamp();
254: buf.append(' '); //NOI18N
255: buf.append(timestamp);
256: }
257: setName(buf.toString());
258: }
259:
260: private void setTimeStamp() {
261:
262: try {
263: long ldate = Long.valueOf(id).longValue();
264: Date date = new Date(ldate);
265:
266: StringBuilder buf = new StringBuilder('['); //NOI18N
267: DateFormat df = DateFormat
268: .getTimeInstance(DateFormat.SHORT);
269: buf.append(df.format(date));
270: buf.append(" "); //NOI18N
271: df = DateFormat.getDateInstance(DateFormat.SHORT);
272: buf.append(df.format(date));
273: buf.append(']'); //NOI18N
274: timestamp = buf.toString();
275: } catch (Exception e) {
276: }
277: }
278:
279: public String toString() {
280: StringBuilder buf = new StringBuilder("TransactionNode: "); //NOI18N
281: buf.append(this .getName());
282: buf.append('\n'); //NOI18N
283: buf.append("id="); //NOI18N
284: buf.append(id);
285:
286: buf.append('\n'); //NOI18N
287: buf.append("method="); //NOI18N
288: buf.append(method);
289:
290: buf.append('\n'); //NOI18N
291: buf.append("uri="); //NOI18N
292: buf.append(uri);
293:
294: buf.append('\n'); //NOI18N
295: buf.append("current="); //NOI18N
296: buf.append(String.valueOf(current));
297: buf.append('\n'); //NOI18N
298:
299: return buf.toString();
300: }
301: } // TransactionNode
|