a9175681804242fd4de0d0f0ebc5b5ffc80ca804
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / selection / EditFromSelectionWizard.java
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.ui.selection;
12
13 import org.eclipse.core.runtime.IStatus;
14 import org.eclipse.jface.wizard.Wizard;
15 import org.eclipse.jface.wizard.WizardDialog;
16 import org.eclipse.swt.events.SelectionEvent;
17 import org.eclipse.swt.events.SelectionListener;
18 import org.eclipse.ui.IEditorPart;
19
20 import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade;
21 import eu.etaxonomy.cdm.api.facade.DerivedUnitFacadeNotSupportedException;
22 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
23 import eu.etaxonomy.cdm.model.agent.Institution;
24 import eu.etaxonomy.cdm.model.agent.Person;
25 import eu.etaxonomy.cdm.model.agent.Team;
26 import eu.etaxonomy.cdm.model.common.ICdmBase;
27 import eu.etaxonomy.cdm.model.common.User;
28 import eu.etaxonomy.cdm.model.molecular.Amplification;
29 import eu.etaxonomy.cdm.model.molecular.Primer;
30 import eu.etaxonomy.cdm.model.name.NonViralName;
31 import eu.etaxonomy.cdm.model.occurrence.Collection;
32 import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
33 import eu.etaxonomy.cdm.model.reference.Reference;
34 import eu.etaxonomy.taxeditor.model.AbstractUtility;
35 import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
36 import eu.etaxonomy.taxeditor.model.MessagingUtils;
37 import eu.etaxonomy.taxeditor.model.TextHelper;
38 import eu.etaxonomy.taxeditor.newWizard.AmplificationGeneralWizardPage;
39 import eu.etaxonomy.taxeditor.newWizard.PrimerWizardPage;
40 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
41 import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
42 import eu.etaxonomy.taxeditor.ui.section.agent.InstitutionWizardPage;
43 import eu.etaxonomy.taxeditor.ui.section.agent.PersonWizardPage;
44 import eu.etaxonomy.taxeditor.ui.section.agent.TeamWizardPage;
45 import eu.etaxonomy.taxeditor.ui.section.name.NonViralNameWizardPage;
46 import eu.etaxonomy.taxeditor.ui.section.occurrence.CollectionWizardPage;
47 import eu.etaxonomy.taxeditor.ui.section.occurrence.DerivedUnitBaseWizardPage;
48 import eu.etaxonomy.taxeditor.ui.section.occurrence.DerivedUnitGeneralWizardPage;
49 import eu.etaxonomy.taxeditor.ui.section.occurrence.FieldUnitWizardPage;
50 import eu.etaxonomy.taxeditor.ui.section.occurrence.GatheringEventWizardPage;
51 import eu.etaxonomy.taxeditor.ui.section.reference.ReferenceWizardPage;
52 import eu.etaxonomy.taxeditor.ui.section.user.UserDetailWizardPage;
53
54 /**
55 * <p>
56 * EditFromSelectionWizard class.
57 * </p>
58 *
59 * @author n.hoffmann
60 * @created Jun 1, 2010
61 * @version 1.0
62 */
63 public class EditFromSelectionWizard extends Wizard implements
64 SelectionListener {
65
66 private final EntitySelectionElement selectionElement;
67
68 /**
69 * <p>
70 * Constructor for EditFromSelectionWizard.
71 * </p>
72 *
73 * @param selectionElement
74 * a
75 * {@link eu.etaxonomy.taxeditor.ui.selection.EntitySelectionElement}
76 * object.
77 */
78 public EditFromSelectionWizard(EntitySelectionElement selectionElement) {
79 this.selectionElement = selectionElement;
80 this.setWindowTitle(String.format("Edit %s", TextHelper.deproxyClassName(selectionElement.getEntity().getClass())));
81 }
82
83 /** {@inheritDoc} */
84 @Override
85 public void addPages() {
86
87 CdmFormFactory formFactory = selectionElement.getFormFactory();
88 ICdmBase entity = (ICdmBase) HibernateProxyHelper
89 .deproxy(selectionElement.getEntity());
90
91 if (entity instanceof Reference) {
92 addPage(new ReferenceWizardPage(formFactory,
93 selectionElement.getConversationHolder(),
94 (Reference) entity));
95 } else if (entity instanceof Team) {
96 addPage(new TeamWizardPage(formFactory,
97 selectionElement.getConversationHolder(), (Team) entity));
98 } else if (entity instanceof Person) {
99 addPage(new PersonWizardPage(formFactory,
100 selectionElement.getConversationHolder(), (Person) entity));
101 } else if (entity instanceof NonViralName) {
102 addPage(new NonViralNameWizardPage(formFactory,
103 selectionElement.getConversationHolder(),
104 (NonViralName) entity));
105 } else if (entity instanceof DerivedUnit) {
106
107 DerivedUnitFacade facade;
108 try {
109 facade = DerivedUnitFacade.NewInstance(
110 (DerivedUnit) entity,
111 PreferencesUtil.getDerivedUnitConfigurator());
112 } catch (DerivedUnitFacadeNotSupportedException e) {
113 // we should never get here
114 throw new IllegalStateException();
115 }
116
117 addPage(new DerivedUnitGeneralWizardPage(formFactory,
118 selectionElement.getConversationHolder(), facade));
119 addPage(new GatheringEventWizardPage(formFactory,
120 selectionElement.getConversationHolder(), facade));
121 addPage(new FieldUnitWizardPage(formFactory,
122 selectionElement.getConversationHolder(), facade));
123 addPage(new DerivedUnitBaseWizardPage(formFactory,
124 selectionElement.getConversationHolder(), facade));
125 } else if (entity instanceof Collection) {
126 addPage(new CollectionWizardPage(formFactory,
127 selectionElement.getConversationHolder(),
128 (Collection) entity));
129 } else if (entity instanceof Institution) {
130 addPage(new InstitutionWizardPage(formFactory,
131 selectionElement.getConversationHolder(),
132 (Institution) entity));
133 } else if (entity instanceof User) {
134 addPage(new UserDetailWizardPage(formFactory,
135 selectionElement.getConversationHolder(),
136 (User) entity));
137 } else if (entity instanceof Primer) {
138 addPage(new PrimerWizardPage(formFactory,
139 selectionElement.getConversationHolder(),
140 (Primer) entity));
141 } else if (entity instanceof Amplification) {
142 addPage(new AmplificationGeneralWizardPage(formFactory,
143 selectionElement.getConversationHolder(),
144 (Amplification) entity));
145 } else {
146 MessagingUtils.warningDialog("Missing interface", this,
147 "No detail element for current selection");
148 }
149
150 }
151
152 /*
153 * (non-Javadoc)
154 *
155 * @see org.eclipse.jface.wizard.Wizard#performFinish()
156 */
157 /** {@inheritDoc} */
158 @Override
159 public boolean performFinish() {
160 IEditorPart activeEditor = AbstractUtility.getActiveEditor();
161 /*
162 * forceDirty() for cases when one or more EditFromSelectionWizards are
163 * opened cascadingly to trigger the save button. Otherwise the dirty
164 * state would not be set and the user will have no feedback that he/she
165 * actually still has ounsaved changes. This is necessary because
166 * editing in these wizards is done on clones of the CDM entities which
167 * are merged back with the original when pressing "Finish"
168 */
169 if (activeEditor instanceof IDirtyMarkable){
170 ((IDirtyMarkable) activeEditor).forceDirty();
171 }
172 return true;
173 }
174
175 /** {@inheritDoc} */
176 @Override
177 public void widgetSelected(SelectionEvent e) {
178 WizardDialog dialog = new WizardDialog(selectionElement.getShell(),
179 this);
180 if (dialog.open() == IStatus.OK) {
181 selectionElement.updateFromWizard();
182 }
183 }
184
185 /** {@inheritDoc} */
186 @Override
187 public void widgetDefaultSelected(SelectionEvent e) {
188 }
189 }