1 package org.catacomb.druid.swing;
2
3 import org.catacomb.druid.gui.base.DummyTree;
4 import org.catacomb.druid.swing.ui.DruidTreeUI;
5 import org.catacomb.interlish.structure.SelectionActor;
6 import org.catacomb.interlish.structure.Tree;
7 import org.catacomb.report.E;
8
9
10 import java.awt.Color;
11 import java.awt.event.InputEvent;
12 import java.awt.event.MouseEvent;
13 import java.awt.event.MouseListener;
14 import java.util.HashMap;
15
16 import javax.swing.JCheckBox;
17 import javax.swing.JTree;
18 import javax.swing.event.TreeSelectionEvent;
19 import javax.swing.event.TreeSelectionListener;
20 import javax.swing.tree.TreePath;
21 import javax.swing.tree.TreeSelectionModel;
22
23 import java.util.ArrayList;
24
25
26 public class DCheckboxTree extends JTree implements TreeSelectionListener, MouseListener {
27
28 static final long serialVersionUID = 1001;
29
30
31 public SelectionActor selectionActor;
32
33
34 DTreeModel dTreeModel;
35 DMenu dMenu;
36
37 boolean p_doneMouseListener;
38
39
40 ToggleItem[] toggleItems;
41 HashMap<String, ToggleItem> tiHM;
42
43
44 private CheckTreeSelectionModel selectionModel;
45 int hotspot = new JCheckBox().getPreferredSize().width;
46
47
48
49 public DCheckboxTree() {
50 super();
51 p_doneMouseListener = false;
52
53 setForeground(Color.black);
54 setUI(new DruidTreeUI());
55 tiHM = new HashMap<String, ToggleItem>();
56 getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
57 TCRenderer renderer = new TCRenderer();
58 setCellRenderer(renderer);
59 addTreeSelectionListener(this);
60 setTree(new DummyTree());
61 }
62
63
64
65 public void setSelectionActor(SelectionActor sa) {
66 selectionActor = sa;
67 }
68
69
70 public void setTree(Tree tree) {
71 dTreeModel = new DTreeModel(tree);
72 setModel(dTreeModel);
73
74 selectionModel = new CheckTreeSelectionModel(dTreeModel);
75 setCellRenderer(new CheckTreeRenderer(new TCRenderer(), selectionModel));
76 addMouseListener(this);
77 selectionModel.addTreeSelectionListener(this);
78 }
79
80
81
82 public void mouseClicked(MouseEvent me) {
83 TreePath path = getPathForLocation(me.getX(), me.getY());
84 if (path == null) {
85 return;
86 }
87
88 if (me.getX() > getPathBounds(path).x + hotspot) {
89 return;
90 }
91
92 boolean selected = selectionModel.isPathSelected(path, true);
93 selectionModel.removeTreeSelectionListener(this);
94
95 try {
96 if (selected) {
97 selectionModel.removeSelectionPath(path);
98 } else {
99
100
101
102 selectionModel.addSelectionPath(path);
103
104
105
106
107
108 }
109 } finally {
110 selectionModel.addTreeSelectionListener(this);
111 treeDidChange();
112 }
113 }
114
115
116 public CheckTreeSelectionModel getSelectionModel() {
117 if (selectionModel == null) {
118 selectionModel = new CheckTreeSelectionModel(new DTreeModel(new EmptyTree()));
119 }
120 return selectionModel;
121 }
122
123
124 public void valueChanged(TreeSelectionEvent e) {
125 treeDidChange();
126 }
127
128
129 public void clear() {
130 setModel(new DTreeModel(new EmptyTree()));
131 }
132
133
134 public void dTreeExpandPath(Object[] oa) {
135 TreePath tp = new TreePath(oa);
136 expandPath(tp);
137 }
138
139
140 public void ensureVisible(Object[] oa) {
141 if (oa.length > 1) {
142 Object[] oas = new Object[oa.length - 1];
143 for (int i = 0; i < oa.length - 1; i++) {
144 oas[i] = oa[i];
145 }
146 dTreeExpandPath(oas);
147 }
148
149
150 dTreeExpandPath(oa);
151 }
152
153
154
155 public void setMenu(DMenu dm) {
156 if (!p_doneMouseListener) {
157 addMouseListener(this);
158 p_doneMouseListener = true;
159 }
160 dMenu = dm;
161
162 }
163
164
165 public void mousePressed(MouseEvent e) {
166 int modif = e.getModifiers();
167
168 if (dMenu != null) {
169 dMenu.preShowSync();
170 if ((modif & InputEvent.BUTTON3_MASK) != 0) {
171 dMenu.getPopupMenu().show(this, e.getX(), e.getY());
172 }
173 }
174 }
175
176
177
178 public void setBg(Color c) {
179 setBackground(c);
180 }
181
182
183
184
185
186
187 public void mouseReleased(MouseEvent e) {
188 }
189
190
191
192 public void mouseEntered(MouseEvent e) {
193 }
194
195 public void mouseExited(MouseEvent e) {
196 }
197
198
199
200
201
202
203
204 private String makeSlashPath(TreePath tp) {
205 Object[] oa = tp.getPath();
206
207 StringBuffer sb = new StringBuffer();
208
209 for (int i = 1; i < oa.length-1; i++) {
210 sb.append(oa[i].toString());
211 sb.append("/");
212 }
213 sb.append(oa[oa.length - 1].toString());
214 return sb.toString();
215 }
216
217
218 public String[] getMultiStringSelection() {
219 TreePath[] tpa = selectionModel.getSelectionPaths();
220 String[] ret = new String[tpa.length];
221 for (int i = 0; i < tpa.length; i++) {
222 ret[i] = makeSlashPath(tpa[i]);
223 }
224 return ret;
225 }
226
227 public String[] getExpandedMultiStringSelection() {
228 ArrayList<String> asa = new ArrayList<String>();
229
230 for (TreePath tp : selectionModel.getSelectionPaths()) {
231 for (TreePath stp : selectionModel.getDescendantPaths(tp)) {
232 asa.add(makeSlashPath(stp));
233 }
234 }
235
236 return asa.toArray(new String[0]);
237 }
238
239
240
241
242
243 public void addStringSelection(String sp) {
244
245 Tree tr = dTreeModel.getTree();
246 Object[] oa = tr.getObjectPath(sp, true);
247 if (oa == null) {
248 E.warning("cant get path for " + sp);
249 } else {
250 TreePath tp = new TreePath(oa);
251 TreePath[] tpa = { tp };
252 selectionModel.addSelectionPaths(tpa);
253 }
254 }
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269 public void setSelected(String[] content) {
270 selectionModel.clearSelection();
271 if (content == null) {
272
273 } else {
274 for (String sp : content) {
275 sp = sp.trim();
276 if (sp.length() > 0) {
277 addStringSelection(sp);
278 }
279 }
280 }
281 }
282
283
284
285 }