1
2 package org.catacomb.druid.swing;
3
4 import java.awt.Color;
5 import java.awt.dnd.DnDConstants;
6 import java.awt.event.InputEvent;
7 import java.awt.event.MouseEvent;
8 import java.awt.event.MouseListener;
9
10 import javax.swing.JTree;
11 import javax.swing.event.TreeSelectionEvent;
12 import javax.swing.event.TreeSelectionListener;
13 import javax.swing.tree.TreePath;
14 import javax.swing.tree.TreeSelectionModel;
15
16 import org.catacomb.druid.swing.dnd.InternalTransferHandler;
17 import org.catacomb.druid.swing.ui.DruidTreeUI;
18 import org.catacomb.interlish.structure.SelectionActor;
19 import org.catacomb.interlish.structure.Tree;
20
21
22 public class DTree extends JTree
23 implements TreeSelectionListener, MouseListener {
24 static final long serialVersionUID = 1001;
25
26
27 public SelectionActor selectionActor;
28
29
30 DTreeDragSource dTreeDragSource;
31 DTreeDropTarget dTreeDropTarget;
32
33 DTreeModel dTreeModel;
34 DMenu dMenu;
35
36 boolean p_doneMouseListener;
37
38 TCRenderer renderer;
39
40 public DTree() {
41 super();
42 p_doneMouseListener = false;
43
44
45 setForeground(Color.black);
46
47 setUI(new DruidTreeUI());
48
49 init();
50
51 addTreeSelectionListener(this);
52 }
53
54
55
56 public void setSelectionActor(SelectionActor sa) {
57 selectionActor = sa;
58 }
59
60
61 public void valueChanged(TreeSelectionEvent e) {
62 Object obj = getLastSelectedPathComponent();
63
64 if (selectionActor != null) {
65 selectionActor.selectionAction(obj, "DTree");
66 }
67
68 }
69
70
71
72 public void setBg(Color c) {
73 setBackground(c);
74 renderer.setBackgroundNonSelectionColor(c);
75 }
76
77
78 public void init() {
79 getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
80 renderer = new TCRenderer();
81 setCellRenderer(renderer);
82 }
83
84
85 public void setTree(Tree tree) {
86 dTreeModel = new DTreeModel(tree);
87 setModel(dTreeModel);
88 }
89
90 public void treeModified() {
91 dTreeModel.treeModified();
92 }
93
94
95 public void clear() {
96 setModel(new DTreeModel(new EmptyTree()));
97 }
98
99
100 public void dTreeExpandPath(Object[] oa) {
101 TreePath tp = new TreePath(oa);
102 expandPath(tp);
103 }
104
105
106 public void setSelected(Object[] oa) {
107 TreePath tp = new TreePath(oa);
108 setSelectionPath(tp);
109 }
110
111
112
113 public void setMenu(DMenu dm) {
114 if (!p_doneMouseListener) {
115 addMouseListener(this);
116 p_doneMouseListener = true;
117 }
118 dMenu = dm;
119
120 }
121
122
123 public void mousePressed(MouseEvent e) {
124 int modif = e.getModifiers();
125
126 if (dMenu != null) {
127 dMenu.preShowSync();
128 if ((modif & InputEvent.BUTTON3_MASK) != 0) {
129 dMenu.getPopupMenu().show(this, e.getX(), e.getY());
130 }
131 }
132 }
133
134
135 public void mouseClicked(MouseEvent e) {}
136 public void mouseReleased(MouseEvent e) {}
137 public void mouseEntered(MouseEvent e) {}
138 public void mouseExited(MouseEvent e) {}
139
140
141
142
143 public void enableDrag() {
144 if (dTreeDragSource == null) {
145 dTreeDragSource = new DTreeDragSource(this, DnDConstants.ACTION_COPY_OR_MOVE);
146 }
147
148 if (dTreeDropTarget == null) {
149 dTreeDropTarget = new DTreeDropTarget(this);
150 }
151
152 setTransferHandler(new InternalTransferHandler());
153
154 setDragEnabled(true);
155 }
156
157
158 public void ensureVisible(String sfc) {
159 }
160
161
162 }