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 @ 45155
History | View | Annotate | Download (10.8 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.List; |
11 |
import javax.swing.DefaultComboBoxModel; |
12 |
import javax.swing.ImageIcon; |
13 |
import javax.swing.JComponent; |
14 |
import javax.swing.JLabel; |
15 |
import javax.swing.JList; |
16 |
import javax.swing.event.ChangeEvent; |
17 |
import javax.swing.event.ListSelectionEvent; |
18 |
import org.apache.commons.io.FilenameUtils; |
19 |
import org.apache.commons.lang3.StringUtils; |
20 |
import org.gvsig.expressionevaluator.Code; |
21 |
import org.gvsig.expressionevaluator.ExpressionBuilder; |
22 |
import static org.gvsig.expressionevaluator.ExpressionBuilder.FUNCTION_LET; |
23 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator; |
24 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorManager; |
25 |
import org.gvsig.expressionevaluator.ExpressionUtils; |
26 |
import org.gvsig.expressionevaluator.Formatter; |
27 |
import org.gvsig.expressionevaluator.Function; |
28 |
import org.gvsig.expressionevaluator.SymbolTable; |
29 |
import org.gvsig.expressionevaluator.SymbolTableFactory; |
30 |
import org.gvsig.fmap.dal.DALLocator; |
31 |
import org.gvsig.fmap.dal.exception.DataException; |
32 |
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
33 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
34 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
35 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
36 |
import org.gvsig.fmap.dal.feature.FeatureStoreProviderFactory; |
37 |
import org.gvsig.fmap.dal.feature.FeatureType; |
38 |
import org.gvsig.fmap.dal.swing.impl.featuretype.FeatureAttributeListCellRenderer; |
39 |
import org.gvsig.fmap.dal.swing.impl.featuretype.DefaultFeatureAttributesSelectionPanel; |
40 |
import org.gvsig.tools.dataTypes.DataType; |
41 |
import org.gvsig.tools.swing.api.FilteredListController; |
42 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
43 |
import org.gvsig.tools.swing.api.ToolsSwingManager; |
44 |
import org.gvsig.tools.swing.icontheme.IconTheme; |
45 |
import org.gvsig.fmap.dal.swing.featurequery.FeatureQueryGroupByPanel; |
46 |
import org.gvsig.tools.dataTypes.DataTypes; |
47 |
import org.gvsig.tools.swing.api.FilteredListModel; |
48 |
|
49 |
/**
|
50 |
*
|
51 |
* @author jjdelcerro
|
52 |
*/
|
53 |
public class DefaultFeatureQueryGroupByPanel |
54 |
extends DefaultFeatureQueryGroupByPanelView
|
55 |
implements FeatureQueryGroupByPanel {
|
56 |
|
57 |
private class AttributesWithAggregateRenderer extends FeatureAttributeListCellRenderer { |
58 |
|
59 |
@Override
|
60 |
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { |
61 |
JLabel c = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); //To change body of generated methods, choose Tools | Templates. |
62 |
FeatureAttributeDescriptor attrdesc = (FeatureAttributeDescriptor) value; |
63 |
String aggregate = query.getAggregateFunctions().get(attrdesc.getName());
|
64 |
if( aggregate !=null ) { |
65 |
c.setText(c.getText() + " ["+aggregate+"]"); |
66 |
} else {
|
67 |
c.setText(c.getText() + " [<NULL>]");
|
68 |
} |
69 |
return c;
|
70 |
} |
71 |
} |
72 |
|
73 |
private FeatureStore store;
|
74 |
private FeatureType featureType;
|
75 |
|
76 |
private FilteredListController aggregateFunctionsController;
|
77 |
private DefaultFeatureAttributesSelectionPanel groupByPanel;
|
78 |
|
79 |
private FeatureQuery query;
|
80 |
|
81 |
public DefaultFeatureQueryGroupByPanel() {
|
82 |
this.initComponents();
|
83 |
} |
84 |
|
85 |
@Override
|
86 |
public JComponent asJComponent() { |
87 |
return this; |
88 |
} |
89 |
|
90 |
@Override
|
91 |
public ImageIcon loadImage(String imageName) { |
92 |
String name = FilenameUtils.getBaseName(imageName);
|
93 |
IconTheme theme = ToolsSwingLocator.getIconThemeManager().getDefault(); |
94 |
if (theme.exists(name)) {
|
95 |
return theme.get(name);
|
96 |
} |
97 |
URL url = this.getClass().getResource(name + ".png"); |
98 |
if (url == null) { |
99 |
return null; |
100 |
} |
101 |
return new ImageIcon(url); |
102 |
} |
103 |
|
104 |
@Override
|
105 |
public void setStore(FeatureStore store) { |
106 |
try {
|
107 |
this.featureType = store.getDefaultFeatureType();
|
108 |
this.store = store;
|
109 |
this.query = store.createFeatureQuery();
|
110 |
this.groupByPanel.setFeatureType(featureType);
|
111 |
this.groupByPanel.setSelectedNames(query.getGroupByColumns());
|
112 |
this.updateControls();
|
113 |
} catch (DataException ex) {
|
114 |
throw new RuntimeException("Can't assign store", ex); |
115 |
} |
116 |
} |
117 |
|
118 |
private void initComponents() { |
119 |
ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager(); |
120 |
|
121 |
toolsSwingManager.translate(this.btnAttributesFilter);
|
122 |
toolsSwingManager.translate(this.tabGroup);
|
123 |
|
124 |
this.groupByPanel = new DefaultFeatureAttributesSelectionPanel(); |
125 |
this.groupByPanel.allowCalculatedAttributes(true); |
126 |
|
127 |
this.pnlGroupByAttributes.setLayout(new BorderLayout()); |
128 |
this.pnlGroupByAttributes.add(this.groupByPanel, BorderLayout.CENTER); |
129 |
|
130 |
this.lstAttributes.setCellRenderer(new AttributesWithAggregateRenderer()); |
131 |
this.lstAttributes.addListSelectionListener((ListSelectionEvent e) -> { |
132 |
if (e.getValueIsAdjusting()) {
|
133 |
return;
|
134 |
} |
135 |
doSelectAttribute(); |
136 |
}); |
137 |
|
138 |
this.cboAggretateFunctions.addActionListener((ActionEvent e) -> { |
139 |
doSelectAttribute(); |
140 |
}); |
141 |
|
142 |
this.aggregateFunctionsController = toolsSwingManager.createFilteredListController(
|
143 |
lstAttributes, |
144 |
txtAttributesFilter, |
145 |
btnAttributesFilter |
146 |
); |
147 |
this.aggregateFunctionsController.addChangeListener((ChangeEvent ce) -> { |
148 |
doSelectAttribute(); |
149 |
}); |
150 |
Dimension sz = this.getPreferredSize(); |
151 |
if (sz.width < DEFAULT_WIDTH) {
|
152 |
sz.width = DEFAULT_WIDTH; |
153 |
} |
154 |
if (sz.height < DEFAULT_HEIGHT) {
|
155 |
sz.height = DEFAULT_HEIGHT; |
156 |
} |
157 |
this.setPreferredSize(sz);
|
158 |
} |
159 |
|
160 |
private void doSelectAttribute() { |
161 |
String previuos = this.txtCurrentAttributeName.getText(); |
162 |
if( !StringUtils.isBlank(previuos) ) {
|
163 |
String aggregate = (String) this.cboAggretateFunctions.getSelectedItem(); |
164 |
if( StringUtils.isBlank(aggregate) || aggregate.equalsIgnoreCase("<NULL>") ) { |
165 |
this.query.getAggregateFunctions().remove(previuos);
|
166 |
} else {
|
167 |
this.query.getAggregateFunctions().put(previuos, aggregate);
|
168 |
} |
169 |
this.updateControls();
|
170 |
} |
171 |
|
172 |
FeatureAttributeDescriptor attr = (FeatureAttributeDescriptor) this.lstAttributes.getSelectedValue();
|
173 |
if( attr == null ) { |
174 |
return;
|
175 |
} |
176 |
this.txtCurrentAttributeName.setText(attr.getName());
|
177 |
String aggregate = this.query.getAggregateFunctions().get(attr.getName()); |
178 |
if( StringUtils.isBlank(aggregate) || aggregate.equalsIgnoreCase("<NULL>") ) { |
179 |
this.cboAggretateFunctions.setSelectedIndex(0); |
180 |
} else {
|
181 |
this.cboAggretateFunctions.setSelectedItem(aggregate);
|
182 |
} |
183 |
} |
184 |
|
185 |
private void updateControls() { |
186 |
int indexAttributes = this.lstAttributes.getSelectedIndex(); |
187 |
if( this.cboAggretateFunctions.getModel().getSize() < 1 ) { |
188 |
List<String> aggregateFunctions = new ArrayList<>(); |
189 |
java.util.function.Function<String, Integer> isAllowed = ((FeatureStoreProviderFactory)(store.getProviderFactory())).allowFunction(); |
190 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
191 |
for (SymbolTableFactory factory : manager.getSymbolTableFactories()) {
|
192 |
if( StringUtils.equalsIgnoreCase(Function.GROUP_AGGREGATE, factory.getName()) ) {
|
193 |
SymbolTable symbolTable = factory.create(); |
194 |
for (Function function : symbolTable) {
|
195 |
if( isAllowed.apply(function.name())!=DataType.NO ) {
|
196 |
aggregateFunctions.add(function.name()); |
197 |
} |
198 |
} |
199 |
break;
|
200 |
} |
201 |
} |
202 |
Collections.sort(aggregateFunctions);
|
203 |
DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>(); |
204 |
model.addElement("<NULL>");
|
205 |
for (String functionName : aggregateFunctions) { |
206 |
model.addElement(functionName); |
207 |
} |
208 |
|
209 |
this.cboAggretateFunctions.setModel(model);
|
210 |
this.cboAggretateFunctions.setSelectedIndex(0); |
211 |
this.txtCurrentAttributeName.setText(""); |
212 |
} |
213 |
ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager(); |
214 |
FilteredListModel model = toolsSwingManager.createFilteredListModel(); |
215 |
for (FeatureAttributeDescriptor attr : featureType) {
|
216 |
model.addElement(attr); |
217 |
} |
218 |
model.setFilter(this.txtAttributesFilter.getText());
|
219 |
model.sort(true);
|
220 |
this.lstAttributes.setModel(model);
|
221 |
this.lstAttributes.setSelectedIndex(indexAttributes);
|
222 |
this.txtCurrentAttributeName.setText(""); |
223 |
this.cboAggretateFunctions.setSelectedIndex(0); |
224 |
|
225 |
//
|
226 |
//groupByPanel.setSelectedNames(query.getGroupByColumns());
|
227 |
} |
228 |
|
229 |
@Override
|
230 |
public FeatureQuery fetch(FeatureQuery query) {
|
231 |
if( query == null ) { |
232 |
return this.query.getCopy(); |
233 |
} |
234 |
query.getGroupByColumns().clear(); |
235 |
query.getGroupByColumns().addAll(this.groupByPanel.getSelectedNames());
|
236 |
query.getAggregateFunctions().clear(); |
237 |
query.getAggregateFunctions().putAll(this.query.getAggregateFunctions());
|
238 |
|
239 |
List<String> selectedNames = this.groupByPanel.getSelectedNames(); |
240 |
for (String selectedName : selectedNames) { |
241 |
if (this.featureType.getAttributeDescriptor(selectedName) == null) { |
242 |
try {
|
243 |
Code selectedNameCode = ExpressionUtils.compile(selectedName); |
244 |
if (selectedNameCode.code() == Code.CALLABLE) {
|
245 |
Code.Callable callable = (Code.Callable) selectedNameCode; |
246 |
if (callable.name().equalsIgnoreCase(FUNCTION_LET)) {
|
247 |
String exp = callable.parameters().get(1).toString(); |
248 |
String attrNameInExpression = (String) ((ExpressionBuilder.Constant) callable.parameters().get(0).toValue()).value(); |
249 |
if (query.getExtraColumn().get(attrNameInExpression)!=null) { |
250 |
query.getExtraColumn().remove(attrNameInExpression); |
251 |
} |
252 |
EditableFeatureAttributeDescriptor newExtraColumn = query.getExtraColumn().add(attrNameInExpression); |
253 |
newExtraColumn.setDataType(DataTypes.STRING); |
254 |
newExtraColumn.setSize(newExtraColumn.getDataType().getDefaultSize()); |
255 |
newExtraColumn.setFeatureAttributeEmulator( |
256 |
"current_row().getExtraValue('"+attrNameInExpression+"')"); |
257 |
} |
258 |
} |
259 |
} catch (Exception ex) { |
260 |
throw new RuntimeException("Not able to create column by expression in groupby query", ex); |
261 |
} |
262 |
} |
263 |
} |
264 |
|
265 |
|
266 |
return query;
|
267 |
} |
268 |
|
269 |
@Override
|
270 |
public FeatureQuery fetch() {
|
271 |
return this.fetch(null); |
272 |
} |
273 |
|
274 |
@Override
|
275 |
public void put(FeatureQuery query) { |
276 |
this.query.copyFrom(query);
|
277 |
groupByPanel.setSelectedNames(query.getGroupByColumns()); |
278 |
this.updateControls();
|
279 |
} |
280 |
|
281 |
public static void selfRegister() { |
282 |
|
283 |
} |
284 |
|
285 |
} |