001: package vicazh.hyperpool;
002:
003: import java.awt.*;
004: import java.awt.event.*;
005: import java.beans.*;
006: import java.lang.management.*;
007: import java.util.logging.*;
008: import javax.management.*;
009: import javax.management.remote.*;
010: import javax.swing.*;
011: import javax.xml.transform.*;
012:
013: /**
014: * The graphic communicator
015: *
016: * @author Victor Zhigunov
017: * @version 0.4.0
018: */
019: public class ICom extends IElement implements ComMBean, ActionListener,
020: PropertyChangeListener {
021: private IReader reader;
022:
023: private IEditor editor;
024:
025: private IWriter writer;
026:
027: private JDialog dialogConnect;
028:
029: private JOptionPane optionPaneConnect;
030:
031: private JTextField text;
032:
033: private JDialog dialogMsg;
034:
035: private JOptionPane optionPaneMsg;
036:
037: /**
038: * @param reader
039: * reader
040: * @param editor
041: * editor
042: * @param writer
043: * writer
044: * @param dialogConnect
045: * connect dialog
046: * @param text
047: * url field
048: * @param dialogMsg
049: * message dialog
050: */
051: public ICom(IReader reader, IEditor editor, IWriter writer,
052: JDialog dialogConnect, JTextField text, JDialog dialogMsg) {
053: super (ComMBean.class, null);
054: this .reader = reader;
055: this .editor = editor;
056: this .writer = writer;
057: this .dialogConnect = dialogConnect;
058: optionPaneConnect = (JOptionPane) dialogConnect
059: .getContentPane();
060: optionPaneConnect.addPropertyChangeListener(this );
061: this .text = text;
062: this .dialogMsg = dialogMsg;
063: optionPaneMsg = (JOptionPane) dialogMsg.getContentPane();
064: optionPaneMsg.addPropertyChangeListener(this );
065: }
066:
067: private MBeanServerConnection mbsc;
068:
069: private JMXConnector jmxc;
070:
071: public void disconnect() throws Exception {
072: mbsc = null;
073: if (jmxc != null)
074: jmxc.close();
075: reader.disconnect();
076: }
077:
078: public void propertyChange(PropertyChangeEvent e) {
079: Component c = ((JComponent) e.getSource())
080: .getTopLevelAncestor();
081: String prop = e.getPropertyName();
082: if (c.isVisible()
083: && (prop.equals(JOptionPane.VALUE_PROPERTY) || prop
084: .equals(JOptionPane.INPUT_VALUE_PROPERTY)))
085: c.setVisible(false);
086: }
087:
088: void connect(String url) throws Exception {
089: if (isConnect())
090: disconnect();
091: if (url == null)
092: mbsc = ManagementFactory.getPlatformMBeanServer();
093: else {
094: try {
095: jmxc = JMXConnectorFactory.connect(new JMXServiceURL(
096: "service:jmx:rmi:///jndi/rmi://" + url
097: + "/jmxrmi"));
098: } catch (Exception e) {
099: dialogMsg.setLocationRelativeTo(dialogMsg.getOwner());
100: dialogMsg.setVisible(true);
101: Object value = optionPaneMsg.getValue();
102: if (value == JOptionPane.UNINITIALIZED_VALUE)
103: return;
104: optionPaneMsg.setValue(JOptionPane.UNINITIALIZED_VALUE);
105: return;
106: }
107: mbsc = jmxc.getMBeanServerConnection();
108: }
109: connect(mbsc);
110: editor.connect(mbsc);
111: }
112:
113: protected void currentNotification(String type, Object value) {
114: try {
115: if (type.equals(ComMBean.CHANGED))
116: editor.changed((ComData) value);
117: else if (type.equals(ComMBean.REMOVE))
118: editor.remove((Integer) value);
119: else if (type.equals(ComMBean.SET))
120: editor.set((ComData) value, true);
121: else if (type.equals(ComMBean.UP))
122: editor.up((Integer) value);
123: else if (type.equals(ComMBean.DOWN))
124: editor.down((Integer) value);
125: else {
126: if (value == null)
127: writer.data = null;
128: else {
129: editor.set((ComData) value, false);
130: writer.data = ((ComData) value).b;
131: }
132: }
133: } catch (Exception e) {
134: Start.logger.log(Level.SEVERE, e.getMessage(), e);
135: }
136: }
137:
138: protected void otherNotification(String type, Object value) {
139: try {
140: if (type.equals(ComMBean.CHANGED))
141: editor.changed((ComData) value);
142: else if (type.equals(ComMBean.REMOVE))
143: editor.remove((Integer) value);
144: else if (type.equals(ComMBean.UP))
145: editor.up((Integer) value);
146: else if (type.equals(ComMBean.DOWN))
147: editor.down((Integer) value);
148: else
149: editor.set((ComData) value, false);
150: } catch (Exception e) {
151: Start.logger.log(Level.SEVERE, e.getMessage(), e);
152: }
153: }
154:
155: void connect() throws Exception {
156: dialogConnect.setLocationRelativeTo(dialogConnect.getOwner());
157: dialogConnect.setVisible(true);
158: Object value = optionPaneConnect.getValue();
159: if (value == JOptionPane.UNINITIALIZED_VALUE)
160: return;
161: optionPaneConnect.setValue(JOptionPane.UNINITIALIZED_VALUE);
162: if (((Integer) value).intValue() != JOptionPane.OK_OPTION)
163: return;
164: connect(text.getText());
165: }
166:
167: public void actionPerformed(ActionEvent e) {
168: try {
169: if (isConnect() && writer.check() == IWriter.Status.CANCEL)
170: return;
171: connect();
172: } catch (Exception ex) {
173: Start.logger.log(Level.SEVERE, ex.getMessage(), ex);
174: }
175: }
176:
177: boolean isConnect() {
178: return mbsc != null;
179: }
180:
181: public byte[] get() throws TransformerException {
182: return ((ComMBean) melement).get();
183: }
184: }
|