ref #8767: check for publish when moving taxon
[taxeditor.git] / eu.etaxonomy.taxeditor.navigation / src / main / java / eu / etaxonomy / taxeditor / navigation / NavigationUtil.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9
10 package eu.etaxonomy.taxeditor.navigation;
11
12 import java.util.Collection;
13 import java.util.HashSet;
14 import java.util.Set;
15 import java.util.UUID;
16
17 import org.eclipse.core.commands.operations.IUndoContext;
18 import org.eclipse.core.commands.operations.UndoContext;
19 import org.eclipse.e4.ui.model.application.MApplication;
20 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
21 import org.eclipse.e4.ui.workbench.modeling.EModelService;
22 import org.eclipse.e4.ui.workbench.modeling.EPartService;
23 import org.eclipse.jface.wizard.WizardDialog;
24 import org.eclipse.swt.widgets.Shell;
25
26 import eu.etaxonomy.cdm.api.service.IClassificationService;
27 import eu.etaxonomy.cdm.api.service.INameService;
28 import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
29 import eu.etaxonomy.cdm.api.service.ITaxonService;
30 import eu.etaxonomy.cdm.model.common.ICdmBase;
31 import eu.etaxonomy.cdm.model.name.TaxonName;
32 import eu.etaxonomy.cdm.model.taxon.Classification;
33 import eu.etaxonomy.cdm.model.taxon.Synonym;
34 import eu.etaxonomy.cdm.model.taxon.Taxon;
35 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
36 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
37 import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto;
38 import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
39 import eu.etaxonomy.taxeditor.editor.EditorUtil;
40 import eu.etaxonomy.taxeditor.editor.e4.TaxonEditorInputE4;
41 import eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditorE4;
42 import eu.etaxonomy.taxeditor.model.AbstractUtility;
43 import eu.etaxonomy.taxeditor.model.MessagingUtils;
44 import eu.etaxonomy.taxeditor.navigation.internal.TaxeditorNavigationPlugin;
45 import eu.etaxonomy.taxeditor.navigation.l10n.Messages;
46 import eu.etaxonomy.taxeditor.newWizard.NewClassificationWizard;
47 import eu.etaxonomy.taxeditor.store.CdmStore;
48 import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
49
50 /**
51 *
52 * @author n.hoffmann
53 * @created 24.03.2009
54 * @version 1.0
55 */
56 public class NavigationUtil extends AbstractUtility{
57
58 private static final String NOT_IMPLEMENTED_YET = Messages.NavigationUtil_NOT_IMPLEMENTED;
59 private static IUndoContext defaultUndoContext;
60
61 /**
62 * <p>openEditor</p>
63 *
64 * @param selectedObject a {@link eu.etaxonomy.cdm.model.common.CdmBase} object.
65 */
66 public static void openEditor(UuidAndTitleCache uuidAndTitleCache, Shell shell, EModelService modelService, EPartService partService, MApplication application){
67 Class<?> type = uuidAndTitleCache.getType();
68 ICdmBase cdmBase = null;
69 if(uuidAndTitleCache instanceof TaxonNodeDto){
70 EditorUtil.openTaxonNodeE4(uuidAndTitleCache.getUuid(), modelService, partService, application);
71 return;
72 }
73 else if(type.equals(Classification.class)){
74 cdmBase = CdmStore.getService(IClassificationService.class).load(uuidAndTitleCache.getUuid());
75 }
76 else if(type.equals(TaxonNode.class)){
77 cdmBase = CdmStore.getService(ITaxonNodeService.class).load(uuidAndTitleCache.getUuid());
78 }
79 else if(TaxonBase.class.isAssignableFrom(type)){
80 cdmBase = CdmStore.getService(ITaxonService.class).load(uuidAndTitleCache.getUuid());
81 }
82 else if(type.equals(TaxonName.class)){
83 cdmBase = CdmStore.getService(INameService.class).load(uuidAndTitleCache.getUuid());
84 }
85 else{
86 MessagingUtils.warningDialog(Messages.NavigationUtil_UNKNOWN_TYPE, NavigationUtil.class, Messages.NavigationUtil_UNKNOWN_TYPE_MESSAGE);
87 }
88 if(cdmBase!=null){
89 openEditor(cdmBase, shell, modelService, partService, application);
90 }
91 else{
92 MessagingUtils.warningDialog(Messages.NavigationUtil_NOT_FOUND, NavigationUtil.class, Messages.NavigationUtil_NOT_FOUND_MESSAGE);
93 }
94 }
95
96 public static void openEditor(ICdmBase selectedObject, Shell shell, EModelService modelService, EPartService partService, MApplication application){
97 UUID entityUuid = selectedObject.getUuid();
98 try {
99 if(selectedObject instanceof TaxonNode){
100 TaxonNode taxonNode = (TaxonNode)selectedObject;
101 Classification classification = taxonNode.getClassification();
102 if(classification!=null && classification.getRootNode().equals(taxonNode)){
103 NewClassificationWizard classificationWizard = new NewClassificationWizard();
104 classificationWizard.init(null, null);
105 classificationWizard.setEntity(classification);
106 WizardDialog dialog = new WizardDialog(shell, classificationWizard);
107 dialog.open();
108 }
109 else{
110 EditorUtil.openTaxonNodeE4(entityUuid, modelService, partService, application);
111 }
112 }
113 else if(selectedObject instanceof TaxonBase){
114 EditorUtil.openTaxonBaseE4(entityUuid, modelService, partService, application);
115 }
116 else{
117 MessagingUtils.warningDialog(Messages.NavigationUtil_UNSUPPORTED_TYPE, NavigationUtil.class, Messages.NavigationUtil_UNSUPPORTED_TYPE_MESSAGE + selectedObject);
118 }
119 } catch (Exception e) {
120 MessagingUtils.errorDialog(Messages.NavigationUtil_CREATE_FAILED,
121 NavigationUtil.class,
122 e.getMessage(), TaxeditorStorePlugin.PLUGIN_ID,
123 e,
124 true);
125
126 }
127 }
128
129 /**
130 * <p>openEmpty</p>
131 *
132 * @param parentNodeUuid a {@link java.util.UUID} object.
133 */
134 public static void openEmpty(UUID parentNodeUuid) {
135 EditorUtil.openEmptyE4(parentNodeUuid);
136 }
137
138 /**
139 * <p>getUndoContext</p>
140 *
141 * @return a {@link org.eclipse.core.commands.operations.IUndoContext} object.
142 */
143 public static IUndoContext getUndoContext() {
144 // FIXME this has to be more specific. Every widget has to have its own undo context
145 // return IOperationHistory.GLOBAL_UNDO_CONTEXT;
146
147 // Plug-ins that wish their operations to be undoable from workbench views
148 // such as the Navigator or Package Explorer should assign the workbench
149 // undo context to their operations.
150 if (defaultUndoContext == null) {
151 defaultUndoContext = new UndoContext();
152 }
153 return defaultUndoContext;
154 }
155
156 /**
157 * Whether a taxonNode has unsaved changes.
158 *
159 * @param taxonNode a {@link eu.etaxonomy.cdm.model.taxon.TaxonNode} object.
160 * @return a boolean.
161 */
162 public static boolean isDirty(TaxonNodeDto taxonNode, EPartService partService){
163
164 Collection<MPart> dirtyParts = partService.getDirtyParts();
165 for (MPart part : dirtyParts) {
166 if(part.getObject() instanceof TaxonNameEditorE4){
167 TaxonEditorInputE4 input = ((TaxonNameEditorE4) part.getObject()).getEditorInput();
168 if(input.getTaxonNode().getUuid().equals(taxonNode.getUuid())){
169 return true;
170 }
171 }
172 }
173 return false;
174 }
175
176 /**
177 * Whether a taxonNode has unsaved changes.
178 *
179 * @param taxonNode a {@link eu.etaxonomy.cdm.model.taxon.TaxonNode} object.
180 * @return a boolean.
181 */
182 public static boolean isDirty(TaxonNode taxonNode, EPartService partService){
183
184 Collection<MPart> dirtyParts = partService.getDirtyParts();
185 for (MPart part : dirtyParts) {
186 if(part.getObject() instanceof TaxonNameEditorE4){
187 TaxonEditorInputE4 input = ((TaxonNameEditorE4) part.getObject()).getEditorInput();
188 if(input.getTaxonNode().equals(taxonNode)){
189 return true;
190 }
191 }
192 }
193 return false;
194 }
195
196 /**
197 * <p>openSearch</p>
198 *
199 * @param selection a {@link java.lang.Object} object.
200 */
201 public static void openSearch(Object selection) {
202 if(selection instanceof Taxon){
203 Taxon taxon = (Taxon) selection;
204
205 handleOpeningOfMultipleTaxonNodes(taxon.getTaxonNodes());
206
207 }else if(selection instanceof Synonym){
208 Synonym synonym = (Synonym) selection;
209
210 Set<Taxon> accTaxa = new HashSet<Taxon>();
211 if (synonym.getAcceptedTaxon() != null){
212 accTaxa.add(synonym.getAcceptedTaxon());
213 }
214 handleOpeningOfMultipleTaxa(accTaxa);
215
216 }else{
217 MessagingUtils.warningDialog(NOT_IMPLEMENTED_YET, NavigationUtil.class, Messages.NavigationUtil_ORPHAN_NAME_MESSAGE);
218 }
219
220 }
221
222 private static void handleOpeningOfMultipleTaxa(Set<Taxon> acceptedTaxa) {
223 //FIXME E4 migrate/delete
224 // if(acceptedTaxa.size() == 1){
225 // openEditor(acceptedTaxa.iterator().next(), PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
226 // }else if(acceptedTaxa.size() > 1){
227 // // FIXME implement a dialog that shows all possible taxa and let the user choose which he wants to open.
228 // MessagingUtils.warningDialog(NOT_IMPLEMENTED_YET, NavigationUtil.class, Messages.NavigationUtil_MULTI_TREE);
229 // }else if(acceptedTaxa.size() == 0){
230 // // this is an undesired state
231 // MessagingUtils.warningDialog(NOT_IMPLEMENTED_YET, NavigationUtil.class, Messages.NavigationUtil_ORPHAN_TAXON);
232 // }
233 }
234
235 /**
236 * @param taxonNodes
237 */
238 private static void handleOpeningOfMultipleTaxonNodes(
239 Set<TaxonNode> taxonNodes) {
240 //FIXME E4 migrate/delete
241 //
242 // if(taxonNodes.size() == 1){
243 // openEditor(taxonNodes.iterator().next(), PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
244 // }else if(taxonNodes.size() > 1){
245 // // FIXME implement a dialog that shows all possible taxa and let the user choose which he wants to open.
246 // MessagingUtils.warningDialog(NOT_IMPLEMENTED_YET, NavigationUtil.class, "The accepted taxon is in multiple taxonomic trees. We currently do not know which one you want to open. This case is not handled yet by the software.");
247 // }else if(taxonNodes.size() == 0){
248 // // this is an undesired state
249 // MessagingUtils.warningDialog(Messages.NavigationUtil_INCORRECT_STATE, NavigationUtil.class, Messages.NavigationUtil_INCORRECT_STATE_MESSAGE);
250 // }
251 }
252
253 /**
254 * <p>getPluginId</p>
255 *
256 * @return a {@link java.lang.String} object.
257 */
258 public static String getPluginId(){
259 return TaxeditorNavigationPlugin.PLUGIN_ID;
260 }
261
262 }