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