KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > monitor > client > TransactionNode


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.web.monitor.client;
21
22 import org.openide.nodes.Children;
23 import org.openide.util.actions.*;
24 import org.openide.nodes.AbstractNode;
25 import org.openide.util.actions.SystemAction;
26 import org.openide.util.Utilities;
27
28 import java.util.StringTokenizer JavaDoc;
29 import java.text.DateFormat JavaDoc;
30 import java.util.Date JavaDoc;
31 import java.awt.Image JavaDoc;
32 import org.netbeans.modules.web.monitor.data.Constants;
33
34 public class TransactionNode extends AbstractNode {
35
36     String JavaDoc id, method, uri, name = null, timestamp = null;
37     boolean current;
38     private int statusCode;
39     static boolean showTimeStamp = true;
40
41     public TransactionNode(String JavaDoc id, String JavaDoc method, String JavaDoc uri,
42                boolean current, int statusCode) {
43     
44     super(Children.LEAF);
45
46     this.id = id;
47     this.method = method;
48     this.uri = uri;
49     this.current = current;
50         this.statusCode = statusCode;
51
52     setProperties();
53     }
54
55     public TransactionNode(String JavaDoc id, String JavaDoc method, String JavaDoc uri,
56                Children ch, boolean current, int statusCode) {
57     
58     super(ch);
59
60     this.id = id;
61     this.method = method;
62     this.uri = uri;
63     this.current = current;
64         this.statusCode = statusCode;
65         
66     setProperties();
67     }
68
69     // This method is incomplete, URI may need to be truncated...
70
public String JavaDoc getLongName() {
71
72     StringBuffer JavaDoc buf = new StringBuffer JavaDoc(method);
73     buf.append(" "); //NOI18N
74
buf.append(uri);
75     if(timestamp == null) setTimeStamp();
76     buf.append(" "); //NOI18N
77
buf.append(timestamp);
78
79     return buf.toString();
80     }
81     
82     public Image JavaDoc getIcon(int type) {
83         Image JavaDoc base;
84     // Get icon
85
if(method.equals(Constants.Http.GET)) {
86             base = Utilities.loadImage("org/netbeans/modules/web/monitor/client/icons/get.gif");
87     // Post icon
88
} else if(method.equals(Constants.Http.POST)) {
89             base = Utilities.loadImage("org/netbeans/modules/web/monitor/client/icons/post.gif"); // NOI18N
90
// Other
91
} else {
92             base = Utilities.loadImage("org/netbeans/modules/web/monitor/client/icons/other.gif"); // NOI18N
93
}
94         
95         Image JavaDoc badge;
96         if (statusCode >= 400 || statusCode < 0) {
97             badge = Utilities.loadImage("org/netbeans/modules/web/monitor/client/icons/infoBadge.gif"); // NOI18N
98
} else if (statusCode >= 300) {
99             badge = Utilities.loadImage("org/netbeans/modules/web/monitor/client/icons/warningBadge.gif"); // NOI18N
100
} else if (statusCode >= 200) {
101             return base;
102         } else {
103             badge = Utilities.loadImage("org/netbeans/modules/web/monitor/client/icons/errorBadge.gif"); // NOI18N
104
}
105         return Utilities.mergeImages(base, badge, 0, 0);
106     }
107     
108     public Image JavaDoc getOpenedIcon(int type) {
109         return getIcon(type);
110     }
111     
112     public String JavaDoc getID() {
113     return id;
114     }
115
116     public String JavaDoc getMethod() {
117     return method;
118     }
119
120     public String JavaDoc getURI() {
121     return uri;
122     }
123
124     public boolean isCurrent() {
125     return current;
126     }
127
128     public void setCurrent(boolean b) {
129     current = b;
130     }
131
132     /* Getter for set of actions that should be present in the
133      * popup menu of this node. This set is used in construction of
134      * menu returned from getContextMenu and specially when a menu for
135      * more nodes is constructed.
136      *
137      * @return array of system actions that should be in popup menu
138      */

139
140     protected SystemAction[] createActions () {
141
142     if(current) {
143         return new SystemAction[] {
144         SystemAction.get(SaveAction.class),
145         null,
146         SystemAction.get(ReplayAction.class),
147         SystemAction.get(EditReplayAction.class),
148         null,
149         SystemAction.get(DeleteAction.class)
150         };
151     }
152      
153     return new SystemAction[] {
154         SystemAction.get(ReplayAction.class),
155         SystemAction.get(EditReplayAction.class),
156         null,
157         SystemAction.get(DeleteAction.class),
158     };
159     }
160
161
162     public SystemAction[] getActions () {
163
164     if(current) {
165         return new SystemAction[] {
166         SystemAction.get(SaveAction.class),
167         null,
168         SystemAction.get(ReplayAction.class),
169         SystemAction.get(EditReplayAction.class),
170         null,
171         SystemAction.get(DeleteAction.class)
172         };
173     }
174      
175     return new SystemAction[] {
176         SystemAction.get(ReplayAction.class),
177         SystemAction.get(EditReplayAction.class),
178         null,
179         SystemAction.get(DeleteAction.class),
180     };
181     }
182
183     /** Can this node be copied?
184      * @return <code>true</code> in the default implementation
185      */

186     public boolean canCopy () {
187     return true;
188     }
189
190     /** Can this node be cut?
191      * @return <code>false</code> in the default implementation
192      */

193     public boolean canCut () {
194     return false;
195     }
196
197     /**
198      * Set whether the timestamp is shown or not
199      */

200     public static void toggleTimeStamp() {
201     if(showTimeStamp) showTimeStamp = false;
202     else showTimeStamp = true;
203     }
204
205     /**
206      * Is the timestamp showing
207      */

208     public static boolean showTimeStamp() {
209     return showTimeStamp;
210     }
211
212     private void setProperties() {
213     setNameString();
214     setShortDescription(uri);
215     }
216     
217     public void setNameString() {
218     
219     String JavaDoc name = null;
220     if(uri.equals("/")) name = uri; //NOI18N
221
else {
222         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(uri,"/"); //NOI18N
223
while(st.hasMoreTokens()) name = st.nextToken();
224     }
225     
226     StringBuilder JavaDoc buf = new StringBuilder JavaDoc(method);
227     buf.append(' '); //NOI18N
228
buf.append(name);
229     if(showTimeStamp) {
230         if(timestamp == null) setTimeStamp();
231         buf.append(' '); //NOI18N
232
buf.append(timestamp);
233     }
234     setName(buf.toString());
235     }
236
237     private void setTimeStamp() {
238     
239     try {
240         long ldate = Long.valueOf(id).longValue();
241         Date JavaDoc date = new Date JavaDoc(ldate);
242
243         StringBuilder JavaDoc buf = new StringBuilder JavaDoc('['); //NOI18N
244
DateFormat JavaDoc df = DateFormat.getTimeInstance(DateFormat.SHORT);
245         buf.append(df.format(date));
246         buf.append(" "); //NOI18N
247
df = DateFormat.getDateInstance(DateFormat.SHORT);
248         buf.append(df.format(date));
249         buf.append(']'); //NOI18N
250
timestamp = buf.toString();
251     }
252     catch(Exception JavaDoc e) {}
253     }
254     
255     public String JavaDoc toString() {
256     StringBuilder JavaDoc buf = new StringBuilder JavaDoc("TransactionNode: "); //NOI18N
257
buf.append(this.getName());
258     buf.append('\n'); //NOI18N
259
buf.append("id="); //NOI18N
260
buf.append(id);
261
262     buf.append('\n'); //NOI18N
263
buf.append("method="); //NOI18N
264
buf.append(method);
265
266     buf.append('\n'); //NOI18N
267
buf.append("uri="); //NOI18N
268
buf.append(uri);
269
270     buf.append('\n'); //NOI18N
271
buf.append("current="); //NOI18N
272
buf.append(String.valueOf(current));
273     buf.append('\n'); //NOI18N
274

275     return buf.toString();
276     }
277 } // TransactionNode
278

279
280
281
282
283
284
Popular Tags