home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Late Night VRML 2.0 with Java CD-ROM
/
code.zip
/
Ch12
/
ui
/
tree
/
TreeNode.java
< prev
next >
Wrap
Text File
|
1997-01-02
|
8KB
|
349 lines
/* file : TreeNode.java
* purpose : represents a node in a Tree
* may have any number of children
* children are ordered (by time of creation)
* author : Thomas Koch
* copyright : 1995 , GMD
* (Forschungszentrum Informationstechnik GmbH, FIT.CSCW,
* GMD Schloss Birlinghoven, 53754 Sankt Augustin, Germany).
*
*
* Extended to include image labels and highlighting.
* Author : Cameron Gillies
*/
package ui.tree;
import java.lang.*;
import java.util.*;
import java.net.URL;
import java.awt.*;
import java.awt.image.*;
import java.net.MalformedURLException;
import java.io.*;
import ui.tree.*;
public class TreeNode extends Canvas implements ImageObserver
{
private Vector children = null; // Vector of TreeNode
protected String info = null; // encoded node position in tree
protected boolean is_selected,is_open;
protected Image oIcon = null;
protected Image cIcon = null;
protected Image showImage = null;
protected URL url = null;
protected int imgHeight = 0;
protected int imgWidth = 0;
protected Color drawColor;
protected Color highlightColor;
public int x,y; // x,y will be set by TreeView
public int wid,hei; // wid,hei should be set in subclass
public TreeNode()
{
this("","","");
}
public TreeNode(String label, String cPath)
{
this(label,cPath,cPath);
}
public TreeNode(String label, String cPath, String oPath)
{
is_selected = false;
is_open = true;
if((cPath!="") || (cPath != null))
cIcon = setUpImages(cPath,cIcon);
if((oPath!="") || (oPath != null))
oIcon = setUpImages(oPath,oIcon);
if(is_open)
prepareImage(oIcon, this);
else
prepareImage(cIcon, this);
info = new String(label); // root
children = new Vector(3);
// paranoia setting :
x = 0 ;
y = 0 ;
wid = 40;
hei = 20;
}
private URL stringToURL(String path)
{
String urlPrefix = "file:";
// File f = new File(path);
// if (f.exists())
// {
try
{
//System.out.println("URL = " + urlPrefix + path);
return new URL(urlPrefix + path);
}
catch(MalformedURLException ex)
{
System.out.println("Malformed URL");
return null;
}
// }
// else
// return null;
}
/*
** Loads in the required image and prepares it to be shown
*/
protected Image setUpImages(String fname,Image img)
{
url = stringToURL(fname);
if (url == null)
{
System.out.println("Unable to open file - wrong path");
return img;
}
else
{
//System.out.println("Url = " + url);
img = Toolkit.getDefaultToolkit().getImage(url);
}
return img;
}
// override this to show your own data presented by node
public void draw(Graphics g)
{
FontMetrics fm = g.getFontMetrics();
String str = new String("");
if ((!is_open) && (!isLeaf()))
{
showImage = cIcon;
str += "[+] ";
}
else
showImage = oIcon;
str +=info;
imgWidth = 0;
imgHeight = 0;
if (showImage!=null)
{
imgWidth = showImage.getWidth(this);
imgHeight = showImage.getHeight(this);
g.drawImage(showImage, x , y , this);
}
// set the highlighted area
wid = imgWidth+fm.stringWidth(str)+2;
hei = fm.getHeight()+3;
if (is_selected)
{
drawColor = Color.white;
highlightColor = Color.blue;
}
else
{
drawColor = Color.black;
highlightColor = Color.white;
}
g.setColor(highlightColor);
g.fillRect(x+imgWidth+2,y,fm.stringWidth(str)+2,fm.getHeight()+2);
g.setColor(drawColor);
g.drawString(str,x+imgWidth+3,y+hei-5);
}
public boolean imageUpdate(Image img, int flags, int x, int y, int w, int h)
{
super.imageUpdate(img,flags,x,y,w,h);
if((flags & ALLBITS)==0)
repaint(100);
return super.imageUpdate(img, flags, x, y, w, h);
}
public synchronized void addNotify()
{
super.addNotify();
repaint();
}
public boolean inside(int xc,int yc) {
if ((xc<x) || (xc>x+wid))
return false;
if ((yc<y) || (yc>y+hei))
return false;
return true;
}
public String toString() {
return info;
}
public void setString(String str)
{
info = str;
}
public int numberChilds() {
return children.size();
}
public TreeNode getChild(int i) {
return (TreeNode) children.elementAt(i);
}
public boolean isLeaf() {
return (children.isEmpty());
}
public boolean isSelected() {
return is_selected;
}
public boolean isOpen() {
return is_open;
}
public void select() {
is_selected=true;
}
public void unSelect() {
is_selected=false;
}
public void open() {
is_open = true;
}
public void close() {
is_open = false;
}
public void addChild(TreeNode n) {
int s = children.size();
children.addElement(n);
}
public void changeInfo(int atpos,int toval) {
String sub1 = info.substring(0,atpos);
String val = String.valueOf(toval);
String sub2 = null;
if ((atpos+1)<info.length())
sub2 = val.concat(info.substring(atpos+1,info.length()));
else sub2 = val;
info = sub1.concat(sub2);
// System.out.println("changed info string to "+info);
for (int i=0;i<numberChilds();i++) {
TreeNode child = getChild(i);
child.changeInfo(atpos,toval);
}
}
public void removeSubtree() {
for (int i=0;i<numberChilds();i++) {
TreeNode child = getChild(i);
if (!child.isLeaf())
child.removeSubtree();
}
children.removeAllElements();
}
public void removeSubtree(TreeTool t) {
// System.out.println("TreeNode::removeSubtree(TreeTool) - this is node "+this);
for (int i=0;i<numberChilds();i++) {
TreeNode child = getChild(i);
if (!child.isLeaf())
child.removeSubtree(t);
}
TreeNode tokill = null;
do {
try {
tokill = (TreeNode) children.lastElement();
} catch(Exception e) { tokill=null; } // no more children
if(tokill!=null) {
children.removeElement(tokill);
t.notifyRemove(tokill);
}
} while(tokill!=null);
}
public boolean removeChild(TreeNode n) { // removes complete subtree !
// System.out.println("TreeNode::removeChild("+n+") - this is node "+this);
TreeNode update = null;
int index = children.indexOf(n);
if (index<0) {
System.out.println("TreeNode::removeChild() Error - not a child");
return false;
}
n.removeSubtree();
children.removeElementAt(index);
// run the garbage collector !
System.gc();
int pos = info.length();
while (index<numberChilds()) {
update = getChild(index);
update.changeInfo(pos,index);
index++;
}
return true;
}
public boolean removeChild(TreeNode n,TreeTool t) { // removes complete subtree !
// System.out.println("TreeNode::removeChild("+n+",TreeTool) - this is node "+this);
TreeNode update = null;
int index = children.indexOf(n);
if (index<0) {
System.out.println("TreeNode::removeChild() Error - not a child");
return false;
}
n.removeSubtree(t);
children.removeElementAt(index);
t.notifyRemove(n);
n = null;
// run the garbage collector !
System.gc();
int pos = info.length();
while (index<numberChilds()) {
update = getChild(index);
update.changeInfo(pos,index);
index++;
}
return true;
}
public boolean isChild(TreeNode n) {
return children.contains(n);
}
public boolean equals(Object obj) {
TreeNode node = (TreeNode)obj;
if(node == this)
return true;
else
return false;
}
}