Project

General

Profile

Download (11.8 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.HashMap;
14
import java.util.HashSet;
15
import java.util.Map;
16
import java.util.Set;
17
import java.util.UUID;
18

    
19
import org.eclipse.core.commands.Command;
20
import org.eclipse.core.commands.ParameterizedCommand;
21
import org.eclipse.core.commands.common.NotDefinedException;
22
import org.eclipse.core.commands.operations.IUndoContext;
23
import org.eclipse.core.commands.operations.UndoContext;
24
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
25
import org.eclipse.e4.ui.workbench.modeling.EPartService;
26
import org.eclipse.jface.wizard.WizardDialog;
27
import org.eclipse.swt.widgets.Shell;
28
import org.eclipse.ui.IWorkbenchWindow;
29
import org.eclipse.ui.PartInitException;
30
import org.eclipse.ui.PlatformUI;
31
import org.eclipse.ui.commands.ICommandService;
32
import org.eclipse.ui.handlers.IHandlerService;
33

    
34
import eu.etaxonomy.cdm.api.service.IClassificationService;
35
import eu.etaxonomy.cdm.api.service.INameService;
36
import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
37
import eu.etaxonomy.cdm.api.service.ITaxonService;
38
import eu.etaxonomy.cdm.model.common.ICdmBase;
39
import eu.etaxonomy.cdm.model.description.PolytomousKey;
40
import eu.etaxonomy.cdm.model.name.TaxonName;
41
import eu.etaxonomy.cdm.model.taxon.Classification;
42
import eu.etaxonomy.cdm.model.taxon.Synonym;
43
import eu.etaxonomy.cdm.model.taxon.Taxon;
44
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
45
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
46
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
47
import eu.etaxonomy.taxeditor.editor.EditorUtil;
48
import eu.etaxonomy.taxeditor.editor.e4.TaxonEditorInputE4;
49
import eu.etaxonomy.taxeditor.editor.internal.TaxeditorEditorPlugin;
50
import eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditorE4;
51
import eu.etaxonomy.taxeditor.model.AbstractUtility;
52
import eu.etaxonomy.taxeditor.model.MessagingUtils;
53
import eu.etaxonomy.taxeditor.navigation.internal.TaxeditorNavigationPlugin;
54
import eu.etaxonomy.taxeditor.navigation.l10n.Messages;
55
import eu.etaxonomy.taxeditor.navigation.navigator.e4.TaxonNavigatorE4;
56
import eu.etaxonomy.taxeditor.newWizard.NewClassificationWizard;
57
import eu.etaxonomy.taxeditor.store.CdmStore;
58
import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
59

    
60
/**
61
 * <p>NavigationUtil class.</p>
62
 *
63
 * @author n.hoffmann
64
 * @created 24.03.2009
65
 * @version 1.0
66
 */
67
public class NavigationUtil extends AbstractUtility{
68

    
69
    private static final String NOT_IMPLEMENTED_YET = Messages.NavigationUtil_NOT_IMPLEMENTED;
70
    private static final String ERROR_OPENING_THE_EDITOR = Messages.NavigationUtil_OPEN_ERROR;
71
    private static IUndoContext defaultUndoContext;
72

    
73
	/**
74
	 * <p>openEditor</p>
75
	 *
76
	 * @param selectedObject a {@link eu.etaxonomy.cdm.model.common.CdmBase} object.
77
	 */
78
	public static void openEditor(UuidAndTitleCache uuidAndTitleCache){
79
	    Class<?> type = uuidAndTitleCache.getType();
80
	    ICdmBase cdmBase = null;
81
	    if(type.equals(Classification.class)){
82
	        cdmBase = CdmStore.getService(IClassificationService.class).load(uuidAndTitleCache.getUuid());
83
	    }
84
	    else if(type.equals(TaxonNode.class)){
85
	    	cdmBase = CdmStore.getService(ITaxonNodeService.class).load(uuidAndTitleCache.getUuid());
86
	    }
87
	    else if(TaxonBase.class.isAssignableFrom(type)){
88
	    	cdmBase = CdmStore.getService(ITaxonService.class).load(uuidAndTitleCache.getUuid());
89
	    }
90
	    else if(type.equals(TaxonName.class)){
91
	    	cdmBase = CdmStore.getService(INameService.class).load(uuidAndTitleCache.getUuid());
92
	    }
93
	    else{
94
	        MessagingUtils.warningDialog(Messages.NavigationUtil_UNKNOWN_TYPE, NavigationUtil.class, Messages.NavigationUtil_UNKNOWN_TYPE_MESSAGE);
95
	    }
96
	    if(cdmBase!=null){
97
	    	openEditor(cdmBase, PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
98
	    }
99
	    else{
100
	    	MessagingUtils.warningDialog(Messages.NavigationUtil_NOT_FOUND, NavigationUtil.class, Messages.NavigationUtil_NOT_FOUND_MESSAGE);
101
	    }
102
	}
103

    
104
	public static void openEditor(ICdmBase selectedObject, Shell shell){
105
		UUID entityUuid = selectedObject.getUuid();
106
		try {
107
			if(selectedObject instanceof TaxonNode){
108
			    TaxonNode taxonNode = (TaxonNode)selectedObject;
109
			    Classification classification = taxonNode.getClassification();
110
			    if(classification!=null && classification.getRootNode().equals(taxonNode)){
111
			        NewClassificationWizard classificationWizard = new NewClassificationWizard();
112
			        classificationWizard.init(null, null);
113
			        classificationWizard.setEntity(classification);
114
			        WizardDialog dialog = new WizardDialog(shell, classificationWizard);
115
			        dialog.open();
116
			    }
117
			    else{
118
			        EditorUtil.openTaxonNodeE4(entityUuid);
119
			    }
120
			}else if(selectedObject instanceof TaxonBase){
121
				TaxonBase taxonBase = (TaxonBase)selectedObject;
122
				if(taxonBase.isOrphaned()){
123
					openInBulkEditor(taxonBase);
124
				}
125
				else{
126
					EditorUtil.openTaxonBaseE4(entityUuid);
127
				}
128
			}else if(selectedObject instanceof TaxonName){
129
				openInBulkEditor(selectedObject);
130
			}else if(selectedObject instanceof PolytomousKey){
131
				EditorUtil.openPolytomousKey(entityUuid);
132
			}else{
133
				MessagingUtils.warningDialog(Messages.NavigationUtil_UNSUPPORTED_TYPE, NavigationUtil.class, Messages.NavigationUtil_UNSUPPORTED_TYPE_MESSAGE + selectedObject);
134
			}
135
		} catch (PartInitException e) {
136
			MessagingUtils.error(NavigationUtil.class, ERROR_OPENING_THE_EDITOR, e);
137
		} catch (Exception e) {
138
		    MessagingUtils.errorDialog(Messages.NavigationUtil_CREATE_FAILED,
139
		            NavigationUtil.class,
140
		            e.getMessage(), TaxeditorStorePlugin.PLUGIN_ID,
141
		            e,
142
		            true);
143

    
144
		}
145
	}
146

    
147
	private static void openInBulkEditor(ICdmBase selectedObject) {
148
		ICommandService commandService = PlatformUI.getWorkbench().getService(ICommandService.class);
149
		IHandlerService handlerService = PlatformUI.getWorkbench().getService(IHandlerService.class);
150
		String openInBulkEditorCommand = "eu.etaxonomy.taxeditor.bulkeditor.openBulkEditorForIdentifiableEntity"; //$NON-NLS-1$
151
		Command command = commandService.getCommand(openInBulkEditorCommand);
152
		if(command.isDefined()){
153
			Map<String, UUID> params = new HashMap<String, UUID>();
154
			params.put(openInBulkEditorCommand+".uuid", selectedObject.getUuid()); //$NON-NLS-1$
155
			ParameterizedCommand parameterizedCommand = ParameterizedCommand.generateCommand(command, params);
156
			try {
157
				if(parameterizedCommand!=null){
158
					handlerService.executeCommand(parameterizedCommand, null);
159
					return;
160
				}
161
				else{
162
					handlerService.executeCommand(command.getId(), null);
163
					return;
164
				}
165
			} catch (NotDefinedException nde) {
166
				throw new RuntimeException("Could not find open command: " + command.getId()); //$NON-NLS-1$
167
			} catch (Exception exception) {
168
				MessagingUtils.error(NavigationUtil.class, "An exception occured while trying to execute "+command.getId(), exception); //$NON-NLS-1$
169
			}
170
		}
171
	}
172

    
173
	/**
174
	 * <p>openEmpty</p>
175
	 *
176
	 * @param parentNodeUuid a {@link java.util.UUID} object.
177
	 */
178
	public static void openEmpty(UUID parentNodeUuid) {
179
		try {
180
			EditorUtil.openEmptyE4(parentNodeUuid);
181
		} catch (PartInitException e) {
182
			MessagingUtils.error(NavigationUtil.class, ERROR_OPENING_THE_EDITOR, e);
183
		}
184
	}
185

    
186
	/**
187
	 * <p>getShell</p>
188
	 *
189
	 * @return a {@link org.eclipse.swt.widgets.Shell} object.
190
	 */
191
	public static Shell getShell() {
192
		return getActiveWindow().getShell();
193
	}
194

    
195
	/**
196
	 * <p>getActiveWindow</p>
197
	 *
198
	 * @return a {@link org.eclipse.ui.IWorkbenchWindow} object.
199
	 */
200
	public static IWorkbenchWindow getActiveWindow() {
201
		return TaxeditorNavigationPlugin.getDefault().getWorkbench().
202
				getActiveWorkbenchWindow();
203
	}
204

    
205
	/**
206
	 * <p>getWorkbenchUndoContext</p>
207
	 *
208
	 * @return a {@link org.eclipse.core.commands.operations.IUndoContext} object.
209
	 */
210
	public static IUndoContext getWorkbenchUndoContext() {
211
		return TaxeditorEditorPlugin.getDefault().getWorkbench().
212
					getOperationSupport().getUndoContext();
213
	}
214

    
215
	/**
216
	 * <p>getUndoContext</p>
217
	 *
218
	 * @return a {@link org.eclipse.core.commands.operations.IUndoContext} object.
219
	 */
220
	public static IUndoContext getUndoContext() {
221
		// FIXME this has to be more specific. Every widget has to have its own undo context
222
//		return IOperationHistory.GLOBAL_UNDO_CONTEXT;
223

    
224
		// Plug-ins that wish their operations to be undoable from workbench views
225
		// such as the Navigator or Package Explorer should assign the workbench
226
		// undo context to their operations.
227
		if (defaultUndoContext == null) {
228
			defaultUndoContext = new UndoContext();
229
		}
230
		return defaultUndoContext;
231
	}
232

    
233
	/**
234
	 * Whether a taxonNode has unsaved changes.
235
	 *
236
	 * @param taxonNode a {@link eu.etaxonomy.cdm.model.taxon.TaxonNode} object.
237
	 * @return a boolean.
238
	 */
239
	public static boolean isDirty(TaxonNode taxonNode, EPartService partService){
240

    
241
	    Collection<MPart> dirtyParts = partService.getDirtyParts();
242
	    for (MPart part : dirtyParts) {
243
            if(part.getObject() instanceof TaxonNameEditorE4){
244
                TaxonEditorInputE4 input = ((TaxonNameEditorE4) part.getObject()).getEditorInput();
245
                if(input.getTaxonNode().equals(taxonNode)){
246
                    return true;
247
                }
248
            }
249
        }
250
		return false;
251
	}
252

    
253
	/**
254
	 * <p>openSearch</p>
255
	 *
256
	 * @param selection a {@link java.lang.Object} object.
257
	 */
258
	public static void openSearch(Object selection) {
259
		if(selection instanceof Taxon){
260
			Taxon taxon = (Taxon) selection;
261

    
262
			handleOpeningOfMultipleTaxonNodes(taxon.getTaxonNodes());
263

    
264
		}else if(selection instanceof Synonym){
265
			Synonym synonym = (Synonym) selection;
266

    
267
			Set<Taxon> accTaxa = new HashSet<Taxon>();
268
			if (synonym.getAcceptedTaxon() != null){
269
				accTaxa.add(synonym.getAcceptedTaxon());
270
			}
271
			handleOpeningOfMultipleTaxa(accTaxa);
272

    
273
		}else{
274
			MessagingUtils.warningDialog(NOT_IMPLEMENTED_YET, NavigationUtil.class, Messages.NavigationUtil_ORPHAN_NAME_MESSAGE);
275
		}
276

    
277
	}
278

    
279
	private static void handleOpeningOfMultipleTaxa(Set<Taxon> acceptedTaxa) {
280
		if(acceptedTaxa.size() == 1){
281
			openEditor(acceptedTaxa.iterator().next(), PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
282
		}else if(acceptedTaxa.size() > 1){
283
			// FIXME implement a dialog that shows all possible taxa and let the user choose which he wants to open.
284
			MessagingUtils.warningDialog(NOT_IMPLEMENTED_YET, NavigationUtil.class, Messages.NavigationUtil_MULTI_TREE);
285
		}else if(acceptedTaxa.size() == 0){
286
			// this is an undesired state
287
			MessagingUtils.warningDialog(NOT_IMPLEMENTED_YET, NavigationUtil.class, Messages.NavigationUtil_ORPHAN_TAXON);
288
		}
289
	}
290

    
291
	/**
292
	 * @param taxonNodes
293
	 */
294
	private static void handleOpeningOfMultipleTaxonNodes(
295
			Set<TaxonNode> taxonNodes) {
296

    
297
		if(taxonNodes.size() == 1){
298
			openEditor(taxonNodes.iterator().next(), PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
299
		}else if(taxonNodes.size() > 1){
300
			// FIXME implement a dialog that shows all possible taxa and let the user choose which he wants to open.
301
			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.");
302
		}else if(taxonNodes.size() == 0){
303
			// this is an undesired state
304
			MessagingUtils.warningDialog(Messages.NavigationUtil_INCORRECT_STATE, NavigationUtil.class, Messages.NavigationUtil_INCORRECT_STATE_MESSAGE);
305
		}
306
	}
307

    
308
	public static TaxonNavigatorE4 getNavigator(boolean restore) {
309
	    //FIXME E4 migrate or delete
310
	    return null;
311
//		return (TaxonNavigatorE4) getView(TaxonNavigator.ID, restore);
312
	}
313

    
314
	/**
315
	 * <p>getPluginId</p>
316
	 *
317
	 * @return a {@link java.lang.String} object.
318
	 */
319
	public static String getPluginId(){
320
		return TaxeditorNavigationPlugin.PLUGIN_ID;
321
	}
322

    
323
}
(1-1/4)