001: package vicazh.hyperpool.stream.net.http.html;
002:
003: import javax.management.*;
004: import javax.swing.*;
005: import javax.swing.text.*;
006: import java.awt.event.*;
007: import java.beans.*;
008: import java.io.*;
009: import java.net.*;
010: import java.util.List;
011: import java.util.logging.*;
012: import javax.swing.event.*;
013: import vicazh.hyperpool.*;
014: import vicazh.hyperpool.Start;
015:
016: /**
017: * The graphic task service
018: *
019: * @author Victor Zhigunov
020: * @version 0.4.0
021: */
022: public class ITaskService extends IService implements TaskServiceMBean,
023: ActionListener, PropertyChangeListener, ListSelectionListener {
024:
025: private JTable table;
026:
027: private ITaskModel model;
028:
029: private JDialog dialogNew;
030:
031: private JOptionPane optionPaneNew;
032:
033: private JTextField textNewAddress;
034:
035: private JSlider sliderNewLevel;
036:
037: private JTextField textNewStart;
038:
039: private JSlider sliderNewThreads;
040:
041: private JTextField textNewSave;
042:
043: private JButton buttonEdit;
044:
045: private JButton buttonGo;
046:
047: private JButton buttonStop;
048:
049: private JButton buttonBreak;
050:
051: private JButton buttonDelete;
052:
053: private String prefix;
054:
055: private AbstractButton buttonAutorun;
056:
057: /**
058: * @param name
059: * service name
060: * @param table
061: * service table
062: * @param dialogNew
063: * new task dialog
064: * @param textNewAddress
065: * new address text field
066: * @param sliderNewLevel
067: * level slider
068: * @param textNewStart
069: * start address text field
070: * @param sliderNewThreads
071: * threads slider
072: * @param textNewSave
073: * save folder text field
074: * @param buttonEdit
075: * edit task button
076: * @param buttonGo
077: * go task button
078: * @param buttonStop
079: * stop task button
080: * @param buttonBreak
081: * break task button
082: * @param buttonDelete
083: * delete task button
084: * @param prefix
085: * save dir prefix
086: * @param buttonAutorun
087: * autorun button
088: */
089: public ITaskService(String name, JTable table, JDialog dialogNew,
090: JTextField textNewAddress, JSlider sliderNewLevel,
091: JTextField textNewStart, JSlider sliderNewThreads,
092: JTextField textNewSave, JButton buttonEdit,
093: JButton buttonGo, JButton buttonStop, JButton buttonBreak,
094: JButton buttonDelete, String prefix,
095: AbstractButton buttonAutorun) {
096: super (TaskServiceMBean.class, name);
097: table.getSelectionModel().addListSelectionListener(this );
098: this .table = table;
099: model = (ITaskModel) table.getModel();
100: this .dialogNew = dialogNew;
101: optionPaneNew = (JOptionPane) dialogNew.getContentPane();
102: optionPaneNew.addPropertyChangeListener(this );
103: this .textNewAddress = textNewAddress;
104: this .sliderNewLevel = sliderNewLevel;
105: this .textNewStart = textNewStart;
106: this .sliderNewThreads = sliderNewThreads;
107: this .textNewSave = textNewSave;
108: buttonEdit.setEnabled(false);
109: buttonEdit.addActionListener(this );
110: this .buttonEdit = buttonEdit;
111: buttonGo.setEnabled(false);
112: buttonGo.addActionListener(this );
113: this .buttonGo = buttonGo;
114: buttonStop.setEnabled(false);
115: buttonStop.addActionListener(this );
116: this .buttonStop = buttonStop;
117: buttonBreak.setEnabled(false);
118: buttonBreak.addActionListener(this );
119: this .buttonBreak = buttonBreak;
120: buttonDelete.setEnabled(false);
121: buttonDelete.addActionListener(this );
122: this .buttonDelete = buttonDelete;
123: this .prefix = prefix;
124: this .buttonAutorun = buttonAutorun;
125: updateUI();
126: }
127:
128: private boolean isTimer;
129:
130: public void valueChanged(ListSelectionEvent e) {
131: if (!e.getValueIsAdjusting() && !isTimer)
132: set();
133: }
134:
135: void set() {
136: int i = table.getSelectedRow();
137: if (i == -1) {
138: set(buttonEdit, false);
139: set(buttonGo, false);
140: set(buttonStop, false);
141: set(buttonBreak, false);
142: set(buttonDelete, false);
143: } else {
144: Task t = (Task) model.getValueAt(i, 0);
145: set(buttonEdit, t.status == Task.Status.READY);
146: set(buttonGo, t.status == Task.Status.READY);
147: set(buttonStop, t.status == Task.Status.RUN);
148: set(buttonBreak, t.status == Task.Status.RUN
149: || t.status == Task.Status.STOP);
150: set(buttonDelete, true);
151: }
152: }
153:
154: private void set(AbstractButton button, boolean status) {
155: if (button.isEnabled() && !status || !button.isEnabled()
156: && status)
157: button.setEnabled(status);
158: }
159:
160: public void actionPerformed(ActionEvent e) {
161: Object source = e.getSource();
162: if (source instanceof Timer) {
163: List<Task> tasks = getTasks();
164: if (tasks == null)
165: return;
166: int j = table.getSelectedRow();
167: Object o = null;
168: if (j != -1 && j < model.data.size())
169: o = model.data.get(j);
170: isTimer = true;
171: model.setData(tasks);
172: isTimer = false;
173: if (o != null)
174: for (int i = 0; i < model.getRowCount(); i++)
175: if (model.data.get(i).equals(o)) {
176: table.setRowSelectionInterval(i, i);
177: break;
178: }
179: set();
180: } else if (source == buttonDelete)
181: try {
182: setAttribute(TaskServiceMBean.REMOVE, ((Task) model
183: .getValueAt(table.getSelectedRow(), 0)).id);
184: } catch (Exception ex) {
185: Start.logger.log(Level.SEVERE, ex.getMessage(), ex);
186: }
187: else if (source == buttonEdit) {
188: Task t = (Task) model.getValueAt(table.getSelectedRow(), 0);
189: textNewAddress.setText(t.getAddress());
190: sliderNewLevel.setValue(t.getLevel());
191: textNewStart.setText(t.getStart());
192: sliderNewThreads.setValue(t.getThreads());
193: textNewSave.setText(t.dir2path());
194: buttonAutorun.setSelected(t.getAutorun());
195: dialogNew.setLocationRelativeTo(dialogNew.getOwner());
196: dialogNew.setVisible(true);
197: Object value = optionPaneNew.getValue();
198: if (value == JOptionPane.UNINITIALIZED_VALUE)
199: return;
200: optionPaneNew.setValue(JOptionPane.UNINITIALIZED_VALUE);
201: if (((Integer) value).intValue() == JOptionPane.OK_OPTION)
202: set(t);
203: try {
204: setAttribute(TaskServiceMBean.EDIT, t);
205: } catch (Exception ex) {
206: Start.logger.log(Level.SEVERE, ex.getMessage(), ex);
207: }
208: } else if (source == buttonGo)
209: try {
210: setAttribute(TaskServiceMBean.GO, ((Task) model
211: .getValueAt(table.getSelectedRow(), 0)).id);
212: } catch (Exception ex) {
213: Start.logger.log(Level.SEVERE, ex.getMessage(), ex);
214: }
215: else if (source == buttonStop)
216: try {
217: setAttribute(TaskServiceMBean.STOP, ((Task) model
218: .getValueAt(table.getSelectedRow(), 0)).id);
219: } catch (Exception ex) {
220: Start.logger.log(Level.SEVERE, ex.getMessage(), ex);
221: }
222: else if (source == buttonBreak)
223: try {
224: setAttribute(TaskServiceMBean.BREAK, ((Task) model
225: .getValueAt(table.getSelectedRow(), 0)).id);
226: } catch (Exception ex) {
227: Start.logger.log(Level.SEVERE, ex.getMessage(), ex);
228: }
229: else {
230: JTextComponent text = new JTextField();
231: text.paste();
232: String s = text.getText();
233: try {
234: URL url = new URL(s);
235: textNewAddress.setText(s);
236: textNewStart.setText(url.getProtocol() + "://"
237: + url.getHost());
238: textNewSave.setText((prefix + url.getHost()).replace(
239: '/', File.separatorChar));
240: } catch (MalformedURLException ex) {
241: textNewSave.setText(textNewSave.getText().replace('/',
242: File.separatorChar));
243: }
244: dialogNew.setLocationRelativeTo(dialogNew.getOwner());
245: dialogNew.setVisible(true);
246: Object value = optionPaneNew.getValue();
247: if (value == JOptionPane.UNINITIALIZED_VALUE)
248: return;
249: optionPaneNew.setValue(JOptionPane.UNINITIALIZED_VALUE);
250: if (((Integer) value).intValue() != JOptionPane.OK_OPTION)
251: return;
252: Task t = new Task();
253: set(t);
254: try {
255: setAttribute(TaskServiceMBean.ADD, t);
256: } catch (Exception ex) {
257: Start.logger.log(Level.SEVERE, ex.getMessage(), ex);
258: }
259: }
260: }
261:
262: private void set(Task t) {
263: t.setAddress(textNewAddress.getText().replace(" ", "%20"));
264: t.setLevel(sliderNewLevel.getValue());
265: t.setStart(textNewStart.getText().replace(" ", "%20"));
266: t.setThreads(sliderNewThreads.getValue());
267: t.path2dir(textNewSave.getText());
268: t.setAutorun(buttonAutorun.isSelected());
269: }
270:
271: public void propertyChange(PropertyChangeEvent e) {
272: String prop = e.getPropertyName();
273: if (dialogNew.isVisible()
274: && e.getSource() == optionPaneNew
275: && (prop.equals(JOptionPane.VALUE_PROPERTY) || prop
276: .equals(JOptionPane.INPUT_VALUE_PROPERTY)))
277: dialogNew.setVisible(false);
278: }
279:
280: public void updateUI() {
281: SwingUtilities.updateComponentTreeUI(dialogNew);
282: }
283:
284: public List<Task> getTasks() {
285: try {
286: return ((TaskServiceMBean) melement).getTasks();
287: } catch (RuntimeException e) {
288: }
289: return null;
290: }
291:
292: private Timer timer;
293:
294: public void connect(MBeanServerConnection mbsc) throws Exception {
295: super .connect(mbsc);
296: timer = new Timer(500, this );
297: timer.start();
298: }
299:
300: public void disconnect() throws Exception {
301: super.disconnect();
302: timer.stop();
303: }
304: }
|