From: Jie B. <ba...@us...> - 2006-06-29 22:16:00
|
Update of /cvsroot/cob/COBEditor/src/edu/iastate/utils/tree In directory sc8-pr-cvs4.sourceforge.net:/tmp/cvs-serv7692/src/edu/iastate/utils/tree Added Files: TypedNode.java TypedTreeRender.java TypedTree.java SortTreeModel.java TreeNodeDeleteEditing.java TreeNodeCommentEditing.java TreeNodeRenameEditing.java TreeNodeMoveEditing.java TypedTreeEditor.java TreeNodeInsertEditing.java TreePopupMenuListener.java Log Message: Jie 2006-jun-29 renamed indus.tree package to utils.tree --- NEW FILE: TypedTreeRender.java --- package edu.iastate.utils.tree; import java.util.HashMap; import java.util.Map; import java.awt.Color; import java.awt.Component; import java.awt.Graphics; import javax.swing.Icon; import javax.swing.JLabel; import javax.swing.JTree; import javax.swing.tree.TreeCellRenderer; import edu.iastate.ato.shared.IndusConstants; public class TypedTreeRender extends JLabel implements TreeCellRenderer { public TypedTreeRender() { icons.put(TypedNode.ROOT + "", IndusConstants.iconRoot); icons.put(TypedNode.PACKAGE + "", IndusConstants.iconPackage); icons.put(TypedNode.CLASS + "", IndusConstants.iconClass); icons.put(TypedNode.PROPERTY + "", IndusConstants.iconProperty); icons.put(TypedNode.INSTANCE + "", IndusConstants.iconInstance); icons.put(TypedNode.ALL_CLASSES + "", IndusConstants.iconAllClasses); icons.put(TypedNode.ALL_PROPERTIES + "", IndusConstants.iconAllProperties); icons.put(TypedNode.ALL_INSTANCES + "", IndusConstants.iconAllInstances); icons.put(TypedNode.ATTRIBUTE + "", IndusConstants.iconSchema); icons.put(TypedNode.AVH + "", IndusConstants.iconAVHValue); icons.put(TypedNode.DB + "", IndusConstants.iconDB); } // static protected Font defaultFont; static public Map<String, Icon> icons = new HashMap(); /** Color to use for the background when selected. */ static protected final Color SelectedBackgroundColor = Color.yellow; /** Whether or not the item that was last configured is selected. */ protected boolean selected; /** * This is messaged from JTree whenever it needs to get the size * of the component or it wants to draw it. * This attempts to set the font based on value, which will be * a TreeNode. */ public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) { String stringValue = tree.convertValueToText(value, selected, expanded, leaf, row, hasFocus); // Set the text. setText(stringValue); // Tooltips used by the tree. setToolTipText(stringValue); // Set the image. // 2004-10-01 Jie Bao debug: should may sure the type of node if (value instanceof TypedNode) { TypedNode theNode = (TypedNode) value; // 2005-08-23: node may has it's own icon definition Icon icon = theNode.getIcon(); if (icon != null) { setIcon(icon); } else { setIcon( (Icon) icons.get(theNode.getType() + "")); } Color color = theNode.getColor(); if (color != null) { setForeground( (Color) color); } else { setForeground(Color.black); } } // Update the selected flag for the next paint. // } this.selected = selected; return this; } /** /** * paint is subclassed to draw the background correctly. JLabel * currently does not allow backgrounds other than white, and it * will also fill behind the icon. Something that isn't desirable. */ public void paint(Graphics g) { Color bColor; Icon currentI = getIcon(); if (selected) { bColor = SelectedBackgroundColor; } else if (getParent() != null) { /* Pick background color up from parent (which will come from the JTree we're contained in). */ bColor = getParent().getBackground(); } else { bColor = getBackground(); } g.setColor(bColor); if (currentI != null && getText() != null) { int offset = (currentI.getIconWidth() + getIconTextGap()); if (getComponentOrientation().isLeftToRight()) { g.fillRect(offset, 0, getWidth() - 1 - offset, getHeight() - 1); } else { g.fillRect(0, 0, getWidth() - 1 - offset, getHeight() - 1); } } else { g.fillRect(0, 0, getWidth() - 1, getHeight() - 1); } super.paint(g); } } --- NEW FILE: TreeNodeDeleteEditing.java --- package edu.iastate.utils.tree; import edu.iastate.utils.undo.EditingAction; /** * @since 2005-04-20 */ public class TreeNodeDeleteEditing extends EditingAction { TypedNode parent; TypedTree tree; public TreeNodeDeleteEditing(TypedTree tree, TypedNode location, TypedNode parent) { this.tree = tree; this.location = location; this.parent = parent; this.summary = "Delete node '" + location + "' from '" + parent+"'"; } public void undo() { // insert location to parent if ( ( (TypedNode) location).getParent() != null) { tree.getModel().removeNodeFromParent( (TypedNode) location); } tree.getModel().insertNodeInto( (TypedNode) location, parent, parent.getChildCount()); tree.expandNode( (TypedNode) location); } public void redo() { // remove location from parent if (parent.isNodeChild( (TypedNode) location)) { tree.getModel().removeNodeFromParent( (TypedNode) location); } } } --- NEW FILE: TypedTree.java --- package edu.iastate.utils.tree; import java.util.Enumeration; import java.util.Vector; import javax.swing.JEditorPane; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.DefaultTreeModel; import edu.iastate.utils.gui.JTreeEx; /** * Generic tree data structure with typed node * * @author Jie Bao * @since 2004-05-01 */ public class TypedTree extends JTreeEx { public TypedTree(DefaultTreeModel model) { super(model); } public TypedTree() { super(); } public TypedTree(TypedNode top) { super(top); this.setTop(top); } public String toString() { return toString(false); } public String toString(boolean toHTML) { //String blank = toHTML ? " " : " "; String endline = "|---"; String line = toHTML ? "| " : "| "; String crlf = toHTML ? "<BR></BR>" : "\n"; if (getTop() == null) { return ""; } String toPrint = ""; Enumeration e = getTop().preorderEnumeration(); while (e.hasMoreElements()) { DefaultMutableTreeNode nn = (DefaultMutableTreeNode) e.nextElement(); String leading = line; if (nn.getLevel() == 0) { leading = ""; } else if (nn.getLevel() == 1) { leading = endline; } else { leading = endline; for (int i = 0; i < nn.getLevel() - 1; i++) { leading = line + leading; } } toPrint += leading + nn + crlf; } if (toHTML) { return toPrint; } else { return toPrint; } } public void setTop(TypedNode top) { getModel().setRoot(top); } public TypedNode getTop() { return (TypedNode) getModel().getRoot(); } public void buildSampleTree() { TypedNode top = new TypedNode("http://semanticWWW.com/indus.owl#USA"); TypedNode iowa = new TypedNode( "http://semanticWWW.com/indus.owl#Iowa"); top.add(iowa); iowa.add(new TypedNode( "http://semanticWWW.com/indus.owl#Ames")); iowa.add(new TypedNode( "http://semanticWWW.com/indus.owl#DesMoines")); TypedNode va = new TypedNode( "http://semanticWWW.com/indus.owl#Virginia"); top.add(va); va.add(new TypedNode( "http://semanticWWW.com/indus.owl#Richmond")); va.add(new TypedNode( "http://semanticWWW.com/indus.owl#Petersberg")); setTop(top); } /** * replace the value of old node to that of the new node. if new node has * children, they will be children of the old node. * @param tree TypedTree * @param node TypedNode * @param newNode TypedNode * @return TypedNode - the old node * @since 2005-03-31 * @author Jie Bao */ public static TypedNode amendNode(TypedTree tree, TypedNode node, TypedNode newNode) { try { DefaultTreeModel model = tree.getModel(); // get the existing children Vector oldSons = new Vector(); int oldSonCount = node.getChildCount(); //System.out.println("node has children " + oldSonCount); for (int j = 0; j < oldSonCount; j++) { TypedNode son = (TypedNode) node.getChildAt(j); oldSons.add(son.getUserObject()); } //System.out.println("oldSons" + oldSons); // add all children of new node to oldNode if it's new Vector newSons = new Vector(); for (Enumeration e = newNode.children(); e.hasMoreElements(); ) { newSons.add(e.nextElement()); } for (Enumeration e = newSons.elements(); e.hasMoreElements(); ) { TypedNode kid = (TypedNode) e.nextElement(); System.out.println(kid); if (!oldSons.contains(kid.getUserObject())) { model.insertNodeInto(kid, node, node.getChildCount()); } //node.add(kid); } //Debug.trace(child + " children moved"); // copy value of new node to old node //if (node.getParent() != null) //{ //TypedNode parent = (TypedNode) node.getParent(); //int index = parent.getIndex(node); //model.removeNodeFromParent(node); //Debug.trace("remove"); //model.insertNodeInto(newNode, parent, index); //Debug.trace("insert"); node.setUserObject(newNode.getUserObject()); node.setComment(newNode.getComment()); //} return node; } catch (Exception ex) { ex.printStackTrace(); return null; } } // for test purpose public static void main(String[] args) { TypedTree t = new TypedTree(); t.buildSampleTree(); JFrame frame = new JFrame(); frame.setSize(800, 600); JEditorPane pane = new JEditorPane(); frame.getContentPane().add(new JScrollPane(pane)); pane.setContentType("text/html"); pane.setText(t.toString(true)); frame.setVisible(true); // System.out.print(t); } } --- NEW FILE: TreePopupMenuListener.java --- package edu.iastate.utils.tree; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.Icon; import javax.swing.JMenuItem; import javax.swing.JPanel; import javax.swing.JPopupMenu; import javax.swing.KeyStroke; import javax.swing.tree.TreePath; public abstract class TreePopupMenuListener extends MouseAdapter { public JPanel fatherPanel; public JPopupMenu popup = new JPopupMenu(); public TypedTree tree; public TreePopupMenuListener() { super(); } protected void addMenuItem(String text, Icon icon, ActionListener listener, JPopupMenu menu, String shortKey) { JMenuItem jMenuItem = (icon == null) ? new JMenuItem(text) : new JMenuItem(text, icon); jMenuItem.addActionListener(listener); if (shortKey != null) { jMenuItem.setAccelerator(KeyStroke.getKeyStroke(shortKey)); } menu.add(jMenuItem); } /** * Simplified addMenuItem * @param text String * @param listener ActionListener * @author Jie Bao * @since 2005-03-30 */ protected void addMenuItem(String text, ActionListener listener) { addMenuItem(text, null, listener, popup, null); } protected void addMenuItem(String text, Icon icon, ActionListener listener) { addMenuItem(text, icon, listener, popup, null); } public void mouseReleased(MouseEvent e) { showMenu(e); } public void showMenu(MouseEvent e) { TreePath selPath = tree.getPathForLocation(e.getX(), e.getY()); if (e.isPopupTrigger() && selPath != null) { tree.setSelectionPath(selPath); final TypedNode selectedNode = (TypedNode) selPath.getLastPathComponent(); boolean showpopup = false; popup = new JPopupMenu(); buildContextMenu(selectedNode); // show the menu showpopup = true; if (showpopup) { popup.show(e.getComponent(), e.getX(), e.getY()); } } } abstract protected void buildContextMenu(TypedNode selectedNode); } --- NEW FILE: TypedTreeEditor.java --- package edu.iastate.utils.tree; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JOptionPane; import javax.swing.tree.DefaultTreeModel; import edu.iastate.utils.undo.BulkEditingAction; import edu.iastate.utils.undo.EditingAction; import edu.iastate.utils.undo.UndoRedoStack; public abstract class TypedTreeEditor extends TreePopupMenuListener { public TypedTreeEditor() { super(); } /** * update the tree * * @param top MyNode */ protected void updateTree() { tree.repaint(); } abstract protected void changed(TypedNode theNode); // 2005-04-17 /** * @author Jie Bao * @since 1.0 2005-03-31 */ protected class DefaultDeleteAllChildrenAction implements ActionListener { TypedNode theNode; public DefaultDeleteAllChildrenAction(TypedNode theNode) { this.theNode = theNode; } public void actionPerformed(ActionEvent e) { deleteAllChildren(theNode); } } protected void deleteAllChildren(TypedNode theNode) { DefaultTreeModel model = (DefaultTreeModel) tree.getModel(); int child = theNode.getChildCount(); BulkEditingAction bulk = new BulkEditingAction(theNode); bulk.summary = "Delete all children of '" + theNode + "'"; for (int i = 0; i < child; i++) { TypedNode kid = (TypedNode) theNode.getChildAt(0); model.removeNodeFromParent(kid); TreeNodeDeleteEditing action = new TreeNodeDeleteEditing( tree, kid, theNode); bulk.addAction(action); } history.addAction(bulk); changed(theNode); } /** * @author Jie Bao * @since 1.0 2005-03-31 */ protected class DefaultDeleteButKeepChildrenAction implements ActionListener { TypedNode theNode; public DefaultDeleteButKeepChildrenAction(TypedNode theNode) { this.theNode = theNode; } public void actionPerformed(ActionEvent e) { DefaultTreeModel model = (DefaultTreeModel) tree.getModel(); TypedNode parent = (TypedNode) theNode.getParent(); int child = theNode.getChildCount(); BulkEditingAction bulk = new BulkEditingAction(parent); bulk.summary = "Delete '" + theNode + "' and keep its children"; for (int i = 0; i < child; i++) { TypedNode kid = (TypedNode) theNode.getChildAt(0); //model.removeNodeFromParent(kid); model.insertNodeInto(kid, parent, parent.getChildCount()); TreeNodeMoveEditing action = new TreeNodeMoveEditing( tree, kid, theNode, parent); bulk.addAction(action); } model.removeNodeFromParent(theNode); TreeNodeDeleteEditing action = new TreeNodeDeleteEditing(tree, theNode, parent); bulk.addAction(action); history.addAction(bulk); changed(theNode); changed(parent); } } protected class DefaultEditCommentsAction implements ActionListener { TypedNode theNode; public DefaultEditCommentsAction(TypedNode theNode) { this.theNode = theNode; } public void actionPerformed(ActionEvent e) { String oldComments = (String) theNode.getComment(); String newComments = JOptionPane.showInputDialog( "Give comment for the node", oldComments); if (newComments == null) { return; } theNode.setComment(newComments); changed(theNode); TreeNodeCommentEditing action = new TreeNodeCommentEditing( theNode, oldComments, newComments); history.addAction(action); } } /** * @author Jie Bao * @since 1.0 2004-10-08 */ protected class DefaultDeleteAction implements ActionListener { TypedNode theNode; public DefaultDeleteAction(TypedNode theNode) { this.theNode = theNode; } public void actionPerformed(ActionEvent e) { delete(theNode); } /** * DefaultDeleteAction */ public DefaultDeleteAction() { } } protected void delete(TypedNode theNode) { TypedNode parent = (TypedNode) theNode.getParent(); if (parent != null) { tree.getModel().removeNodeFromParent(theNode); changed(theNode); changed(parent); TreeNodeDeleteEditing action = new TreeNodeDeleteEditing( tree, theNode, parent); history.addAction(action); } } protected abstract class DefaultInsertParentAction implements ActionListener { public TypedNode theNode; public DefaultInsertParentAction(TypedNode theNode) { this.theNode = theNode; } public void actionPerformed(ActionEvent e) { TypedNode parent = (TypedNode) theNode.getParent(); if (parent != null) { TypedNode newNode = getNewNode(); if (newNode == null) { return; } DefaultTreeModel model = tree.getModel(); model.insertNodeInto(newNode, parent, parent.getChildCount()); BulkEditingAction bulk = new BulkEditingAction(theNode); bulk.summary = "Insert parent '" + newNode + "' on '" + theNode + "'"; EditingAction action = new TreeNodeInsertEditing( tree, newNode, parent); bulk.addAction(action); model.removeNodeFromParent(theNode); action = new TreeNodeDeleteEditing(tree, theNode, parent); bulk.addAction(action); model.insertNodeInto(theNode, newNode, 0); action = new TreeNodeInsertEditing(tree, theNode, newNode); bulk.addAction(action); history.addAction(bulk); changed(theNode); } } protected abstract TypedNode getNewNode(); } protected abstract class DefaultRenameAction implements ActionListener { public TypedNode theNode; public DefaultRenameAction(TypedNode theNode) { this.theNode = theNode; } public void actionPerformed(ActionEvent e) { Object oldName = theNode.getUserObject(); Object newName = getNewUserObject(); if (newName != null) { theNode.setUserObject(newName); changed(theNode); TreeNodeRenameEditing action = new TreeNodeRenameEditing( theNode, oldName, newName); history.addAction(action); } } protected abstract Object getNewUserObject(); } // 2005-04-20 public class DefaultUndoAction implements ActionListener { public void actionPerformed(ActionEvent e) { undo(); } } public void undo() { if (history.canUndo()) { EditingAction action = history.undo(); changed( (TypedNode) action.location); } } // 2005-04-20 public class DefaultRedoAction implements ActionListener { public void actionPerformed(ActionEvent e) { redo(); } } public void redo() { if (history.canRedo()) { EditingAction action = history.redo(); changed( (TypedNode) action.location); } } protected abstract class DeafultCreateSubValueAction implements ActionListener { public TypedNode parent; public DeafultCreateSubValueAction(TypedNode parent) { this.parent = parent; } public void actionPerformed(ActionEvent e) { TypedNode newNode = getNewNode(); if (newNode != null) { tree.getModel().insertNodeInto(newNode, parent, parent.getChildCount()); tree.expandPath(newNode); tree.setSelectionPath(tree.getPath(newNode)); changed(newNode); EditingAction action = new TreeNodeInsertEditing( tree, newNode, parent); history.addAction(action); } } protected abstract TypedNode getNewNode(); } // 2005-04-20 public UndoRedoStack history = new UndoRedoStack(); public void addHistory(EditingAction action) { history.addAction(action); } } --- NEW FILE: TypedNode.java --- package edu.iastate.utils.tree; import java.awt.Color; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.Icon; /** * Node with a type * @author Jie Bao * @since 1.0 2004-10-02 */ public class TypedNode extends DefaultMutableTreeNode implements Comparable { // type of the node protected short type; protected Object comment; // 2004-10-13 protected Color color; // 2005-04-07 public Object clone() { TypedNode t = new TypedNode(this.getUserObject()); t.type = this.type; t.comment = this.comment; t.color = this.color; return t; } public short getType() { return type; } public void setType(short type) { this.type = type; } public TypedNode(Object userObject) { super(userObject); } /** * Icon of the node * This method should be overloaded in your class * @return Icon * @see TypedTreeRender * @author Jie Bao * @since 2005-08-23 */ public Icon getIcon() { return null; } /** * @param userObject Object * @param type short * @param comment String * @since 2004-10-18 */ public TypedNode(Object userObject, short type, String comment) { super(userObject); setType(type); setComment(comment); } public boolean equals(Object node) { if (node instanceof TypedNode) { return getLocalName().equals( ( (TypedNode) node).getLocalName()); } else { return false; } } /** * Get name of the node * @since 2004-10-13 */ public String getLocalName() { return getUserObject().toString(); } public Object getComment() { return comment; } public Color getColor() { return color; } public void setComment(Object comment) { this.comment = comment; } public void setColor(Color color) { this.color = color; } /** * toXML - plain text only YET * * @return String * @since 2004-10-18 */ public String toXML() { return getUserObject().toString(); } /** * read from XML - plain text only YET * @param xml String * @since 2004-10-18 */ public void fromXML(String xml) { this.setUserObject(xml); } public final static short ROOT = 0; public final static short ALL_CLASSES = 1; public final static short ALL_PROPERTIES = 2; public final static short ALL_INSTANCES = 3; public final static short CLASS = 4; public final static short PROPERTY = 5; public final static short INSTANCE = 6; public final static short PACKAGE = 7; public final static short DOMAIN = 8; // used on INDUS Data source editor public final static short ATTRIBUTE = 9; public final static short AVH = 10; public final static short DB = 11; public final static short TABLE = 12; /** * * @param o Object * @return int * @since 2004-10-18 */ public int compareTo(Object o) { if (o instanceof TypedNode) { return getLocalName().compareTo( ( (TypedNode) o).getLocalName()); } return -2; } /** * TypedNode */ public TypedNode() { } } --- NEW FILE: TreeNodeMoveEditing.java --- package edu.iastate.utils.tree; import edu.iastate.utils.undo.EditingAction; /** * @since 2005-04-20 */ public class TreeNodeMoveEditing extends EditingAction { TypedTree tree; TypedNode oldParent, newParent; public TreeNodeMoveEditing(TypedTree tree, TypedNode location, TypedNode oldParent, TypedNode newParent) { this.tree = tree; this.location = location; this.oldParent = oldParent; this.newParent = newParent; this.summary = "Move node '" + location + "' from '" + oldParent + "' to '" + newParent + "'"; } public void undo() { // remove location from newParent if (newParent.isNodeChild( (TypedNode) location)) { tree.getModel().removeNodeFromParent( (TypedNode) location); } // and insert it to oldParent tree.getModel().insertNodeInto( (TypedNode) location, oldParent, oldParent.getChildCount()); tree.expandNode( (TypedNode) location); } public void redo() { // remove location from oldParent if (oldParent.isNodeChild( (TypedNode) location)) { tree.getModel().removeNodeFromParent( (TypedNode) location); } // insert location to newParent tree.getModel().insertNodeInto( (TypedNode) location, newParent, newParent.getChildCount()); tree.expandNode( (TypedNode) location); } } --- NEW FILE: TreeNodeRenameEditing.java --- package edu.iastate.utils.tree; import edu.iastate.utils.undo.EditingAction; /** * @since 2005-04-20 */ public class TreeNodeRenameEditing extends EditingAction { Object oldValue, newValue; public TreeNodeRenameEditing(TypedNode location, Object oldValue, Object newValue) { this.location = location; this.oldValue = oldValue; this.newValue = newValue; this.summary = "Change node '" + oldValue + "' -> '" + newValue + "'"; } public void undo() { ( (TypedNode) location).setUserObject(oldValue); } public void redo() { ( (TypedNode) location).setUserObject(newValue); } } --- NEW FILE: SortTreeModel.java --- package edu.iastate.utils.tree; // SortTreeModel.java // This class is similar to the DefaultTreeModel, but it keeps // a node's children in alphabetical order. // import java.util.Comparator; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.DefaultTreeModel; import javax.swing.tree.MutableTreeNode; import javax.swing.tree.TreeNode; /** * http://examples.oreilly.com/jswing2/code/ch17/SortTreeModel.java * * imported and modified 2005-04-21 * @author Jie Bao */ public class SortTreeModel extends DefaultTreeModel { private Comparator comparator; public SortTreeModel(TreeNode root) { super(root); comparator = new TreeStringComparator(); } public SortTreeModel(TreeNode node, Comparator c) { super(node); comparator = c; } public SortTreeModel(TreeNode node, boolean asksAllowsChildren, Comparator c) { super(node, asksAllowsChildren); comparator = c; } public void insertNodeInto(MutableTreeNode child, MutableTreeNode parent) { int index = findIndexFor(child, parent); super.insertNodeInto(child, parent, index); } public void insertNodeInto(MutableTreeNode child, MutableTreeNode par, int i) { // The index is useless in this model, so just ignore it. insertNodeInto(child, par); } // Perform a recursive binary search on the children to find the right // insertion point for the next node. private int findIndexFor(MutableTreeNode child, MutableTreeNode parent) { int cc = parent.getChildCount(); if (cc == 0) { return 0; } if (cc == 1) { return comparator.compare(child, parent.getChildAt(0)) <= 0 ? 0 : 1; } return findIndexFor(child, parent, 0, cc - 1); // First & last index } private int findIndexFor(MutableTreeNode child, MutableTreeNode parent, int i1, int i2) { if (i1 == i2) { return comparator.compare(child, parent.getChildAt(i1)) <= 0 ? i1 : i1 + 1; } int half = (i1 + i2) / 2; if (comparator.compare(child, parent.getChildAt(half)) <= 0) { return findIndexFor(child, parent, i1, half); } return findIndexFor(child, parent, half + 1, i2); } } class TreeStringComparator implements Comparator { public int compare(Object o1, Object o2) { if (! (o1 instanceof DefaultMutableTreeNode && o2 instanceof DefaultMutableTreeNode)) { throw new IllegalArgumentException( "Can only compare DefaultMutableTreeNode objects"); } String s1 = ( (DefaultMutableTreeNode) o1).getUserObject().toString(); String s2 = ( (DefaultMutableTreeNode) o2).getUserObject().toString(); return s1.compareToIgnoreCase(s2); } } --- NEW FILE: TreeNodeInsertEditing.java --- package edu.iastate.utils.tree; import edu.iastate.utils.undo.EditingAction; /** * @since 2005-04-20 */ public class TreeNodeInsertEditing extends EditingAction { TypedNode parent; TypedTree tree; public TreeNodeInsertEditing(TypedTree tree, TypedNode location, TypedNode parent) { this.tree = tree; this.location = location; this.parent = parent; this.summary = "Insert node '" + location + "' under '" + parent+"'"; } public void redo() { // insert location to parent if ( ( (TypedNode) location).getParent() != null) { tree.getModel().removeNodeFromParent( (TypedNode) location); } tree.getModel().insertNodeInto( (TypedNode) location, parent, parent.getChildCount()); tree.expandNode( (TypedNode) location); } public void undo() { // remove location from parent if (parent.isNodeChild( (TypedNode) location)) { tree.getModel().removeNodeFromParent( (TypedNode) location); } } } --- NEW FILE: TreeNodeCommentEditing.java --- package edu.iastate.utils.tree; import edu.iastate.utils.undo.EditingAction; /** * @since 2005-04-20 */ public class TreeNodeCommentEditing extends EditingAction { Object oldValue, newValue; public TreeNodeCommentEditing(TypedNode location, Object oldValue, Object newValue) { this.location = location; this.oldValue = oldValue; this.newValue = newValue; this.summary = "Change node '" + location + "'s comment '" + oldValue + "' -> '" + newValue + "'"; } public void undo() { ( (TypedNode) location).setComment(oldValue); } public void redo() { ( (TypedNode) location).setComment(newValue); } } |