svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.swing / org.gvsig.fmap.dal.swing.impl / src / main / java / org / gvsig / fmap / dal / swing / impl / searchpanel / SearchUtils.java @ 44259
History | View | Annotate | Download (5.78 KB)
1 | 44259 | jjdelcerro | package org.gvsig.fmap.dal.swing.impl.searchpanel; |
---|---|---|---|
2 | |||
3 | import java.util.ArrayList; |
||
4 | import java.util.Collection; |
||
5 | import java.util.Collections; |
||
6 | import java.util.Comparator; |
||
7 | import java.util.List; |
||
8 | import java.util.Iterator; |
||
9 | import java.util.function.Predicate; |
||
10 | import org.apache.commons.lang3.StringUtils; |
||
11 | import org.gvsig.fmap.dal.exception.DataException; |
||
12 | import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
||
13 | import org.gvsig.fmap.dal.feature.FeatureStore; |
||
14 | import org.gvsig.fmap.dal.feature.FeatureType; |
||
15 | import org.gvsig.tools.dataTypes.DataType; |
||
16 | import org.gvsig.tools.dataTypes.DataTypes; |
||
17 | import org.gvsig.tools.util.ChainedIterator; |
||
18 | |||
19 | /**
|
||
20 | *
|
||
21 | * @author jjdelcerro
|
||
22 | */
|
||
23 | public class SearchUtils { |
||
24 | |||
25 | private SearchUtils() {
|
||
26 | |||
27 | } |
||
28 | |||
29 | private static final RecentUsedsAttributesImpl RECENTS_USEDS = new RecentUsedsAttributesImpl(); |
||
30 | |||
31 | public static final Predicate<FeatureAttributeDescriptor> BASIC_TYPES_FILTER = new Predicate<FeatureAttributeDescriptor>() { |
||
32 | @Override
|
||
33 | public boolean test(FeatureAttributeDescriptor attrdesc) { |
||
34 | DataType t = attrdesc.getDataType(); |
||
35 | boolean r = !(t.isContainer() || t.isDynObject() || t.isObject());
|
||
36 | return r;
|
||
37 | } |
||
38 | }; |
||
39 | |||
40 | public static Predicate<FeatureAttributeDescriptor> ALL_FILTER = new Predicate<FeatureAttributeDescriptor>() { |
||
41 | @Override
|
||
42 | public boolean test(FeatureAttributeDescriptor t) { |
||
43 | return true; |
||
44 | } |
||
45 | }; |
||
46 | |||
47 | public static final Comparator<FeatureAttributeDescriptor> LABEL_ORDER = new Comparator<FeatureAttributeDescriptor>() { |
||
48 | @Override
|
||
49 | public int compare(FeatureAttributeDescriptor o1, FeatureAttributeDescriptor o2) { |
||
50 | return StringUtils.compareIgnoreCase(o1.getLabel(), o2.getLabel());
|
||
51 | } |
||
52 | }; |
||
53 | |||
54 | public static final Comparator<FeatureAttributeDescriptor> STR_INT_LONG_LABEL_ORDER = new Comparator<FeatureAttributeDescriptor>() { |
||
55 | @Override
|
||
56 | public int compare(FeatureAttributeDescriptor o1, FeatureAttributeDescriptor o2) { |
||
57 | int t1 = o1.getType();
|
||
58 | int t2 = o2.getType();
|
||
59 | if( t1==DataTypes.STRING ) {
|
||
60 | if( t2==DataTypes.STRING ) {
|
||
61 | return StringUtils.compareIgnoreCase(o1.getLabel(), o2.getLabel());
|
||
62 | } |
||
63 | return -1; |
||
64 | } else if( t2==DataTypes.STRING ) { |
||
65 | return 1; |
||
66 | } |
||
67 | if( t1==DataTypes.INT || t1==DataTypes.LONG ) {
|
||
68 | if( t2==DataTypes.INT || t2==DataTypes.LONG ) {
|
||
69 | return StringUtils.compareIgnoreCase(o1.getLabel(), o2.getLabel());
|
||
70 | } |
||
71 | return -1; |
||
72 | } else if( t2==DataTypes.INT || t2==DataTypes.LONG ) { |
||
73 | return 1; |
||
74 | } |
||
75 | return StringUtils.compareIgnoreCase(o1.getLabel(), o2.getLabel());
|
||
76 | } |
||
77 | }; |
||
78 | |||
79 | public static List<FeatureAttributeDescriptor> getOrderedAttributes( |
||
80 | FeatureStore store, |
||
81 | Predicate<FeatureAttributeDescriptor> filter,
|
||
82 | Comparator<FeatureAttributeDescriptor> comparator,
|
||
83 | int max
|
||
84 | ) { |
||
85 | try {
|
||
86 | return getOrderedAttributes(store.getDefaultFeatureType(), filter, comparator, max);
|
||
87 | } catch (DataException ex) {
|
||
88 | throw new RuntimeException(ex); |
||
89 | } |
||
90 | } |
||
91 | public static List<FeatureAttributeDescriptor> getOrderedAttributes( |
||
92 | FeatureType featureType, |
||
93 | Predicate<FeatureAttributeDescriptor> filter,
|
||
94 | Comparator<FeatureAttributeDescriptor> comparator,
|
||
95 | int max
|
||
96 | ) { |
||
97 | List<FeatureAttributeDescriptor> mostUsed = new ArrayList<>(); |
||
98 | List<FeatureAttributeDescriptor> favorites = new ArrayList<>(); |
||
99 | List<FeatureAttributeDescriptor> normal = new ArrayList<>(); |
||
100 | |||
101 | for (FeatureAttributeDescriptor attribute : getRecentUseds().getAttributes(featureType)) {
|
||
102 | if (filter.test(attribute)) {
|
||
103 | mostUsed.add(attribute); |
||
104 | } |
||
105 | } |
||
106 | for (FeatureAttributeDescriptor attribute : getFavoritesAttributes(featureType)) {
|
||
107 | if (contains(mostUsed, attribute.getName())) {
|
||
108 | continue;
|
||
109 | } |
||
110 | if (filter.test(attribute)) {
|
||
111 | favorites.add(attribute); |
||
112 | } |
||
113 | } |
||
114 | for (FeatureAttributeDescriptor attribute : featureType) {
|
||
115 | if (contains(mostUsed, attribute.getName())) {
|
||
116 | continue;
|
||
117 | } |
||
118 | if (contains(favorites, attribute.getName())) {
|
||
119 | continue;
|
||
120 | } |
||
121 | if (filter.test(attribute)) {
|
||
122 | normal.add(attribute); |
||
123 | } |
||
124 | } |
||
125 | mostUsed.sort(comparator); |
||
126 | favorites.sort(comparator); |
||
127 | normal.sort(comparator); |
||
128 | |||
129 | if (max < 0) { |
||
130 | max = Integer.MAX_VALUE;
|
||
131 | } |
||
132 | Iterator<FeatureAttributeDescriptor> it = new ChainedIterator<>( |
||
133 | mostUsed.iterator(), |
||
134 | favorites.iterator(), |
||
135 | normal.iterator() |
||
136 | ); |
||
137 | List<FeatureAttributeDescriptor> r = new ArrayList<>(); |
||
138 | int n = 0; |
||
139 | while (n < max && it.hasNext()) {
|
||
140 | r.add(it.next()); |
||
141 | n++; |
||
142 | } |
||
143 | return r;
|
||
144 | } |
||
145 | |||
146 | private static boolean contains(List<FeatureAttributeDescriptor> attributes, String name) { |
||
147 | if (StringUtils.isBlank(name)) {
|
||
148 | return false; |
||
149 | } |
||
150 | for (FeatureAttributeDescriptor attribute : attributes) {
|
||
151 | if (StringUtils.equalsIgnoreCase(name, attribute.getName())) {
|
||
152 | return true; |
||
153 | } |
||
154 | } |
||
155 | return false; |
||
156 | } |
||
157 | |||
158 | public static RecentUsedsAttributesImpl getRecentUseds() { |
||
159 | return RECENTS_USEDS;
|
||
160 | } |
||
161 | |||
162 | public static Collection<FeatureAttributeDescriptor> getFavoritesAttributes(FeatureType featureType) { |
||
163 | return Collections.EMPTY_LIST; |
||
164 | } |
||
165 | |||
166 | } |