Project

General

Profile

Download (8.88 KB) Statistics
| Branch: | Tag: | Revision:
1 e8409423 n.hoffmann
/**
2
* Copyright (C) 2007 EDIT
3 cdf77847 Patric Plitzner
* European Distributed Institute of Taxonomy
4 e8409423 n.hoffmann
* http://www.e-taxonomy.eu
5 cdf77847 Patric Plitzner
*
6 e8409423 n.hoffmann
* 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 bc330e88 Patrick Plitzner
import java.util.Collection;
13 d34abf30 Andreas Müller
import java.util.HashSet;
14 5dd980ce n.hoffmann
import java.util.Set;
15 e8409423 n.hoffmann
import java.util.UUID;
16
17
import org.eclipse.core.commands.operations.IUndoContext;
18 9651449a p.ciardelli
import org.eclipse.core.commands.operations.UndoContext;
19 be02f4f9 Patrick Plitzner
import org.eclipse.e4.ui.model.application.MApplication;
20 bc330e88 Patrick Plitzner
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
21 be02f4f9 Patrick Plitzner
import org.eclipse.e4.ui.workbench.modeling.EModelService;
22 bc330e88 Patrick Plitzner
import org.eclipse.e4.ui.workbench.modeling.EPartService;
23 5c823b08 p.plitzner
import org.eclipse.jface.wizard.WizardDialog;
24 e8409423 n.hoffmann
import org.eclipse.swt.widgets.Shell;
25
26 de83ea1f Patrick Plitzner
import eu.etaxonomy.cdm.api.service.IClassificationService;
27
import eu.etaxonomy.cdm.api.service.INameService;
28
import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
29 0aeb6eff Patrick Plitzner
import eu.etaxonomy.cdm.api.service.ITaxonService;
30 610a1d3d n.hoffmann
import eu.etaxonomy.cdm.model.common.ICdmBase;
31 e3a4a3ff Andreas Müller
import eu.etaxonomy.cdm.model.name.TaxonName;
32 5c823b08 p.plitzner
import eu.etaxonomy.cdm.model.taxon.Classification;
33 5dd980ce n.hoffmann
import eu.etaxonomy.cdm.model.taxon.Synonym;
34 e8409423 n.hoffmann
import eu.etaxonomy.cdm.model.taxon.Taxon;
35 09c72303 n.hoffmann
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
36 5dd980ce n.hoffmann
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
37 0aeb6eff Patrick Plitzner
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
38 e8409423 n.hoffmann
import eu.etaxonomy.taxeditor.editor.EditorUtil;
39 bc330e88 Patrick Plitzner
import eu.etaxonomy.taxeditor.editor.e4.TaxonEditorInputE4;
40
import eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditorE4;
41 42bcf48d n.hoffmann
import eu.etaxonomy.taxeditor.model.AbstractUtility;
42 41e2f693 Cherian Mathew
import eu.etaxonomy.taxeditor.model.MessagingUtils;
43 e8409423 n.hoffmann
import eu.etaxonomy.taxeditor.navigation.internal.TaxeditorNavigationPlugin;
44 939829bc Patrick Plitzner
import eu.etaxonomy.taxeditor.navigation.l10n.Messages;
45 5c823b08 p.plitzner
import eu.etaxonomy.taxeditor.newWizard.NewClassificationWizard;
46 0aeb6eff Patrick Plitzner
import eu.etaxonomy.taxeditor.store.CdmStore;
47 1b626d51 Cherian Mathew
import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
48 e8409423 n.hoffmann
49
/**
50 3be6ef3e n.hoffmann
 *
51 e8409423 n.hoffmann
 * @author n.hoffmann
52
 * @created 24.03.2009
53
 * @version 1.0
54
 */
