Project

General

Profile

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

    
18
import org.eclipse.core.commands.Command;
19
import org.eclipse.core.commands.ParameterizedCommand;
20
import org.eclipse.core.commands.common.NotDefinedException;
21
import org.eclipse.core.commands.operations.IUndoContext;
22
import org.eclipse.core.commands.operations.UndoContext;
23
import org.eclipse.jface.viewers.StructuredSelection;
24
import org.eclipse.jface.wizard.WizardDialog;
25
import org.eclipse.swt.widgets.Display;
26
import org.eclipse.swt.widgets.Shell;
27
import org.eclipse.ui.IEditorPart;
28
import org.eclipse.ui.IEditorReference;
29
import org.eclipse.ui.IWorkbenchWindow;
30
import org.eclipse.ui.PartInitException;
31
import org.eclipse.ui.PlatformUI;
32
import org.eclipse.ui.commands.ICommandService;
33
import org.eclipse.ui.handlers.IHandlerService;
34
import org.eclipse.ui.navigator.CommonViewer;
35

    
36
import eu.etaxonomy.cdm.api.service.IClassificationService;
37
import eu.etaxonomy.cdm.api.service.INameService;
38
import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
39
import eu.etaxonomy.cdm.api.service.ITaxonService;
40
import eu.etaxonomy.cdm.model.common.ICdmBase;
41
import eu.etaxonomy.cdm.model.description.PolytomousKey;
42
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
43
import eu.etaxonomy.cdm.model.taxon.Classification;
44
import eu.etaxonomy.cdm.model.taxon.Synonym;
45
import eu.etaxonomy.cdm.model.taxon.Taxon;
46
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
47
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
48
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
49
import eu.etaxonomy.taxeditor.editor.EditorUtil;
50
import eu.etaxonomy.taxeditor.editor.TaxonEditorInput;
51
import eu.etaxonomy.taxeditor.editor.internal.TaxeditorEditorPlugin;
52
import eu.etaxonomy.taxeditor.model.AbstractUtility;
53
import eu.etaxonomy.taxeditor.model.MessagingUtils;
54
import eu.etaxonomy.taxeditor.navigation.internal.TaxeditorNavigationPlugin;
55
import eu.etaxonomy.taxeditor.navigation.navigator.TaxonNavigator;
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
	private static IUndoContext defaultUndoContext;
69

    
70
	/**
71
	 * <p>openEditor</p>
72
	 *
73
	 * @param selectedObject a {@link eu.etaxonomy.cdm.model.common.CdmBase} object.
74
	 */
75
	public static void openEditor(UuidAndTitleCache uuidAndTitleCache){
76
	    Class type = uuidAndTitleCache.getType();
77
	    ICdmBase cdmBase = null;
78
	    if(type.equals(Classification.class)){
79
	        cdmBase = CdmStore.getService(IClassificationService.class).load(uuidAndTitleCache.getUuid());
80
	    }
81
	    else if(type.equals(TaxonNode.class)){
82
	    	cdmBase = CdmStore.getService(ITaxonNodeService.class).load(uuidAndTitleCache.getUuid());
83
	    }
84
	    else if(TaxonBase.class.isAssignableFrom(type)){
85
	    	cdmBase = CdmStore.getService(ITaxonService.class).load(uuidAndTitleCache.getUuid());
86
	    }
87
	    else if(type.equals(TaxonNameBase.class)){
88
	    	cdmBase = CdmStore.getService(INameService.class).load(uuidAndTitleCache.getUuid());
89
	    }
90
	    else{
91
	        MessagingUtils.warningDialog("Unknown type", NavigationUtil.class, "There is no editor available to open this object.");
92
	    }
93
	    if(cdmBase!=null){
94
	    	openEditor(cdmBase);
95
	    }
96
	    else{
97
	    	MessagingUtils.warningDialog("Cdm entity not found", NavigationUtil.class, "CDM entity could not be found in the database.");
98
	    }
99
	}
100

    
101
	public static void openEditor(ICdmBase selectedObject){
102
		UUID entityUuid = selectedObject.getUuid();
103
		try {
104
			if(selectedObject instanceof Classification){
105
		        NewClassificationWizard classificationWizard = new NewClassificationWizard();
106
		        classificationWizard.init(null, null);
107
		        classificationWizard.setEntity((Classification) selectedObject);
108
		        WizardDialog dialog = new WizardDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), classificationWizard);
109
		        dialog.open();
110
			}
