[Bprocessor-commit] gui/src/net/sourceforge/bprocessor/gui/treeview SurfacesTreeView.java,NONE,1.1
Status: Pre-Alpha
Brought to you by:
henryml
From: Jesper P. <je...@us...> - 2005-07-01 11:28:11
|
Update of /cvsroot/bprocessor/gui/src/net/sourceforge/bprocessor/gui/treeview In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv5343 Added Files: SurfacesTreeView.java Log Message: Initial import --- NEW FILE: SurfacesTreeView.java --- //--------------------------------------------------------------------------------- // $Id: SurfacesTreeView.java,v 1.1 2005/07/01 11:28:01 jews Exp $ // // Copyright (c) 2005 The BProcessor Team (http://bprocessor.sourceforge.net) // Released under the Lesser GNU Public License v2.1 //--------------------------------------------------------------------------------- package net.sourceforge.bprocessor.gui.treeview; import net.sourceforge.bprocessor.kernel.notification.Notification; import net.sourceforge.bprocessor.kernel.notification.NotificationListener; import net.sourceforge.bprocessor.kernel.notification.Notifier; import net.sourceforge.bprocessor.model.Surface; import net.sourceforge.bprocessor.model.SurfaceFacade; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.util.ArrayList; import java.util.Enumeration; import java.util.Iterator; import java.util.List; import java.util.Vector; import javax.swing.event.TreeModelEvent; import javax.swing.event.TreeModelListener; import javax.swing.tree.TreeModel; import javax.swing.tree.TreeNode; import javax.swing.tree.TreePath; import org.apache.log4j.Logger; /** * The surfaces tree view */ public class SurfacesTreeView extends TreeView implements NotificationListener { /** The logger */ private static Logger log = Logger.getLogger(SurfacesTreeView.class); /** * Constructor */ public SurfacesTreeView() { super(); setModel(new SurfaceModel()); addMouseListener(new MouseAdaptor()); Notifier.getInstance().addListener(this); } /** * Handle a notification * @param n The notification */ public void handleNotification(Notification n) { if (log.isDebugEnabled()) { log.debug(n); } String type = n.getType(); if (type.equals(Notification.SURFACE_CREATED)) { SurfaceFacade sf = SurfaceFacade.getInstance(); Surface s = sf.findById(n.getObject()); RootNode rn = (RootNode)getModel().getRoot(); SurfaceNode sn = new SurfaceNode(n.getObject(), s.getName(), rn); rn.add(sn); TreePath tp = new TreePath(new Object[] {rn}); getModel().valueForPathChanged(tp, sn); } } /** * Should the listener handle this notification * @param type The notification type * @return True if it should handle it; otherwise false */ public boolean isNotificationEnabled(String type) { if (type.equals(Notification.SURFACE_CREATED) || type.equals(Notification.SURFACE_DELETED) || type.equals(Notification.SURFACE_RENAMED) || type.equals(Notification.SURFACE_SELECTED) || type.equals(Notification.SURFACE_MODIFIED)) { return true; } return false; } /** * The surface model */ class SurfaceModel implements TreeModel { /** The tree listeners */ private List listeners; /** The root */ private RootNode root; /** * Constructor */ SurfaceModel() { this.listeners = new ArrayList(); this.root = new RootNode(); } /** * Adds a tree model listener * @param l The listener */ public void addTreeModelListener(TreeModelListener l) { listeners.add(l); } /** * Removes a tree model listener * @param l The listener */ public void removeTreeModelListener(TreeModelListener l) { listeners.remove(l); } /** * Get the child of a parent * @param parent The parent * @param index The child index * @return The child */ public Object getChild(Object parent, int index) { TreeNode n = root.findNode(parent); if (n != null) { if (index < n.getChildCount()) { return n.getChildAt(index); } } return null; } /** * Get the child count of a parent * @param parent The parent * @return The child count */ public int getChildCount(Object parent) { TreeNode n = root.findNode(parent); if (n != null) { return n.getChildCount(); } return 0; } /** * Get the index of the child * @param parent The parent * @param child The child * @return The index */ public int getIndexOfChild(Object parent, Object child) { TreeNode n = root.findNode(parent); if (n != null) { return n.getIndex((TreeNode)child); } return -1; } /** * Get the root of the model * @return The root */ public Object getRoot() { return root; } /** * Is the node a leaf * @param node The node * @return True if leaf; otherwise false */ public boolean isLeaf(Object node) { TreeNode n = root.findNode(node); if (n != null) { return n.isLeaf(); } return true; } /** * The value for a path has changed * @param path The path * @param newValue The new value */ public void valueForPathChanged(TreePath path, Object newValue) { Iterator it = listeners.iterator(); TreeModelEvent e = new TreeModelEvent(newValue, path); while (it.hasNext()) { TreeModelListener l = (TreeModelListener)it.next(); l.treeStructureChanged(e); } } } /** * Generic node */ abstract class GenericNode implements TreeNode { /** Type: Root */ static final int TYPE_ROOT = 0; /** Type: Surface */ static final int TYPE_SURFACE = 1; /** Type */ private int type; /** * Constructor * @param type The type */ GenericNode(int type) { this.type = type; } /** * Get the children of this node * @return The children */ public abstract Enumeration children(); /** * Does this node allow children * @return True for yes; otherwise false */ public abstract boolean getAllowsChildren(); /** * Get the child at a given position * @param childIndex The index * @return The child node */ public abstract TreeNode getChildAt(int childIndex); /** * Get the child count * @return The count */ public abstract int getChildCount(); /** * Get the index of a node * @param node The node * @return The index */ public abstract int getIndex(TreeNode node); /** * Get the parent of this node * @return The parent */ public abstract TreeNode getParent(); /** * Is this node a leaf ? * @return True if leaf; otherwise false */ public abstract boolean isLeaf(); /** * Find a node * @param o The object * @return The node or null if not found */ public GenericNode findNode(Object o) { if (this == o) { return this; } Enumeration e = children(); while (e.hasMoreElements()) { GenericNode gn = (GenericNode)e.nextElement(); GenericNode result = gn.findNode(o); if (result != null) { return result; } } return null; } /** * Get the type of the node * @return The type */ public int getType() { return type; } } /** * Root node */ class RootNode extends GenericNode { /** Surface nodes */ private Vector nodes; /** * Constructor */ RootNode() { super(GenericNode.TYPE_ROOT); this.nodes = new Vector(); } /** * Add a surface * @param s The surface */ public void add(SurfaceNode s) { nodes.add(s); } /** * Remove a surface * @param s The surface */ public void remove(SurfaceNode s) { nodes.remove(s); } /** * Get the children of this node * @return The children */ public Enumeration children() { return nodes.elements(); } /** * Does this node allow children * @return True for yes; otherwise false */ public boolean getAllowsChildren() { return true; } /** * Get the child at a given position * @param childIndex The index * @return The child node */ public TreeNode getChildAt(int childIndex) { return (TreeNode)nodes.elementAt(childIndex); } /** * Get the child count * @return The count */ public int getChildCount() { return nodes.size(); } /** * Get the index of a node * @param node The node * @return The index */ public int getIndex(TreeNode node) { return nodes.indexOf(node); } /** * Get the parent of this node * @return The parent */ public TreeNode getParent() { return null; } /** * Is this node a leaf ? * @return True if leaf; otherwise false */ public boolean isLeaf() { return false; } /** * String representation * @return The representation */ public String toString() { return "Surfaces"; } } /** * Surface node */ class SurfaceNode extends GenericNode { /** The object id */ private Long id; /** The name */ private String name; /** The parent */ private RootNode parent; /** Surface nodes */ private Vector nodes; /** * Constructor * @param id The id of the object * @param name The name * @param parent The parent */ SurfaceNode(Long id, String name, RootNode parent) { super(GenericNode.TYPE_SURFACE); this.id = id; this.name = name; this.parent = parent; this.nodes = new Vector(); } /** * Get the id * @return The id */ public Long getId() { return id; } /** * Get the name * @return The name */ public String getName() { return name; } /** * Set the name * @param name The name */ public void setName(String name) { this.name = name; } /** * Get the children of this node * @return The children */ public Enumeration children() { return nodes.elements(); } /** * Does this node allow children * @return True for yes; otherwise false */ public boolean getAllowsChildren() { return true; } /** * Get the child at a given position * @param childIndex The index * @return The child node */ public TreeNode getChildAt(int childIndex) { return (TreeNode)nodes.elementAt(childIndex); } /** * Get the child count * @return The count */ public int getChildCount() { return nodes.size(); } /** * Get the index of a node * @param node The node * @return The index */ public int getIndex(TreeNode node) { return nodes.indexOf(node); } /** * Get the parent of this node * @return The parent */ public TreeNode getParent() { return parent; } /** * Is this node a leaf ? * @return True if leaf; otherwise false */ public boolean isLeaf() { return false; } /** * String representation * @return The representation */ public String toString() { return getName(); } } /** * Mouse adaptor */ class MouseAdaptor implements MouseListener { /** * Mouse clicked * @param e The mouse event */ public void mouseClicked(MouseEvent e) { } /** * Mouse button was pressed * @param e The mouse event */ public void mousePressed(MouseEvent e) { int selRow = getRowForLocation(e.getX(), e.getY()); TreePath selPath = getPathForLocation(e.getX(), e.getY()); if (selRow != -1) { GenericNode gn = (GenericNode)selPath.getLastPathComponent(); if (gn.getType() == GenericNode.TYPE_SURFACE) { SurfaceNode sn = (SurfaceNode)gn; Notification n = new Notification(Notification.SURFACE_SELECTED, sn.getId()); Notifier.getInstance().sendNotification(n); } } } /** * Mouse button was released * @param e The mouse event */ public void mouseReleased(MouseEvent e) { } /** * Mouse exit * @param e The mouse event */ public void mouseExited(MouseEvent e) { } /** * Mouse enter * @param e The mouse event */ public void mouseEntered(MouseEvent e) { } } } |