Project

General

Profile

Download (8.49 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.ui.basic.MPart;
20
import org.eclipse.e4.ui.workbench.modeling.EPartService;
21
import org.eclipse.jface.wizard.WizardDialog;
22
import org.eclipse.swt.widgets.Shell;
23

    
24
import eu.etaxonomy.cdm.api.service.IClassificationService;
25
import eu.etaxonomy.cdm.api.service.INameService;
26
import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
27
import eu.etaxonomy.cdm.api.service.ITaxonService;
28
import eu.etaxonomy.cdm.model.common.ICdmBase;
29
import eu.etaxonomy.cdm.model.name.TaxonName;
30
import eu.etaxonomy.cdm.model.taxon.Classification;
31
import eu.etaxonomy.cdm.model.taxon.Synonym;
32
import eu.etaxonomy.cdm.model.taxon.Taxon;
33
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
34
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
35
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
36
import eu.etaxonomy.taxeditor.editor.EditorUtil;
37
import eu.etaxonomy.taxeditor.editor.e4.TaxonEditorInputE4;
38
import eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditorE4;
39
import eu.etaxonomy.taxeditor.model.AbstractUtility;
40
import eu.etaxonomy.taxeditor.model.MessagingUtils;
41
import eu.etaxonomy.taxeditor.navigation.internal.TaxeditorNavigationPlugin;
42
import eu.etaxonomy.taxeditor.navigation.l10n.Messages;
43
import eu.etaxonomy.taxeditor.newWizard.NewClassificationWizard;
44
import eu.etaxonomy.taxeditor.store.CdmStore;
45
import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
46

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

    
55
    private static final String NOT_IMPLEMENTED_YET = Messages.NavigationUtil_NOT_IMPLEMENTED;
56
    private static IUndoContext defaultUndoContext;
57

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

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

    
119
		}
120
	}
121

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

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

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

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

    
157
	    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
		return false;
167
	}
168

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

    
178
			handleOpeningOfMultipleTaxonNodes(taxon.getTaxonNodes());
179

    
180
		}else if(selection instanceof Synonym){
181
			Synonym synonym = (Synonym) selection;
182

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

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

    
193
	}
194

    
195
	private static void handleOpeningOfMultipleTaxa(Set<Taxon> acceptedTaxa) {
196
	    //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
	}
207

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

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

    
235
}
(1-1/4)