76 |
76 |
import org.gvsig.tools.dynform.DynFormManager;
|
77 |
77 |
import org.gvsig.tools.dynform.JDynForm;
|
78 |
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;
|
79 |
84 |
import org.gvsig.tools.dynform.JDynFormSet.JDynFormSetListener;
|
80 |
85 |
import org.gvsig.tools.dynobject.DynClass;
|
81 |
86 |
import org.gvsig.tools.dynobject.DynObject;
|
... | ... | |
102 |
107 |
@SuppressWarnings("UseSpecificCatch")
|
103 |
108 |
public class DefaultJFeaturesForm implements JFeaturesForm {
|
104 |
109 |
|
|
110 |
private final String STARTEDITING_ACTION = "startEditing";
|
|
111 |
private final String FINISHEDITING_ACTION = "finishEditing";
|
|
112 |
|
|
113 |
|
105 |
114 |
public class DefaultFeaturesFormContext implements FeaturesFormContext {
|
106 |
115 |
|
107 |
116 |
private DefaultFeaturesFormContext() {
|
... | ... | |
196 |
205 |
private JPanel panel;
|
197 |
206 |
private JDynFormSet formset;
|
198 |
207 |
private FeatureStore store;
|
199 |
|
private FeaturePagingHelper ph;
|
|
208 |
private FeaturePagingHelper features;
|
200 |
209 |
private DynFormDefinition definition = null;
|
201 |
210 |
private FeatureQuery currentQuery;
|
202 |
211 |
private List<Action> otherActions;
|
... | ... | |
225 |
234 |
if( this.currentQuery!=null ) {
|
226 |
235 |
this.currentQuery.retrievesAllAttributes();
|
227 |
236 |
}
|
228 |
|
this.ph = DALLocator.getDataManager().createFeaturePagingHelper(store, this.currentQuery, PAGE_SIZE);
|
229 |
|
this.formset.setValues(ph.asListOfDynObjects());
|
|
237 |
this.features = DALLocator.getDataManager().createFeaturePagingHelper(store, this.currentQuery, PAGE_SIZE);
|
|
238 |
this.formset.setValues(features.asListOfDynObjects());
|
230 |
239 |
} catch (Exception ex) {
|
231 |
240 |
throw new RuntimeException("Can't update form", ex);
|
232 |
241 |
}
|
|
242 |
updateButtonEnabledStatus();
|
233 |
243 |
}
|
234 |
244 |
|
235 |
245 |
@Override
|
236 |
246 |
public JComponent asJComponent() {
|
237 |
|
if (this.ph == null) {
|
|
247 |
if (this.features == null) {
|
238 |
248 |
try {
|
239 |
249 |
updateForm();
|
240 |
250 |
} catch (Exception ex) {
|
... | ... | |
270 |
280 |
}
|
271 |
281 |
this.store = store;
|
272 |
282 |
DisposeUtils.bind(store);
|
273 |
|
this.ph = null;
|
|
283 |
this.features = null;
|
274 |
284 |
}
|
275 |
285 |
|
276 |
286 |
private void doDispose() {
|
... | ... | |
278 |
288 |
this.store = null;
|
279 |
289 |
this.panel = null;
|
280 |
290 |
this.formset = null;
|
281 |
|
this.ph = null;
|
|
291 |
this.features = null;
|
282 |
292 |
this.definition = null;
|
283 |
293 |
this.currentQuery = null;
|
284 |
294 |
this.otherActions = null;
|
... | ... | |
324 |
334 |
|
325 |
335 |
this.formset.addListener(new FormSetListener());
|
326 |
336 |
}
|
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 |
|
}
|
|
337 |
updateButtonEnabledStatus();
|
340 |
338 |
return this.formset;
|
341 |
339 |
}
|
342 |
340 |
|
... | ... | |
358 |
356 |
|
359 |
357 |
@Override
|
360 |
358 |
public Feature get(long index) {
|
361 |
|
if( this.formset==null || this.ph==null ) {
|
|
359 |
if( this.formset==null || this.features==null ) {
|
362 |
360 |
return null;
|
363 |
361 |
}
|
364 |
362 |
try {
|
365 |
|
return this.ph.getFeatureAt(index);
|
|
363 |
return this.features.getFeatureAt(index);
|
366 |
364 |
} catch (BaseException ex) {
|
367 |
365 |
return null;
|
368 |
366 |
}
|
... | ... | |
378 |
376 |
this.putValue(NAME,null);
|
379 |
377 |
this.putValue(SHORT_DESCRIPTION,i18nManager.getTranslation("_Stop_editing"));
|
380 |
378 |
this.putValue(SMALL_ICON, iconTheme.get("table-stop-editing"));
|
381 |
|
this.putValue(ACTION_COMMAND_KEY, "finishEditing");
|
|
379 |
this.putValue(ACTION_COMMAND_KEY, FINISHEDITING_ACTION);
|
382 |
380 |
|
383 |
381 |
this.setEnabled(store.isEditing());
|
384 |
382 |
store.addObserver(this);
|
... | ... | |
410 |
408 |
LOGGER.warn("Can't finish editing in FeatureForm ("+store.getName()+").",ex);
|
411 |
409 |
}
|
412 |
410 |
}
|
|
411 |
updateButtonEnabledStatus();
|
413 |
412 |
}
|
414 |
413 |
|
415 |
414 |
@Override
|
... | ... | |
423 |
422 |
case FeatureStoreNotification.AFTER_STARTEDITING:
|
424 |
423 |
case FeatureStoreNotification.AFTER_FINISHEDITING:
|
425 |
424 |
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 |
|
}
|
|
425 |
updateButtonEnabledStatus();
|
435 |
426 |
break;
|
436 |
427 |
}
|
437 |
428 |
}
|
438 |
429 |
}
|
439 |
430 |
|
440 |
431 |
}
|
441 |
|
|
|
432 |
|
442 |
433 |
private class StartEditingAction extends AbstractAction implements Observer {
|
443 |
434 |
|
444 |
435 |
@SuppressWarnings({"OverridableMethodCallInConstructor", "LeakingThisInConstructor"})
|
... | ... | |
449 |
440 |
this.putValue(NAME,null);
|
450 |
441 |
this.putValue(SHORT_DESCRIPTION,i18nManager.getTranslation("_Start_editing"));
|
451 |
442 |
this.putValue(SMALL_ICON, iconTheme.get("table-start-editing"));
|
452 |
|
this.putValue(ACTION_COMMAND_KEY, "startEditing");
|
|
443 |
this.putValue(ACTION_COMMAND_KEY, STARTEDITING_ACTION);
|
453 |
444 |
|
454 |
445 |
this.setEnabled(!store.isEditing());
|
455 |
446 |
store.addObserver(this);
|
... | ... | |
464 |
455 |
LOGGER.warn("Can't finish editing in FeatureForm ("+store.getName()+").",ex);
|
465 |
456 |
}
|
466 |
457 |
}
|
|
458 |
updateButtonEnabledStatus();
|
467 |
459 |
}
|
468 |
460 |
|
469 |
461 |
@Override
|
... | ... | |
477 |
469 |
case FeatureStoreNotification.AFTER_STARTEDITING:
|
478 |
470 |
case FeatureStoreNotification.AFTER_FINISHEDITING:
|
479 |
471 |
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 |
|
}
|
|
472 |
updateButtonEnabledStatus();
|
489 |
473 |
break;
|
490 |
474 |
}
|
491 |
475 |
}
|
... | ... | |
495 |
479 |
|
496 |
480 |
@Override
|
497 |
481 |
public void setQuery(FeatureQuery query) {
|
498 |
|
if (this.ph != null) {
|
|
482 |
if (this.features != null) {
|
499 |
483 |
if (this.formset != null && !formset.isReadOnly() && this.formset.isAutosave() && this.formset.countValues() > 0) {
|
500 |
484 |
if (!store.isEditing()) {
|
501 |
485 |
try {
|
... | ... | |
523 |
507 |
winmgr.showWindow(this.asJComponent(), title, mode);
|
524 |
508 |
}
|
525 |
509 |
|
526 |
|
private void saveChanges(JDynFormSet dynformSet) {
|
|
510 |
private void saveChanges(JDynFormSet theFormSet) {
|
527 |
511 |
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");
|
|
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"));
|
537 |
531 |
}
|
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 |
|
}
|
|
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();
|
567 |
538 |
}
|
568 |
539 |
|
569 |
540 |
}
|
... | ... | |
583 |
554 |
}
|
584 |
555 |
}
|
585 |
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 |
|
586 |
623 |
@Override
|
587 |
624 |
public long getDataSetSize() {
|
588 |
|
if (this.ph != null) {
|
589 |
|
return ph.getTotalSize();
|
|
625 |
if (this.features != null) {
|
|
626 |
return features.getTotalSize();
|
590 |
627 |
}
|
591 |
628 |
return 0;
|
592 |
629 |
}
|
... | ... | |
610 |
647 |
@Override
|
611 |
648 |
public void formMovedTo(int currentPosition) throws AbortActionException {
|
612 |
649 |
LOGGER.trace("formMovedTo " + currentPosition);
|
|
650 |
updateButtonEnabledStatus();
|
613 |
651 |
}
|
614 |
652 |
|
615 |
653 |
@Override
|
616 |
654 |
public void formBeforeSave(JDynFormSet dynformSet) throws AbortActionException {
|
617 |
655 |
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 |
|
}
|
|
656 |
updateButtonEnabledStatus();
|
625 |
657 |
}
|
626 |
658 |
|
627 |
659 |
@Override
|
628 |
660 |
public void formBeforeNew(JDynFormSet dynformSet) throws AbortActionException {
|
629 |
661 |
LOGGER.trace("formBeforeNew");
|
|
662 |
updateButtonEnabledStatus();
|
630 |
663 |
}
|
631 |
664 |
|
632 |
665 |
@Override
|
633 |
666 |
public void formBeforeDelete(JDynFormSet dynformSet) throws AbortActionException {
|
634 |
667 |
LOGGER.trace("formBeforeDelete");
|
|
668 |
updateButtonEnabledStatus();
|
635 |
669 |
}
|
636 |
670 |
|
637 |
671 |
@Override
|
638 |
672 |
public void formAfterSave(JDynFormSet dynformSet) throws AbortActionException {
|
639 |
673 |
LOGGER.trace("formAfterSave");
|
640 |
674 |
saveChanges(dynformSet);
|
|
675 |
updateButtonEnabledStatus();
|
641 |
676 |
}
|
642 |
677 |
|
643 |
678 |
@Override
|
644 |
679 |
public void formAfterNew(JDynFormSet dynformSet) throws AbortActionException {
|
645 |
680 |
LOGGER.trace("formAfterNew");
|
|
681 |
updateButtonEnabledStatus();
|
646 |
682 |
}
|
647 |
683 |
|
648 |
684 |
@Override
|
649 |
685 |
public void formAfterDelete(JDynFormSet dynformSet) throws AbortActionException {
|
650 |
686 |
LOGGER.trace("formAfterDelete");
|
|
687 |
updateButtonEnabledStatus();
|
651 |
688 |
}
|
652 |
689 |
|
653 |
690 |
// @Override
|
... | ... | |
738 |
775 |
@Override
|
739 |
776 |
public void formBeforeCancelNew(JDynFormSet dynformSet) throws AbortActionException {
|
740 |
777 |
LOGGER.trace("formBeforeCancelNew");
|
|
778 |
updateButtonEnabledStatus();
|
741 |
779 |
}
|
742 |
780 |
|
743 |
781 |
@Override
|
744 |
782 |
public void formAfterCancelNew(JDynFormSet dynformSet) throws AbortActionException {
|
745 |
783 |
LOGGER.trace("formAfterCancelNew");
|
|
784 |
updateButtonEnabledStatus();
|
746 |
785 |
}
|
747 |
786 |
}
|
748 |
787 |
|
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 |
|
}
|
|
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 |
// }
|
761 |
800 |
}
|