Source code: org/altara/mars/swingui/FaultListModel.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.util.*;
27 import java.awt.*;
28 import java.awt.event.*;
29 import javax.swing.*;
30 import javax.swing.tree.*;
31 import javax.swing.event.*;
32
33 /** FaultListModel contains a list of service faults. It does this by
34 watching list state changes (it is a StatusChangeListener) and adding
35 each service in transition to a bad state to a list. This provides the
36 user with an at-a-glance list of problems on her network.
37 */
38
39 public class FaultListModel extends AbstractListModel
40 implements StatusChangeListener, MarsModelListener {
41
42 private TreeSet faultedServices;
43 private HashSet hiddenFaults;
44 private ArrayList listCache;
45 private MarsView view;
46
47 public FaultListModel(MarsView view) {
48 this.faultedServices = new TreeSet(new HostServiceOrderComparator());
49 this.hiddenFaults = new HashSet();
50 this.listCache = new ArrayList();
51 this.view = view;
52 updateListCache();
53 }
54
55 public int getSize() {
56 return listCache.size();
57 }
58
59 public Object getElementAt(int index) {
60 return listCache.get(index);
61 }
62
63 public void statusChanged(StatusChangeEvent sce) {
64 if (sce.getNewStatus().isFault()) {
65 faultedServices.add(sce.getService());
66 } else {
67 faultedServices.remove(sce.getService());
68 hiddenFaults.remove(sce.getService());
69 }
70 updateListCache();
71 }
72
73 void hideFault(Service service) {
74 if (faultedServices.contains(service)) {
75 faultedServices.remove(service);
76 hiddenFaults.add(service);
77 }
78 updateListCache();
79 }
80
81 void showAllFaults() {
82 faultedServices.addAll(hiddenFaults);
83 hiddenFaults.clear();
84 updateListCache();
85 }
86
87 int countHiddenFaults() {
88 return hiddenFaults.size();
89 }
90
91 void updateListCache() {
92 int oldsz = listCache.size();
93 listCache = new ArrayList();
94 if (oldsz > 0) fireIntervalRemoved(this,0,oldsz-1);
95 if (Main.getMain().getController().isActive()) {
96 listCache.addAll(faultedServices);
97 if (listCache.size() == 0) {
98 if (countHiddenFaults() == 0) {
99 listCache.add("No faults detected.");
100 } else {
101 listCache.add("No faults shown ("+countHiddenFaults()+
102 " hidden).");
103 }
104 }
105 } else {
106 listCache.add("Not currently monitoring.");
107 }
108 fireIntervalAdded(this,0,listCache.size()-1);
109 // now update the title bar
110 view.updateTitleBar();
111 }
112
113 int getFaultCount() {
114 return faultedServices.size();
115 }
116
117 public void hostChanged(Host host) {
118 updateListCache();
119 }
120
121 public void serviceChanged(Service service) {
122 updateListCache();
123 }
124
125 public void hostListChanged() {
126 pruneDefunctServices();
127 }
128
129 public void serviceListChanged(Host host) {
130 pruneDefunctServices();
131 }
132
133 private void pruneDefunctServices() {
134 // retain only faulted services in the all service list
135 MarsModel model = Main.getMain().getController().getModel();
136 Set services = model.getServiceSet();
137 faultedServices.retainAll(services);
138 hiddenFaults.retainAll(services);
139 // rebuild the list.
140 updateListCache();
141 }
142
143 private static class HostServiceOrderComparator implements Comparator {
144 public int compare(Object a, Object b) {
145 Service sa = (Service)a;
146 Service sb = (Service)b;
147
148 if (sa.getHost() == sb.getHost()) {
149 return sa.getName().compareTo(sb.getName());
150 } else {
151 return sa.getHost().getName().compareTo(sb.getHost().getName());
152 }
153 }
154 }
155 }