Source code: org/altara/mars/swingui/ServiceTreeChangeAdapter.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 /** Translates StatusChangeEvents and the MarsModelListener interface
34 into TreeModelEvents. This allows the serviceTree display to update
35 itself in a timely manner without imposing the peculiarities of the
36 JTree event model on the UI-independent MARS code.
37 */
38
39 public class ServiceTreeChangeAdapter
40 implements StatusChangeListener, MarsModelListener {
41
42 private Controller controller;
43 private MarsModel model;
44 private JTree serviceTree;
45 private DefaultTreeModel stm;
46 private MarsView view;
47
48 public ServiceTreeChangeAdapter(Controller controller, MarsModel model,
49 JTree serviceTree, MarsView view) {
50 // capture instance vars
51 this.controller = controller;
52 this.model = model;
53 this.serviceTree = serviceTree;
54 this.stm = (DefaultTreeModel)serviceTree.getModel();
55 this.view = view;
56
57 // register as a change listener with the controller
58 // (dynamic changes)
59 controller.addStatusChangeListener(this);
60
61 // register as a change listener with the model
62 // (static changes)
63 model.addMarsModelListener(this);
64 }
65
66 void setModel(MarsModel model) {
67 this.model = model;
68 model.addMarsModelListener(this);
69 }
70
71 public void statusChanged(final StatusChangeEvent sce) {
72 SwingUtilities.invokeLater(new Runnable() {
73 public void run() {
74 stm.nodeChanged(sce.getService());
75 }
76 });
77 }
78
79 public void hostChanged(final Host host) {
80 SwingUtilities.invokeLater(new Runnable() {
81 public void run() {
82 stm.nodeChanged(host);
83 view.markUnsavedChanges(true);
84 }
85 });
86 }
87
88 public void serviceChanged(final Service service) {
89 SwingUtilities.invokeLater(new Runnable() {
90 public void run() {
91 stm.nodeChanged(service);
92 view.markUnsavedChanges(true);
93 }
94 });
95 }
96
97 public void hostListChanged() {
98 SwingUtilities.invokeLater(new Runnable() {
99 public void run() {
100 stm.nodeStructureChanged(model);
101 view.markUnsavedChanges(true);
102 }
103 });
104 }
105
106 public void serviceListChanged(final Host host) {
107 SwingUtilities.invokeLater(new Runnable() {
108 public void run() {
109 stm.nodeStructureChanged(host);
110 view.markUnsavedChanges(true);
111 }
112 });
113 }
114
115 public void repaintServiceNodes() {
116 Iterator hosts = model.getHosts();
117 while (hosts.hasNext()) {
118 Iterator services = ((Host)hosts.next()).getServices();
119 while (services.hasNext()) {
120 stm.nodeChanged((Service)services.next());
121 }
122 }
123 }
124 }