111
			else if(selectedObject instanceof TaxonNode){
112
				EditorUtil.openTaxonNode(entityUuid);
113
			}else if(selectedObject instanceof TaxonBase){
114
				TaxonBase taxonBase = (TaxonBase)selectedObject;
115
				if(taxonBase.isOrphaned()){
116
					openInBulkEditor(taxonBase);
117
				}
118
				else{
119
					EditorUtil.openTaxonBase(entityUuid);
120
				}
121
			}else if(selectedObject instanceof TaxonNameBase){
122
				openInBulkEditor(selectedObject);
123
			}else if(selectedObject instanceof PolytomousKey){
124
				EditorUtil.openPolytomousKey(entityUuid);
125
			}else{
126
				MessagingUtils.warningDialog("Unsupported Type", NavigationUtil.class, "No editor exists for the current selection: " + selectedObject);
127
			}
128
		} catch (PartInitException e) {
129
			MessagingUtils.error(NavigationUtil.class, "Error opening the editor", e);
130
		} catch (Exception e) {
131
		    MessagingUtils.errorDialog("Could not create Taxon",
132
		            NavigationUtil.class,
133
		            e.getMessage(), TaxeditorStorePlugin.PLUGIN_ID,
134
		            e,
135
		            true);
136

    
137
		}
138
	}
139

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

    
166
	/**
167
	 * <p>openEmpty</p>
168
	 *
169
	 * @param parentNodeUuid a {@link java.util.UUID} object.
170
	 */
171
	public static void openEmpty(UUID parentNodeUuid) {
172
		try {
173
			EditorUtil.openEmpty(parentNodeUuid);
174
		} catch (PartInitException e) {
175
			MessagingUtils.error(NavigationUtil.class, "Error opening the editor", e);
176
		}
177
	}
178

    
179
	/**
180
	 * <p>getShell</p>
181
	 *
182
	 * @return a {@link org.eclipse.swt.widgets.Shell} object.
183
	 */
184
	public static Shell getShell() {
185
		return getActiveWindow().getShell();
186
	}
187

    
188
	/**
189
	 * <p>getActiveWindow</p>
190
	 *
191
	 * @return a {@link org.eclipse.ui.IWorkbenchWindow} object.
192
	 */
193
	public static IWorkbenchWindow getActiveWindow() {
194
		return TaxeditorNavigationPlugin.getDefault().getWorkbench().
195
				getActiveWorkbenchWindow();
196
	}
197

    
198
	/**
199
	 * <p>getWorkbenchUndoContext</p>
200
	 *
201
	 * @return a {@link org.eclipse.core.commands.operations.IUndoContext} object.
202
	 */
203
	public static IUndoContext getWorkbenchUndoContext() {
204
		return TaxeditorEditorPlugin.getDefault().getWorkbench().
205
					getOperationSupport().getUndoContext();
206
	}
207

    
208
	/**
209
	 * <p>getUndoContext</p>
210
	 *
211
	 * @return a {@link org.eclipse.core.commands.operations.IUndoContext} object.
212
	 */
213
	public static IUndoContext getUndoContext() {
214
		// FIXME this has to be more specific. Every widget has to have its own undo context
215
//		return IOperationHistory.GLOBAL_UNDO_CONTEXT;
216

    
217
		// Plug-ins that wish their operations to be undoable from workbench views
218
		// such as the Navigator or Package Explorer should assign the workbench
219
		// undo context to their operations.
220
		if (defaultUndoContext == null) {
221
			defaultUndoContext = new UndoContext();
222
		}
223
		return defaultUndoContext;
224
	}
225

    
226
	/**
227
	 * Whether a taxonNode has unsaved changes.
228
	 *
229
	 * @param taxonNode a {@link eu.etaxonomy.cdm.model.taxon.TaxonNode} object.
230
	 * @return a boolean.
231
	 */
232
	public static boolean isDirty(TaxonNode taxonNode){
233

    
234
		for (IEditorReference reference : getActivePage().getEditorReferences()) {
235

    
236
			try {
237
				if (reference.getEditorInput() instanceof TaxonEditorInput) {
238
					TaxonEditorInput editorInput = (TaxonEditorInput) reference.getEditorInput();
239
					if(editorInput.getTaxonNode().equals(taxonNode) && reference.isDirty()){
240
						return true;
241
					}
242
				}
243
			} catch (PartInitException e) {
244
				MessagingUtils.error(NavigationUtil.class, e.getMessage(), e);
245
				throw new RuntimeException(e);
246
			}
247

    
248
		}
249
		return false;
250
	}
251

    
252
	/**
253
	 * <p>selectInNavigator</p>
254
	 *
255
	 * @param element a {@link java.lang.Object} object.
256
	 * @param parentElement a {@link java.lang.Object} object.
257
	 */