55
public class NavigationUtil extends AbstractUtility{
56 939829bc Patrick Plitzner
57
    private static final String NOT_IMPLEMENTED_YET = Messages.NavigationUtil_NOT_IMPLEMENTED;
58
    private static IUndoContext defaultUndoContext;
59 cdf77847 Patric Plitzner
60 3be6ef3e n.hoffmann
	/**
61
	 * <p>openEditor</p>
62
	 *
63
	 * @param selectedObject a {@link eu.etaxonomy.cdm.model.common.CdmBase} object.
64
	 */
65 be02f4f9 Patrick Plitzner
	public static void openEditor(UuidAndTitleCache uuidAndTitleCache, Shell shell, EModelService modelService, EPartService partService, MApplication application){
66 e3a4a3ff Andreas Müller
	    Class<?> type = uuidAndTitleCache.getType();
67 baa11275 Patrick Plitzner
	    ICdmBase cdmBase = null;
68 de83ea1f Patrick Plitzner
	    if(type.equals(Classification.class)){
69 baa11275 Patrick Plitzner
	        cdmBase = CdmStore.getService(IClassificationService.class).load(uuidAndTitleCache.getUuid());
70 de83ea1f Patrick Plitzner
	    }
71
	    else if(type.equals(TaxonNode.class)){
72 baa11275 Patrick Plitzner
	    	cdmBase = CdmStore.getService(ITaxonNodeService.class).load(uuidAndTitleCache.getUuid());
73 de83ea1f Patrick Plitzner
	    }
74 f58309c9 Katja Luther
	    else if(TaxonBase.class.isAssignableFrom(type)){
75 baa11275 Patrick Plitzner
	    	cdmBase = CdmStore.getService(ITaxonService.class).load(uuidAndTitleCache.getUuid());
76 de83ea1f Patrick Plitzner
	    }
77 e3a4a3ff Andreas Müller
	    else if(type.equals(TaxonName.class)){
78 baa11275 Patrick Plitzner
	    	cdmBase = CdmStore.getService(INameService.class).load(uuidAndTitleCache.getUuid());
79 de83ea1f Patrick Plitzner
	    }
80
	    else{
81 939829bc Patrick Plitzner
	        MessagingUtils.warningDialog(Messages.NavigationUtil_UNKNOWN_TYPE, NavigationUtil.class, Messages.NavigationUtil_UNKNOWN_TYPE_MESSAGE);
82 0aeb6eff Patrick Plitzner
	    }
83 baa11275 Patrick Plitzner
	    if(cdmBase!=null){
84 be02f4f9 Patrick Plitzner
	    	openEditor(cdmBase, shell, modelService, partService, application);
85 baa11275 Patrick Plitzner
	    }
86
	    else{
87 939829bc Patrick Plitzner
	    	MessagingUtils.warningDialog(Messages.NavigationUtil_NOT_FOUND, NavigationUtil.class, Messages.NavigationUtil_NOT_FOUND_MESSAGE);
88 baa11275 Patrick Plitzner
	    }
89 0aeb6eff Patrick Plitzner
	}
90 de83ea1f Patrick Plitzner
91 be02f4f9 Patrick Plitzner
	public static void openEditor(ICdmBase selectedObject, Shell shell, EModelService modelService, EPartService partService, MApplication application){
92 cdf77847 Patric Plitzner
		UUID entityUuid = selectedObject.getUuid();
93
		try {
94 e67b0fb8 Patrick Plitzner
			if(selectedObject instanceof TaxonNode){
95 bfff18ea Patrick Plitzner
			    TaxonNode taxonNode = (TaxonNode)selectedObject;
96
			    Classification classification = taxonNode.getClassification();
97
			    if(classification!=null && classification.getRootNode().equals(taxonNode)){
98
			        NewClassificationWizard classificationWizard = new NewClassificationWizard();
99
			        classificationWizard.init(null, null);
100
			        classificationWizard.setEntity(classification);
101 9befe8ce Patrick Plitzner
			        WizardDialog dialog = new WizardDialog(shell, classificationWizard);
102 bfff18ea Patrick Plitzner
			        dialog.open();
103
			    }
104
			    else{
105 be02f4f9 Patrick Plitzner
			        EditorUtil.openTaxonNodeE4(entityUuid, modelService, partService, application);
106 bfff18ea Patrick Plitzner
			    }
107 5362d1b1 Patrick Plitzner
			}
108
			else if(selectedObject instanceof TaxonBase){
109 be02f4f9 Patrick Plitzner
			    EditorUtil.openTaxonBaseE4(entityUuid, modelService, partService, application);
110 5362d1b1 Patrick Plitzner
			}
111
			else{
112 939829bc Patrick Plitzner
				MessagingUtils.warningDialog(Messages.NavigationUtil_UNSUPPORTED_TYPE, NavigationUtil.class, Messages.NavigationUtil_UNSUPPORTED_TYPE_MESSAGE + selectedObject);
113 09c72303 n.hoffmann
			}
114 27b59ab1 n.hoffmann
		} catch (Exception e) {
115 939829bc Patrick Plitzner
		    MessagingUtils.errorDialog(Messages.NavigationUtil_CREATE_FAILED,
116 1b626d51 Cherian Mathew
		            NavigationUtil.class,
117
		            e.getMessage(), TaxeditorStorePlugin.PLUGIN_ID,
118
		            e,
119
		            true);
120
121 27b59ab1 n.hoffmann
		}
122
	}
123 cdf77847 Patric Plitzner
124 3be6ef3e n.hoffmann
	/**
125
	 * <p>openEmpty</p>
126
	 *
127
	 * @param parentNodeUuid a {@link java.util.UUID} object.
128
	 */
129 27b59ab1 n.hoffmann
	public static void openEmpty(UUID parentNodeUuid) {
130 22f2d1a4 Patrick Plitzner
	    EditorUtil.openEmptyE4(parentNodeUuid);
131 9651449a p.ciardelli
	}
132 e8409423 n.hoffmann
133 3be6ef3e n.hoffmann
	/**
134
	 * <p>getUndoContext</p>
135
	 *
136
	 * @return a {@link org.eclipse.core.commands.operations.IUndoContext} object.
137
	 */
138 e8409423 n.hoffmann
	public static IUndoContext getUndoContext() {
139
		// FIXME this has to be more specific. Every widget has to have its own undo context
140 9651449a p.ciardelli
//		return IOperationHistory.GLOBAL_UNDO_CONTEXT;
141 cdf77847 Patric Plitzner
142
		// Plug-ins that wish their operations to be undoable from workbench views
143
		// such as the Navigator or Package Explorer should assign the workbench
144
		// undo context to their operations.
145 9651449a p.ciardelli
		if (defaultUndoContext == null) {
146
			defaultUndoContext = new UndoContext();
147
		}
148
		return defaultUndoContext;
149 e8409423 n.hoffmann
	}
150
151 5dd980ce n.hoffmann
	/**
152
	 * Whether a taxonNode has unsaved changes.
153 3be6ef3e n.hoffmann
	 *
154
	 * @param taxonNode a {@link eu.etaxonomy.cdm.model.taxon.TaxonNode} object.
155
	 * @return a boolean.
156 5dd980ce n.hoffmann
	 */
157 bc330e88 Patrick Plitzner
	public static boolean isDirty(TaxonNode taxonNode, EPartService partService){
158 cdf77847 Patric Plitzner
159 bc330e88 Patrick Plitzner
	    Collection<MPart> dirtyParts = partService.getDirtyParts();
160
	    for (MPart part : dirtyParts) {
161
            if(part.getObject() instanceof TaxonNameEditorE4){
162
                TaxonEditorInputE4 input = ((TaxonNameEditorE4) part.getObject()).getEditorInput();
163
                if(input.getTaxonNode().equals(taxonNode)){
164
                    return true;
165
                }
166
            }
167
        }
168 e8409423 n.hoffmann
		return false;
169
	}
170
171 5dd980ce n.hoffmann
	/**
172 3be6ef3e n.hoffmann
	 * <p>openSearch</p>
173
	 *
174
	 * @param selection a {@link java.lang.Object} object.
175 5dd980ce n.hoffmann
	 */
176
	public static void openSearch(Object selection) {
177
		if(selection instanceof Taxon){
178
			Taxon taxon = (Taxon) selection;
179 cdf77847 Patric Plitzner
180 5dd980ce n.hoffmann
			handleOpeningOfMultipleTaxonNodes(taxon.getTaxonNodes());
181 cdf77847 Patric Plitzner
182 5dd980ce n.hoffmann
		}else if(selection instanceof Synonym){
183
			Synonym synonym = (Synonym) selection;
184 cdf77847 Patric Plitzner
185 d34abf30 Andreas Müller
			Set<Taxon> accTaxa = new HashSet<Taxon>();
186
			if (synonym.getAcceptedTaxon() != null){
187
				accTaxa.add(synonym.getAcceptedTaxon());
188
			}
189
			handleOpeningOfMultipleTaxa(accTaxa);
190 cdf77847 Patric Plitzner
191 5dd980ce n.hoffmann
		}else{
192 939829bc Patrick Plitzner
			MessagingUtils.warningDialog(NOT_IMPLEMENTED_YET, NavigationUtil.class, Messages.NavigationUtil_ORPHAN_NAME_MESSAGE);
193 5dd980ce n.hoffmann
		}
194 cdf77847 Patric Plitzner
195 5dd980ce n.hoffmann
	}
196
197 09c72303 n.hoffmann
	private static void handleOpeningOfMultipleTaxa(Set<Taxon> acceptedTaxa) {
198 22f2d1a4 Patrick Plitzner
	    //FIXME E4 migrate/delete
199
//		if(acceptedTaxa.size() == 1){
200
//			openEditor(acceptedTaxa.iterator().next(), PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
201
//		}else if(acceptedTaxa.size() > 1){
202
//			// FIXME implement a dialog that shows all possible taxa and let the user choose which he wants to open.
203
//			MessagingUtils.warningDialog(NOT_IMPLEMENTED_YET, NavigationUtil.class, Messages.NavigationUtil_MULTI_TREE);
204
//		}else if(acceptedTaxa.size() == 0){
205
//			// this is an undesired state
206
//			MessagingUtils.warningDialog(NOT_IMPLEMENTED_YET, NavigationUtil.class, Messages.NavigationUtil_ORPHAN_TAXON);
207
//		}
208 09c72303 n.hoffmann
	}
209
210 5dd980ce n.hoffmann
	/**
211
	 * @param taxonNodes
212
	 */
213
	private static void handleOpeningOfMultipleTaxonNodes(
214
			Set<TaxonNode> taxonNodes) {
215 22f2d1a4 Patrick Plitzner
	    //FIXME E4 migrate/delete
216
//
217
//		if(taxonNodes.size() == 1){
218
//			openEditor(taxonNodes.iterator().next(), PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
219
//		}else if(taxonNodes.size() > 1){
220
//			// FIXME implement a dialog that shows all possible taxa and let the user choose which he wants to open.
221
//			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.");
222
//		}else if(taxonNodes.size() == 0){
223
//			// this is an undesired state
224
//			MessagingUtils.warningDialog(Messages.NavigationUtil_INCORRECT_STATE, NavigationUtil.class, Messages.NavigationUtil_INCORRECT_STATE_MESSAGE);
225
//		}
226 d2443357 n.hoffmann
	}
227 a2353579 n.hoffmann
228 3be6ef3e n.hoffmann
	/**
229
	 * <p>getPluginId</p>
230
	 *
231
	 * @return a {@link java.lang.String} object.
232
	 */
233 b9a0d300 l.morris
	public static String getPluginId(){
234 74b9f670 n.hoffmann
		return TaxeditorNavigationPlugin.PLUGIN_ID;
235
	}
236 27b59ab1 n.hoffmann
237 3be6ef3e n.hoffmann
}