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 / featureform / swing / impl / DefaultJFeaturesForm.java @ 44488
History | View | Annotate | Download (30 KB)
1 |
/* gvSIG. Desktop Geographic Information System.
|
---|---|
2 |
*
|
3 |
* Copyright ? 2007-2014 gvSIG Association
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
18 |
* MA 02110-1301, USA.
|
19 |
*
|
20 |
* For any additional information, do not hesitate to contact us
|
21 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
22 |
*/
|
23 |
package org.gvsig.featureform.swing.impl; |
24 |
|
25 |
import java.awt.BorderLayout; |
26 |
import java.awt.Dimension; |
27 |
import java.awt.event.ActionEvent; |
28 |
import java.awt.event.ComponentAdapter; |
29 |
import java.awt.event.ComponentEvent; |
30 |
import java.util.ArrayList; |
31 |
import java.util.Collection; |
32 |
import java.util.List; |
33 |
import javax.swing.AbstractAction; |
34 |
import javax.swing.Action; |
35 |
import static javax.swing.Action.ACTION_COMMAND_KEY; |
36 |
import static javax.swing.Action.NAME; |
37 |
import static javax.swing.Action.SHORT_DESCRIPTION; |
38 |
import static javax.swing.Action.SMALL_ICON; |
39 |
|
40 |
import javax.swing.JComponent; |
41 |
import javax.swing.JOptionPane; |
42 |
import javax.swing.JPanel; |
43 |
import org.apache.commons.lang3.StringUtils; |
44 |
import org.gvsig.expressionevaluator.Expression; |
45 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator; |
46 |
import org.gvsig.expressionevaluator.ExpressionUtils; |
47 |
|
48 |
import org.slf4j.Logger; |
49 |
import org.slf4j.LoggerFactory; |
50 |
|
51 |
import org.gvsig.featureform.swing.JFeaturesForm; |
52 |
import org.gvsig.fmap.dal.DALLocator; |
53 |
import org.gvsig.fmap.dal.DataStore; |
54 |
import org.gvsig.fmap.dal.StoresRepository; |
55 |
import org.gvsig.fmap.dal.exception.DataException; |
56 |
import org.gvsig.fmap.dal.expressionevaluator.ExpressionEvaluator; |
57 |
import org.gvsig.fmap.dal.feature.Feature; |
58 |
import org.gvsig.fmap.dal.feature.FacadeOfAFeature; |
59 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
60 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
61 |
import org.gvsig.fmap.dal.feature.FeatureStoreNotification; |
62 |
import org.gvsig.fmap.dal.feature.FeatureType; |
63 |
import org.gvsig.fmap.dal.feature.paging.FeaturePagingHelper; |
64 |
import org.gvsig.fmap.dal.swing.AbstractDALActionFactory.AbstractDALActionContext; |
65 |
import org.gvsig.fmap.dal.swing.DALActionFactory; |
66 |
import org.gvsig.fmap.dal.swing.DALSwingLocator; |
67 |
import org.gvsig.fmap.dal.swing.DataSwingManager; |
68 |
import org.gvsig.fmap.dal.swing.impl.DefaultDataSwingManager; |
69 |
import org.gvsig.fmap.dal.swing.impl.actions.ShowFormAction.ShowFormActionFactory; |
70 |
import org.gvsig.fmap.dal.swing.searchpanel.FeatureStoreSearchPanel; |
71 |
import org.gvsig.tools.ToolsLocator; |
72 |
import org.gvsig.tools.dispose.DisposeUtils; |
73 |
import org.gvsig.tools.dynform.AbortActionException; |
74 |
import org.gvsig.tools.dynform.DynFormDefinition; |
75 |
import org.gvsig.tools.dynform.DynFormLocator; |
76 |
import org.gvsig.tools.dynform.DynFormManager; |
77 |
import org.gvsig.tools.dynform.JDynForm; |
78 |
import org.gvsig.tools.dynform.JDynFormSet; |
79 |
import static org.gvsig.tools.dynform.JDynFormSet.ACTION_CANCEL_NEW; |
80 |
import static org.gvsig.tools.dynform.JDynFormSet.ACTION_DELETE; |
81 |
import static org.gvsig.tools.dynform.JDynFormSet.ACTION_NEW; |
82 |
import static org.gvsig.tools.dynform.JDynFormSet.ACTION_SAVE; |
83 |
import static org.gvsig.tools.dynform.JDynFormSet.ACTION_SEARCH; |
84 |
import org.gvsig.tools.dynform.JDynFormSet.JDynFormSetListener; |
85 |
import org.gvsig.tools.dynobject.DynClass; |
86 |
import org.gvsig.tools.dynobject.DynObject; |
87 |
import org.gvsig.tools.evaluator.Evaluator; |
88 |
import org.gvsig.tools.exception.BaseException; |
89 |
import org.gvsig.tools.i18n.I18nManager; |
90 |
import org.gvsig.tools.observer.Observable; |
91 |
import org.gvsig.tools.observer.Observer; |
92 |
import org.gvsig.tools.service.ServiceException; |
93 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
94 |
import org.gvsig.tools.swing.api.threadsafedialogs.ThreadSafeDialogsManager; |
95 |
import org.gvsig.tools.swing.api.windowmanager.Dialog; |
96 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
97 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2; |
98 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager.MODE; |
99 |
import org.gvsig.tools.swing.icontheme.IconTheme; |
100 |
import org.gvsig.tools.resourcesstorage.ResourcesStorage; |
101 |
import org.gvsig.tools.script.ScriptManager; |
102 |
|
103 |
/**
|
104 |
* @author fdiaz
|
105 |
*
|
106 |
*/
|
107 |
@SuppressWarnings("UseSpecificCatch") |
108 |
public class DefaultJFeaturesForm implements JFeaturesForm { |
109 |
|
110 |
private final String STARTEDITING_ACTION = "startEditing"; |
111 |
private final String FINISHEDITING_ACTION = "finishEditing"; |
112 |
|
113 |
|
114 |
public class DefaultFeaturesFormContext implements FeaturesFormContext { |
115 |
|
116 |
private DefaultFeaturesFormContext() {
|
117 |
|
118 |
} |
119 |
|
120 |
@Override
|
121 |
public FeatureStore getFeatureStore() {
|
122 |
return store;
|
123 |
} |
124 |
|
125 |
@Override
|
126 |
public FeatureType getFeatureType() {
|
127 |
try {
|
128 |
return store.getDefaultFeatureType();
|
129 |
} catch (DataException ex) {
|
130 |
return null; |
131 |
} |
132 |
} |
133 |
|
134 |
@Override
|
135 |
public ResourcesStorage getResourcesStorage() {
|
136 |
if( store == null ) { |
137 |
return null; |
138 |
} |
139 |
return store.getResourcesStorage();
|
140 |
} |
141 |
|
142 |
@Override
|
143 |
public StoresRepository getStoresRepository() {
|
144 |
if( store == null ) { |
145 |
return null; |
146 |
} |
147 |
return store.getStoresRepository();
|
148 |
} |
149 |
|
150 |
@Override
|
151 |
public ScriptManager getScriptManager() {
|
152 |
return ExpressionEvaluatorLocator.getExpressionEvaluatorManager();
|
153 |
} |
154 |
|
155 |
|
156 |
} |
157 |
|
158 |
private class FormActionContext extends AbstractDALActionContext { |
159 |
|
160 |
public FormActionContext() {
|
161 |
super(ACTION_CONTEXT_NAME);
|
162 |
} |
163 |
|
164 |
@Override
|
165 |
public DataStore getStore() {
|
166 |
return store;
|
167 |
} |
168 |
|
169 |
@Override
|
170 |
public Expression getFilter() { |
171 |
FeatureQuery theCurrentQuery = currentQuery; |
172 |
if( theCurrentQuery!=null ) { |
173 |
Evaluator filter = theCurrentQuery.getFilter(); |
174 |
if( filter instanceof ExpressionEvaluator ) { |
175 |
Expression expression = ((ExpressionEvaluator)filter).getExpression();
|
176 |
return expression;
|
177 |
} |
178 |
} |
179 |
return null; |
180 |
} |
181 |
|
182 |
@Override
|
183 |
public int getSelectedsCount() { |
184 |
return 0; |
185 |
} |
186 |
|
187 |
@Override
|
188 |
public Expression getFilterForSelecteds() { |
189 |
return null; |
190 |
} |
191 |
|
192 |
@Override
|
193 |
public JComponent getActionButton(String actionName) { |
194 |
if( formset == null ) { |
195 |
return null; |
196 |
} |
197 |
return formset.getActionButton(actionName);
|
198 |
} |
199 |
|
200 |
} |
201 |
|
202 |
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultJFeaturesForm.class); |
203 |
|
204 |
private static final int PAGE_SIZE = 500; |
205 |
private JPanel panel; |
206 |
private JDynFormSet formset;
|
207 |
private FeatureStore store;
|
208 |
private FeaturePagingHelper features;
|
209 |
private DynFormDefinition definition = null; |
210 |
private FeatureQuery currentQuery;
|
211 |
private List<Action> otherActions; |
212 |
|
213 |
public DefaultJFeaturesForm() {
|
214 |
this.otherActions = new ArrayList<>(); |
215 |
this.panel = new JPanel(new BorderLayout()); |
216 |
this.panel.addComponentListener(new ComponentAdapter() { |
217 |
@Override
|
218 |
public void componentHidden(ComponentEvent e) { |
219 |
doDispose(); |
220 |
} |
221 |
}); |
222 |
} |
223 |
|
224 |
@Override
|
225 |
public void setPreferredSize(Dimension dimension) { |
226 |
// panel.setPreferredSize(dimension);
|
227 |
} |
228 |
|
229 |
private void updateForm() { |
230 |
if (this.formset == null) { |
231 |
this.panel.add(this.getFormset().asJComponent(), BorderLayout.CENTER); |
232 |
} |
233 |
try {
|
234 |
if( this.currentQuery!=null ) { |
235 |
this.currentQuery.retrievesAllAttributes();
|
236 |
} |
237 |
this.features = DALLocator.getDataManager().createFeaturePagingHelper(store, this.currentQuery, PAGE_SIZE); |
238 |
this.formset.setValues(features.asListOfDynObjects());
|
239 |
} catch (Exception ex) { |
240 |
throw new RuntimeException("Can't update form", ex); |
241 |
} |
242 |
updateButtonEnabledStatus(); |
243 |
} |
244 |
|
245 |
@Override
|
246 |
public JComponent asJComponent() { |
247 |
if (this.features == null) { |
248 |
try {
|
249 |
updateForm(); |
250 |
} catch (Exception ex) { |
251 |
throw new RuntimeException(ex); |
252 |
} |
253 |
} |
254 |
return this.panel; |
255 |
} |
256 |
|
257 |
@Override
|
258 |
public void bind(FeatureStore store) { |
259 |
if (store == null) { |
260 |
throw new IllegalArgumentException("bind need a store as parameter, not a null."); |
261 |
} |
262 |
try {
|
263 |
DefaultDataSwingManager manager = (DefaultDataSwingManager) DALSwingLocator.getSwingManager(); |
264 |
DynClass theDefinition = manager.featureType2DynClass(store, store.getDefaultFeatureType()); |
265 |
this.bind(store, theDefinition);
|
266 |
} catch (Exception ex) { |
267 |
throw new RuntimeException("Can't bind store '" + store.getName() + "' to form", ex); |
268 |
} |
269 |
} |
270 |
|
271 |
public void bind(FeatureStore store, DynClass definition) throws ServiceException, DataException { |
272 |
if (this.store == store) { |
273 |
return;
|
274 |
} |
275 |
DynFormManager formManager = DynFormLocator.getDynFormManager(); |
276 |
this.definition = formManager.getDefinition(definition);
|
277 |
if (formset != null) { |
278 |
this.panel.remove(formset.asJComponent());
|
279 |
this.formset = null; |
280 |
} |
281 |
this.store = store;
|
282 |
DisposeUtils.bind(store); |
283 |
this.features = null; |
284 |
} |
285 |
|
286 |
private void doDispose() { |
287 |
DisposeUtils.disposeQuietly(store); |
288 |
this.store = null; |
289 |
this.panel = null; |
290 |
this.formset = null; |
291 |
this.features = null; |
292 |
this.definition = null; |
293 |
this.currentQuery = null; |
294 |
this.otherActions = null; |
295 |
} |
296 |
|
297 |
@Override
|
298 |
public JDynFormSet getFormset() {
|
299 |
if (this.formset == null) { |
300 |
DynFormManager formManager = DynFormLocator.getDynFormManager(); |
301 |
this.formset = formManager.createJDynFormSet(
|
302 |
new DefaultFeaturesFormContext(),
|
303 |
this.definition,
|
304 |
null
|
305 |
); |
306 |
List<String> groups = this.definition.getGroups(); |
307 |
if( groups.size()==1 && groups.get(0)==null ) { |
308 |
this.formset.setLayoutMode(JDynForm.USE_PLAIN);
|
309 |
} else {
|
310 |
this.formset.setLayoutMode(JDynForm.USE_TABS);
|
311 |
} |
312 |
this.formset.setAllowNew(true); |
313 |
this.formset.setAllowDelete(true); |
314 |
this.formset.setAllowUpdate(true); |
315 |
this.formset.setAllowClose(true); |
316 |
this.formset.setAllowSearch(true); |
317 |
this.formset.setAutosave(true); |
318 |
|
319 |
this.formset.addAction(new StartEditingAction()); |
320 |
this.formset.addAction(new FinishEditingAction()); |
321 |
|
322 |
FormActionContext actionContext = new FormActionContext();
|
323 |
Collection<DALActionFactory> factories = DALSwingLocator.getSwingManager().getStoreActions();
|
324 |
for (DALActionFactory factory : factories) {
|
325 |
if( StringUtils.equalsIgnoreCase(factory.getName(), ShowFormActionFactory.ACTION_NAME) ) {
|
326 |
continue;
|
327 |
} |
328 |
Action action = factory.createAction(actionContext);
|
329 |
this.formset.addAction(action);
|
330 |
} |
331 |
for( Action action : this.otherActions ) { |
332 |
this.formset.addAction(action);
|
333 |
} |
334 |
|
335 |
this.formset.addListener(new FormSetListener()); |
336 |
} |
337 |
updateButtonEnabledStatus(); |
338 |
return this.formset; |
339 |
} |
340 |
|
341 |
@Override
|
342 |
public void addAction(Action action) { |
343 |
this.otherActions.add(action);
|
344 |
if( this.formset!=null ) { |
345 |
this.formset.addAction(action);
|
346 |
} |
347 |
} |
348 |
|
349 |
@Override
|
350 |
public long getCurrentIndex() { |
351 |
if( this.formset==null ) { |
352 |
return -1; |
353 |
} |
354 |
return this.formset.getCurrentIndex(); |
355 |
} |
356 |
|
357 |
@Override
|
358 |
public Feature get(long index) { |
359 |
if( this.formset==null || this.features==null ) { |
360 |
return null; |
361 |
} |
362 |
try {
|
363 |
return this.features.getFeatureAt(index); |
364 |
} catch (BaseException ex) {
|
365 |
return null; |
366 |
} |
367 |
} |
368 |
|
369 |
private class FinishEditingAction extends AbstractAction implements Observer { |
370 |
|
371 |
@SuppressWarnings({"OverridableMethodCallInConstructor", "LeakingThisInConstructor"}) |
372 |
public FinishEditingAction() {
|
373 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
374 |
IconTheme iconTheme = ToolsSwingLocator.getIconThemeManager().getDefault(); |
375 |
|
376 |
this.putValue(NAME,null); |
377 |
this.putValue(SHORT_DESCRIPTION,i18nManager.getTranslation("_Stop_editing")); |
378 |
this.putValue(SMALL_ICON, iconTheme.get("table-stop-editing")); |
379 |
this.putValue(ACTION_COMMAND_KEY, FINISHEDITING_ACTION);
|
380 |
|
381 |
this.setEnabled(store.isEditing());
|
382 |
store.addObserver(this);
|
383 |
} |
384 |
|
385 |
@Override
|
386 |
public void actionPerformed(ActionEvent ae) { |
387 |
if( store.isEditing() ) {
|
388 |
try {
|
389 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
390 |
ThreadSafeDialogsManager dialogManager = ToolsSwingLocator.getThreadSafeDialogsManager(); |
391 |
int x = dialogManager.confirmDialog(
|
392 |
"? Desea terminar edicion y guardar los cambios ?\n\nPulse cancelar para cancelar la edicion y los cambios.",
|
393 |
i18nManager.getTranslation("_Stop_editing"),
|
394 |
JOptionPane.YES_NO_CANCEL_OPTION,
|
395 |
JOptionPane.QUESTION_MESSAGE
|
396 |
); |
397 |
switch(x) {
|
398 |
case JOptionPane.YES_OPTION: |
399 |
store.finishEditing(); |
400 |
break;
|
401 |
case JOptionPane.NO_OPTION: |
402 |
break;
|
403 |
case JOptionPane.CANCEL_OPTION: |
404 |
store.cancelEditing(); |
405 |
break;
|
406 |
} |
407 |
} catch (DataException ex) {
|
408 |
LOGGER.warn("Can't finish editing in FeatureForm ("+store.getName()+").",ex); |
409 |
} |
410 |
} |
411 |
updateButtonEnabledStatus(); |
412 |
} |
413 |
|
414 |
@Override
|
415 |
public void update(Observable observable, Object notification) { |
416 |
if( store == null || formset==null ) { |
417 |
return;
|
418 |
} |
419 |
if( notification instanceof FeatureStoreNotification ) { |
420 |
FeatureStoreNotification n = (FeatureStoreNotification) notification; |
421 |
switch( n.getType() ) {
|
422 |
case FeatureStoreNotification.AFTER_STARTEDITING:
|
423 |
case FeatureStoreNotification.AFTER_FINISHEDITING:
|
424 |
case FeatureStoreNotification.AFTER_CANCELEDITING:
|
425 |
updateButtonEnabledStatus(); |
426 |
break;
|
427 |
} |
428 |
} |
429 |
} |
430 |
|
431 |
} |
432 |
|
433 |
private class StartEditingAction extends AbstractAction implements Observer { |
434 |
|
435 |
@SuppressWarnings({"OverridableMethodCallInConstructor", "LeakingThisInConstructor"}) |
436 |
public StartEditingAction() {
|
437 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
438 |
IconTheme iconTheme = ToolsSwingLocator.getIconThemeManager().getDefault(); |
439 |
|
440 |
this.putValue(NAME,null); |
441 |
this.putValue(SHORT_DESCRIPTION,i18nManager.getTranslation("_Start_editing")); |
442 |
this.putValue(SMALL_ICON, iconTheme.get("table-start-editing")); |
443 |
this.putValue(ACTION_COMMAND_KEY, STARTEDITING_ACTION);
|
444 |
|
445 |
this.setEnabled(!store.isEditing());
|
446 |
store.addObserver(this);
|
447 |
} |
448 |
|
449 |
@Override
|
450 |
public void actionPerformed(ActionEvent ae) { |
451 |
if( !store.isEditing() ) {
|
452 |
try {
|
453 |
store.edit(); |
454 |
} catch (DataException ex) {
|
455 |
LOGGER.warn("Can't finish editing in FeatureForm ("+store.getName()+").",ex); |
456 |
} |
457 |
} |
458 |
updateButtonEnabledStatus(); |
459 |
} |
460 |
|
461 |
@Override
|
462 |
public void update(Observable observable, Object notification) { |
463 |
if( store == null || formset==null ) { |
464 |
return;
|
465 |
} |
466 |
if( notification instanceof FeatureStoreNotification ) { |
467 |
FeatureStoreNotification n = (FeatureStoreNotification) notification; |
468 |
switch( n.getType() ) {
|
469 |
case FeatureStoreNotification.AFTER_STARTEDITING:
|
470 |
case FeatureStoreNotification.AFTER_FINISHEDITING:
|
471 |
case FeatureStoreNotification.AFTER_CANCELEDITING:
|
472 |
updateButtonEnabledStatus(); |
473 |
break;
|
474 |
} |
475 |
} |
476 |
} |
477 |
|
478 |
} |
479 |
|
480 |
@Override
|
481 |
public void setQuery(FeatureQuery query) { |
482 |
if (this.features != null) { |
483 |
if (this.formset != null && !formset.isReadOnly() && this.formset.isAutosave() && this.formset.countValues() > 0) { |
484 |
if (!store.isEditing()) {
|
485 |
try {
|
486 |
store.edit(); |
487 |
} catch (DataException e1) {
|
488 |
throw new RuntimeException("Can't set query", e1); |
489 |
} |
490 |
} |
491 |
saveChanges(this.formset);
|
492 |
} |
493 |
} |
494 |
this.currentQuery = query;
|
495 |
updateForm(); |
496 |
} |
497 |
|
498 |
private FeatureQuery getCurrentQuery() {
|
499 |
return this.currentQuery; |
500 |
} |
501 |
|
502 |
@Override
|
503 |
public void showForm(MODE mode) { |
504 |
this.panel.add(this.getFormset().asJComponent(), BorderLayout.CENTER); |
505 |
WindowManager winmgr = ToolsSwingLocator.getWindowManager(); |
506 |
String title = this.definition.getLabel(); |
507 |
winmgr.showWindow(this.asJComponent(), title, mode);
|
508 |
} |
509 |
|
510 |
private void saveChanges(JDynFormSet theFormSet) { |
511 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
512 |
try {
|
513 |
if( theFormSet.isInNewState() ) {
|
514 |
Feature newFeature = store.createNewFeature(false);
|
515 |
DynObject newElement = newFeature.getAsDynObject(); |
516 |
theFormSet.getFormValues(newElement); |
517 |
features.insert(((FacadeOfAFeature)newElement).getEditableFeature()); |
518 |
this.formset.message(i18n.getTranslation("_Record_saved")); |
519 |
try {
|
520 |
this.formset.setValues(features.asListOfDynObjects());
|
521 |
this.formset.setCurrentIndex((int)(features.getTotalSize())-1); |
522 |
} catch(Exception ex) { |
523 |
LOGGER.warn("Can't reload form data after insert.",ex);
|
524 |
} |
525 |
} else {
|
526 |
int index = theFormSet.getCurrentIndex();
|
527 |
DynObject currentElement = theFormSet.get(index); |
528 |
theFormSet.getFormValues(currentElement); |
529 |
features.update(((FacadeOfAFeature)currentElement).getEditableFeature()); |
530 |
this.formset.message(i18n.getTranslation("_Record_saved")); |
531 |
} |
532 |
} catch(Exception ex) { |
533 |
theFormSet.message(i18n.getTranslation("error_saving_data_will_not_save"));
|
534 |
throw new RuntimeException("Can't save values",ex); |
535 |
|
536 |
} finally {
|
537 |
updateButtonEnabledStatus(); |
538 |
} |
539 |
|
540 |
} |
541 |
|
542 |
@Override
|
543 |
public void saveChanges() { |
544 |
if (this.formset != null && this.formset.countValues() > 0) { |
545 |
if (store != null && !store.isEditing()) { |
546 |
try {
|
547 |
store.edit(); |
548 |
} catch (DataException e1) {
|
549 |
LOGGER.warn("Can't edit the store " + store.getName());
|
550 |
throw new RuntimeException("Can't save changes.", e1); |
551 |
} |
552 |
} |
553 |
this.saveChanges(this.formset); |
554 |
} |
555 |
} |
556 |
|
557 |
private void updateButtonEnabledStatus() { |
558 |
if( this.formset == null ) { |
559 |
return;
|
560 |
} |
561 |
if( this.store == null || store.isBroken() || store.isAppending() ) { |
562 |
this.formset.setReadOnly(true); |
563 |
formset.setActionVisible(STARTEDITING_ACTION, true);
|
564 |
formset.setActionEnabled(STARTEDITING_ACTION, true);
|
565 |
formset.setActionVisible(FINISHEDITING_ACTION, false);
|
566 |
formset.setActionEnabled(FINISHEDITING_ACTION, false);
|
567 |
formset.setActionEnabled(ACTION_DELETE, false);
|
568 |
formset.setActionEnabled(ACTION_NEW,false);
|
569 |
formset.setActionEnabled(ACTION_CANCEL_NEW,false);
|
570 |
formset.setActionEnabled(ACTION_SAVE,false);
|
571 |
formset.setActionEnabled(ACTION_SEARCH,false);
|
572 |
return;
|
573 |
} |
574 |
if( store.isEditing() ) {
|
575 |
this.formset.setReadOnly(false); |
576 |
formset.setActionVisible(STARTEDITING_ACTION, false);
|
577 |
formset.setActionEnabled(STARTEDITING_ACTION, false);
|
578 |
formset.setActionVisible(FINISHEDITING_ACTION, true);
|
579 |
formset.setActionEnabled(FINISHEDITING_ACTION, true);
|
580 |
if( formset.isInNewState() ) {
|
581 |
formset.setActionEnabled(ACTION_DELETE, false);
|
582 |
formset.setActionEnabled(ACTION_SEARCH,false);
|
583 |
formset.setActionEnabled(ACTION_NEW,false);
|
584 |
formset.setActionEnabled(ACTION_CANCEL_NEW,true);
|
585 |
formset.setActionEnabled(ACTION_SAVE,true);
|
586 |
|
587 |
} else if( this.features.isEmpty() ) { |
588 |
formset.setActionEnabled(ACTION_DELETE, false);
|
589 |
formset.setActionEnabled(ACTION_SEARCH,false);
|
590 |
formset.setActionEnabled(ACTION_NEW,true);
|
591 |
formset.setActionEnabled(ACTION_CANCEL_NEW,true);
|
592 |
formset.setActionEnabled(ACTION_SAVE,false);
|
593 |
|
594 |
} else if( formset.getForm().isModified() ) { |
595 |
formset.setActionEnabled(ACTION_DELETE, true);
|
596 |
formset.setActionEnabled(ACTION_SEARCH,true);
|
597 |
formset.setActionEnabled(ACTION_NEW,true);
|
598 |
formset.setActionEnabled(ACTION_CANCEL_NEW,false);
|
599 |
formset.setActionEnabled(ACTION_SAVE,true);
|
600 |
|
601 |
} else {
|
602 |
formset.setActionEnabled(ACTION_DELETE, true);
|
603 |
formset.setActionEnabled(ACTION_SEARCH,true);
|
604 |
formset.setActionEnabled(ACTION_NEW,true);
|
605 |
formset.setActionEnabled(ACTION_CANCEL_NEW,false);
|
606 |
formset.setActionEnabled(ACTION_SAVE,true); //false); |
607 |
} |
608 |
|
609 |
return;
|
610 |
} |
611 |
this.formset.setReadOnly(true); |
612 |
formset.setActionVisible(STARTEDITING_ACTION, true);
|
613 |
formset.setActionEnabled(STARTEDITING_ACTION, true);
|
614 |
formset.setActionVisible(FINISHEDITING_ACTION, false);
|
615 |
formset.setActionEnabled(FINISHEDITING_ACTION, false);
|
616 |
formset.setActionEnabled(ACTION_DELETE, false);
|
617 |
formset.setActionEnabled(ACTION_NEW,false);
|
618 |
formset.setActionEnabled(ACTION_CANCEL_NEW,false);
|
619 |
formset.setActionEnabled(ACTION_SAVE,false);
|
620 |
formset.setActionEnabled(ACTION_SEARCH,true);
|
621 |
} |
622 |
|
623 |
@Override
|
624 |
public long getDataSetSize() { |
625 |
if (this.features != null) { |
626 |
return features.getTotalSize();
|
627 |
} |
628 |
return 0; |
629 |
} |
630 |
|
631 |
@Override
|
632 |
public FeatureStore getFeatureStore() {
|
633 |
return this.store; |
634 |
} |
635 |
|
636 |
private class FormSetListener implements JDynFormSetListener { |
637 |
|
638 |
@Override
|
639 |
public void formMessage(String message) { |
640 |
} |
641 |
|
642 |
@Override
|
643 |
public void formClose() { |
644 |
panel.setVisible(false);
|
645 |
} |
646 |
|
647 |
@Override
|
648 |
public void formMovedTo(int currentPosition) throws AbortActionException { |
649 |
LOGGER.trace("formMovedTo " + currentPosition);
|
650 |
updateButtonEnabledStatus(); |
651 |
} |
652 |
|
653 |
@Override
|
654 |
public void formBeforeSave(JDynFormSet dynformSet) throws AbortActionException { |
655 |
LOGGER.trace("formBeforeSave");
|
656 |
updateButtonEnabledStatus(); |
657 |
} |
658 |
|
659 |
@Override
|
660 |
public void formBeforeNew(JDynFormSet dynformSet) throws AbortActionException { |
661 |
LOGGER.trace("formBeforeNew");
|
662 |
updateButtonEnabledStatus(); |
663 |
} |
664 |
|
665 |
@Override
|
666 |
public void formBeforeDelete(JDynFormSet dynformSet) throws AbortActionException { |
667 |
LOGGER.trace("formBeforeDelete");
|
668 |
updateButtonEnabledStatus(); |
669 |
} |
670 |
|
671 |
@Override
|
672 |
public void formAfterSave(JDynFormSet dynformSet) throws AbortActionException { |
673 |
LOGGER.trace("formAfterSave");
|
674 |
saveChanges(dynformSet); |
675 |
updateButtonEnabledStatus(); |
676 |
} |
677 |
|
678 |
@Override
|
679 |
public void formAfterNew(JDynFormSet dynformSet) throws AbortActionException { |
680 |
LOGGER.trace("formAfterNew");
|
681 |
updateButtonEnabledStatus(); |
682 |
} |
683 |
|
684 |
@Override
|
685 |
public void formAfterDelete(JDynFormSet dynformSet) throws AbortActionException { |
686 |
LOGGER.trace("formAfterDelete");
|
687 |
updateButtonEnabledStatus(); |
688 |
} |
689 |
|
690 |
// @Override
|
691 |
// public void formBeforeSearch(JDynFormSet dynformSet) throws AbortActionException {
|
692 |
// LOGGER.trace("formBeforeSearch");
|
693 |
// DataSwingManager dataSwingmanager = DALSwingLocator.getSwingManager();
|
694 |
// WindowManager_v2 winmgr = (WindowManager_v2) ToolsSwingLocator.getWindowManager();
|
695 |
//
|
696 |
// JExpressionBuilder builder = dataSwingmanager.createQueryFilterExpresion(store);
|
697 |
// FeatureQuery currentQuery = getCurrentQuery();
|
698 |
// if( currentQuery!=null ) {
|
699 |
// Evaluator filter = currentQuery.getFilter();
|
700 |
// if( filter instanceof ExpressionEvaluator ) {
|
701 |
// Expression expression = ((ExpressionEvaluator)filter).getExpression();
|
702 |
// builder.setExpression(expression);
|
703 |
// }
|
704 |
// }
|
705 |
// Dialog dialog = winmgr.createDialog(
|
706 |
// builder.asJComponent(),
|
707 |
// "Filtro",
|
708 |
// "Creacion de filtro sobre '"+store.getName()+"'",
|
709 |
// WindowManager_v2.BUTTONS_OK_CANCEL
|
710 |
// );
|
711 |
// dialog.show(WindowManager.MODE.DIALOG);
|
712 |
// if( dialog.getAction() == WindowManager_v2.BUTTON_OK ) {
|
713 |
// Expression expresion = builder.getExpression();
|
714 |
// try {
|
715 |
// FeatureQuery query = store.createFeatureQuery();
|
716 |
// if( ExpressionUtils.isPhraseEmpty(expresion) ) {
|
717 |
// query.clearFilter();
|
718 |
// } else {
|
719 |
// query.setFilter(expresion);
|
720 |
// }
|
721 |
// setQuery(query);
|
722 |
// } catch (Exception ex) {
|
723 |
// LOGGER.warn("Can't apply filter '" + expresion + "'.", ex);
|
724 |
// }
|
725 |
// }
|
726 |
// }
|
727 |
|
728 |
@Override
|
729 |
public void formBeforeSearch(JDynFormSet dynformSet) throws AbortActionException { |
730 |
LOGGER.trace("formBeforeSearch");
|
731 |
DataSwingManager dataSwingmanager = DALSwingLocator.getSwingManager(); |
732 |
WindowManager_v2 winmgr = (WindowManager_v2) ToolsSwingLocator.getWindowManager(); |
733 |
|
734 |
final FeatureStoreSearchPanel searchPanel = dataSwingmanager.createFeatureStoreSearchPanel(store);
|
735 |
searchPanel.setShowActions(false);
|
736 |
FeatureQuery currentQuery = getCurrentQuery(); |
737 |
if( currentQuery!=null ) { |
738 |
Evaluator filter = currentQuery.getFilter(); |
739 |
if( filter instanceof ExpressionEvaluator ) { |
740 |
Expression expression = ((ExpressionEvaluator)filter).getExpression();
|
741 |
searchPanel.setFilter(expression); |
742 |
} |
743 |
} |
744 |
searchPanel.asJComponent().setPreferredSize( |
745 |
new Dimension(FeatureStoreSearchPanel.DEFAULT_WIDTH, 320) |
746 |
); |
747 |
Dialog dialog = winmgr.createDialog(
|
748 |
searchPanel.asJComponent(), |
749 |
"Filtro",
|
750 |
"Creacion de filtro sobre '"+store.getName()+"'", |
751 |
WindowManager_v2.BUTTONS_OK_CANCEL |
752 |
); |
753 |
dialog.show(WindowManager.MODE.DIALOG); |
754 |
if( dialog.getAction() == WindowManager_v2.BUTTON_OK ) {
|
755 |
Expression expresion = searchPanel.getFilter();
|
756 |
try {
|
757 |
FeatureQuery query = store.createFeatureQuery(); |
758 |
if( ExpressionUtils.isPhraseEmpty(expresion) ) {
|
759 |
query.clearFilter(); |
760 |
} else {
|
761 |
query.setFilter(expresion); |
762 |
} |
763 |
setQuery(query); |
764 |
} catch (Exception ex) { |
765 |
LOGGER.warn("Can't apply filter '" + expresion + "'.", ex); |
766 |
} |
767 |
} |
768 |
} |
769 |
|
770 |
@Override
|
771 |
public void formAfterSearch(JDynFormSet dynformSet) throws AbortActionException { |
772 |
LOGGER.trace("formAfterSearch");
|
773 |
} |
774 |
|
775 |
@Override
|
776 |
public void formBeforeCancelNew(JDynFormSet dynformSet) throws AbortActionException { |
777 |
LOGGER.trace("formBeforeCancelNew");
|
778 |
updateButtonEnabledStatus(); |
779 |
} |
780 |
|
781 |
@Override
|
782 |
public void formAfterCancelNew(JDynFormSet dynformSet) throws AbortActionException { |
783 |
LOGGER.trace("formAfterCancelNew");
|
784 |
updateButtonEnabledStatus(); |
785 |
} |
786 |
} |
787 |
|
788 |
// private static class StoreEditException extends AbortActionException {
|
789 |
//
|
790 |
// /**
|
791 |
// *
|
792 |
// */
|
793 |
// private static final long serialVersionUID = -7682017811778577130L;
|
794 |
//
|
795 |
// public StoreEditException(Throwable cause, String storename) {
|
796 |
// super("Can't edit the store '%(storename)'", cause, "cant_edit_the store_XstorenameX", serialVersionUID);
|
797 |
// setValue("storename", storename);
|
798 |
// }
|
799 |
// }
|
800 |
} |