1
2
3
4
5 package de.mindcrimeilab.xsanalyzer.model;
6
7 import javax.swing.event.TreeSelectionEvent;
8 import javax.swing.event.TreeSelectionListener;
9 import javax.swing.tree.TreePath;
10 import javax.swing.tree.TreeSelectionModel;
11
12 import org.springframework.binding.value.support.AbstractValueModel;
13
14
15
16
17
18
19
20 public class TreeSelectionValueModelAdapter extends AbstractValueModel implements TreeSelectionListener {
21
22 private final TreeSelectionModel model;
23
24 private TreePath[] currentSelection = new TreePath[0];
25
26 private boolean skipSelectionModelUpdate = false;
27
28 public TreeSelectionValueModelAdapter(TreeSelectionModel model) {
29 this.model = model;
30 this.model.addTreeSelectionListener(this);
31 }
32
33
34
35
36
37
38 @Override
39 public void valueChanged(TreeSelectionEvent evt) {
40 skipSelectionModelUpdate = true;
41 setValue(getSelectedRows());
42 skipSelectionModelUpdate = false;
43 }
44
45
46
47
48
49
50 @Override
51 public Object getValue() {
52 return currentSelection;
53 }
54
55
56
57
58
59
60 @Override
61 public void setValue(Object newValue) {
62 logger.debug("New Value " + newValue);
63 TreePath[] newSelection = (TreePath[]) newValue;
64 if (hasChanged(currentSelection, newSelection)) {
65
66 TreePath[] oldValue = currentSelection;
67 currentSelection = newSelection;
68 fireValueChange(oldValue, currentSelection);
69
70 if (!skipSelectionModelUpdate) {
71 model.removeTreeSelectionListener(this);
72 model.clearSelection();
73
74 model.addSelectionPaths(newSelection);
75
76
77 model.addTreeSelectionListener(this);
78 }
79 }
80 }
81
82
83
84
85 private boolean hasChanged(TreePath[] oldValue, TreePath[] newValue) {
86 if (null == oldValue && null == newValue) { return false; }
87 if ((null == oldValue && null != newValue) || (null != oldValue && null == newValue)) { return false; }
88 if (oldValue.length == newValue.length) {
89 for (int i = 0; i < newValue.length; i++) {
90 if (oldValue[i] != newValue[i]) { return true; }
91 }
92 return false;
93 }
94 return true;
95 }
96
97 private Object getSelectedRows() {
98 return model.getSelectionPaths();
99 }
100 }