Project

General

Profile

Download (8.13 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
 * Copyright (C) 2007 EDIT
4
 * European Distributed Institute of Taxonomy
5
 * http://www.e-taxonomy.eu
6
 *
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.
9
 */
10

    
11
package eu.etaxonomy.taxeditor.newWizard;
12

    
13
import java.util.HashMap;
14
import java.util.Map;
15
import java.util.UUID;
16

    
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;
29

    
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;
51

    
52
/**
53
 * @author n.hoffmann
54
 * @created Jun 16, 2010
55
 * @version 1.0
56
 */
57
public class NewDerivedUnitBaseWizard extends AbstractNewEntityWizard<SpecimenOrObservationBase> implements IDerivedUnitFacadePart{
58
    private SpecimenOrObservationType specOrObsType = null;
59

    
60
    public NewDerivedUnitBaseWizard() {
61
        super();
62
        CdmStore.getCurrentSessionManager().bindNullSession();
63
    }
64

    
65
    public NewDerivedUnitBaseWizard(SpecimenOrObservationType specOrObsType) {
66
        super();
67
        this.specOrObsType = specOrObsType;
68
    }
69
    /** {@inheritDoc} */
70
    @Override
71
    public void addPages() {
72
        DerivedUnitFacade facade = null;
73
        try {
74
            if(getEntity() instanceof DerivedUnit) {
75
                facade = DerivedUnitFacade.NewInstance((DerivedUnit)getEntity(), PreferencesUtil.getDerivedUnitConfigurator());
76
                addPage(new DerivedUnitGeneralWizardPage(formFactory, getConversationHolder(), facade));
77
                addPage(new GatheringEventWizardPage(formFactory, getConversationHolder(), facade));
78
                addPage(new FieldUnitWizardPage(formFactory, getConversationHolder(), facade));
79
                addPage(new DerivedUnitBaseWizardPage(formFactory, getConversationHolder(), facade));
80
            }
81
            else if(getEntity() instanceof FieldUnit){
82
                facade = DerivedUnitFacade.NewInstance(SpecimenOrObservationType.FieldUnit, (FieldUnit)getEntity(), PreferencesUtil.getDerivedUnitConfigurator());
83
                addPage(new FieldUnitGeneralWizardPage(formFactory, getConversationHolder(), facade));
84
                addPage(new GatheringEventWizardPage(formFactory, getConversationHolder(), facade));
85
                addPage(new FieldUnitWizardPage(formFactory, getConversationHolder(), facade));
86
            }
87
        } catch (DerivedUnitFacadeNotSupportedException e) {
88
            // we should never get here
89
            throw new IllegalStateException();
90
        }
91
    }
92

    
93
    /** {@inheritDoc} */
94
    @Override
95
    protected SpecimenOrObservationBase createNewEntity() {
96
        if (specOrObsType == null) {
97
            return DerivedUnit.NewInstance(SpecimenOrObservationType.PreservedSpecimen);
98
        } else {
99
            if (SpecimenOrObservationType.Media.equals(specOrObsType)
100
                    || ((specOrObsType.getKindOf() != null) && specOrObsType.getKindOf().equals(SpecimenOrObservationType.Media))) {
101
                MediaSpecimen mediaSpecimen = MediaSpecimen.NewInstance(specOrObsType);
102
                //a MediaSpecimen with no media attached does not make sense. Hence, we add one
103
                mediaSpecimen.setMediaSpecimen(Media.NewInstance());
104
                return mediaSpecimen;
105
            } else if (specOrObsType.equals(SpecimenOrObservationType.FieldUnit)) {
106
                return FieldUnit.NewInstance();
107
            } else {
108
                return DerivedUnit.NewInstance(specOrObsType);
109
            }
110
        }
111
    }
112

    
113

    
114
	/** {@inheritDoc} */
115
	@Override
116
	protected void saveEntity() {
117
	    if(CdmStore.getCurrentSessionManager().isRemoting()) {
118
	        CdmStore.getService(IOccurrenceService.class).merge(getEntity(), true);
119
	    } else {
120
	        CdmStore.getService(IOccurrenceService.class).save(getEntity());
121
	    }
122
	}
123

    
124

    
125
    @Override
126
    protected String getEntityName() {
127
        return "Specimen";
128
    }
129

    
130
    /**
131
     * {@inheritDoc}
132
     */
133
    @Override
134
    public boolean performFinish() {
135
        boolean performFinish = super.performFinish();
136
        int returnCode = IDialogConstants.NO_ID;
137
        if (!PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.PROMPT_FOR_OPEN_SPECIMEN_IN_EDITOR)){
138
            MessageDialogWithToggle messageDialog = MessageDialogWithToggle.openYesNoQuestion(getShell(),
139
                    "Choose opening option", "Do you want to open the specimen in the specimen editor?",
140
                    "Remember my decision?", false, PreferencesUtil.getPreferenceStore(),
141
                    IPreferenceKeys.ALWAYS_OPEN_SPECIMEN_IN_EDITOR);
142
            returnCode = messageDialog.getReturnCode();
143
            PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.PROMPT_FOR_OPEN_SPECIMEN_IN_EDITOR, messageDialog.getToggleState());
144
            if (returnCode != IDialogConstants.YES_ID) {
145
                return performFinish;
146
            }
147
        }
148
        if (returnCode==IDialogConstants.YES_ID ||
149
                PreferencesUtil.getPreferenceStore().getString(IPreferenceKeys.ALWAYS_OPEN_SPECIMEN_IN_EDITOR)
150
                .equals(MessageDialogWithToggle.ALWAYS)) {
151
            IHandlerService handlerService = (IHandlerService) AbstractUtility.getService(IHandlerService.class);
152

    
153
            // get the command from plugin.xml
154
            IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
155
            ICommandService cmdService = (ICommandService) window.getService(ICommandService.class);
156
            Command command = cmdService.getCommand("eu.etaxonomy.taxeditor.editor.openSpecimenEditor");
157
            Map<String, UUID> parameters = new HashMap<String, UUID>();
158
            parameters.put("eu.etaxonomy.taxeditor.specimenUuidParameter", getEntity().getUuid());
159
            ParameterizedCommand parameterizedCommand = ParameterizedCommand.generateCommand(command, parameters);
160

    
161

    
162
            // execute the command
163
            try {
164
                handlerService.executeCommand(parameterizedCommand, null);
165
            } catch (ExecutionException e) {
166
                MessagingUtils.error(DerivateSearchCompositeController.class, e);
167
            } catch (NotDefinedException e) {
168
                MessagingUtils.error(DerivateSearchCompositeController.class, e);
169
            } catch (NotEnabledException e) {
170
                MessagingUtils.error(DerivateSearchCompositeController.class, e);
171
            } catch (NotHandledException e) {
172
                MessagingUtils.error(DerivateSearchCompositeController.class, e);
173
            }
174
        }
175

    
176
        return performFinish;
177
    }
178

    
179

    
180
}
(8-8/22)