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