KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jimm > datavision > gui > FieldPickerTree


1 package jimm.datavision.gui;
2 import java.awt.datatransfer.*;
3 import java.awt.dnd.*;
4 import java.awt.event.MouseAdapter JavaDoc;
5 import java.awt.event.MouseEvent JavaDoc;
6 import javax.swing.*;
7 import javax.swing.tree.*;
8
9 /**
10  * The {#link FieldPickerWin} uses this JTree subclass.
11  *
12  * @author Jim Menard, <a HREF="mailto:jimm@io.com">jimm@io.com</a>
13  */

14 public class FieldPickerTree
15     extends JTree
16     implements DragGestureListener, DragSourceListener
17 {
18
19 public FieldPickerTree(DefaultTreeModel model) {
20     super(model);
21
22     DragSource dragSource = DragSource.getDefaultDragSource();
23
24     dragSource.createDefaultDragGestureRecognizer(
25           this, // component where drag originates
26
DnDConstants.ACTION_COPY_OR_MOVE, // actions
27
this); // drag gesture recognizer
28

29     // Listen for double clicks
30
addMouseListener(new MouseAdapter JavaDoc() {
31     public void mousePressed(MouseEvent JavaDoc e) {
32 // int selRow = getRowForLocation(e.getX(), e.getY());
33
TreePath selPath = getPathForLocation(e.getX(), e.getY());
34         if (selPath != null && e.getClickCount() == 2)
35         doubleClicked(selPath);
36         super.mousePressed(e);
37     }
38     });
39 }
40
41 /**
42  * Handles double clicks. Opens an editor if one is available. The
43  * <code>FPLeafInfo</code> subclasses know how to open themselves, and
44  * it is harmless to try to open one that has no editor.
45  *
46  * @param selPath a tree path
47  */

48 protected void doubleClicked(TreePath selPath) {
49     DefaultMutableTreeNode typeNode =
50     (DefaultMutableTreeNode)getSelectionPath().getLastPathComponent();
51     Object JavaDoc obj = typeNode.getUserObject();
52     if (obj instanceof FPLeafInfo)
53     ((FPLeafInfo)obj).openEditor();
54 }
55
56 public void dragGestureRecognized(DragGestureEvent e) {
57     DefaultMutableTreeNode typeNode =
58     (DefaultMutableTreeNode)getSelectionPath().getLastPathComponent();
59     Object JavaDoc obj = typeNode.getUserObject();
60     if (obj instanceof FPLeafInfo) {
61     FPLeafInfo info = (FPLeafInfo)obj;
62     if (info != null) {
63         e.startDrag(DragSource.DefaultCopyDrop, // cursor
64
new StringSelection(info.dragString()), // transferable
65
this); // drag source listener
66
}
67     }
68 }
69
70 public void dragDropEnd(DragSourceDropEvent e) {}
71 public void dragEnter(DragSourceDragEvent e) {}
72 public void dragExit(DragSourceEvent e) {}
73 public void dragOver(DragSourceDragEvent e) {}
74 public void dropActionChanged(DragSourceDragEvent e) {}
75
76 /** Removes the currently selected node. */
77 public void removeCurrentNode() {
78     TreePath currentSelection = getSelectionPath();
79     if (currentSelection != null) {
80     DefaultMutableTreeNode currentNode = (DefaultMutableTreeNode)
81         (currentSelection.getLastPathComponent());
82     MutableTreeNode parent = (MutableTreeNode)(currentNode.getParent());
83     if (parent != null) {
84         ((DefaultTreeModel)getModel()).removeNodeFromParent(currentNode);
85     }
86     }
87 }
88
89
90 }
91
Popular Tags