Revision 43983 trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.swing/org.gvsig.expressionevaluator.swing.impl/src/main/java/org/gvsig/expressionevaluator/swing/impl/FilteredListModel.java
FilteredListModel.java | ||
---|---|---|
12 | 12 |
import javax.swing.SwingUtilities; |
13 | 13 |
import org.apache.commons.io.FilenameUtils; |
14 | 14 |
import org.apache.commons.lang3.StringUtils; |
15 |
import org.gvsig.expressionevaluator.swing.Element.ConstantElement; |
|
16 |
import org.gvsig.tools.swing.api.ListElement; |
|
15 | 17 |
|
16 | 18 |
/** |
17 | 19 |
* |
18 | 20 |
* @author jjdelcerro |
19 | 21 |
*/ |
20 | 22 |
class FilteredListModel extends AbstractListModel { |
21 |
|
|
22 |
private List<Object> elements;
|
|
23 |
private List<Object> filteredElements;
|
|
23 |
|
|
24 |
private List<ListElement> elements;
|
|
25 |
private List<ListElement> filteredElements;
|
|
24 | 26 |
private String filter; |
25 | 27 |
|
26 | 28 |
public FilteredListModel() { |
... | ... | |
39 | 41 |
return this.filteredElements.get(index); |
40 | 42 |
} |
41 | 43 |
|
42 |
void addElement(String element) {
|
|
44 |
void addElement(ListElement element) {
|
|
43 | 45 |
this.elements.add(element); |
44 | 46 |
if (this.elements != this.filteredElements) { |
45 | 47 |
this.filteredElements.add(element); |
... | ... | |
51 | 53 |
this.filter = null; |
52 | 54 |
this.filteredElements = this.elements; |
53 | 55 |
} else { |
56 |
this.filter = filter; |
|
54 | 57 |
this.filteredElements = new ArrayList<>(); |
55 |
for (Object element : elements) {
|
|
56 |
if (FilenameUtils.wildcardMatch(element.toString(), this.filter)) {
|
|
58 |
for (ListElement element : elements) {
|
|
59 |
if (FilenameUtils.wildcardMatch(element.getValue().toString().toLowerCase(), this.filter.toLowerCase())) {
|
|
57 | 60 |
this.filteredElements.add(element); |
58 | 61 |
} |
59 | 62 |
} |
60 | 63 |
} |
64 |
fireContentsChanged(); |
|
65 |
} |
|
66 |
|
|
67 |
private void fireContentsChanged() { |
|
68 |
if( !SwingUtilities.isEventDispatchThread() ) { |
|
69 |
SwingUtilities.invokeLater(new Runnable() { |
|
70 |
@Override |
|
71 |
public void run() { |
|
72 |
fireContentsChanged(); |
|
73 |
} |
|
74 |
}); |
|
75 |
return; |
|
76 |
} |
|
61 | 77 |
this.fireContentsChanged(this, 0, this.filteredElements.size()); |
78 |
} |
|
79 |
|
|
80 |
public String getFilter() { |
|
81 |
return this.filter; |
|
62 | 82 |
} |
83 |
|
|
84 |
public void sort(final boolean ascending) { |
|
85 |
// this.sortForeground(ascending); |
|
86 |
this.sortBackground(ascending); |
|
87 |
} |
|
63 | 88 |
|
64 |
public void sort(final boolean ascending) {
|
|
89 |
private void sortBackground(final boolean ascending) {
|
|
65 | 90 |
Thread th = new Thread(new Runnable() { |
66 | 91 |
@Override |
67 | 92 |
public void run() { |
68 |
final List<Object> items = new ArrayList(elements); |
|
69 |
items.sort(new Comparator<Object>() { |
|
70 |
@Override |
|
71 |
public int compare(Object o1, Object o2) { |
|
72 |
int r; |
|
73 |
if( o1 == null && o2 == null ) { |
|
74 |
r = 0; |
|
75 |
} else if( o1 == null && o2 != null ) { |
|
76 |
r = -1; |
|
77 |
} else if( o1 != null && o2 == null ) { |
|
78 |
r = 1; |
|
79 |
} else { |
|
80 |
r = o1.toString().compareTo(o2.toString()); |
|
81 |
} |
|
82 |
if( ascending ) { |
|
83 |
return r; |
|
84 |
} |
|
85 |
return r * -1; |
|
86 |
} |
|
87 |
}); |
|
88 |
SwingUtilities.invokeLater(new Runnable() { |
|
89 |
@Override |
|
90 |
public void run() { |
|
91 |
elements = items; |
|
92 |
if( filter!=null ) { |
|
93 |
setFilter(filter); |
|
94 |
} |
|
95 |
} |
|
96 |
}); |
|
93 |
sortForeground(ascending); |
|
97 | 94 |
} |
98 | 95 |
}, "FilteredListModelSorter"); |
99 | 96 |
th.start(); |
100 | 97 |
} |
101 | 98 |
|
102 |
public String getFilter() { |
|
103 |
return this.filter; |
|
99 |
private void sortForeground(final boolean ascending) { |
|
100 |
final List<ListElement> items = new ArrayList<>(elements); |
|
101 |
final Comparator comparator = ComparatorUtils.naturalComparator(); |
|
102 |
items.sort(new Comparator<ListElement>() { |
|
103 |
@Override |
|
104 |
public int compare(ListElement o1, ListElement o2) { |
|
105 |
int r; |
|
106 |
if (o1 == null && o2 == null) { |
|
107 |
r = 0; |
|
108 |
} else if (o1 == null && o2 != null) { |
|
109 |
r = -1; |
|
110 |
} else if (o1 != null && o2 == null) { |
|
111 |
r = 1; |
|
112 |
} else { |
|
113 |
if( o1.getValue() instanceof ConstantElement ) { |
|
114 |
r = comparator.compare( |
|
115 |
((ConstantElement)o1.getValue()).getValue(), |
|
116 |
((ConstantElement)o2.getValue()).getValue() |
|
117 |
); |
|
118 |
} else { |
|
119 |
r = comparator.compare(o1.getValue(),o2.getValue()); |
|
120 |
} |
|
121 |
} |
|
122 |
if (ascending) { |
|
123 |
return r; |
|
124 |
} |
|
125 |
return r * -1; |
|
126 |
} |
|
127 |
}); |
|
128 |
elements = items; |
|
129 |
filteredElements = elements; |
|
130 |
if (filter != null) { |
|
131 |
setFilter(filter); |
|
132 |
} else { |
|
133 |
fireContentsChanged(); |
|
134 |
} |
|
104 | 135 |
} |
105 |
|
|
136 |
|
|
137 |
void addElement(Object data) { |
|
138 |
ListElement e = new ListElement(data.toString(), data); |
|
139 |
this.addElement(e); |
|
140 |
} |
|
106 | 141 |
} |
Also available in: Unified diff