source: src/main/java/genius/gui/panels/SelectionModelAdapter.java@ 209

Last change on this file since 209 was 1, checked in by Wouter Pasman, 6 years ago

Initial import : Genius 9.0.0

File size: 5.3 KB
Line 
1package genius.gui.panels;
2
3import java.util.ArrayList;
4import java.util.List;
5
6import javax.swing.ListSelectionModel;
7import javax.swing.event.ListSelectionEvent;
8import javax.swing.event.ListSelectionListener;
9
10import genius.core.listener.Listener;
11
12/**
13 * Adapts the {@link SubsetSelectionModel} to a {@link ListSelectionModel}.
14 *
15 * The ListSelectionModel (jlist) supports quite complex selection modes:
16 * ctrl-click , dragging, and combinations of these. We really only want to
17 * support dragging so we ignore the ctrl- and removal stuff.
18 */
19public class SelectionModelAdapter<ItemType> implements ListSelectionModel {
20
21 private SubsetSelectionModel<ItemType> model;
22
23 /**
24 * drag start point. Valid while user is dragging
25 */
26 private int anchorSelectionIndex = -1;
27 /**
28 * current drag end point. Valid while user is dragging
29 */
30 private int leadSelectionIndex = -1;
31 /**
32 * True while the user is dragging.
33 */
34 private boolean valueIsAdjusting;
35 /**
36 * Selected selection mode.
37 */
38 private int selectionMode;
39
40 /**
41 * The selection in the model at the time the drag starts
42 */
43 private List<ItemType> initialSelection;
44
45 public SelectionModelAdapter(SubsetSelectionModel<ItemType> model) {
46 this.model = model;
47
48 }
49
50 @Override
51 public void setSelectionInterval(int index0, int index1) {
52 if (anchorSelectionIndex == -1) {
53 // user just started dragging
54 anchorSelectionIndex = index0;
55 initialSelection = model.getSelectedItems();
56 }
57 leadSelectionIndex = index1;
58 reverseSelection();
59 }
60
61 /**
62 * Reverse the selection state of items in the range [anchor, lead]
63 */
64 private void reverseSelection() {
65 ArrayList<ItemType> newSelection = new ArrayList<ItemType>(initialSelection);
66 for (int n = Math.min(anchorSelectionIndex, leadSelectionIndex); //
67 n <= Math.max(anchorSelectionIndex, leadSelectionIndex); n++) {
68 ItemType item = model.getAllItems().get(n);
69 // reverse the selection of item.
70 if (newSelection.contains(item)) {
71 newSelection.remove(item);
72 } else {
73 newSelection.add(item);
74 }
75 model.select(newSelection);
76 }
77
78 }
79
80 @Override
81 public void addSelectionInterval(int index0, int index1) {
82 System.out.println("ignore User added " + index0 + " " + index1);
83 }
84
85 @Override
86 public void removeSelectionInterval(int index0, int index1) {
87 System.out.println("ignore user removed " + index0 + " " + index1);
88 }
89
90 @Override
91 public int getMinSelectionIndex() {
92 if (model.getSelectedItems().isEmpty()) {
93 return -1;
94 }
95 return model.getAllItems().indexOf(model.getSelectedItems().get(0));
96 }
97
98 @Override
99 public int getMaxSelectionIndex() {
100 return model.getAllItems().size() - 1;
101 }
102
103 @Override
104 public boolean isSelectedIndex(int index) {
105 return model.getSelectedItems().contains(model.getAllItems().get(index));
106 }
107
108 @Override
109 public int getAnchorSelectionIndex() {
110 return anchorSelectionIndex;
111 }
112
113 @Override
114 public void setAnchorSelectionIndex(int index) {
115 System.out.println("anchor set to " + index);
116 anchorSelectionIndex = index;
117 }
118
119 @Override
120 public int getLeadSelectionIndex() {
121 return leadSelectionIndex;
122 }
123
124 @Override
125 public void setLeadSelectionIndex(int index) {
126 leadSelectionIndex = index;
127 }
128
129 @Override
130 public void clearSelection() {
131 model.select(new ArrayList<ItemType>());
132 }
133
134 @Override
135 public boolean isSelectionEmpty() {
136 return model.getSelectedItems().isEmpty();
137 }
138
139 @Override
140 public void insertIndexInterval(int index, int length, boolean before) {
141 System.out.println("insert index blabla" + index + length);
142 }
143
144 @Override
145 public void removeIndexInterval(int index0, int index1) {
146 // ???????
147 }
148
149 @Override
150 public void setValueIsAdjusting(boolean valueIsAdjusting) {
151 this.valueIsAdjusting = valueIsAdjusting;
152 if (this.valueIsAdjusting) {
153 // start drag
154 anchorSelectionIndex = -1;
155 }
156 }
157
158 @Override
159 public boolean getValueIsAdjusting() {
160 return valueIsAdjusting;
161 }
162
163 @Override
164 public void setSelectionMode(int selectionMode) {
165 this.selectionMode = selectionMode;
166 }
167
168 @Override
169 public int getSelectionMode() {
170 return selectionMode;
171 }
172
173 @Override
174 public void addListSelectionListener(final ListSelectionListener x) {
175 model.addListener(new ListSelectionListenerAdapter<ItemType>(x));
176 }
177
178 @Override
179 public void removeListSelectionListener(ListSelectionListener x) {
180 model.removeListener(new ListSelectionListenerAdapter<ItemType>(x));
181 }
182
183}
184
185/**
186 * adapts a {@link ListSelectionListener} to a {@link Listener}
187 *
188 * @param <ItemType>
189 */
190class ListSelectionListenerAdapter<ItemType> implements Listener<ItemType> {
191
192 private ListSelectionListener listener;
193
194 public ListSelectionListenerAdapter(ListSelectionListener x) {
195 this.listener = x;
196 }
197
198 @Override
199 public void notifyChange(Object data) {
200 // our model does not report this kind of detail. Hack it.
201 listener.valueChanged(new ListSelectionEvent(this, 0, 0, false));
202 }
203
204 @Override
205 public int hashCode() {
206 final int prime = 31;
207 int result = 1;
208 result = prime * result + ((listener == null) ? 0 : listener.hashCode());
209 return result;
210 }
211
212 @Override
213 public boolean equals(Object obj) {
214 if (this == obj)
215 return true;
216 if (obj == null)
217 return false;
218 if (getClass() != obj.getClass())
219 return false;
220 ListSelectionListenerAdapter other = (ListSelectionListenerAdapter) obj;
221 if (listener == null) {
222 if (other.listener != null)
223 return false;
224 } else if (!listener.equals(other.listener))
225 return false;
226 return true;
227 }
228
229}
Note: See TracBrowser for help on using the repository browser.