svn-gvsig-desktop / 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 @ 43983
History | View | Annotate | Download (4.26 KB)
1 | 43939 | jjdelcerro | /*
|
---|---|---|---|
2 | * To change this license header, choose License Headers in Project Properties.
|
||
3 | * To change this template file, choose Tools | Templates
|
||
4 | * and open the template in the editor.
|
||
5 | */
|
||
6 | package org.gvsig.expressionevaluator.swing.impl; |
||
7 | |||
8 | import java.util.ArrayList; |
||
9 | import java.util.Comparator; |
||
10 | import java.util.List; |
||
11 | import javax.swing.AbstractListModel; |
||
12 | import javax.swing.SwingUtilities; |
||
13 | import org.apache.commons.io.FilenameUtils; |
||
14 | import org.apache.commons.lang3.StringUtils; |
||
15 | 43983 | jjdelcerro | import org.gvsig.expressionevaluator.swing.Element.ConstantElement; |
16 | import org.gvsig.tools.swing.api.ListElement; |
||
17 | 43939 | jjdelcerro | |
18 | /**
|
||
19 | *
|
||
20 | * @author jjdelcerro
|
||
21 | */
|
||
22 | class FilteredListModel extends AbstractListModel { |
||
23 | 43983 | jjdelcerro | |
24 | private List<ListElement> elements; |
||
25 | private List<ListElement> filteredElements; |
||
26 | 43939 | jjdelcerro | private String filter; |
27 | |||
28 | public FilteredListModel() {
|
||
29 | this.elements = new ArrayList<>(); |
||
30 | this.filteredElements = this.elements; |
||
31 | this.filter = null; |
||
32 | } |
||
33 | |||
34 | @Override
|
||
35 | public int getSize() { |
||
36 | return this.filteredElements.size(); |
||
37 | } |
||
38 | |||
39 | @Override
|
||
40 | public Object getElementAt(int index) { |
||
41 | return this.filteredElements.get(index); |
||
42 | } |
||
43 | |||
44 | 43983 | jjdelcerro | void addElement(ListElement element) {
|
45 | 43939 | jjdelcerro | this.elements.add(element);
|
46 | if (this.elements != this.filteredElements) { |
||
47 | this.filteredElements.add(element);
|
||
48 | } |
||
49 | } |
||
50 | |||
51 | public void setFilter(String filter) { |
||
52 | if (StringUtils.isEmpty(filter)) {
|
||
53 | this.filter = null; |
||
54 | this.filteredElements = this.elements; |
||
55 | } else {
|
||
56 | 43983 | jjdelcerro | this.filter = filter;
|
57 | 43939 | jjdelcerro | this.filteredElements = new ArrayList<>(); |
58 | 43983 | jjdelcerro | for (ListElement element : elements) {
|
59 | if (FilenameUtils.wildcardMatch(element.getValue().toString().toLowerCase(), this.filter.toLowerCase())) { |
||
60 | 43939 | jjdelcerro | this.filteredElements.add(element);
|
61 | } |
||
62 | } |
||
63 | } |
||
64 | 43983 | jjdelcerro | 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 | } |
||
77 | 43939 | jjdelcerro | this.fireContentsChanged(this, 0, this.filteredElements.size()); |
78 | 43983 | jjdelcerro | } |
79 | |||
80 | public String getFilter() { |
||
81 | return this.filter; |
||
82 | 43939 | jjdelcerro | } |
83 | 43983 | jjdelcerro | |
84 | public void sort(final boolean ascending) { |
||
85 | // this.sortForeground(ascending);
|
||
86 | this.sortBackground(ascending);
|
||
87 | } |
||
88 | 43939 | jjdelcerro | |
89 | 43983 | jjdelcerro | private void sortBackground(final boolean ascending) { |
90 | 43939 | jjdelcerro | Thread th = new Thread(new Runnable() { |
91 | @Override
|
||
92 | public void run() { |
||
93 | 43983 | jjdelcerro | sortForeground(ascending); |
94 | 43939 | jjdelcerro | } |
95 | }, "FilteredListModelSorter");
|
||
96 | th.start(); |
||
97 | } |
||
98 | |||
99 | 43983 | jjdelcerro | 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 | } |
||
135 | 43939 | jjdelcerro | } |
136 | 43983 | jjdelcerro | |
137 | void addElement(Object data) { |
||
138 | ListElement e = new ListElement(data.toString(), data);
|
||
139 | this.addElement(e);
|
||
140 | } |
||
141 | 43939 | jjdelcerro | } |