Project

General

Profile

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