001: package vicazh.hyperpool;
002:
003: import java.util.*;
004: import javax.management.*;
005: import javax.swing.tree.*;
006:
007: /**
008: * The abstract editor
009: *
010: * @author Victor Zhigunov
011: * @version 0.4.0
012: */
013: abstract public class AbstractEditor extends
014: NotificationBroadcasterSupport {
015:
016: protected Map<Integer, DefaultMutableTreeNode> map = new HashMap<Integer, DefaultMutableTreeNode>();
017:
018: protected DefaultMutableTreeNode node;
019:
020: protected void remove() {
021: Object object = node.getUserObject();
022: parent = (DefaultMutableTreeNode) node.getParent();
023: i = parent.getIndex(node);
024: if (!(object instanceof GroupMBean)
025: && object instanceof LineMBean) {
026: ((GroupMBean) parent.getUserObject()).getObjects()
027: .remove(i);
028: DefaultMutableTreeNode snode = parent.getNextSibling();
029: if (snode != null)
030: ((SelectorMBean) snode.getUserObject()).removed(i);
031: } else {
032: DefaultMutableTreeNode nodenext = node.getPreviousSibling();
033: Object next = null;
034: if (nodenext != null)
035: next = nodenext.getUserObject();
036: if (next == null
037: && !(parent.getUserObject() instanceof ProjectMBean)
038: && !(parent.getUserObject() instanceof GroupMBean)
039: && parent.getUserObject() instanceof LineMBean)
040: // object is last
041: next = ((GroupMBean) ((DefaultMutableTreeNode) parent
042: .getParent()).getUserObject()).getObject();
043: if (parent.getChildCount() == i + 1)
044: // element is first
045: prev(parent, next);
046: else
047: // element is not first
048: prev((DefaultMutableTreeNode) parent.getChildAt(i + 1),
049: next);
050: }
051: }
052:
053: private void prev(DefaultMutableTreeNode prev, Object object) {
054: Object o = prev.getUserObject();
055: if (o instanceof LineMBean) {
056: ((LineMBean) o).setObject(object);
057: if (o instanceof GroupMBean)
058: // ends of lines
059: for (int i = 0; i < prev.getChildCount(); i++) {
060: DefaultMutableTreeNode n = (DefaultMutableTreeNode) prev
061: .getChildAt(i);
062: if (n.getChildCount() == 0)
063: // line is empty
064: prev(n, object);
065: else
066: // last element
067: prev((DefaultMutableTreeNode) n.getChildAt(0),
068: object);
069: }
070: } else if (o instanceof ServiceMBean)
071: ((ServiceMBean) o).setElement((ElementMBean) object);
072: else
073: ((SelectorMBean) o).setGroup((GroupMBean) object);
074: }
075:
076: protected DefaultMutableTreeNode parent;
077:
078: protected int i;
079:
080: protected Object next;
081:
082: protected void put(Object object) {
083: parent = node;
084: i = 0;
085: next = node.getUserObject();
086: if (next instanceof ProjectMBean)
087: next = null;
088: else if ((object instanceof GroupMBean || !(object instanceof LineMBean))
089: && !(next instanceof GroupMBean)
090: && next instanceof LineMBean)
091: // element into line
092: // line is current
093: next = ((GroupMBean) ((DefaultMutableTreeNode) node
094: .getParent()).getUserObject()).getObject();
095: else if (object instanceof LineMBean
096: && next instanceof GroupMBean)
097: // line into group
098: // group is current
099: next = ((GroupMBean) next).getObject();
100: else if (object instanceof LineMBean
101: && next instanceof LineMBean) {
102: // line into group
103: // line is current
104: parent = (DefaultMutableTreeNode) node.getParent();
105: i = parent.getIndex(node) + 1;
106: next = ((GroupMBean) ((DefaultMutableTreeNode) node
107: .getParent()).getUserObject()).getObject();
108: node = node.getLastLeaf();
109: } else {
110: parent = (DefaultMutableTreeNode) node.getParent();
111: i = parent.getIndex(node) + 1;
112: node = node.getLastLeaf();
113: }
114: if (!(object instanceof GroupMBean)
115: && object instanceof LineMBean) {
116: ((LineMBean) object).setObject(next);
117: ((GroupMBean) parent.getUserObject()).getObjects().add(i,
118: (LineMBean) object);
119: DefaultMutableTreeNode snode = parent.getNextSibling();
120: if (snode != null)
121: ((SelectorMBean) snode.getUserObject()).added(i);
122: } else {
123: if (object instanceof SelectorMBean)
124: ((SelectorMBean) object).setGroup((GroupMBean) next);
125: else if (object instanceof ServiceMBean)
126: ((ServiceMBean) object).setElement((ElementMBean) next);
127: else
128: ((LineMBean) object).setObject(next);
129: if (parent.getChildCount() == i)
130: // element is first
131: prev(parent, object);
132: else
133: // element is not first
134: prev((DefaultMutableTreeNode) parent.getChildAt(i),
135: object);
136: }
137: }
138:
139: protected void stop() throws Exception {
140: node = null;
141: }
142:
143: public void upLine() {
144: if (node.getUserObject() instanceof GroupMBean
145: || !(node.getUserObject() instanceof LineMBean))
146: node = (DefaultMutableTreeNode) node.getParent();
147: }
148:
149: public void upGroup() {
150: if (node.getUserObject() instanceof LineMBean)
151: node = (DefaultMutableTreeNode) node.getParent();
152: }
153:
154: abstract protected void move(DefaultMutableTreeNode node1,
155: DefaultMutableTreeNode node2);
156:
157: void up(int currentID) {
158: DefaultMutableTreeNode c = map.get(currentID);
159: DefaultMutableTreeNode n = c.getPreviousSibling();
160: if (n.getUserObject() instanceof SelectorMBean)
161: n = n.getPreviousSibling().getPreviousNode();
162: else {
163: DefaultMutableTreeNode p = n.getPreviousSibling();
164: n = p == null ? n.getPreviousNode() : p;
165: }
166: move(c, n);
167: }
168:
169: void down(int currentID) {
170: DefaultMutableTreeNode c = map.get(currentID);
171: DefaultMutableTreeNode n = c.getNextSibling();
172: if (n.getUserObject() instanceof GroupMBean)
173: n = n.getNextSibling();
174: move(c, n);
175: }
176: }
|