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 / featurequery / DefaultFeatureQueryGroupByPanel.java @ 46558
History | View | Annotate | Download (13.2 KB)
1 |
package org.gvsig.fmap.dal.swing.impl.featurequery; |
---|---|
2 |
|
3 |
import java.awt.BorderLayout; |
4 |
import java.awt.Component; |
5 |
import java.awt.Dimension; |
6 |
import java.awt.event.ActionEvent; |
7 |
import java.net.URL; |
8 |
import java.util.ArrayList; |
9 |
import java.util.Collections; |
10 |
import java.util.HashMap; |
11 |
import java.util.List; |
12 |
import java.util.Map; |
13 |
import javax.swing.DefaultComboBoxModel; |
14 |
import javax.swing.ImageIcon; |
15 |
import javax.swing.JComponent; |
16 |
import javax.swing.JLabel; |
17 |
import javax.swing.JList; |
18 |
import javax.swing.event.ChangeEvent; |
19 |
import javax.swing.event.ListSelectionEvent; |
20 |
import org.apache.commons.io.FilenameUtils; |
21 |
import org.apache.commons.lang3.StringUtils; |
22 |
import org.gvsig.expressionevaluator.Code; |
23 |
import org.gvsig.expressionevaluator.ExpressionBuilder; |
24 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator; |
25 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorManager; |
26 |
import org.gvsig.expressionevaluator.ExpressionUtils; |
27 |
import org.gvsig.expressionevaluator.Function; |
28 |
import org.gvsig.expressionevaluator.SymbolTable; |
29 |
import org.gvsig.expressionevaluator.SymbolTableFactory; |
30 |
import org.gvsig.fmap.dal.exception.DataException; |
31 |
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
32 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
33 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
34 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
35 |
import org.gvsig.fmap.dal.feature.FeatureStoreProviderFactory; |
36 |
import org.gvsig.fmap.dal.feature.FeatureType; |
37 |
import org.gvsig.fmap.dal.swing.impl.featuretype.FeatureAttributeListCellRendererImpl; |
38 |
import org.gvsig.fmap.dal.swing.impl.featuretype.DefaultFeatureAttributesSelectionPanel; |
39 |
import org.gvsig.tools.dataTypes.DataType; |
40 |
import org.gvsig.tools.swing.api.FilteredListController; |
41 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
42 |
import org.gvsig.tools.swing.api.ToolsSwingManager; |
43 |
import org.gvsig.tools.swing.icontheme.IconTheme; |
44 |
import org.gvsig.fmap.dal.swing.featurequery.FeatureQueryGroupByPanel; |
45 |
import org.gvsig.timesupport.DataTypes; |
46 |
import org.gvsig.tools.swing.api.FilteredListModel; |
47 |
|
48 |
/**
|
49 |
*
|
50 |
* @author jjdelcerro
|
51 |
*/
|
52 |
public class DefaultFeatureQueryGroupByPanel |
53 |
extends DefaultFeatureQueryGroupByPanelView
|
54 |
implements FeatureQueryGroupByPanel {
|
55 |
|
56 |
private class AttributesWithAggregateRenderer extends FeatureAttributeListCellRendererImpl { |
57 |
|
58 |
@Override
|
59 |
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { |
60 |
JLabel c = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); //To change body of generated methods, choose Tools | Templates. |
61 |
FeatureAttributeDescriptor attrdesc = (FeatureAttributeDescriptor) value; |
62 |
String aggregate = query.getAggregateFunction(attrdesc.getName());
|
63 |
if (aggregate != null) { |
64 |
c.setText(c.getText() + " [" + aggregate + "]"); |
65 |
} else {
|
66 |
c.setText(c.getText() + " [<NULL>]");
|
67 |
} |
68 |
return c;
|
69 |
} |
70 |
} |
71 |
|
72 |
private FeatureStore store;
|
73 |
private FeatureType featureType;
|
74 |
|
75 |
private FilteredListController attributesFilterController;
|
76 |
private DefaultFeatureAttributesSelectionPanel groupByPanel;
|
77 |
|
78 |
private FeatureQuery query;
|
79 |
|
80 |
public DefaultFeatureQueryGroupByPanel() {
|
81 |
this.initComponents();
|
82 |
} |
83 |
|
84 |
@Override
|
85 |
public JComponent asJComponent() { |
86 |
return this; |
87 |
} |
88 |
|
89 |
@Override
|
90 |
public ImageIcon loadImage(String imageName) { |
91 |
String name = FilenameUtils.getBaseName(imageName);
|
92 |
IconTheme theme = ToolsSwingLocator.getIconThemeManager().getDefault(); |
93 |
if (theme.exists(name)) {
|
94 |
return theme.get(name);
|
95 |
} |
96 |
URL url = this.getClass().getResource(name + ".png"); |
97 |
if (url == null) { |
98 |
return null; |
99 |
} |
100 |
return new ImageIcon(url); |
101 |
} |
102 |
|
103 |
@Override
|
104 |
public void setStore(FeatureStore store) { |
105 |
try {
|
106 |
this.featureType = store.getDefaultFeatureType();
|
107 |
this.store = store;
|
108 |
this.query = store.createFeatureQuery();
|
109 |
this.groupByPanel.setFeatureType(featureType);
|
110 |
this.groupByPanel.setSelectedNames(query.getGroupByColumns());
|
111 |
this.updateControls();
|
112 |
} catch (DataException ex) {
|
113 |
throw new RuntimeException("Can't assign store", ex); |
114 |
} |
115 |
} |
116 |
|
117 |
private void initComponents() { |
118 |
ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager(); |
119 |
|
120 |
toolsSwingManager.translate(this.btnAttributesFilter);
|
121 |
toolsSwingManager.translate(this.tabGroup);
|
122 |
|
123 |
this.groupByPanel = new DefaultFeatureAttributesSelectionPanel(); |
124 |
this.groupByPanel.allowCalculatedAttributes(true); |
125 |
|
126 |
this.pnlGroupByAttributes.setLayout(new BorderLayout()); |
127 |
this.pnlGroupByAttributes.add(this.groupByPanel, BorderLayout.CENTER); |
128 |
|
129 |
this.lstAttributes.setCellRenderer(new AttributesWithAggregateRenderer()); |
130 |
this.lstAttributes.addListSelectionListener((ListSelectionEvent e) -> { |
131 |
if (e.getValueIsAdjusting()) {
|
132 |
return;
|
133 |
} |
134 |
doSelectAttribute(); |
135 |
}); |
136 |
|
137 |
this.attributesFilterController = toolsSwingManager.createFilteredListController(
|
138 |
lstAttributes, |
139 |
txtAttributesFilter, |
140 |
btnAttributesFilter |
141 |
); |
142 |
|
143 |
this.attributesFilterController.addChangeListener((ChangeEvent ce) -> { |
144 |
doSelectAttribute(); |
145 |
}); |
146 |
|
147 |
this.cboAggretateFunctions.addActionListener((ActionEvent e) -> { |
148 |
doSelectAggregateFunction(); |
149 |
}); |
150 |
|
151 |
Dimension sz = this.getPreferredSize(); |
152 |
if (sz.width < DEFAULT_WIDTH) {
|
153 |
sz.width = DEFAULT_WIDTH; |
154 |
} |
155 |
if (sz.height < DEFAULT_HEIGHT) {
|
156 |
sz.height = DEFAULT_HEIGHT; |
157 |
} |
158 |
this.setPreferredSize(sz);
|
159 |
} |
160 |
|
161 |
private void doSelectAttribute() { |
162 |
FeatureAttributeDescriptor attr = (FeatureAttributeDescriptor) this.lstAttributes.getSelectedValue();
|
163 |
if (attr == null) { |
164 |
this.txtCurrentAttributeName.setText(""); |
165 |
this.cboAggretateFunctions.setSelectedIndex(0); |
166 |
return;
|
167 |
} |
168 |
this.txtCurrentAttributeName.setText(attr.getName());
|
169 |
String aggregate = this.query.getAggregateFunction(attr.getName()); |
170 |
if (StringUtils.isBlank(aggregate) || aggregate.equalsIgnoreCase("<NULL>")) { |
171 |
this.cboAggretateFunctions.setSelectedIndex(0); |
172 |
} else {
|
173 |
this.cboAggretateFunctions.setSelectedItem(aggregate);
|
174 |
} |
175 |
} |
176 |
|
177 |
private void doSelectAggregateFunction() { |
178 |
String attribute = this.txtCurrentAttributeName.getText(); |
179 |
if (!StringUtils.isBlank(attribute)) {
|
180 |
String aggregate = (String) this.cboAggretateFunctions.getSelectedItem(); |
181 |
if (StringUtils.isBlank(aggregate) || aggregate.equalsIgnoreCase("<NULL>")) { |
182 |
this.query.removeAggregateFunction(attribute);
|
183 |
} else {
|
184 |
this.query.addAggregate(aggregate, attribute);
|
185 |
} |
186 |
this.updateControls();
|
187 |
} |
188 |
} |
189 |
|
190 |
private void updateControls() { |
191 |
int indexAttributes = this.lstAttributes.getSelectedIndex(); |
192 |
if (this.cboAggretateFunctions.getModel().getSize() < 1) { |
193 |
List<String> aggregateFunctions = new ArrayList<>(); |
194 |
java.util.function.Function<String, Integer> isAllowed = ((FeatureStoreProviderFactory) (store.getProviderFactory())).allowFunction(); |
195 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
196 |
for (SymbolTableFactory factory : manager.getSymbolTableFactories()) {
|
197 |
if (StringUtils.equalsIgnoreCase(Function.GROUP_AGGREGATE, factory.getName())) {
|
198 |
SymbolTable symbolTable = factory.create(); |
199 |
for (Function function : symbolTable) {
|
200 |
if (isAllowed.apply(function.name()) != DataType.NO) {
|
201 |
aggregateFunctions.add(function.name()); |
202 |
} |
203 |
} |
204 |
break;
|
205 |
} |
206 |
} |
207 |
Collections.sort(aggregateFunctions);
|
208 |
DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>(); |
209 |
model.addElement("<NULL>");
|
210 |
for (String functionName : aggregateFunctions) { |
211 |
model.addElement(functionName); |
212 |
} |
213 |
|
214 |
this.cboAggretateFunctions.setModel(model);
|
215 |
this.cboAggretateFunctions.setSelectedIndex(0); |
216 |
this.txtCurrentAttributeName.setText(""); |
217 |
} |
218 |
ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager(); |
219 |
FilteredListModel model = toolsSwingManager.createFilteredListModel(); |
220 |
for (FeatureAttributeDescriptor attr : featureType) {
|
221 |
model.addElement(attr); |
222 |
} |
223 |
for (FeatureAttributeDescriptor attr : featureType.getExtraColumns().getColumns()) {
|
224 |
model.addElement(attr); |
225 |
} |
226 |
for (EditableFeatureAttributeDescriptor attr : query.getExtraColumn().getColumns()) {
|
227 |
model.addElement(attr); |
228 |
} |
229 |
model.setFilter(this.txtAttributesFilter.getText());
|
230 |
model.sort(true);
|
231 |
this.lstAttributes.setModel(model);
|
232 |
this.lstAttributes.setSelectedIndex(indexAttributes);
|
233 |
// this.txtCurrentAttributeName.setText("");
|
234 |
// this.cboAggretateFunctions.setSelectedIndex(0);
|
235 |
|
236 |
//
|
237 |
//groupByPanel.setSelectedNames(query.getGroupByColumns());
|
238 |
} |
239 |
|
240 |
public Map<String, String> getFixedConversion() { |
241 |
// Map<String, String> fixed = new HashMap<>();
|
242 |
// ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder();
|
243 |
//
|
244 |
// for (Map.Entry<String, String> aggregate : this.query.getAggregateFunctions().entrySet()) {
|
245 |
// String key = aggregate.getKey();
|
246 |
// String value = aggregate.getValue();
|
247 |
// FeatureAttributeDescriptor attr = this.featureType.getAttributeDescriptorFromAll(value);
|
248 |
// if(attr==null) {
|
249 |
// attr = this.query.getExtraColumn().get(value);
|
250 |
// if(attr==null) {
|
251 |
// fixed.put(key, value);
|
252 |
// continue;
|
253 |
// }
|
254 |
// }
|
255 |
// if(StringUtils.equalsIgnoreCase(key, "SUM")) {
|
256 |
// switch (attr.getType()) {
|
257 |
// case DataTypes.BOOLEAN:
|
258 |
// case DataTypes.STRING:
|
259 |
// value = builder.let(
|
260 |
// attr.getName(),
|
261 |
// builder.toInteger(
|
262 |
// builder.variable(attr.getName()
|
263 |
// )
|
264 |
// )
|
265 |
// ).toString();
|
266 |
// }
|
267 |
// }
|
268 |
// fixed.put(key, value);
|
269 |
//
|
270 |
// }
|
271 |
//
|
272 |
// return fixed;
|
273 |
return this.query.getAggregateFunctions(); |
274 |
} |
275 |
|
276 |
@Override
|
277 |
public FeatureQuery fetch(FeatureQuery query) {
|
278 |
if (query == null) { |
279 |
FeatureQuery q = this.query.getCopy();
|
280 |
q.getAggregateFunctions().clear(); |
281 |
q.getAggregateFunctions().putAll(getFixedConversion()); |
282 |
return q;
|
283 |
} |
284 |
query.getGroupByColumns().clear(); |
285 |
query.getGroupByColumns().addAll(this.groupByPanel.getSelectedNames());
|
286 |
query.getAggregateFunctions().clear(); |
287 |
query.getAggregateFunctions().putAll(getFixedConversion()); |
288 |
|
289 |
return query;
|
290 |
} |
291 |
|
292 |
@Override
|
293 |
public FeatureQuery fetch() {
|
294 |
return this.fetch(null); |
295 |
} |
296 |
|
297 |
public void removeTypeConversion() { |
298 |
Map<String, String> fixed = new HashMap<>(); |
299 |
|
300 |
for (Map.Entry<String, String> aggregate : this.query.getAggregateFunctions().entrySet()) { |
301 |
String key = aggregate.getKey();
|
302 |
String value = aggregate.getValue();
|
303 |
FeatureAttributeDescriptor attr = this.featureType.getAttributeDescriptorFromAll(value);
|
304 |
if (attr == null) { |
305 |
attr = this.query.getExtraColumn().get(value);
|
306 |
if (attr == null) { |
307 |
switch (key.toLowerCase()) {
|
308 |
case "sum": |
309 |
Code code = ExpressionUtils.compile(value); |
310 |
if (code instanceof Code.Callable) { |
311 |
Code.Callable callable = (Code.Callable) code; |
312 |
switch (callable.name().toLowerCase()) {
|
313 |
case ExpressionBuilder.FUNCTION_LET:
|
314 |
value = ((Code.Identifier) callable.parameters().get(0)).name();
|
315 |
break;
|
316 |
} |
317 |
} |
318 |
break;
|
319 |
} |
320 |
|
321 |
} |
322 |
} |
323 |
|
324 |
fixed.put(key, value); |
325 |
|
326 |
} |
327 |
|
328 |
query.getAggregateFunctions().clear(); |
329 |
query.getAggregateFunctions().putAll(fixed); |
330 |
|
331 |
} |
332 |
|
333 |
@Override
|
334 |
public void put(FeatureQuery query) { |
335 |
this.query.copyFrom(query);
|
336 |
// removeTypeConversion();
|
337 |
groupByPanel.setExtraColumns(query.getExtraColumn().getColumns()); |
338 |
groupByPanel.setSelectedNames(query.getGroupByColumns()); |
339 |
this.updateControls();
|
340 |
} |
341 |
|
342 |
public static void selfRegister() { |
343 |
|
344 |
} |
345 |
|
346 |
} |