258
	public static void selectInNavigator(final Object element, final Object parentElement) {
259
		Display.getDefault().asyncExec(new Runnable(){
260

    
261
			@Override
262
            public void run() {
263
				TaxonNavigator navigator = showNavigator();
264

    
265
				if (navigator != null) {
266
					CommonViewer viewer = navigator.getCommonViewer();
267
					if (viewer != null) {
268
						if (parentElement != null) {
269
							viewer.setExpandedState(parentElement, true);
270
						}
271
						viewer.setSelection(new StructuredSelection(element));
272
					}
273
				}
274
			}
275

    
276
		});
277
	}
278

    
279
	/**
280
	 * <p>openSearch</p>
281
	 *
282
	 * @param selection a {@link java.lang.Object} object.
283
	 */
284
	public static void openSearch(Object selection) {
285
		if(selection instanceof Taxon){
286
			Taxon taxon = (Taxon) selection;
287

    
288
			handleOpeningOfMultipleTaxonNodes(taxon.getTaxonNodes());
289

    
290
		}else if(selection instanceof Synonym){
291
			Synonym synonym = (Synonym) selection;
292

    
293
			Set<Taxon> accTaxa = new HashSet<Taxon>();
294
			if (synonym.getAcceptedTaxon() != null){
295
				accTaxa.add(synonym.getAcceptedTaxon());
296
			}
297
			handleOpeningOfMultipleTaxa(accTaxa);
298

    
299
		}else{
300
			MessagingUtils.warningDialog("Not implemented yet", NavigationUtil.class, "You chose to open a name that has no connection to a taxon. The Editor does not support editing of such a content type at the moment.");
301
		}
302

    
303
	}
304

    
305
	private static void handleOpeningOfMultipleTaxa(Set<Taxon> acceptedTaxa) {
306
		if(acceptedTaxa.size() == 1){
307
			openEditor(acceptedTaxa.iterator().next());
308
		}else if(acceptedTaxa.size() > 1){
309
			// FIXME implement a dialog that shows all possible taxa and let the user choose which he wants to open.
310
			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." +
311
					" This case is not handled yet by the software.");
312
		}else if(acceptedTaxa.size() == 0){
313
			// this is an undesired state
314
			MessagingUtils.warningDialog("Not implemented yet", NavigationUtil.class, "This taxon is not connected to a classification. Currently editing of such taxa is not supported yet.");
315
		}
316
	}
317

    
318
	/**
319
	 * @param taxonNodes
320
	 */
321
	private static void handleOpeningOfMultipleTaxonNodes(
322
			Set<TaxonNode> taxonNodes) {
323

    
324
		if(taxonNodes.size() == 1){
325
			openEditor(taxonNodes.iterator().next());
326
		}else if(taxonNodes.size() > 1){
327
			// FIXME implement a dialog that shows all possible taxa and let the user choose which he wants to open.
328
			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." +
329
					" This case is not handled yet by the software.");
330
		}else if(taxonNodes.size() == 0){
331
			// this is an undesired state
332
			MessagingUtils.warningDialog("Incorrect state", NavigationUtil.class, "The accepted taxon is not in a taxonomic view. This should not have happened.");
333
		}
334
	}
335

    
336
	/**
337
	 * <p>showNavigator</p>
338
	 *
339
	 * @return the TaxonNavigator instance if present
340
	 */
341
	public static TaxonNavigator showNavigator() {
342
		return (TaxonNavigator) showView(TaxonNavigator.ID);
343
	}
344

    
345
	/**
346
	 * <p>getNavigator</p>
347
	 *
348
	 * @param restore a boolean.
349
	 * @return a {@link eu.etaxonomy.taxeditor.navigation.navigator.TaxonNavigator} object.
350
	 */
351
	public static TaxonNavigator getNavigator(boolean restore) {
352
		return (TaxonNavigator) getView(TaxonNavigator.ID, restore);
353
	}
354

    
355
	/**
356
	 * <p>getOpenEditors</p>
357
	 *
358
	 * @return a {@link java.util.Set} object.
359
	 */
360
	public static Set<IEditorPart> getOpenEditors() {
361
		return EditorUtil.getOpenEditors();
362
	}
363

    
364
	/**
365
	 * <p>getPluginId</p>
366
	 *
367
	 * @return a {@link java.lang.String} object.
368
	 */
369
	public static String getPluginId(){
370
		return TaxeditorNavigationPlugin.PLUGIN_ID;
371
	}
372

    
373
}
(1-1/4)