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