001: package vicazh.hyperpool;
002:
003: import javax.management.*;
004: import javax.swing.*;
005: import javax.swing.tree.*;
006: import javax.swing.event.*;
007: import java.awt.*;
008: import java.awt.event.*;
009: import java.beans.*;
010: import java.util.logging.*;
011:
012: /**
013: * The graphic editor
014: *
015: * @author Victor Zhigunov
016: * @version 0.4.0
017: */
018: public class IEditor extends AbstractEditor implements
019: TreeSelectionListener, ActionListener, PropertyChangeListener {
020:
021: private JTree tree;
022:
023: private DefaultTreeModel model;
024:
025: private JList list;
026:
027: private JDialog dialog;
028:
029: private JOptionPane optionPane;
030:
031: private IReader reader;
032:
033: private Container container;
034:
035: private JMenuItem menuService;
036:
037: private AbstractButton buttonService;
038:
039: private JMenuItem menuGroup;
040:
041: private AbstractButton buttonGroup;
042:
043: private JMenuItem menuLine;
044:
045: private AbstractButton buttonLine;
046:
047: private JMenuItem menuSelector;
048:
049: private AbstractButton buttonSelector;
050:
051: private JMenuItem menuUp;
052:
053: private AbstractButton buttonUp;
054:
055: private JMenuItem menuDown;
056:
057: private AbstractButton buttonDown;
058:
059: private JMenuItem menuDelete;
060:
061: private AbstractButton buttonDelete;
062:
063: private String fileGroup;
064:
065: private String fileLine;
066:
067: private JList listSelector;
068:
069: private JDialog dialogSelector;
070:
071: private JOptionPane optionPaneSelector;
072:
073: private JMenuItem menuRename;
074:
075: /**
076: * @param tree
077: * manager tree
078: * @param list
079: * service list
080: * @param dialog
081: * service dialog
082: * @param reader
083: * reader
084: * @param container
085: * container
086: * @param menuService
087: * add service menu item
088: * @param buttonService
089: * add service button
090: * @param menuGroup
091: * add group menu item
092: * @param buttonGroup
093: * add group button
094: * @param menuLine
095: * add line menu item
096: * @param buttonLine
097: * add line button
098: * @param menuSelector
099: * add selector menu item
100: * @param buttonSelector
101: * add selector button
102: * @param menuUp
103: * move up menu item
104: * @param buttonUp
105: * move up button
106: * @param menuDown
107: * move down menu item
108: * @param buttonDown
109: * move down button
110: * @param menuDelete
111: * delete object menu item
112: * @param buttonDelete
113: * delete object button
114: * @param fileGroup
115: * group file
116: * @param fileLine
117: * line file
118: * @param listSelector
119: * selector list
120: * @param dialogSelector
121: * selector dialog
122: * @param menuRename
123: * rename menu item
124: */
125: public IEditor(JTree tree, JList list, JDialog dialog,
126: IReader reader, Container container, JMenuItem menuService,
127: AbstractButton buttonService, JMenuItem menuGroup,
128: AbstractButton buttonGroup, JMenuItem menuLine,
129: AbstractButton buttonLine, JMenuItem menuSelector,
130: AbstractButton buttonSelector, JMenuItem menuUp,
131: AbstractButton buttonUp, JMenuItem menuDown,
132: AbstractButton buttonDown, JMenuItem menuDelete,
133: AbstractButton buttonDelete, String fileGroup,
134: String fileLine, JList listSelector,
135: JDialog dialogSelector, JMenuItem menuRename) {
136: this .tree = tree;
137: tree.addTreeSelectionListener(this );
138: model = (DefaultTreeModel) tree.getModel();
139: this .list = list;
140: this .dialog = dialog;
141: optionPane = (JOptionPane) dialog.getContentPane();
142: optionPane.addPropertyChangeListener(this );
143: this .reader = reader;
144: this .container = container;
145: this .menuService = menuService;
146: menuService.addActionListener(this );
147: this .buttonService = buttonService;
148: buttonService.addActionListener(this );
149: this .menuGroup = menuGroup;
150: menuGroup.addActionListener(this );
151: this .buttonGroup = buttonGroup;
152: buttonGroup.addActionListener(this );
153: this .menuLine = menuLine;
154: menuLine.addActionListener(this );
155: this .buttonLine = buttonLine;
156: buttonLine.addActionListener(this );
157: this .menuSelector = menuSelector;
158: menuSelector.addActionListener(this );
159: this .buttonSelector = buttonSelector;
160: buttonSelector.addActionListener(this );
161: this .menuUp = menuUp;
162: menuUp.addActionListener(this );
163: this .buttonUp = buttonUp;
164: buttonUp.addActionListener(this );
165: this .menuDown = menuDown;
166: menuDown.addActionListener(this );
167: this .buttonDown = buttonDown;
168: buttonDown.addActionListener(this );
169: this .menuDelete = menuDelete;
170: menuDelete.addActionListener(this );
171: this .buttonDelete = buttonDelete;
172: buttonDelete.addActionListener(this );
173: this .fileGroup = fileGroup;
174: this .fileLine = fileLine;
175: this .listSelector = listSelector;
176: this .dialogSelector = dialogSelector;
177: optionPaneSelector = (JOptionPane) dialogSelector
178: .getContentPane();
179: optionPaneSelector.addPropertyChangeListener(this );
180: this .menuRename = menuRename;
181: menuRename.addActionListener(this );
182: valueChanged(null);
183: }
184:
185: public void valueChanged(TreeSelectionEvent e) {
186: DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree
187: .getLastSelectedPathComponent();
188: if (node == null) {
189: tree.setEditable(false);
190: setEnabledService(false);
191: setEnabledGroup(false);
192: setEnabledLine(false);
193: setEnabledSelector(false);
194: setEnabledUp(false);
195: setEnabledDown(false);
196: setEnabledDelete(false);
197: menuRename.setEnabled(false);
198: } else {
199: Object object = node.getUserObject();
200: if (object instanceof ProjectMBean) {
201: tree.setEditable(true);
202: setEnabledService(true);
203: setEnabledGroup(node.getNextNode() == null);
204: setEnabledLine(false);
205: setEnabledSelector(false);
206: setEnabledUp(false);
207: setEnabledDown(false);
208: setEnabledDelete(false);
209: menuRename.setEnabled(true);
210: } else if (object instanceof GroupMBean) {
211: tree.setEditable(true);
212: setEnabledService(false);
213: setEnabledGroup(false);
214: setEnabledLine(true);
215: setEnabledUp(false);
216: setEnabledDown(false);
217: if (node.getNextSibling() == null) {
218: if (node.getChildCount() == 0)
219: setEnabledSelector(false);
220: else {
221: boolean b = true;
222: for (int i = 0; i < node.getChildCount(); i++) {
223: DefaultMutableTreeNode n = (DefaultMutableTreeNode) node
224: .getChildAt(i);
225: if (!n.isLeaf()
226: && ((DefaultMutableTreeNode) n
227: .getLastChild())
228: .getUserObject() instanceof GroupMBean) {
229: b = false;
230: break;
231: }
232: }
233: setEnabledSelector(b);
234: }
235: setEnabledDelete(true);
236: } else {
237: setEnabledSelector(false);
238: setEnabledDelete(false);
239: }
240: menuRename.setEnabled(true);
241: } else if (object instanceof LineMBean) {
242: tree.setEditable(true);
243: setEnabledService(true);
244: setEnabledLine(true);
245: if (((DefaultMutableTreeNode) node.getParent())
246: .getNextSibling() == null) {
247: setEnabledGroup(node.getChildCount() == 0);
248: setEnabledDelete(true);
249: } else {
250: setEnabledGroup(false);
251: setEnabledDelete(((SelectorMBean) ((DefaultMutableTreeNode) node
252: .getParent()).getNextSibling()
253: .getUserObject()).getDef() != ((DefaultMutableTreeNode) node
254: .getParent()).getIndex(node));
255: }
256: setEnabledSelector(false);
257: setEnabledUp(node.getPreviousSibling() != null);
258: setEnabledDown(node.getNextSibling() != null);
259: menuRename.setEnabled(true);
260: } else if (object instanceof ServiceMBean) {
261: tree.setEditable(false);
262: setEnabledService(true);
263: setEnabledGroup(node.getNextSibling() == null);
264: setEnabledLine(false);
265: setEnabledSelector(false);
266: setEnabledUp(node.getPreviousSibling() != null);
267: setEnabledDown(node.getNextSibling() != null
268: && (!(node.getNextSibling().getUserObject() instanceof GroupMBean) || node
269: .getNextSibling().getNextSibling() != null));
270: setEnabledDelete(true);
271: menuRename.setEnabled(false);
272: } else {// selector
273: tree.setEditable(false);
274: setEnabledService(true);
275: setEnabledGroup(node.getNextSibling() == null);
276: setEnabledDelete(node.getNextSibling() == null);
277: setEnabledLine(false);
278: setEnabledSelector(false);
279: setEnabledUp(false);
280: setEnabledDown(false);
281: menuRename.setEnabled(false);
282: }
283: }
284: }
285:
286: private void setEnabledService(boolean b) {
287: menuService.setEnabled(b);
288: buttonService.setEnabled(b);
289: }
290:
291: private void setEnabledGroup(boolean b) {
292: menuGroup.setEnabled(b);
293: buttonGroup.setEnabled(b);
294: }
295:
296: private void setEnabledLine(boolean b) {
297: menuLine.setEnabled(b);
298: buttonLine.setEnabled(b);
299: }
300:
301: private void setEnabledSelector(boolean b) {
302: menuSelector.setEnabled(b);
303: buttonSelector.setEnabled(b);
304: }
305:
306: private void setEnabledUp(boolean b) {
307: menuUp.setEnabled(b);
308: buttonUp.setEnabled(b);
309: }
310:
311: private void setEnabledDown(boolean b) {
312: menuDown.setEnabled(b);
313: buttonDown.setEnabled(b);
314: }
315:
316: private void setEnabledDelete(boolean b) {
317: menuDelete.setEnabled(b);
318: buttonDelete.setEnabled(b);
319: }
320:
321: private ComData data;
322:
323: private boolean isCurrent;
324:
325: public void put(IUnit unit) throws Exception {
326: DefaultMutableTreeNode n = new DefaultMutableTreeNode(unit);
327: unit.id = data.get();
328: if (unit instanceof IElement)
329: ((IElement) unit).connect(mbsc);
330: if (node == null)
331: model.setRoot(n);
332: else {
333: super .put(unit);
334: model.insertNodeInto(n, parent, i);
335: if (parent == model.getRoot())
336: tree.expandRow(0);
337: }
338: node = n;
339: if (manual && isCurrent) {
340: TreePath path = new TreePath(n.getPath());
341: tree.setSelectionPath(path);
342: if (unit instanceof ILine)
343: tree.startEditingAtPath(path);
344: } else
345: valueChanged(null);
346: map.put(unit.id, n);
347: }
348:
349: private void stop(DefaultMutableTreeNode node) throws Exception {
350: for (int i = 0; i < node.getChildCount(); i++)
351: stop((DefaultMutableTreeNode) node.getChildAt(i));
352: IUnit unit = (IUnit) node.getUserObject();
353: if (unit instanceof IElement)
354: ((IElement) unit).disconnect();
355: map.remove(unit.id);
356: }
357:
358: public void stop() throws Exception {
359: if (model.getRoot() != null) {
360: stop((DefaultMutableTreeNode) model.getRoot());
361: model.setRoot(null);
362: }
363: manual = false;
364: super .stop();
365: }
366:
367: public void actionPerformed(ActionEvent e) {
368: node = (DefaultMutableTreeNode) tree
369: .getLastSelectedPathComponent();
370: Object source = e.getSource();
371: try {
372: if (source == menuDelete || source == buttonDelete)
373: com.setAttribute(ComMBean.REMOVE, ((IUnit) node
374: .getUserObject()).id);
375: else if (source == menuUp || source == buttonUp)
376: com.setAttribute(ComMBean.UP, ((IUnit) node
377: .getUserObject()).id);
378: else if (source == menuDown || source == buttonDown)
379: com.setAttribute(ComMBean.DOWN, ((IUnit) node
380: .getUserObject()).id);
381: else if (source == menuService || source == buttonService) {
382: dialog.setLocationRelativeTo(dialog.getOwner());
383: dialog.setVisible(true);
384: Object value = optionPane.getValue();
385: if (value == JOptionPane.UNINITIALIZED_VALUE)
386: return;
387: optionPane.setValue(JOptionPane.UNINITIALIZED_VALUE);
388: if (((Integer) value).intValue() != JOptionPane.OK_OPTION)
389: return;
390: IResource r = (IResource) list.getSelectedValue();
391: if (r != null)
392: send(r.resource);
393: } else if (source == menuGroup || source == buttonGroup)
394: send(fileGroup);
395: else if (source == menuLine || source == buttonLine)
396: send(fileLine);
397: else if (source == menuSelector || source == buttonSelector) {
398: dialogSelector.setLocationRelativeTo(dialogSelector
399: .getOwner());
400: dialogSelector.setVisible(true);
401: Object value = optionPaneSelector.getValue();
402: if (value == JOptionPane.UNINITIALIZED_VALUE)
403: return;
404: optionPaneSelector
405: .setValue(JOptionPane.UNINITIALIZED_VALUE);
406: if (((Integer) value).intValue() != JOptionPane.OK_OPTION)
407: return;
408: IResource r = (IResource) listSelector
409: .getSelectedValue();
410: if (r != null)
411: send(r.resource);
412: } else
413: tree.startEditingAtPath(new TreePath(node.getPath()));
414: } catch (Exception ex) {
415: Start.logger.log(Level.SEVERE, ex.getMessage(), ex);
416: }
417: }
418:
419: private ICom com;
420:
421: /**
422: * @param com
423: * communicator
424: */
425: public void setCom(ICom com) {
426: this .com = com;
427: }
428:
429: private void send(String in) throws Exception {
430: com.setAttribute(ComMBean.SET, new ComData(Reader.get(in),
431: ((IUnit) node.getUserObject()).id));
432: }
433:
434: public void propertyChange(PropertyChangeEvent e) {
435: Component c = ((JComponent) e.getSource())
436: .getTopLevelAncestor();
437: String prop = e.getPropertyName();
438: if (c.isVisible()
439: && (prop.equals(JOptionPane.VALUE_PROPERTY) || prop
440: .equals(JOptionPane.INPUT_VALUE_PROPERTY)))
441: c.setVisible(false);
442: }
443:
444: private boolean manual;
445:
446: public void setManual(boolean manual) {
447: this .manual = manual;
448: }
449:
450: private MBeanServerConnection mbsc;
451:
452: void connect(MBeanServerConnection mbsc) {
453: this .mbsc = mbsc;
454: }
455:
456: void set(ComData data, boolean isCurrent) throws Exception {
457: this .data = data;
458: this .isCurrent = isCurrent;
459: node = map.get(data.currentID);
460: reader.getObject(data.b);
461: }
462:
463: void remove(int currentID) throws Exception {
464: node = map.get(currentID);
465: remove();
466: stop(node);
467: DefaultMutableTreeNode n = node == tree
468: .getLastSelectedPathComponent() ? parent
469: : (DefaultMutableTreeNode) tree
470: .getLastSelectedPathComponent();
471: tree.setSelectionPath(new TreePath(node.getPath()));
472: for (Component c : container.getComponents())
473: if (c.isVisible()) {
474: container.remove(c);
475: break;
476: }
477: model.removeNodeFromParent(node);
478: if (n != null)
479: tree.setSelectionPath(new TreePath(n.getPath()));
480: }
481:
482: void changed(ComData data) {
483: DefaultMutableTreeNode n = map.get(data.currentID);
484: ((LineMBean) n.getUserObject()).setName(new String(data.b));
485: model.nodeChanged(n);
486: }
487:
488: protected void move(DefaultMutableTreeNode node1,
489: DefaultMutableTreeNode node2) {
490: node = node1;
491: remove();
492: DefaultMutableTreeNode l = (DefaultMutableTreeNode) tree
493: .getLastSelectedPathComponent();
494: model.removeNodeFromParent(node);
495: node = node2;
496: super .put(node1.getUserObject());
497: model.insertNodeInto(node1, parent, i);
498: tree.setSelectionPath(new TreePath(l.getPath()));
499: }
500: }
|