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