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 / DefaultFeatureQueryOrderPanel.java @ 47426
History | View | Annotate | Download (13.1 KB)
1 | 44712 | jjdelcerro | package org.gvsig.fmap.dal.swing.impl.featurequery; |
---|---|---|---|
2 | 44644 | jjdelcerro | |
3 | import java.awt.Dimension; |
||
4 | import java.awt.event.ActionEvent; |
||
5 | import java.net.URL; |
||
6 | 46536 | fdiaz | import java.util.ArrayList; |
7 | import java.util.List; |
||
8 | 44644 | jjdelcerro | import javax.swing.ButtonGroup; |
9 | import javax.swing.DefaultListModel; |
||
10 | import javax.swing.ImageIcon; |
||
11 | import javax.swing.JComponent; |
||
12 | import javax.swing.ListCellRenderer; |
||
13 | 46526 | fdiaz | import javax.swing.ListModel; |
14 | 44644 | jjdelcerro | import javax.swing.event.ListSelectionEvent; |
15 | import org.apache.commons.io.FilenameUtils; |
||
16 | import org.gvsig.fmap.dal.exception.DataException; |
||
17 | import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
||
18 | 44712 | jjdelcerro | import org.gvsig.fmap.dal.feature.FeatureQuery; |
19 | 44644 | jjdelcerro | import org.gvsig.fmap.dal.feature.FeatureQueryOrder.FeatureQueryOrderMember; |
20 | import org.gvsig.fmap.dal.feature.FeatureStore; |
||
21 | import org.gvsig.fmap.dal.feature.FeatureType; |
||
22 | import org.gvsig.fmap.dal.swing.DALSwingLocator; |
||
23 | import org.gvsig.tools.swing.api.ActionListenerSupport; |
||
24 | 44712 | jjdelcerro | import org.gvsig.tools.swing.api.FilteredListController; |
25 | 44644 | jjdelcerro | import org.gvsig.tools.swing.api.FilteredListModel; |
26 | import org.gvsig.tools.swing.api.ToolsSwingLocator; |
||
27 | import org.gvsig.tools.swing.api.ToolsSwingManager; |
||
28 | import org.gvsig.tools.swing.icontheme.IconTheme; |
||
29 | 44712 | jjdelcerro | import org.gvsig.fmap.dal.swing.featurequery.FeatureQueryOrderPanel; |
30 | 46863 | jjdelcerro | import static org.gvsig.fmap.dal.swing.impl.DefaultDALSwingLibrary.LIBRARY_NAME; |
31 | import org.gvsig.fmap.dal.swing.impl.expressionevaluator.FeatureStoreElementFactory; |
||
32 | 46238 | jjdelcerro | import org.gvsig.tools.locator.ReferenceNotRegisteredException; |
33 | 46863 | jjdelcerro | import org.gvsig.tools.swing.api.ToolsSwingUtils; |
34 | 44644 | jjdelcerro | |
35 | /**
|
||
36 | *
|
||
37 | * @author jjdelcerro
|
||
38 | */
|
||
39 | 44712 | jjdelcerro | public class DefaultFeatureQueryOrderPanel |
40 | extends DefaultFeatureQueryOrderPanelView
|
||
41 | implements FeatureQueryOrderPanel {
|
||
42 | 44644 | jjdelcerro | |
43 | 44712 | jjdelcerro | private FeatureQuery query;
|
44 | private FeatureType featureType;
|
||
45 | private final ActionListenerSupport actionListenerSupport; |
||
46 | private final ButtonGroup ascendingGroup; |
||
47 | private FilteredListController availableFieldsController;
|
||
48 | 44644 | jjdelcerro | |
49 | 44712 | jjdelcerro | public DefaultFeatureQueryOrderPanel() {
|
50 | this.actionListenerSupport = ToolsSwingLocator.getToolsSwingManager().createActionListenerSupport();
|
||
51 | this.ascendingGroup = new ButtonGroup(); |
||
52 | this.initComponents();
|
||
53 | } |
||
54 | 44644 | jjdelcerro | |
55 | 44712 | jjdelcerro | @Override
|
56 | public FeatureQuery fetch(FeatureQuery query) {
|
||
57 | if (query == null) { |
||
58 | return this.query.getCopy(); |
||
59 | 44644 | jjdelcerro | } |
60 | 44712 | jjdelcerro | query.getOrder().copyFrom(this.query.getOrder());
|
61 | return query;
|
||
62 | } |
||
63 | 44644 | jjdelcerro | |
64 | 44712 | jjdelcerro | @Override
|
65 | public FeatureQuery fetch() {
|
||
66 | return this.fetch(null); |
||
67 | } |
||
68 | 44644 | jjdelcerro | |
69 | 44712 | jjdelcerro | @Override
|
70 | public void put(FeatureQuery query) { |
||
71 | this.query.copyFrom(query);
|
||
72 | this.updateControls();
|
||
73 | } |
||
74 | 44644 | jjdelcerro | |
75 | 44712 | jjdelcerro | private void initComponents() { |
76 | ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager(); |
||
77 | 44644 | jjdelcerro | |
78 | 44873 | jjdelcerro | toolsSwingManager.translate(this.btnColumnAdd);
|
79 | toolsSwingManager.translate(this.btnColumnDown);
|
||
80 | toolsSwingManager.translate(this.btnColumnRemove);
|
||
81 | toolsSwingManager.translate(this.btnColumnSortAscending);
|
||
82 | toolsSwingManager.translate(this.btnColumnSortDescending);
|
||
83 | toolsSwingManager.translate(this.btnColumnUp);
|
||
84 | toolsSwingManager.translate(this.btnColumnsFilter);
|
||
85 | |||
86 | 44712 | jjdelcerro | ListCellRenderer renderer = DALSwingLocator.getDataSwingManager().createDefaultFeatureAttributeListCellRenderer();
|
87 | this.lstAvailableColumns.setCellRenderer(renderer);
|
||
88 | this.lstOrderColumns.setCellRenderer(renderer);
|
||
89 | 44644 | jjdelcerro | |
90 | 44712 | jjdelcerro | this.ascendingGroup.add(this.btnColumnSortAscending); |
91 | this.ascendingGroup.add(this.btnColumnSortDescending); |
||
92 | 44644 | jjdelcerro | |
93 | 44712 | jjdelcerro | this.lstAvailableColumns.addListSelectionListener((ListSelectionEvent e) -> { |
94 | if (e.getValueIsAdjusting()) {
|
||
95 | return;
|
||
96 | } |
||
97 | doSelectAvailableColumn(); |
||
98 | }); |
||
99 | this.lstOrderColumns.addListSelectionListener((ListSelectionEvent e) -> { |
||
100 | if (e.getValueIsAdjusting()) {
|
||
101 | return;
|
||
102 | } |
||
103 | doSelectOrderColumn(); |
||
104 | }); |
||
105 | this.btnColumnAdd.addActionListener((ActionEvent e) -> { |
||
106 | doColumnAdd(); |
||
107 | }); |
||
108 | this.btnColumnRemove.addActionListener((ActionEvent e) -> { |
||
109 | doColumnRemove(); |
||
110 | }); |
||
111 | this.btnColumnDown.addActionListener((ActionEvent e) -> { |
||
112 | doColumnDown(); |
||
113 | }); |
||
114 | this.btnColumnUp.addActionListener((ActionEvent e) -> { |
||
115 | doColumnUp(); |
||
116 | }); |
||
117 | this.btnColumnSortAscending.addActionListener((ActionEvent e) -> { |
||
118 | doColumnSortAscending(); |
||
119 | }); |
||
120 | this.btnColumnSortDescending.addActionListener((ActionEvent e) -> { |
||
121 | doColumnSortDescending(); |
||
122 | }); |
||
123 | this.availableFieldsController = toolsSwingManager.createFilteredListController(
|
||
124 | lstAvailableColumns, |
||
125 | txtColumnsFilter, |
||
126 | btnColumnsFilter |
||
127 | ); |
||
128 | Dimension sz = this.getPreferredSize(); |
||
129 | 44713 | jjdelcerro | if (sz.width < DEFAULT_WIDTH) {
|
130 | sz.width = DEFAULT_WIDTH; |
||
131 | 44644 | jjdelcerro | } |
132 | 44713 | jjdelcerro | if (sz.height < DEFAULT_HEIGHT) {
|
133 | sz.height = DEFAULT_HEIGHT; |
||
134 | 44644 | jjdelcerro | } |
135 | 44712 | jjdelcerro | this.setPreferredSize(sz);
|
136 | } |
||
137 | 44644 | jjdelcerro | |
138 | 44712 | jjdelcerro | private void doSelectAvailableColumn() { |
139 | if (this.lstAvailableColumns.getSelectedIndex() >= 0) { |
||
140 | this.btnColumnAdd.setEnabled(true); |
||
141 | return;
|
||
142 | 44644 | jjdelcerro | } |
143 | 44712 | jjdelcerro | this.btnColumnAdd.setEnabled(false); |
144 | } |
||
145 | 44644 | jjdelcerro | |
146 | 46526 | fdiaz | private void doSelectOrderColumn() { |
147 | int indexOrder = this.lstOrderColumns.getSelectedIndex(); |
||
148 | |||
149 | ListModel modelOrder = this.lstOrderColumns.getModel(); |
||
150 | this.btnColumnUp.setEnabled(indexOrder >= 1); |
||
151 | this.btnColumnDown.setEnabled(indexOrder >= 0 && indexOrder < modelOrder.getSize() - 1); |
||
152 | |||
153 | if (indexOrder >= 0) { |
||
154 | FeatureAttributeDescriptor attr = (FeatureAttributeDescriptor) this.lstOrderColumns.getSelectedValue();
|
||
155 | if (attr != null) { |
||
156 | FeatureQueryOrderMember member = this.query.getOrder().get(attr.getName());
|
||
157 | if (member != null) { |
||
158 | this.btnColumnRemove.setEnabled(true); |
||
159 | // this.btnColumnDown.setEnabled(true);
|
||
160 | // this.btnColumnUp.setEnabled(true);
|
||
161 | this.btnColumnSortAscending.setEnabled(true); |
||
162 | this.btnColumnSortDescending.setEnabled(true); |
||
163 | if (member.getAscending()) {
|
||
164 | this.btnColumnSortAscending.setSelected(true); |
||
165 | this.btnColumnSortDescending.setSelected(false); |
||
166 | } else {
|
||
167 | this.btnColumnSortAscending.setSelected(false); |
||
168 | this.btnColumnSortDescending.setSelected(true); |
||
169 | } |
||
170 | return;
|
||
171 | } |
||
172 | } |
||
173 | 44644 | jjdelcerro | } |
174 | 46526 | fdiaz | |
175 | this.btnColumnRemove.setEnabled(false); |
||
176 | this.btnColumnDown.setEnabled(false); |
||
177 | // this.btnColumnUp.setEnabled(false);
|
||
178 | // this.btnColumnSortAscending.setEnabled(false);
|
||
179 | this.btnColumnSortDescending.setEnabled(false); |
||
180 | this.btnColumnSortAscending.setSelected(false); |
||
181 | this.btnColumnSortDescending.setSelected(false); |
||
182 | 44644 | jjdelcerro | } |
183 | |||
184 | 44712 | jjdelcerro | private void doColumnSortAscending() { |
185 | FeatureAttributeDescriptor attr = (FeatureAttributeDescriptor) this.lstOrderColumns.getSelectedValue();
|
||
186 | if (attr == null) { |
||
187 | return;
|
||
188 | 44644 | jjdelcerro | } |
189 | 44712 | jjdelcerro | FeatureQueryOrderMember member = this.query.getOrder().get(attr.getName());
|
190 | member.setAscending(true);
|
||
191 | } |
||
192 | 44644 | jjdelcerro | |
193 | 44712 | jjdelcerro | private void doColumnSortDescending() { |
194 | FeatureAttributeDescriptor attr = (FeatureAttributeDescriptor) this.lstOrderColumns.getSelectedValue();
|
||
195 | if (attr == null) { |
||
196 | return;
|
||
197 | 44644 | jjdelcerro | } |
198 | 44712 | jjdelcerro | FeatureQueryOrderMember member = this.query.getOrder().get(attr.getName());
|
199 | member.setAscending(false);
|
||
200 | } |
||
201 | 44644 | jjdelcerro | |
202 | 44712 | jjdelcerro | private void doColumnUp() { |
203 | FeatureAttributeDescriptor attr = (FeatureAttributeDescriptor) this.lstOrderColumns.getSelectedValue();
|
||
204 | if (attr == null) { |
||
205 | return;
|
||
206 | 44644 | jjdelcerro | } |
207 | 46526 | fdiaz | int selectedIndex = this.lstOrderColumns.getSelectedIndex(); |
208 | if(selectedIndex<1){ |
||
209 | return;
|
||
210 | } |
||
211 | 44712 | jjdelcerro | this.query.getOrder().movePrevious(attr.getName());
|
212 | 46526 | fdiaz | this.lstOrderColumns.setSelectedIndex(selectedIndex-1); |
213 | 44712 | jjdelcerro | this.updateControls();
|
214 | } |
||
215 | 44644 | jjdelcerro | |
216 | 44712 | jjdelcerro | private void doColumnDown() { |
217 | FeatureAttributeDescriptor attr = (FeatureAttributeDescriptor) this.lstOrderColumns.getSelectedValue();
|
||
218 | if (attr == null) { |
||
219 | return;
|
||
220 | 44644 | jjdelcerro | } |
221 | 46526 | fdiaz | int selectedIndex = this.lstOrderColumns.getSelectedIndex(); |
222 | if(selectedIndex>=this.lstOrderColumns.getModel().getSize()-1){ |
||
223 | return;
|
||
224 | } |
||
225 | 44712 | jjdelcerro | this.query.getOrder().moveNext(attr.getName());
|
226 | 46526 | fdiaz | this.lstOrderColumns.setSelectedIndex(selectedIndex+1); |
227 | 44712 | jjdelcerro | this.updateControls();
|
228 | } |
||
229 | 44644 | jjdelcerro | |
230 | 44712 | jjdelcerro | private void doColumnRemove() { |
231 | FeatureAttributeDescriptor attr = (FeatureAttributeDescriptor) this.lstOrderColumns.getSelectedValue();
|
||
232 | if (attr == null) { |
||
233 | return;
|
||
234 | 44644 | jjdelcerro | } |
235 | 44712 | jjdelcerro | FeatureQueryOrderMember member = this.query.getOrder().get(attr.getName());
|
236 | this.query.getOrder().remove(member);
|
||
237 | this.updateControls();
|
||
238 | } |
||
239 | 44644 | jjdelcerro | |
240 | 44712 | jjdelcerro | private void doColumnAdd() { |
241 | FeatureAttributeDescriptor attr = (FeatureAttributeDescriptor) this.lstAvailableColumns.getSelectedValue();
|
||
242 | if (attr == null) { |
||
243 | return;
|
||
244 | 44644 | jjdelcerro | } |
245 | 44712 | jjdelcerro | this.query.getOrder().add(attr.getName(), true); |
246 | this.updateControls();
|
||
247 | } |
||
248 | 44644 | jjdelcerro | |
249 | 44712 | jjdelcerro | @Override
|
250 | public void setStore(FeatureStore store) { |
||
251 | try {
|
||
252 | this.featureType = store.getDefaultFeatureType();
|
||
253 | this.query = store.createFeatureQuery();
|
||
254 | } catch (DataException ex) {
|
||
255 | throw new RuntimeException("Can't assign store", ex); |
||
256 | 44644 | jjdelcerro | } |
257 | 44712 | jjdelcerro | } |
258 | 44644 | jjdelcerro | |
259 | 44712 | jjdelcerro | @Override
|
260 | public JComponent asJComponent() { |
||
261 | return this; |
||
262 | } |
||
263 | |||
264 | private void updateControls() { |
||
265 | int indexAvailables = this.lstAvailableColumns.getSelectedIndex(); |
||
266 | int indexOrder = this.lstOrderColumns.getSelectedIndex(); |
||
267 | FilteredListModel modelAvailables = ToolsSwingLocator.getToolsSwingManager().createFilteredListModel(); |
||
268 | DefaultListModel<FeatureAttributeDescriptor> modelOrder = new DefaultListModel<>(); |
||
269 | for (FeatureAttributeDescriptor attr : featureType) {
|
||
270 | if (this.query.getOrder() == null || !this.query.getOrder().contains(attr.getName())) { |
||
271 | modelAvailables.addElement(attr); |
||
272 | } |
||
273 | 44644 | jjdelcerro | } |
274 | 45162 | omartinez | for (FeatureAttributeDescriptor attr : this.query.getExtraColumn().getColumns()) { |
275 | if (this.query.getOrder() == null || !this.query.getOrder().contains(attr.getName())) { |
||
276 | modelAvailables.addElement(attr); |
||
277 | } |
||
278 | } |
||
279 | 46536 | fdiaz | List<FeatureQueryOrderMember> membersToRemove = new ArrayList<>(); |
280 | 44712 | jjdelcerro | for (FeatureQueryOrderMember member : this.query.getOrder().members()) { |
281 | FeatureAttributeDescriptor attr = this.featureType.getAttributeDescriptor(member.getAttributeName());
|
||
282 | 45162 | omartinez | if (attr==null) { |
283 | attr = this.query.getExtraColumn().get(member.getAttributeName());
|
||
284 | } |
||
285 | 46536 | fdiaz | if(attr == null){ |
286 | membersToRemove.add(member); |
||
287 | } else {
|
||
288 | modelOrder.addElement(attr); |
||
289 | } |
||
290 | 44712 | jjdelcerro | } |
291 | 46536 | fdiaz | for (FeatureQueryOrderMember member : membersToRemove) {
|
292 | this.query.getOrder().remove(member);
|
||
293 | } |
||
294 | 44740 | jjdelcerro | modelAvailables.setFilter(this.txtColumnsFilter.getText());
|
295 | 44712 | jjdelcerro | modelAvailables.sort(true);
|
296 | this.lstAvailableColumns.setModel(modelAvailables);
|
||
297 | this.lstOrderColumns.setModel(modelOrder);
|
||
298 | if (indexAvailables >= 0 && modelAvailables.getSize() > 0) { |
||
299 | if (indexAvailables >= modelAvailables.getSize()) {
|
||
300 | indexAvailables = modelAvailables.getSize() - 1;
|
||
301 | } |
||
302 | this.lstAvailableColumns.setSelectedIndex(indexAvailables);
|
||
303 | this.btnColumnAdd.setEnabled(true); |
||
304 | } else {
|
||
305 | this.btnColumnAdd.setEnabled(false); |
||
306 | } |
||
307 | 44740 | jjdelcerro | |
308 | |||
309 | 44712 | jjdelcerro | if (indexOrder >= 0 && modelOrder.getSize() > 0) { |
310 | if (indexOrder >= modelOrder.getSize()) {
|
||
311 | indexOrder = modelOrder.getSize() - 1;
|
||
312 | } |
||
313 | this.lstOrderColumns.setSelectedIndex(indexOrder);
|
||
314 | 46526 | fdiaz | this.btnColumnUp.setEnabled(indexOrder >= 1); |
315 | this.btnColumnDown.setEnabled(indexOrder < modelOrder.getSize()-1); |
||
316 | 44712 | jjdelcerro | FeatureAttributeDescriptor attr = modelOrder.get(indexOrder); |
317 | FeatureQueryOrderMember member = this.query.getOrder().get(attr.getName());
|
||
318 | if (member == null) { |
||
319 | this.btnColumnSortAscending.setSelected(true); |
||
320 | this.btnColumnSortDescending.setSelected(false); |
||
321 | this.btnColumnSortAscending.setEnabled(false); |
||
322 | this.btnColumnSortDescending.setEnabled(false); |
||
323 | } else {
|
||
324 | this.btnColumnSortAscending.setEnabled(true); |
||
325 | this.btnColumnSortDescending.setEnabled(true); |
||
326 | if (member.getAscending()) {
|
||
327 | this.btnColumnSortAscending.setSelected(true); |
||
328 | this.btnColumnSortDescending.setSelected(false); |
||
329 | 44644 | jjdelcerro | } else {
|
330 | 44712 | jjdelcerro | this.btnColumnSortAscending.setSelected(false); |
331 | this.btnColumnSortDescending.setSelected(true); |
||
332 | 44644 | jjdelcerro | } |
333 | 44712 | jjdelcerro | } |
334 | this.btnColumnRemove.setEnabled(true); |
||
335 | } else {
|
||
336 | this.btnColumnSortAscending.setSelected(true); |
||
337 | this.btnColumnSortDescending.setSelected(false); |
||
338 | this.btnColumnSortAscending.setEnabled(false); |
||
339 | this.btnColumnSortDescending.setEnabled(false); |
||
340 | this.btnColumnRemove.setEnabled(false); |
||
341 | 46526 | fdiaz | this.btnColumnUp.setEnabled(false); |
342 | this.btnColumnDown.setEnabled(false); |
||
343 | |||
344 | 44644 | jjdelcerro | } |
345 | 44712 | jjdelcerro | } |
346 | 44644 | jjdelcerro | |
347 | 44712 | jjdelcerro | @Override
|
348 | public ImageIcon loadImage(String imageName) { |
||
349 | 46863 | jjdelcerro | return ToolsSwingUtils.loadImage(this, imageName); |
350 | 44712 | jjdelcerro | } |
351 | 44644 | jjdelcerro | |
352 | 44712 | jjdelcerro | public static void selfRegister() { |
353 | 47426 | jjdelcerro | // boolean n = ToolsSwingUtils.registerIcons(
|
354 | // DefaultFeatureQueryOrderPanel.class,
|
||
355 | // null,
|
||
356 | // LIBRARY_NAME,
|
||
357 | // new String[]{ "featurequery", "featurequery-column-add-arrow"},
|
||
358 | // new String[]{ "featurequery", "featurequery-column-remove-arrow"},
|
||
359 | // new String[]{ "featurequery", "featurequery-column-down"},
|
||
360 | // new String[]{ "featurequery", "featurequery-column-up"}
|
||
361 | // );
|
||
362 | 44712 | jjdelcerro | } |
363 | 44644 | jjdelcerro | } |