package vicazh.hyperpool;
import java.util.*;
import javax.management.*;
import javax.swing.tree.*;
/**
* The abstract editor
*
* @author Victor Zhigunov
* @version 0.4.0
*/
abstract public class AbstractEditor extends NotificationBroadcasterSupport {
protected Map<Integer, DefaultMutableTreeNode> map = new HashMap<Integer, DefaultMutableTreeNode>();
protected DefaultMutableTreeNode node;
protected void remove() {
Object object = node.getUserObject();
parent = (DefaultMutableTreeNode) node.getParent();
i = parent.getIndex(node);
if (!(object instanceof GroupMBean) && object instanceof LineMBean) {
((GroupMBean) parent.getUserObject()).getObjects().remove(i);
DefaultMutableTreeNode snode = parent.getNextSibling();
if (snode != null)
((SelectorMBean) snode.getUserObject()).removed(i);
} else {
DefaultMutableTreeNode nodenext = node.getPreviousSibling();
Object next = null;
if (nodenext != null)
next = nodenext.getUserObject();
if (next == null
&& !(parent.getUserObject() instanceof ProjectMBean)
&& !(parent.getUserObject() instanceof GroupMBean)
&& parent.getUserObject() instanceof LineMBean)
// object is last
next = ((GroupMBean) ((DefaultMutableTreeNode) parent
.getParent()).getUserObject()).getObject();
if (parent.getChildCount() == i + 1)
// element is first
prev(parent, next);
else
// element is not first
prev((DefaultMutableTreeNode) parent.getChildAt(i + 1), next);
}
}
private void prev(DefaultMutableTreeNode prev, Object object) {
Object o = prev.getUserObject();
if (o instanceof LineMBean) {
((LineMBean) o).setObject(object);
if (o instanceof GroupMBean)
// ends of lines
for (int i = 0; i < prev.getChildCount(); i++) {
DefaultMutableTreeNode n = (DefaultMutableTreeNode) prev
.getChildAt(i);
if (n.getChildCount() == 0)
// line is empty
prev(n, object);
else
// last element
prev((DefaultMutableTreeNode) n.getChildAt(0), object);
}
} else if (o instanceof ServiceMBean)
((ServiceMBean) o).setElement((ElementMBean) object);
else
((SelectorMBean) o).setGroup((GroupMBean) object);
}
protected DefaultMutableTreeNode parent;
protected int i;
protected Object next;
protected void put(Object object) {
parent = node;
i = 0;
next = node.getUserObject();
if (next instanceof ProjectMBean)
next = null;
else if ((object instanceof GroupMBean || !(object instanceof LineMBean))
&& !(next instanceof GroupMBean) && next instanceof LineMBean)
// element into line
// line is current
next = ((GroupMBean) ((DefaultMutableTreeNode) node.getParent())
.getUserObject()).getObject();
else if (object instanceof LineMBean && next instanceof GroupMBean)
// line into group
// group is current
next = ((GroupMBean) next).getObject();
else if (object instanceof LineMBean && next instanceof LineMBean) {
// line into group
// line is current
parent = (DefaultMutableTreeNode) node.getParent();
i = parent.getIndex(node) + 1;
next = ((GroupMBean) ((DefaultMutableTreeNode) node.getParent())
.getUserObject()).getObject();
node = node.getLastLeaf();
} else {
parent = (DefaultMutableTreeNode) node.getParent();
i = parent.getIndex(node) + 1;
node = node.getLastLeaf();
}
if (!(object instanceof GroupMBean) && object instanceof LineMBean) {
((LineMBean) object).setObject(next);
((GroupMBean) parent.getUserObject()).getObjects().add(i,
(LineMBean) object);
DefaultMutableTreeNode snode = parent.getNextSibling();
if (snode != null)
((SelectorMBean) snode.getUserObject()).added(i);
} else {
if (object instanceof SelectorMBean)
((SelectorMBean) object).setGroup((GroupMBean) next);
else if (object instanceof ServiceMBean)
((ServiceMBean) object).setElement((ElementMBean) next);
else
((LineMBean) object).setObject(next);
if (parent.getChildCount() == i)
// element is first
prev(parent, object);
else
// element is not first
prev((DefaultMutableTreeNode) parent.getChildAt(i), object);
}
}
protected void stop() throws Exception {
node = null;
}
public void upLine() {
if (node.getUserObject() instanceof GroupMBean
|| !(node.getUserObject() instanceof LineMBean))
node = (DefaultMutableTreeNode) node.getParent();
}
public void upGroup() {
if (node.getUserObject() instanceof LineMBean)
node = (DefaultMutableTreeNode) node.getParent();
}
abstract protected void move(DefaultMutableTreeNode node1,
DefaultMutableTreeNode node2);
void up(int currentID) {
DefaultMutableTreeNode c = map.get(currentID);
DefaultMutableTreeNode n = c.getPreviousSibling();
if (n.getUserObject() instanceof SelectorMBean)
n = n.getPreviousSibling().getPreviousNode();
else {
DefaultMutableTreeNode p = n.getPreviousSibling();
n = p == null ? n.getPreviousNode() : p;
}
move(c, n);
}
void down(int currentID) {
DefaultMutableTreeNode c = map.get(currentID);
DefaultMutableTreeNode n = c.getNextSibling();
if (n.getUserObject() instanceof GroupMBean)
n = n.getNextSibling();
move(c, n);
}
}
|