001: package net.xoetrope.swing;
002:
003: import java.awt.event.MouseEvent;
004: import java.awt.event.MouseListener;
005: import javax.swing.JTree;
006: import javax.swing.event.TreeSelectionEvent;
007: import javax.swing.event.TreeSelectionListener;
008: import javax.swing.tree.TreePath;
009:
010: import net.xoetrope.swing.tree.XTreeModelAdapter;
011: import net.xoetrope.xui.XAttributedComponent;
012: import net.xoetrope.xui.XPage;
013: import net.xoetrope.xui.data.XModel;
014: import net.xoetrope.xui.events.XHandlerInvoker;
015: import net.xoetrope.xui.events.XListenerHelper;
016: import net.xoetrope.xui.style.XStyleComponent;
017:
018: /**
019: * @todo add listeners
020: * @todo add data bindings
021: * @todo add simple path/node selection interfaces
022: */
023:
024: /**
025: * A tree control, wraps JTree
026: * <p>Copyright (c) Xoetrope Ltd., 1998-2004<br>
027: * License: see license.txt
028: * $Revision: 1.7 $
029: */
030: public class XTree extends JTree implements XAttributedComponent,
031: TreeSelectionListener, XListenerHelper, XStyleComponent,
032: MouseListener {
033: private XHandlerInvoker invoker;
034: private XModel selectedNode, lastSelectedNode;
035: private String styleName;
036:
037: public XTree() {
038: setScrollsOnExpand(true);
039: addTreeSelectionListener(this );
040: addMouseListener(this );
041: }
042:
043: /**
044: * Set one or more attributes of the component.
045: * @param attribName the name of the attribute
046: * @param attribValue the value of the attribute
047: */
048: public void setAttribute(String attribName, String attribValue) {
049: }
050:
051: /**
052: * Called whenever the value of the selection changes.
053: * @param e the event that characterizes the change.
054: */
055: public void valueChanged(TreeSelectionEvent e) {
056: invokeSelection();
057: }
058:
059: private void invokeSelection() {
060: getSelectedNode();
061: if ((selectedNode != null) && (invoker != null))
062: invoker.invoke();
063: }
064:
065: /**
066: * Get the selected model node.
067: * @return
068: */
069: public XModel getSelectedNode() {
070: TreePath tp = getSelectionPath();
071: if (tp != null) {
072: Object path[] = tp.getPath();
073: if ((path != null) && (path.length > 0))
074: selectedNode = ((XTreeModelAdapter) path[path.length - 1])
075: .getModel();
076: else
077: selectedNode = null;
078:
079: return selectedNode;
080: }
081:
082: return null;
083: }
084:
085: /**
086: * Add an event handler response method to a component such that the page's
087: * response method is invoked when the event occurs
088: * @param page the page containing the method
089: * @param handlerType the type of event handler
090: * @param methodName the method to invoke
091: * @throws NoSuchMethodException
092: */
093: public void addHandler(XPage page, String handlerType,
094: String methodName) throws NoSuchMethodException {
095: invoker = new XHandlerInvoker(page, this , methodName);
096: }
097:
098: public void setStyle(String style) {
099: styleName = style;
100: }
101:
102: public String getStyleName() {
103: return styleName;
104: }
105:
106: public void mouseEntered(MouseEvent me) {
107: }
108:
109: public void mouseExited(MouseEvent me) {
110: }
111:
112: /**
113: * Allow a mouse event to trigger the event if it is the same as the last
114: * selection. In other cases the tree selection listener will handle the event.
115: * @param me
116: */
117: public void mouseClicked(MouseEvent me) {
118: if (lastSelectedNode == getSelectedNode())
119: invokeSelection();
120: lastSelectedNode = selectedNode;
121: }
122:
123: public void mouseReleased(MouseEvent me) {
124: }
125:
126: public void mousePressed(MouseEvent me) {
127: }
128: }
|