From c7a9d4a6bd53ed7d61731770f2f10e8b9fd435f9 Mon Sep 17 00:00:00 2001 From: Fuwn Date: Thu, 12 Feb 2026 22:33:32 -0800 Subject: Initial commit --- NET/worlds/console/Tree.java | 282 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 282 insertions(+) create mode 100644 NET/worlds/console/Tree.java (limited to 'NET/worlds/console/Tree.java') diff --git a/NET/worlds/console/Tree.java b/NET/worlds/console/Tree.java new file mode 100644 index 0000000..b6f6d0c --- /dev/null +++ b/NET/worlds/console/Tree.java @@ -0,0 +1,282 @@ +package NET.worlds.console; + +import NET.worlds.scape.LibraryDropTarget; +import java.util.Hashtable; +import java.util.Vector; + +public class Tree extends TreePanel implements MainCallback, LibraryDropTarget { + private static final long serialVersionUID = -1919976160954835913L; + private int openItem = -1; + private int changeItem = -1; + private TreeCallback owner; + private boolean registered; + + public Tree() { + } + + public Tree(TreeCallback owner) { + this(); + this.setOwner(owner); + } + + public void setOwner(TreeCallback owner) { + this.owner = owner; + } + + public void change(TreeNode root, Object current) { + this.delayRepaints(true); + this.removeAllElements(); + this.addElement(root); + int index = this.search(0, current, new Hashtable()); + + assert index != -1; + + this.sync(index); + } + + public void change(TreeNode root, Vector lineage) { + this.delayRepaints(true); + this.removeAllElements(); + TreeNode parent = root; + this.addElement(root); + int index = 1; + + for (int j = 0; j < lineage.size(); j++) { + Object lookingFor = lineage.elementAt(j); + Vector v = this.getSortedChildren(parent); + parent.setOpen(true); + int count = v.size(); + int foundIndex = -1; + + for (int i = 0; i < count; i++) { + TreeNode child = v.elementAt(i); + int tmp = index + i; + this.insertElementAt(child, tmp); + if (foundIndex == -1 && child.getObject().equals(lookingFor)) { + foundIndex = tmp; + parent = child; + } + } + + assert foundIndex != -1; + + index = foundIndex + 1; + } + + this.sync(index - 1); + } + + public void update() { + Vector newTree = new Vector(); + int count = this.countElements(); + + for (int i = 0; i < count; i++) { + TreeNode e1 = this.elementAt(i); + if (e1.getParent() == null) { + newTree.addElement(e1); + this.recurseAddChildren(newTree, e1); + } + } + + TreeNode selected = this.getSelectedNode(); + count = newTree.size(); + int ix = 0; + + while (ix < count && !newTree.elementAt(ix).equals(selected)) { + ix++; + } + + int hitem = ix < count ? ix : Math.min(count - 1, this.getSelectedIndex()); + this.delayRepaints(true); + this.reset(newTree); + this.sync(hitem); + } + + private void sync(int selected) { + try { + this.select(selected); + Object treeNode = this.getSelectedNode().getObject(); + if (treeNode != null) { + this.owner.treeChange(treeNode); + this.delayRepaints(false); + } + } catch (Exception var3) { + } + } + + private Vector getSortedChildren(TreeNode parent) { + Vector unsorted = (Vector)parent.getChildren(); + Vector sorted = new Vector(unsorted.size()); + if (unsorted != null) { + int icount = unsorted.size(); + + for (int i = 0; i < icount; i++) { + TreeNode e = unsorted.elementAt(i); + if (!parent.shouldSort()) { + sorted.insertElementAt(e, i); + } else { + int jcount = sorted.size(); + int j = 0; + + while (j < jcount && sorted.elementAt(j).toString().compareTo(e.toString()) <= 0) { + j++; + } + + sorted.insertElementAt(e, j); + } + } + } + + return sorted; + } + + private Vector getCurrentChildren(TreeNode parent) { + Vector ret = new Vector(); + int count = this.countElements(); + + for (int i = 0; i < count; i++) { + TreeNode ele = this.elementAt(i); + if (ele.getParent() == parent) { + ret.addElement(ele); + } + } + + return ret; + } + + private static TreeNode maybeUseOldChild(TreeNode newChild, Vector oldChildren) { + int count = oldChildren.size(); + + for (int i = 0; i < count; i++) { + TreeNode oldChild = oldChildren.elementAt(i); + if (oldChild.equals(newChild)) { + return oldChild; + } + } + + return newChild; + } + + private void recurseAddChildren(Vector newTree, TreeNode oldEle) { + if (oldEle.isOpen()) { + Vector oldChildren = this.getCurrentChildren(oldEle); + Vector newChildren = this.getSortedChildren(oldEle); + int count = newChildren.size(); + + for (int i = 0; i < count; i++) { + TreeNode child = newChildren.elementAt(i); + child = maybeUseOldChild(child, oldChildren); + newTree.addElement(child); + this.recurseAddChildren(newTree, child); + } + } + } + + private int search(int index, Object obj, Hashtable lookedAt) { + TreeNode e = this.elementAt(index); + if (e.getObject().equals(obj)) { + return index; + } else { + if (!lookedAt.containsKey(e)) { + lookedAt.put(e, e); + index++; + Vector v = this.getSortedChildren(e); + e.setOpen(true); + if (v != null) { + int count = v.size(); + + for (int i = 0; i < count; i++) { + this.insertElementAt(v.elementAt(i), index + i); + } + + for (int i = 0; i < count; i++) { + int found = this.search(index + i, obj, lookedAt); + if (found != -1) { + return found; + } + } + + for (int ix = 0; ix < count; ix++) { + this.removeElementAt(index); + } + } + + e.setOpen(false); + } + + return -1; + } + } + + private void register() { + if (!this.registered) { + Main.register(this); + this.registered = true; + } + } + + @Override + public void treeSelect(int item) { + if (item != this.getSelectedIndex()) { + this.select(item); + this.changeItem = item; + this.register(); + } + } + + @Override + public void treeOpen(int item) { + if (this.openItem == -1) { + this.openItem = item; + this.register(); + } + } + + @Override + public void setFocus(boolean hasFocus) { + boolean hadFocus = this.hasFocus(); + super.setFocus(hasFocus); + if (hadFocus != hasFocus) { + this.owner.treeFocusChanged(hasFocus); + } + } + + @Override + public synchronized void mainCallback() { + this.delayRepaints(true); + int i = this.changeItem; + this.changeItem = -1; + if (i != -1) { + this.owner.treeChange(this.elementAt(i).getObject()); + } + + i = this.openItem; + this.openItem = -1; + if (i != -1) { + TreeNode e = this.elementAt(i); + boolean isOpen = !e.isOpen(); + e.setOpen(isOpen); + i++; + if (isOpen) { + Vector v = this.getSortedChildren(e); + if (v != null) { + int count = v.size(); + + for (int j = 0; j < count; j++) { + this.insertElementAt(v.elementAt(j), i++); + } + } + } else { + while (i < this.countElements() && e.isDescendant(this.elementAt(i))) { + this.removeElementAt(i); + } + } + + this.repaint(); + } + + this.delayRepaints(false); + this.registered = false; + Main.unregister(this); + } +} -- cgit v1.2.3