From 66710e60dd1b7feb089e2668a80f930712c2c828 Mon Sep 17 00:00:00 2001 From: jochen Date: Wed, 24 Feb 1999 19:14:59 +0000 Subject: [PATCH] cache isLeaf, insert elements sorted git-svn-id: https://svn.code.sf.net/p/jode/code/trunk@296 379699f6-c40d-0410-875b-85095c16579e --- jode/jode/swingui/PackagesTreeModel.java | 36 ++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/jode/jode/swingui/PackagesTreeModel.java b/jode/jode/swingui/PackagesTreeModel.java index d49933a..fe1b5be 100644 --- a/jode/jode/swingui/PackagesTreeModel.java +++ b/jode/jode/swingui/PackagesTreeModel.java @@ -11,20 +11,38 @@ public class PackagesTreeModel implements TreeModel { class TreeElement { String fullName; String name; + boolean leaf; public TreeElement(String prefix, String name) { this.fullName = prefix+name; this.name = name; + this.leaf = !ClassInfo.isPackage(fullName); } public String getFullName() { return fullName; } + public String getName() { + return name; + } + + public boolean isLeaf() { + return leaf; + } + public String toString() { return name; } + public int compareTo(Object o) { + TreeElement other = (TreeElement) o; + if (leaf != other.leaf) + // files come after directories + return leaf ? 1 : -1; + return fullName.compareTo(other.fullName); + } + public boolean equals(Object o) { return (o instanceof TreeElement) && fullName == ((TreeElement)o).fullName; @@ -46,7 +64,21 @@ public class PackagesTreeModel implements TreeModel { Enumeration enum = ClassInfo.getClassesAndPackages(parent.getFullName()); while (enum.hasMoreElements()) { - v.addElement(new TreeElement(prefix, (String)enum.nextElement())); + //insert sorted and remove double elements; + String name = (String)enum.nextElement(); + TreeElement newElem = new TreeElement(prefix, name); + for (int i=0; ; i++) { + if (i == v.size()) { + v.addElement(newElem); + break; + } + int compare = newElem.compareTo(v.elementAt(i)); + if (compare < 0) { + v.insertElementAt(newElem, i); + break; + } else if (compare == 0) + break; + } } result = new TreeElement[v.size()]; v.copyInto(result); @@ -88,7 +120,7 @@ public class PackagesTreeModel implements TreeModel { } public boolean isLeaf(Object node) { - return !ClassInfo.isPackage(((TreeElement) node).getFullName()); + return ((TreeElement)node).isLeaf(); } public String getFullName(Object node) {