001: package vicazh.hyperpool.stream.net.http;
002:
003: import javax.management.*;
004: import javax.swing.*;
005: import java.awt.event.*;
006: import javax.swing.event.*;
007: import vicazh.hyperpool.*;
008: import vicazh.hyperpool.Start;
009: import java.beans.*;
010: import java.util.List;
011: import java.util.logging.*;
012:
013: /**
014: * The graphic reconnect service
015: *
016: * @author Victor Zhigunov
017: * @version 0.4.0
018: */
019: public class IReconnectService extends IService implements
020: ReconnectServiceMBean, ActionListener, ListSelectionListener,
021: PropertyChangeListener {
022: private JTable table;
023:
024: private IReconnectModel model;
025:
026: private JButton buttonReconnect;
027:
028: private JDialog dialog;
029:
030: private JOptionPane optionPane;
031:
032: private JFormattedTextField textRetry;
033:
034: private JFormattedTextField textTimeout;
035:
036: private JProgressBar bar;
037:
038: /**
039: * @param name
040: * service name
041: * @param table
042: * service table
043: * @param buttonReconnect
044: * reconnect button
045: * @param dialog
046: * options dialog
047: * @param textRetry
048: * retry value field
049: * @param textTimeout
050: * timeout value field
051: */
052: public IReconnectService(String name, JTable table,
053: JButton buttonReconnect, JDialog dialog,
054: JFormattedTextField textRetry,
055: JFormattedTextField textTimeout, JProgressBar bar) {
056: super (ReconnectServiceMBean.class, name);
057: this .table = table;
058: model = (IReconnectModel) table.getModel();
059: table.getSelectionModel().addListSelectionListener(this );
060: this .buttonReconnect = buttonReconnect;
061: buttonReconnect.setEnabled(false);
062: buttonReconnect.addActionListener(this );
063: this .dialog = dialog;
064: optionPane = (JOptionPane) dialog.getContentPane();
065: optionPane.addPropertyChangeListener(this );
066: this .textRetry = textRetry;
067: this .textTimeout = textTimeout;
068: this .bar = bar;
069: updateUI();
070: }
071:
072: private boolean isTimer;
073:
074: public void actionPerformed(ActionEvent e) {
075: Object source = e.getSource();
076: if (source instanceof Timer) {
077: List<ReconnectItem> items = getItems();
078: if (items == null)
079: return;
080: int j = table.getSelectedRow();
081: Object o = null;
082: if (j != -1 && j < model.data.size())
083: o = model.data.get(j);
084: isTimer = true;
085: model.setData(items);
086: isTimer = false;
087: if (o != null)
088: for (int i = 0; i < model.getRowCount(); i++)
089: if (model.data.get(i).equals(o)) {
090: table.setRowSelectionInterval(i, i);
091: break;
092: }
093: set();
094: } else if (source == buttonReconnect)
095: try {
096: setAttribute(ReconnectServiceMBean.RECONNECT,
097: ((ReconnectItem) model.getValueAt(table
098: .getSelectedRow(), 1)).id);
099: } catch (Exception ex) {
100: Start.logger.log(Level.SEVERE, ex.getMessage(), ex);
101: }
102: else {
103: dialog.setLocationRelativeTo(dialog.getOwner());
104: dialog.setVisible(true);
105: Object value = optionPane.getValue();
106: if (value == JOptionPane.UNINITIALIZED_VALUE)
107: return;
108: optionPane.setValue(JOptionPane.UNINITIALIZED_VALUE);
109: if (((Integer) value).intValue() != JOptionPane.OK_OPTION)
110: return;
111: ReconnectService s = new ReconnectService();
112: s.setRetry((Integer) textRetry.getValue());
113: s.setTimeout((Integer) textTimeout.getValue());
114: try {
115: setAttribute(ReconnectServiceMBean.OPTIONS, s);
116: } catch (Exception ex) {
117: Start.logger.log(Level.SEVERE, ex.getMessage(), ex);
118: }
119: }
120: }
121:
122: public void valueChanged(ListSelectionEvent e) {
123: if (!e.getValueIsAdjusting() && !isTimer)
124: set();
125: }
126:
127: private void set() {
128: int i = table.getSelectedRow();
129: if (i == -1)
130: buttonReconnect.setEnabled(false);
131: else
132: buttonReconnect.setEnabled(((ReconnectItem) model
133: .getValueAt(i, 1)).accept);
134: }
135:
136: public void propertyChange(PropertyChangeEvent e) {
137: String prop = e.getPropertyName();
138: if (dialog.isVisible()
139: && (e.getSource() == optionPane)
140: && (prop.equals(JOptionPane.VALUE_PROPERTY) || prop
141: .equals(JOptionPane.INPUT_VALUE_PROPERTY)))
142: dialog.setVisible(false);
143: }
144:
145: private Timer timer;
146:
147: public void connect(MBeanServerConnection mbsc) throws Exception {
148: super .connect(mbsc);
149: timer = new Timer(500, this );
150: timer.start();
151: }
152:
153: public void disconnect() throws Exception {
154: super .disconnect();
155: timer.stop();
156: }
157:
158: public void updateUI() {
159: SwingUtilities.updateComponentTreeUI(dialog);
160: bar.updateUI();
161: }
162:
163: public List<ReconnectItem> getItems() {
164: try {
165: return ((ReconnectServiceMBean) melement).getItems();
166: } catch (RuntimeException e) {
167: }
168: return null;
169: }
170:
171: protected void otherNotification(String type, Object value) {
172: textRetry.setValue(((ReconnectService) value).getRetry());
173: textTimeout.setValue(((ReconnectService) value).getTimeout());
174: }
175: }
|