Source code: org/altara/mars/swingui/DetailListModel.java
1 /* MARS Network Monitor Swing User Interface
2 Copyright (C) 1999 Brian H. Trammell
3 Copyright (C) 2002 Leapfrog Research & Development, LLC
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, it is available at
17 http:///www.gnu.org/copyleft/gpl.html, or by writing to the
18 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.
20 */
21
22 package org.altara.mars.swingui;
23
24 import org.altara.mars.*;
25 import org.altara.mars.engine.*;
26 import java.text.*;
27 import java.util.*;
28 import java.awt.*;
29 import java.awt.event.*;
30 import javax.swing.*;
31 import javax.swing.tree.*;
32 import javax.swing.event.*;
33
34 /** Presents details about a service's status in a list view.
35 */
36
37 public class DetailListModel extends AbstractListModel
38 implements ProbeListener, TreeSelectionListener {
39
40 private DateFormat df;
41 private Service service;
42 private ArrayList listCache;
43
44 public DetailListModel() {
45 this.listCache = new ArrayList();
46 this.service = null;
47 updateListCache();
48 this.df = DateFormat.getTimeInstance(DateFormat.LONG);
49 }
50
51 public int getSize() {
52 return listCache.size();
53 }
54
55 public Object getElementAt(int index) {
56 return listCache.get(index);
57 }
58
59 public void probeRun(ProbeEvent pe) {
60 // rebuild the list cache if our service is the
61 // one that was probed
62 if (pe.getService() == service) {
63 updateListCache();
64 }
65 }
66
67 public void valueChanged(TreeSelectionEvent tse) {
68 TreePath leadPath = tse.getNewLeadSelectionPath();
69 if (leadPath == null) {
70 setService(null);
71 } else {
72 Object lead = leadPath.getLastPathComponent();
73 if (lead == null || !(lead instanceof Service)) {
74 setService(null);
75 } else {
76 setService((Service)lead);
77 }
78 }
79 }
80
81 private void setService(Service service) {
82 this.service = service;
83 updateListCache();
84 }
85
86 Service getService() {
87 return this.service;
88 }
89
90 private void updateListCache() {
91 // remove the previous list contents
92 int oldsz = listCache.size();
93 if (oldsz > 0) fireIntervalRemoved(this,0,oldsz-1);
94
95 // create a new list
96 listCache = new ArrayList();
97
98 // say very little if no service selected
99 if (service == null) {
100 listCache.add("No service selected");
101 return;
102 }
103
104 // grab the service's host and status
105 Host host = service.getHost();
106 Status status = service.getStatus();
107
108 // present a full service report
109 listCache.add("Service "+service.getName()+" on "+host.getName()+
110 ":"+service.getPort());
111 listCache.add("Type: "+service.getSvcType()+" Status: "+
112 status.toString());
113
114 // display last checked time, if it's been checked.
115 if (status.getTimestamp() > -1) {
116 listCache.add("Last checked "+
117 df.format(new Date(status.getTimestamp())));
118 }
119 // display response time, if available
120 if (status.getResponseTime() > -1) {
121 listCache.add("Response time was "+status.getResponseTime()+" ms");
122 }
123
124 // show all properties, too
125 Iterator propNames = status.getPropertyNames();
126 while (propNames.hasNext()) {
127 String propName = (String)propNames.next();
128 String propValue = (String)status.getProperty(propName);
129 // special multiline treatment for received:
130 if (propName.equals("received")) {
131 listCache.add(propName+":");
132 StringTokenizer lineSplitter =
133 new StringTokenizer(propValue,"\015\012");
134 while (lineSplitter.hasMoreTokens()) {
135 listCache.add(lineSplitter.nextToken());
136 }
137 } else {
138 listCache.add(propName+": "+propValue);
139 }
140 }
141
142 // signal the re-add of list elements
143 if (listCache.size() > 0) fireIntervalAdded(this,0,listCache.size()-1);
144 }
145 }