001: package vicazh.hyperpool.stream.net.http;
002:
003: import javax.swing.*;
004: import javax.swing.tree.*;
005: import javax.swing.event.*;
006: import vicazh.hyperpool.*;
007: import vicazh.hyperpool.Start;
008: import java.awt.event.*;
009: import java.util.*;
010: import java.util.logging.*;
011:
012: /**
013: * The graphic monitor service
014: *
015: * @author Victor Zhigunov
016: * @version 0.4.0
017: */
018: public class IMonitorService extends IService implements
019: MonitorServiceMBean, TreeSelectionListener, ActionListener {
020: private JTree tree;
021:
022: private JButton buttonClose;
023:
024: private DefaultTreeModel model;
025:
026: private DefaultMutableTreeNode root;
027:
028: /**
029: * @param name
030: * service name
031: * @param tree
032: * service tree
033: * @param buttonClose
034: * close connection button
035: */
036: public IMonitorService(String name, JTree tree, JButton buttonClose) {
037: super (MonitorServiceMBean.class, name);
038: this .tree = tree;
039: this .buttonClose = buttonClose;
040: model = (DefaultTreeModel) tree.getModel();
041: root = (DefaultMutableTreeNode) model.getRoot();
042: tree.addTreeSelectionListener(this );
043: buttonClose.setEnabled(false);
044: buttonClose.addActionListener(this );
045: }
046:
047: public void valueChanged(TreeSelectionEvent e) {
048: DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree
049: .getLastSelectedPathComponent();
050: buttonClose.setEnabled(node != null
051: && (node != root || node.getChildCount() > 0));
052: }
053:
054: public void actionPerformed(ActionEvent e) {
055: close((DefaultMutableTreeNode) tree
056: .getLastSelectedPathComponent());
057: }
058:
059: private void close(DefaultMutableTreeNode node) {
060: for (int i = 0; i < node.getChildCount(); i++)
061: close((DefaultMutableTreeNode) node.getChildAt(i));
062: Object o = node.getUserObject();
063: if (o instanceof MonitorItem)
064: try {
065: setAttribute(MonitorServiceMBean.BREAK,
066: ((MonitorItem) o).id);
067: } catch (Exception e) {
068: Start.logger.log(Level.SEVERE, e.getMessage(), e);
069: }
070: }
071:
072: protected void currentNotification(String type, Object value) {
073: for (Object item : (List<?>) value) {
074: DefaultMutableTreeNode client = getNode(root,
075: ((MonitorItem) item).client);
076: if (client == null) {
077: client = new DefaultMutableTreeNode(
078: ((MonitorItem) item).client);
079: model
080: .insertNodeInto(client, root, root
081: .getChildCount());
082: tree.expandPath(new TreePath(root.getPath()));
083: }
084: if (((MonitorItem) item).host == null) {
085: DefaultMutableTreeNode node = new DefaultMutableTreeNode(
086: item);
087: model.insertNodeInto(node, client, client
088: .getChildCount());
089: tree.expandPath(new TreePath(client.getPath()));
090: } else {
091: DefaultMutableTreeNode host = getNode(client,
092: ((MonitorItem) item).host);
093: if (host == null) {
094: host = new DefaultMutableTreeNode(
095: ((MonitorItem) item).host);
096: model.insertNodeInto(host, client, client
097: .getChildCount());
098: tree.expandPath(new TreePath(client.getPath()));
099: }
100: DefaultMutableTreeNode node = new DefaultMutableTreeNode(
101: item);
102: model.insertNodeInto(node, host, host.getChildCount());
103: tree.expandPath(new TreePath(host.getPath()));
104: }
105:
106: }
107: }
108:
109: protected void otherNotification(String type, Object value) {
110: // System.out.println("call notif:" + type + ":"
111: // + ((MonitorItem) value).id + ":" + value);
112: if (type.equals(MonitorServiceMBean.CLIENT)) {
113: DefaultMutableTreeNode client = getNode(root,
114: ((MonitorItem) value).client);
115: if (client == null) {
116: client = new DefaultMutableTreeNode(
117: ((MonitorItem) value).client);
118: model
119: .insertNodeInto(client, root, root
120: .getChildCount());
121: tree.expandPath(new TreePath(root.getPath()));
122: }
123: DefaultMutableTreeNode node = new DefaultMutableTreeNode(
124: value);
125: model.insertNodeInto(node, client, client.getChildCount());
126: tree.expandPath(new TreePath(client.getPath()));
127: } else if (type.equals(MonitorServiceMBean.OPEN)) {
128: DefaultMutableTreeNode client = getNode(root,
129: ((MonitorItem) value).client);
130: // if (client == null)
131: // System.out.println("client not found:"
132: // + ((MonitorItem) value).client);
133: DefaultMutableTreeNode node = getNode(client, value);
134: if (node == null)
135: return;
136: // Start.logger.info("con not found:" + value);
137: if (((MonitorItem) value).host == null) {
138: node.setUserObject(value);
139: model.nodeChanged(node);
140: } else {
141: model.removeNodeFromParent(node);
142: DefaultMutableTreeNode host = getNode(client,
143: ((MonitorItem) value).host);
144: if (host == null) {
145: host = new DefaultMutableTreeNode(
146: ((MonitorItem) value).host);
147: model.insertNodeInto(host, client, client
148: .getChildCount());
149: tree.expandPath(new TreePath(client.getPath()));
150: }
151: node = new DefaultMutableTreeNode(value);
152: model.insertNodeInto(node, host, host.getChildCount());
153: tree.expandPath(new TreePath(host.getPath()));
154: }
155: } else if (type.equals(MonitorServiceMBean.END)) {
156: DefaultMutableTreeNode client = getNode(root,
157: ((MonitorItem) value).client);
158: // if (client == null)
159: // System.out.println("client2 not found:"
160: // + ((MonitorItem) value).client);
161: if (((MonitorItem) value).host == null) {
162: DefaultMutableTreeNode node = getNode(client, value);
163: // if (node == null)
164: // System.out.println("con3 not found:" + value);
165: ((MonitorItem) node.getUserObject()).name = null;
166: model.nodeChanged(node);
167: } else {
168: DefaultMutableTreeNode host = getNode(client,
169: ((MonitorItem) value).host);
170: if (host == null)
171: return;
172: DefaultMutableTreeNode node = getNode(host, value);
173: if (node == null)
174: return;
175: // Start.logger.info("con4 not found:" + value);
176: model.removeNodeFromParent(node);
177: if (host.getChildCount() == 0)
178: model.removeNodeFromParent(host);
179: ((MonitorItem) value).name = null;
180: node = new DefaultMutableTreeNode(value);
181: model.insertNodeInto(node, client, client
182: .getChildCount());
183: tree.expandPath(new TreePath(client.getPath()));
184: }
185: } else {
186: DefaultMutableTreeNode client = getNode(root,
187: ((MonitorItem) value).client);
188: // if (client == null)
189: // System.out.println("client3 not found:"
190: // + ((MonitorItem) value).client);
191: DefaultMutableTreeNode node = getNode(client, value);
192: if (node == null) {
193: DefaultMutableTreeNode host = getNode(client,
194: ((MonitorItem) value).host);
195: // if (host == null)
196: // System.out.println("host not found:"
197: // + ((MonitorItem) value).host);
198: node = getNode(host, value);
199: model.removeNodeFromParent(node);
200: if (host.getChildCount() == 0)
201: model.removeNodeFromParent(host);
202: } else {
203: model.removeNodeFromParent(node);
204: }
205: if (client.getChildCount() == 0)
206: model.removeNodeFromParent(client);
207: }
208: }
209:
210: private DefaultMutableTreeNode getNode(
211: DefaultMutableTreeNode parent, Object object) {
212: for (int i = parent.getChildCount() - 1; i >= 0; i--) {
213: DefaultMutableTreeNode node = (DefaultMutableTreeNode) parent
214: .getChildAt(i);
215: if (node.getUserObject().equals(object))
216: return node;
217: }
218: return null;
219: }
220: }
|