Project

General

Profile

Download (8.88 KB) Statistics
| Branch: | Tag: | Revision:
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.UuidAndTitleCache;
38
import eu.etaxonomy.taxeditor.editor.EditorUtil;
39
import eu.etaxonomy.taxeditor.editor.e4.TaxonEditorInputE4;
40
import eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditorE4;
41
import eu.etaxonomy.taxeditor.model.AbstractUtility;
42
import eu.etaxonomy.taxeditor.model.MessagingUtils;
43
import eu.etaxonomy.taxeditor.navigation.internal.TaxeditorNavigationPlugin;
44
import eu.etaxonomy.taxeditor.navigation.l10n.Messages;
45
import eu.etaxonomy.taxeditor.newWizard.NewClassificationWizard;
46
import eu.etaxonomy.taxeditor.store.CdmStore;
47
import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
48

    
49
/**
50
 *
51
 * @author n.hoffmann
52
 * @created 24.03.2009
53
 * @version 1.0
54
 */
55
public class NavigationUtil extends AbstractUtility{
56

    
57
    private static final String NOT_IMPLEMENTED_YET = Messages.NavigationUtil_NOT_IMPLEMENTED;
58
    private static IUndoContext defaultUndoContext;
59

    
60
	/**
61
	 * <p>openEditor</p>
62
	 *
63
	 * @param selectedObject a {@link eu.etaxonomy.cdm.model.common.CdmBase} object.
64
	 */
65
	public static void openEditor(UuidAndTitleCache uuidAndTitleCache, Shell shell, EModelService modelService, EPartService partService, MApplication application){
66
	    Class<?> type = uuidAndTitleCache.getType();
67
	    ICdmBase cdmBase = null;
68
	    if(type.equals(Classification.class)){
69
	        cdmBase = CdmStore.getService(IClassificationService.class).load(uuidAndTitleCache.getUuid());
70
	    }
71
	    else if(type.equals(TaxonNode.class)){
72
	    	cdmBase = CdmStore.getService(ITaxonNodeService.class).load(uuidAndTitleCache.getUuid());
73
	    }
74
	    else if(TaxonBase.class.isAssignableFrom(type)){
75
	    	cdmBase = CdmStore.getService(ITaxonService.class).load(uuidAndTitleCache.getUuid());
76
	    }
77
	    else if(type.equals(TaxonName.class)){
78
	    	cdmBase = CdmStore.getService(INameService.class).load(uuidAndTitleCache.getUuid());
79
	    }
80
	    else{
81
	        MessagingUtils.warningDialog(Messages.NavigationUtil_UNKNOWN_TYPE, NavigationUtil.class, Messages.NavigationUtil_UNKNOWN_TYPE_MESSAGE);
82
	    }
83
	    if(cdmBase!=null){
84
	    	openEditor(cdmBase, shell, modelService, partService, application);
85
	    }
86
	    else{
87
	    	MessagingUtils.warningDialog(Messages.NavigationUtil_NOT_FOUND, NavigationUtil.class, Messages.NavigationUtil_NOT_FOUND_MESSAGE);
88
	    }
89
	}
90

    
91
	public static void openEditor(ICdmBase selectedObject, Shell shell, EModelService modelService, EPartService partService, MApplication application){
92
		UUID entityUuid = selectedObject.getUuid();
93
		try {
94
			if(selectedObject instanceof TaxonNode){
95
			    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
			        WizardDialog dialog = new WizardDialog(shell, classificationWizard);
102
			        dialog.open();
103
			    }
104
			    else{
105
			        EditorUtil.openTaxonNodeE4(entityUuid, modelService, partService, application);
106
			    }
107
			}
108
			else if(selectedObject instanceof TaxonBase){
109
			    EditorUtil.openTaxonBaseE4(entityUuid, modelService, partService, application);
110
			}
111
			else{
112
				MessagingUtils.warningDialog(Messages.NavigationUtil_UNSUPPORTED_TYPE, NavigationUtil.class, Messages.NavigationUtil_UNSUPPORTED_TYPE_MESSAGE + selectedObject);
113
			}
114
		} catch (Exception e) {
115
		    MessagingUtils.errorDialog(Messages.NavigationUtil_CREATE_FAILED,
116
		            NavigationUtil.class,
117
		            e.getMessage(), TaxeditorStorePlugin.PLUGIN_ID,
118
		            e,
119
		            true);
120

    
121
		}
122
	}
123

    
124
	/**
125
	 * <p>openEmpty</p>
126
	 *
127
	 * @param parentNodeUuid a {@link java.util.UUID} object.
128
	 */
129
	public static void openEmpty(UUID parentNodeUuid) {
130
	    EditorUtil.openEmptyE4(parentNodeUuid);
131
	}
132

    
133
	/**
134
	 * <p>getUndoContext</p>
135
	 *
136
	 * @return a {@link org.eclipse.core.commands.operations.IUndoContext} object.
137
	 */
138
	public static IUndoContext getUndoContext() {
139
		// FIXME this has to be more specific. Every widget has to have its own undo context
140
//		return IOperationHistory.GLOBAL_UNDO_CONTEXT;
141

    
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
		if (defaultUndoContext == null) {
146
			defaultUndoContext = new UndoContext();
147
		}
148
		return defaultUndoContext;
149
	}
150

    
151
	/**
152
	 * Whether a taxonNode has unsaved changes.
153
	 *
154
	 * @param taxonNode a {@link eu.etaxonomy.cdm.model.taxon.TaxonNode} object.
155
	 * @return a boolean.
156
	 */
157
	public static boolean isDirty(TaxonNode taxonNode, EPartService partService){
158

    
159
	    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
		return false;
169
	}
170

    
171
	/**
172
	 * <p>openSearch</p>
173
	 *
174
	 * @param selection a {@link java.lang.Object} object.
175
	 */
176
	public static void openSearch(Object selection) {
177
		if(selection instanceof Taxon){
178
			Taxon taxon = (Taxon) selection;
179

    
180
			handleOpeningOfMultipleTaxonNodes(taxon.getTaxonNodes());
181

    
182
		}else if(selection instanceof Synonym){
183
			Synonym synonym = (Synonym) selection;
184

    
185
			Set<Taxon> accTaxa = new HashSet<Taxon>();
186
			if (synonym.getAcceptedTaxon() != null){
187
				accTaxa.add(synonym.getAcceptedTaxon());
188
			}
189
			handleOpeningOfMultipleTaxa(accTaxa);
190

    
191
		}else{
192
			MessagingUtils.warningDialog(NOT_IMPLEMENTED_YET, NavigationUtil.class, Messages.NavigationUtil_ORPHAN_NAME_MESSAGE);
193
		}
194

    
195
	}
196

    
197
	private static void handleOpeningOfMultipleTaxa(Set<Taxon> acceptedTaxa) {
198
	    //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
	}
209

    
210
	/**
211
	 * @param taxonNodes
212
	 */
213
	private static void handleOpeningOfMultipleTaxonNodes(
214
			Set<TaxonNode> taxonNodes) {
215
	    //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
	}
227

    
228
	/**
229
	 * <p>getPluginId</p>
230
	 *
231
	 * @return a {@link java.lang.String} object.
232
	 */
233
	public static String getPluginId(){
234
		return TaxeditorNavigationPlugin.PLUGIN_ID;
235
	}
236

    
237
}
(2-2/5)