1 package be
.nikiroo
.utils
.ui
;
3 import java
.util
.ArrayList
;
6 import javax
.swing
.JList
;
8 public class ListSnapshot
{
10 private List
<Object
> elements
= new ArrayList
<Object
>();
12 public ListSnapshot(JList list
) {
15 for (int index
: list
.getSelectedIndices()) {
16 elements
.add(list
.getModel().getElementAt(index
));
24 public void applyTo(JList list
) {
25 List
<Integer
> indices
= new ArrayList
<Integer
>();
26 for (int i
= 0; i
< list
.getModel().getSize(); i
++) {
27 Object newObject
= list
.getModel().getElementAt(i
);
28 for (Object oldObject
: elements
) {
29 if (isSameElement(oldObject
, newObject
)) {
36 int a
[] = new int[indices
.size()];
37 for (int i
= 0; i
< indices
.size(); i
++) {
38 a
[i
] = indices
.get(i
);
40 list
.setSelectedIndices(a
);
43 // You can override this
44 protected boolean isSameElement(Object oldElement
, Object newElement
) {
45 if (oldElement
== null || newElement
== null)
46 return oldElement
== null && newElement
== null;
48 return oldElement
.toString().equals(newElement
.toString());
52 public String
toString() {
53 StringBuilder builder
= new StringBuilder();
54 builder
.append("List Snapshot of: ").append(list
).append("\n");
55 builder
.append("Selected elements:\n");
56 for (Object element
: elements
) {
57 builder
.append("\t").append(element
).append("\n");
60 return builder
.toString();