001: package vicazh.hyperpool.stream.net.http;
002:
003: import javax.swing.*;
004: import javax.swing.tree.*;
005: import javax.swing.text.*;
006: import javax.swing.event.*;
007: import org.jdesktop.jdic.desktop.*;
008: import java.awt.event.*;
009: import java.io.*;
010: import java.net.*;
011: import java.util.logging.*;
012: import vicazh.hyperpool.stream.*;
013: import vicazh.hyperpool.Start;
014:
015: /**
016: * The graphic explorer service
017: *
018: * @author Victor Zhigunov
019: * @version 0.4.0
020: */
021: public class IExplorerService extends IPeriodService implements
022: ExplorerServiceMBean, TreeSelectionListener, ActionListener,
023: ListSelectionListener, TreeExpansionListener {
024: private JTree tree;
025:
026: private DefaultTreeModel model;
027:
028: private IExplorerNode root;
029:
030: private JTable table;
031:
032: private IExplorerModel mFile;
033:
034: private JButton buttonRefresh;
035:
036: private JButton buttonOpen;
037:
038: private JDialog dialog;
039:
040: private JOptionPane optionPane;
041:
042: private JTextField textDir;
043:
044: private JFormattedTextField textSize;
045:
046: private JFormattedTextField textMax;
047:
048: /**
049: * @param name
050: * service name
051: * @param buttonClear
052: * clear button
053: * @param dialogClear
054: * clear dialog
055: * @param buttonStart
056: * start date range button
057: * @param buttonEnd
058: * end date range button
059: * @param box
060: * time period value combo box
061: * @param dialogDate
062: * date select dialog
063: * @param b1
064: * period radio button
065: * @param b2
066: * range radio button
067: * @param tree
068: * service tree
069: * @param table
070: * service table
071: * @param buttonRefresh
072: * refresh button
073: * @param buttonOpen
074: * open button
075: * @param dialog
076: * options dialog
077: * @param textDir
078: * directory value field
079: * @param textSize
080: * set size text field
081: * @param textMax
082: * set files maximum size text field
083: */
084: public IExplorerService(String name, JButton buttonClear,
085: JDialog dialogClear, JButton buttonStart,
086: JButton buttonEnd, JComboBox box, JDialog dialogDate,
087: AbstractButton b1, AbstractButton b2, JTree tree,
088: JTable table, JButton buttonRefresh, JButton buttonOpen,
089: JDialog dialog, JTextField textDir,
090: JFormattedTextField textSize, JFormattedTextField textMax) {
091: super (ExplorerServiceMBean.class, name, buttonClear,
092: dialogClear, buttonStart, buttonEnd, box, dialogDate,
093: b1, b2);
094: this .tree = tree;
095: model = (DefaultTreeModel) tree.getModel();
096: root = (IExplorerNode) model.getRoot();
097: this .table = table;
098: mFile = (IExplorerModel) table.getModel();
099: tree.addTreeSelectionListener(this );
100: tree.addTreeExpansionListener(this );
101: table.getSelectionModel().addListSelectionListener(this );
102: this .buttonRefresh = buttonRefresh;
103: buttonRefresh.addActionListener(this );
104: this .buttonOpen = buttonOpen;
105: buttonOpen.setEnabled(false);
106: buttonOpen.addActionListener(this );
107: this .dialog = dialog;
108: optionPane = (JOptionPane) dialog.getContentPane();
109: optionPane.addPropertyChangeListener(this );
110: this .textDir = textDir;
111: this .textSize = textSize;
112: this .textMax = textMax;
113: updateUI();
114: }
115:
116: public void valueChanged(TreeSelectionEvent e) {
117: IExplorerNode node = (IExplorerNode) tree
118: .getLastSelectedPathComponent();
119: if (node == null)
120: node = root;
121: mFile.setData(listFiles(node.dir, pc));
122: }
123:
124: public void valueChanged(ListSelectionEvent e) {
125: if (e.getValueIsAdjusting())
126: return;
127: if (table.getSelectedRow() == -1)
128: buttonOpen.setEnabled(false);
129: else {
130: buttonOpen.setEnabled(true);
131: JTextComponent t = new JTextField(((IExplorerDoc) table
132: .getValueAt(table.getSelectedRow(), 0)).file);
133: t.selectAll();
134: t.copy();
135: }
136: }
137:
138: protected void hold() {
139: super .hold();
140: buttonRefresh.setEnabled(false);
141: tree.setEnabled(false);
142: }
143:
144: public void actionPerformed(ActionEvent e) {
145: Object source = e.getSource();
146: if (source == buttonOpen) {
147: try {
148: Desktop.browse(new URL(((IExplorerDoc) table
149: .getValueAt(table.getSelectedRow(), 0)).file));
150: } catch (Exception ex) {
151: Start.logger.log(Level.SEVERE, ex.getMessage(), ex);
152: }
153: } else if (source == buttonRefresh) {
154: tree.collapseRow(0);
155: tree.expandRow(0);
156: } else {
157: dialog.setLocationRelativeTo(dialog.getOwner());
158: dialog.setVisible(true);
159: Object value = optionPane.getValue();
160: if (value == JOptionPane.UNINITIALIZED_VALUE)
161: return;
162: optionPane.setValue(JOptionPane.UNINITIALIZED_VALUE);
163: if (((Integer) value).intValue() != JOptionPane.OK_OPTION)
164: return;
165: ExplorerService s = new ExplorerService();
166: s.setDir(textDir.getText());
167: s.setSize((Long) textSize.getValue());
168: s.setMax((Long) textMax.getValue());
169: try {
170: setAttribute(FileServiceMBean.OPTIONS, s);
171: } catch (Exception ex) {
172: Start.logger.log(Level.SEVERE, ex.getMessage(), ex);
173: }
174: }
175: }
176:
177: protected void set() {
178: super .set();
179: buttonRefresh.setEnabled(isDirExists);
180: tree.setEnabled(isDirExists);
181: if (!isDirExists) {
182: root.removeAllChildren();
183: model.reload();
184: }
185: }
186:
187: public void updateUI() {
188: SwingUtilities.updateComponentTreeUI(dialog);
189: super .updateUI();
190: }
191:
192: public void treeExpanded(TreeExpansionEvent event) {
193: hold();
194: final IExplorerNode node = (IExplorerNode) event.getPath()
195: .getLastPathComponent();
196: node.removeAllChildren();
197: model.reload(node);
198: new Thread() {
199: public void run() {
200: final File[] list = select(node.dir, pc);
201: SwingUtilities.invokeLater(new Runnable() {
202: public void run() {
203: if (list != null)
204: for (File f : list) {
205: Thread.yield();
206: node.add(new IExplorerNode(f, null));
207: model.reload(node);
208: }
209: IExplorerService.this .set();
210: }
211: });
212: }
213: }.start();
214: }
215:
216: public void treeCollapsed(TreeExpansionEvent event) {
217: }
218:
219: public File[] select(File file, PeriodControl pc) {
220: return ((ExplorerServiceMBean) melement).select(file, pc);
221: }
222:
223: public long lastModified(File file) {
224: return ((ExplorerServiceMBean) melement).lastModified(file);
225: }
226:
227: public File[] listFiles(File file, PeriodControl pc) {
228: return ((ExplorerServiceMBean) melement).listFiles(file, pc);
229: }
230:
231: protected void otherNotification(String type, Object value) {
232: if (type.equals(FileServiceMBean.OPTIONS)) {
233: textDir.setText(((FileService) value).getDir());
234: textSize.setValue(((ExplorerService) value).getSize());
235: textMax.setValue(((ExplorerService) value).getMax());
236: }
237: super.otherNotification(type, value);
238: }
239: }
|