3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
11 package eu
.etaxonomy
.taxeditor
.newWizard
;
13 import java
.util
.HashMap
;
15 import java
.util
.UUID
;
17 import org
.eclipse
.core
.commands
.Command
;
18 import org
.eclipse
.core
.commands
.ExecutionException
;
19 import org
.eclipse
.core
.commands
.NotEnabledException
;
20 import org
.eclipse
.core
.commands
.NotHandledException
;
21 import org
.eclipse
.core
.commands
.ParameterizedCommand
;
22 import org
.eclipse
.core
.commands
.common
.NotDefinedException
;
23 import org
.eclipse
.jface
.dialogs
.IDialogConstants
;
24 import org
.eclipse
.jface
.dialogs
.MessageDialogWithToggle
;
25 import org
.eclipse
.ui
.IWorkbenchWindow
;
26 import org
.eclipse
.ui
.PlatformUI
;
27 import org
.eclipse
.ui
.commands
.ICommandService
;
28 import org
.eclipse
.ui
.handlers
.IHandlerService
;
30 import eu
.etaxonomy
.cdm
.api
.facade
.DerivedUnitFacade
;
31 import eu
.etaxonomy
.cdm
.api
.facade
.DerivedUnitFacadeNotSupportedException
;
32 import eu
.etaxonomy
.cdm
.api
.service
.IOccurrenceService
;
33 import eu
.etaxonomy
.cdm
.model
.media
.Media
;
34 import eu
.etaxonomy
.cdm
.model
.occurrence
.DerivedUnit
;
35 import eu
.etaxonomy
.cdm
.model
.occurrence
.FieldUnit
;
36 import eu
.etaxonomy
.cdm
.model
.occurrence
.MediaSpecimen
;
37 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationBase
;
38 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationType
;
39 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
40 import eu
.etaxonomy
.taxeditor
.model
.IDerivedUnitFacadePart
;
41 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
42 import eu
.etaxonomy
.taxeditor
.preference
.IPreferenceKeys
;
43 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
44 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
45 import eu
.etaxonomy
.taxeditor
.ui
.section
.occurrence
.DerivedUnitBaseWizardPage
;
46 import eu
.etaxonomy
.taxeditor
.ui
.section
.occurrence
.DerivedUnitGeneralWizardPage
;
47 import eu
.etaxonomy
.taxeditor
.ui
.section
.occurrence
.FieldUnitGeneralWizardPage
;
48 import eu
.etaxonomy
.taxeditor
.ui
.section
.occurrence
.FieldUnitWizardPage
;
49 import eu
.etaxonomy
.taxeditor
.ui
.section
.occurrence
.GatheringEventWizardPage
;
50 import eu
.etaxonomy
.taxeditor
.view
.derivateSearch
.DerivateSearchCompositeController
;
54 * @created Jun 16, 2010
57 public class NewDerivedUnitBaseWizard
extends AbstractNewEntityWizard
<SpecimenOrObservationBase
> implements IDerivedUnitFacadePart
{
58 private SpecimenOrObservationType specOrObsType
= null;
60 public NewDerivedUnitBaseWizard() {
64 public NewDerivedUnitBaseWizard(SpecimenOrObservationType specOrObsType
) {
66 this.specOrObsType
= specOrObsType
;
70 public void addPages() {
71 DerivedUnitFacade facade
= null;
73 if(getEntity() instanceof DerivedUnit
) {
74 facade
= DerivedUnitFacade
.NewInstance((DerivedUnit
)getEntity(), PreferencesUtil
.getDerivedUnitConfigurator());
75 addPage(new DerivedUnitGeneralWizardPage(formFactory
, getConversationHolder(), facade
));
76 addPage(new GatheringEventWizardPage(formFactory
, getConversationHolder(), facade
));
77 addPage(new FieldUnitWizardPage(formFactory
, getConversationHolder(), facade
));
78 addPage(new DerivedUnitBaseWizardPage(formFactory
, getConversationHolder(), facade
));
80 else if(getEntity() instanceof FieldUnit
){
81 facade
= DerivedUnitFacade
.NewInstance(SpecimenOrObservationType
.FieldUnit
, (FieldUnit
)getEntity(), PreferencesUtil
.getDerivedUnitConfigurator());
82 addPage(new FieldUnitGeneralWizardPage(formFactory
, getConversationHolder(), facade
));
83 addPage(new GatheringEventWizardPage(formFactory
, getConversationHolder(), facade
));
84 addPage(new FieldUnitWizardPage(formFactory
, getConversationHolder(), facade
));
86 } catch (DerivedUnitFacadeNotSupportedException e
) {
87 // we should never get here
88 throw new IllegalStateException();
94 protected SpecimenOrObservationBase
createNewEntity() {
95 if (specOrObsType
== null) {
96 return DerivedUnit
.NewInstance(SpecimenOrObservationType
.PreservedSpecimen
);
98 if (SpecimenOrObservationType
.Media
.equals(specOrObsType
)
99 || ((specOrObsType
.getKindOf() != null) && specOrObsType
.getKindOf().equals(SpecimenOrObservationType
.Media
))) {
100 MediaSpecimen mediaSpecimen
= MediaSpecimen
.NewInstance(specOrObsType
);
101 //a MediaSpecimen with no media attached does not make sense. Hence, we add one
102 mediaSpecimen
.setMediaSpecimen(Media
.NewInstance());
103 return mediaSpecimen
;
104 } else if (specOrObsType
.equals(SpecimenOrObservationType
.FieldUnit
)) {
105 return FieldUnit
.NewInstance();
107 return DerivedUnit
.NewInstance(specOrObsType
);
114 protected void saveEntity() {
115 CdmStore
.getService(IOccurrenceService
.class).saveOrUpdate(getEntity());
119 protected String
getEntityName() {
127 public boolean performFinish() {
128 boolean performFinish
= super.performFinish();
129 int returnCode
= IDialogConstants
.NO_ID
;
130 if (!PreferencesUtil
.getPreferenceStore().getBoolean(IPreferenceKeys
.PROMPT_FOR_OPEN_SPECIMEN_IN_EDITOR
)){
131 MessageDialogWithToggle messageDialog
= MessageDialogWithToggle
.openYesNoQuestion(getShell(),
132 "Choose opening option", "Do you want to open the specimen in the specimen editor?",
133 "Remember my decision?", false, PreferencesUtil
.getPreferenceStore(),
134 IPreferenceKeys
.ALWAYS_OPEN_SPECIMEN_IN_EDITOR
);
135 returnCode
= messageDialog
.getReturnCode();
136 PreferencesUtil
.getPreferenceStore().setValue(IPreferenceKeys
.PROMPT_FOR_OPEN_SPECIMEN_IN_EDITOR
, messageDialog
.getToggleState());
137 if (returnCode
!= IDialogConstants
.YES_ID
) {
138 return performFinish
;
141 if (returnCode
==IDialogConstants
.YES_ID
||
142 PreferencesUtil
.getPreferenceStore().getString(IPreferenceKeys
.ALWAYS_OPEN_SPECIMEN_IN_EDITOR
)
143 .equals(MessageDialogWithToggle
.ALWAYS
)) {
144 IHandlerService handlerService
= (IHandlerService
) AbstractUtility
.getService(IHandlerService
.class);
146 // get the command from plugin.xml
147 IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
148 ICommandService cmdService
= (ICommandService
) window
.getService(ICommandService
.class);
149 Command command
= cmdService
.getCommand("eu.etaxonomy.taxeditor.editor.openSpecimenEditor");
150 Map
<String
, UUID
> parameters
= new HashMap
<String
, UUID
>();
151 parameters
.put("eu.etaxonomy.taxeditor.specimenUuidParameter", getEntity().getUuid());
152 ParameterizedCommand parameterizedCommand
= ParameterizedCommand
.generateCommand(command
, parameters
);
154 // execute the command
156 handlerService
.executeCommand(parameterizedCommand
, null);
157 } catch (ExecutionException e
) {
158 MessagingUtils
.error(DerivateSearchCompositeController
.class, e
);
159 } catch (NotDefinedException e
) {
160 MessagingUtils
.error(DerivateSearchCompositeController
.class, e
);
161 } catch (NotEnabledException e
) {
162 MessagingUtils
.error(DerivateSearchCompositeController
.class, e
);
163 } catch (NotHandledException e
) {
164 MessagingUtils
.error(DerivateSearchCompositeController
.class, e
);
167 return performFinish
;