org.fife.rsta.ac.SourceTreeNode Maven / Gradle / Ivy
/*
* 10/09/2011
*
* Copyright (C) 2011 Robert Futrell
* robert_futrell at users.sourceforge.net
* http://fifesoft.com/rsyntaxtextarea
*
* This library is distributed under a modified BSD license. See the included
* RSTALanguageSupport.License.txt file for details.
*/
package org.fife.rsta.ac;
import java.util.Collections;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Vector;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.MutableTreeNode;
import javax.swing.tree.TreeNode;
/**
* Base class for tree nodes in an {@link AbstractSourceTree}. They can be
* sorted and filtered based on user input.
*
* @author Robert Futrell
* @version 1.0
* @see AbstractSourceTree
*/
public class SourceTreeNode extends DefaultMutableTreeNode {
private boolean sortable;
private boolean sorted;
private String prefix;
private Vector visibleChildren;
private int sortPriority;
public SourceTreeNode(Object userObject) {
this(userObject, false);
}
public SourceTreeNode(Object userObject, boolean sorted) {
super(userObject);
visibleChildren = new Vector();
setSortable(true);
setSorted(sorted);
}
public void add(MutableTreeNode child) {
//super.add(child);
if(child!=null && child.getParent()==this) {
insert(child, super.getChildCount() - 1);
}
else {
insert(child, super.getChildCount());
}
if (sortable && sorted) {
refreshVisibleChildren(); // TODO: Find index and add for performance
}
}
public Enumeration children() {
return visibleChildren.elements();
}
public int compareTo(Object obj) {
int res = -1;
if (obj instanceof SourceTreeNode) {
SourceTreeNode stn2 = (SourceTreeNode)obj;
res = getSortPriority() - stn2.getSortPriority();
if (res==0 && ((SourceTreeNode)getParent()).isSorted()) {
res = toString().compareToIgnoreCase(stn2.toString());
}
}
return res;
}
/**
* Returns a comparator used to sort the child nodes of this node.
* The default implementation sorts alphabetically. Subclasses may want
* to override to return a comparator that groups by type of node and sorts
* by group, etc.
*
* @return A comparator.
*/
public Comparator createComparator() {
return new Comparator() {
public int compare(Object o1, Object o2) {
SourceTreeNode stn1 = (SourceTreeNode)o1;
SourceTreeNode stn2 = (SourceTreeNode)o2;
return stn1.compareTo(stn2);
}
};
}
/**
* Filters the children of this tree node based on the specified prefix.
*
* @param prefix The prefix. If this is null
, all possible
* children are shown. This should be all lower case.
*/
protected void filter(String prefix) {
this.prefix = prefix;
refreshVisibleChildren();
for (int i=0; i 0 ? getChildAt(index - 1) : null;
}
public int getChildCount() {
return visibleChildren.size();
}
public int getIndex(TreeNode child) {
if (child==null) {
throw new IllegalArgumentException("child cannot be null");
}
for (int i=0; i")) { // Strip out HTML
text = text.replaceAll("<[^>]+>", "");
}
if (!text.toLowerCase().startsWith(prefix)) {
//System.out.println("Removing tree node: " + text);
i.remove();
}
}
}
}
}
}
/**
* Sets whether this particular node's children are sortable. Usually,
* only tree nodes containing only "leaves" should be sorted (for example,
* a "types" node).
*
* @param sortable Whether this node's children are sortable.
* @see #isSortable()
*/
public void setSortable(boolean sortable) {
this.sortable = sortable;
}
/**
* Sets whether this tree node (and any child sortable tree nodes) are
* sorting their children.
*
* @param sorted Whether sorting is enabled.
* @see #isSorted()
*/
public void setSorted(boolean sorted) {
if (sorted!=this.sorted) {
// This individual node may not be sortable...
if (sortable) {
this.sorted = sorted;
refreshVisibleChildren();
}
// But its children could still be.
for (int i=0; i