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