|
What this is
Other links
The source code
/*
* Sun Public License Notice
*
* The contents of this file are subject to the Sun Public License
* Version 1.0 (the "License"). You may not use this file except in
* compliance with the License. A copy of the License is available at
* http://www.sun.com/
*
* The Original Code is NetBeans. The Initial Developer of the Original
* Code is Sun Microsystems, Inc. Portions Copyright 1997-2000 Sun
* Microsystems, Inc. All Rights Reserved.
*/
package org.netbeans.modules.debugger.jpda.models;
import java.lang.ref.WeakReference;
import java.util.Vector;
import java.util.WeakHashMap;
import org.netbeans.spi.viewmodel.ComputingException;
import org.netbeans.spi.viewmodel.NoInformationException;
import org.netbeans.spi.viewmodel.TreeModel;
import org.netbeans.spi.viewmodel.TreeModelListener;
import org.netbeans.spi.viewmodel.UnknownTypeException;
/**
* Helps to translate one tree to another.
*
* @author Jan Jancura
*/
public abstract class TranslatingTreeModel implements TreeModel {
private TreeModel model;
private Vector listeners = new Vector ();
private Listener listener = new Listener ();
/* original Object to a new one.*/
private WeakHashMap cache = new WeakHashMap ();
private WeakHashMap bcache = new WeakHashMap ();
/**
* Creates a new instance of translating tree model for given
* tree model.
*
* @param model a tree model to be translated
*/
public TranslatingTreeModel (TreeModel model) {
this.model = model;
model.addTreeModelListener (listener);
}
/**
* Returns original tree model.
*
* @return original tree model
*/
protected TreeModel getModel () {
return model;
}
/**
* Creates a new translated node for given original one.
*
* @param o a node to be translated
* @return a new translated node
*/
protected abstract Object createTranslation (Object o)
throws UnknownTypeException;
/**
* Translates one node to another.
*
* @param o a node to be translated
* @return translated node
*/
protected Object translate (Object o) throws UnknownTypeException {
Object r = null;
WeakReference wr = (WeakReference) cache.get (o);
if (wr != null)
r = wr.get ();
if (r == null) {
r = createTranslation (o);
cache.put (o, new WeakReference (r));
bcache.put (r, o);
}
return r;
}
/**
* Returns original node for given translated one.
*
* @param o a translated node
* @return original node for given translated one
*/
protected Object getOriginalObject (Object o) {
return bcache.get (o);
}
/**
* Returns the translated root node of the tree or null, if the tree is empty.
*
* @return the translated root node of the tree or null
*/
public Object getRoot () {
try {
return translate (model.getRoot ());
} catch (UnknownTypeException e) {
return null;
}
}
/**
* Returns translated children for given parent on given indexes.
*
* @param parent a parent of returned nodes
* @throws NoInformationException if the set of children can not be
* resolved
* @throws ComputingException if the children resolving process
* is time consuming, and will be performed off-line
* @throws UnknownTypeException if this TreeModel implementation is not
* able to resolve dchildren for given node type
*
* @return translated children for given parent on given indexes
*/
public Object[] getChildren (Object parent, int from, int to)
throws NoInformationException, ComputingException, UnknownTypeException {
Object p = getOriginalObject (parent);
if (p == null) throw new UnknownTypeException (parent);
Object[] ch = model.getChildren (p, from, to);
int i, k = ch.length;
for (i = 0; i < k; i++)
ch [i] = translate (ch [i]);
return ch;
}
/**
* Returns number of children for given node.
*
* @param node the parent node
* @throws UnknownTypeException if this TreeModel implementation is not
* able to resolve children for given node type
*
* @return true if node is leaf
*/
public int getChildrenCount (Object node) throws NoInformationException,
ComputingException, UnknownTypeException {
Object p = getOriginalObject (node);
if (p == null) throw new UnknownTypeException (node);
return model.getChildrenCount (p);
}
/**
* Returns true if node is leaf.
*
* @throws UnknownTypeException if this TreeModel implementation is not
* able to resolve dchildren for given node type
* @return true if node is leaf
*/
public boolean isLeaf (Object node) throws UnknownTypeException {
Object o = getOriginalObject (node);
if (o == null) throw new UnknownTypeException (node);
return model.isLeaf (o);
}
/**
* Registers given listener.
*
* @param l the listener to add
*/
public void addTreeModelListener (TreeModelListener l) {
listeners.add (l);
}
/**
* Unregisters given listener.
*
* @param l the listener to remove
*/
public void removeTreeModelListener (TreeModelListener l) {
listeners.remove (l);
}
void fireTreeChanged () {
Vector v = (Vector) listeners.clone ();
int i, k = v.size ();
for (i = 0; i < k; i++)
((TreeModelListener) v.get (i)).treeChanged ();
}
void fireTreeNodeChanged (Object parent) {
Vector v = (Vector) listeners.clone ();
int i, k = v.size ();
for (i = 0; i < k; i++)
((TreeModelListener) v.get (i)).treeNodeChanged (parent);
}
private class Listener implements TreeModelListener {
public void treeNodeChanged (Object node) {
try {
fireTreeNodeChanged (translate (node));
} catch (UnknownTypeException e) {
}
}
public void treeChanged () {
fireTreeChanged ();
}
}
}
|
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.