Project

General

Profile

Download (9.4 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
package eu.etaxonomy.taxeditor.navigation;
10

    
11
import java.util.Collection;
12
import java.util.HashSet;
13
import java.util.Set;
14
import java.util.UUID;
15

    
16
import org.eclipse.core.commands.operations.IUndoContext;
17
import org.eclipse.core.commands.operations.UndoContext;
18
import org.eclipse.e4.ui.model.application.MApplication;
19
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
20
import org.eclipse.e4.ui.workbench.modeling.EModelService;
21
import org.eclipse.e4.ui.workbench.modeling.EPartService;
22
import org.eclipse.jface.wizard.WizardDialog;
23
import org.eclipse.swt.widgets.Shell;
24

    
25
import eu.etaxonomy.cdm.api.service.IClassificationService;
26
import eu.etaxonomy.cdm.api.service.INameService;
27
import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
28
import eu.etaxonomy.cdm.api.service.ITaxonService;
29
import eu.etaxonomy.cdm.model.common.ICdmBase;
30
import eu.etaxonomy.cdm.model.name.TaxonName;
31
import eu.etaxonomy.cdm.model.taxon.Classification;
32
import eu.etaxonomy.cdm.model.taxon.Synonym;
33
import eu.etaxonomy.cdm.model.taxon.Taxon;
34
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
35
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
36
import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto;
37
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
38
import eu.etaxonomy.taxeditor.editor.EditorUtil;
39
import eu.etaxonomy.taxeditor.editor.e4.TaxonEditorInput;
40
import eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditor;
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
 * @author n.hoffmann
51
 * @created 24.03.2009
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, EModelService modelService, EPartService partService, MApplication application){
64
	    Class<?> type = uuidAndTitleCache.getType();
65
	    ICdmBase cdmBase = null;
66
	    if(uuidAndTitleCache instanceof TaxonNodeDto){
67
            EditorUtil.openTaxonNodeE4(uuidAndTitleCache.getUuid(), modelService, partService, application);
68
            return;
69
        }
70
	    else if(type.equals(Classification.class)){
71
	        cdmBase = CdmStore.getService(IClassificationService.class).load(uuidAndTitleCache.getUuid());
72
	    }
73
	    else if(type.equals(TaxonNode.class)){
74
	    	cdmBase = CdmStore.getService(ITaxonNodeService.class).load(uuidAndTitleCache.getUuid());
75
	    }
76
	    else if(TaxonBase.class.isAssignableFrom(type)){
77
	    	cdmBase = CdmStore.getService(ITaxonService.class).load(uuidAndTitleCache.getUuid());
78
	    }
79
	    else if(type.equals(TaxonName.class)){
80
	    	cdmBase = CdmStore.getService(INameService.class).load(uuidAndTitleCache.getUuid());
81
	    }
82
	    else{
83
	        MessagingUtils.warningDialog(Messages.NavigationUtil_UNKNOWN_TYPE, NavigationUtil.class, Messages.NavigationUtil_UNKNOWN_TYPE_MESSAGE);
84
	    }
85
	    if(cdmBase!=null){
86
	    	openEditor(cdmBase, shell, modelService, partService, application);
87
	    }
88
	    else{
89
	    	MessagingUtils.warningDialog(Messages.NavigationUtil_NOT_FOUND, NavigationUtil.class, Messages.NavigationUtil_NOT_FOUND_MESSAGE);
90
	    }
91
	}
92

    
93
	public static void openEditor(ICdmBase selectedObject, Shell shell, EModelService modelService, EPartService partService, MApplication application){
94
		UUID entityUuid = selectedObject.getUuid();
95
		try {
96
			if(selectedObject instanceof TaxonNode){
97
			    TaxonNode taxonNode = (TaxonNode)selectedObject;
98
			    Classification classification = taxonNode.getClassification();
99
			    if(classification!=null && classification.getRootNode().equals(taxonNode)){
100
			        NewClassificationWizard classificationWizard = new NewClassificationWizard();
101
			        classificationWizard.init(null, null);
102
			        classificationWizard.setEntity(classification);
103
			        WizardDialog dialog = new WizardDialog(shell, classificationWizard);
104
			        dialog.open();
105
			    }
106
			    else{
107
			        EditorUtil.openTaxonNodeE4(entityUuid, modelService, partService, application);
108
			    }
109
			}
110
			else if(selectedObject instanceof TaxonBase){
111
			    EditorUtil.openTaxonBaseE4(entityUuid, modelService, partService, application);
112
			}
113
			else{
114
				MessagingUtils.warningDialog(Messages.NavigationUtil_UNSUPPORTED_TYPE, NavigationUtil.class, Messages.NavigationUtil_UNSUPPORTED_TYPE_MESSAGE + selectedObject);
115
			}
116
		} catch (Exception e) {
117
		    MessagingUtils.errorDialog(Messages.NavigationUtil_CREATE_FAILED,
118
		            NavigationUtil.class,
119
		            e.getMessage(), TaxeditorStorePlugin.PLUGIN_ID,
120
		            e,
121
		            true);
122
		}
123
	}
124

    
125
	public static void openEmpty(UUID parentNodeUuid) {
126
	    EditorUtil.openEmptyE4(parentNodeUuid);
127
	}
128

    
129
	public static IUndoContext getUndoContext() {
130
		// FIXME this has to be more specific. Every widget has to have its own undo context
131
//		return IOperationHistory.GLOBAL_UNDO_CONTEXT;
132

    
133
		// Plug-ins that wish their operations to be undoable from workbench views
134
		// such as the Navigator or Package Explorer should assign the workbench
135
		// undo context to their operations.
136
		if (defaultUndoContext == null) {
137
			defaultUndoContext = new UndoContext();
138
		}
139
		return defaultUndoContext;
140
	}
141

    
142
	/**
143
	 * Whether a taxonNode has unsaved changes.
144
	 *
145
	 * @param taxonNode a {@link eu.etaxonomy.cdm.model.taxon.TaxonNode} object.
146
	 * @return a boolean.
147
	 */
148
	public static boolean isDirty(TaxonNodeDto taxonNode, EPartService partService){
149

    
150
	    Collection<MPart> dirtyParts = partService.getDirtyParts();
151
	    for (MPart part : dirtyParts) {
152
            if(part.getObject() instanceof TaxonNameEditor){
153
                TaxonEditorInput input = ((TaxonNameEditor) part.getObject()).getEditorInput();
154
                if(input.getTaxonNode().getUuid().equals(taxonNode.getUuid())){
155
                    return true;
156
                }
157
            }
158
        }
159
		return false;
160
	}
161

    
162
	/**
163
     * Whether a taxonNode has unsaved changes.
164
     *
165
     * @param taxonNode a {@link eu.etaxonomy.cdm.model.taxon.TaxonNode} object.
166
     * @return a boolean.
167
     */
168
    public static boolean isDirty(TaxonNode taxonNode, EPartService partService){
169

    
170
        Collection<MPart> dirtyParts = partService.getDirtyParts();
171
        for (MPart part : dirtyParts) {
172
            if(part.getObject() instanceof TaxonNameEditor){
173
                TaxonEditorInput input = ((TaxonNameEditor) part.getObject()).getEditorInput();
174
                if(input.getTaxonNode().equals(taxonNode)){
175
                    return true;
176
                }
177
            }
178
        }
179
        return false;
180
    }
181

    
182
	public static void openSearch(Object selection) {
183
		if(selection instanceof Taxon){
184
			Taxon taxon = (Taxon) selection;
185

    
186
			handleOpeningOfMultipleTaxonNodes(taxon.getTaxonNodes());
187

    
188
		}else if(selection instanceof Synonym){
189
			Synonym synonym = (Synonym) selection;
190

    
191
			Set<Taxon> accTaxa = new HashSet<Taxon>();
192
			if (synonym.getAcceptedTaxon() != null){
193
				accTaxa.add(synonym.getAcceptedTaxon());
194
			}
195
			handleOpeningOfMultipleTaxa(accTaxa);
196

    
197
		}else{
198
			MessagingUtils.warningDialog(NOT_IMPLEMENTED_YET, NavigationUtil.class, Messages.NavigationUtil_ORPHAN_NAME_MESSAGE);
199
		}
200

    
201
	}
202

    
203
	private static void handleOpeningOfMultipleTaxa(Set<Taxon> acceptedTaxa) {
204
	    //FIXME E4 migrate/delete
205
//		if(acceptedTaxa.size() == 1){
206
//			openEditor(acceptedTaxa.iterator().next(), PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
207
//		}else if(acceptedTaxa.size() > 1){
208
//			// FIXME implement a dialog that shows all possible taxa and let the user choose which he wants to open.
209
//			MessagingUtils.warningDialog(NOT_IMPLEMENTED_YET, NavigationUtil.class, Messages.NavigationUtil_MULTI_TREE);
210
//		}else if(acceptedTaxa.size() == 0){
211
//			// this is an undesired state
212
//			MessagingUtils.warningDialog(NOT_IMPLEMENTED_YET, NavigationUtil.class, Messages.NavigationUtil_ORPHAN_TAXON);
213
//		}
214
	}
215

    
216
	/**
217
	 * @param taxonNodes
218
	 */
219
	private static void handleOpeningOfMultipleTaxonNodes(
220
			Set<TaxonNode> taxonNodes) {
221
	    //FIXME E4 migrate/delete
222
//
223
//		if(taxonNodes.size() == 1){
224
//			openEditor(taxonNodes.iterator().next(), PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
225
//		}else if(taxonNodes.size() > 1){
226
//			// FIXME implement a dialog that shows all possible taxa and let the user choose which he wants to open.
227
//			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.");
228
//		}else if(taxonNodes.size() == 0){
229
//			// this is an undesired state
230
//			MessagingUtils.warningDialog(Messages.NavigationUtil_INCORRECT_STATE, NavigationUtil.class, Messages.NavigationUtil_INCORRECT_STATE_MESSAGE);
231
//		}
232
	}
233

    
234
	public static String getPluginId(){
235
		return TaxeditorNavigationPlugin.PLUGIN_ID;
236
	}
237
}
(2-2/5)