Project

General

Profile

Download (12.9 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.TaxonName;
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.l10n.Messages;
56
import eu.etaxonomy.taxeditor.navigation.navigator.TaxonNavigator;
57
import eu.etaxonomy.taxeditor.newWizard.NewClassificationWizard;
58
import eu.etaxonomy.taxeditor.store.CdmStore;
59
import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
60

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

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

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

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

    
147
		}
148
	}
149

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

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

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

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

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

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

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

    
236
	/**
237
	 * Whether a taxonNode has unsaved changes.
238
	 *
239
	 * @param taxonNode a {@link eu.etaxonomy.cdm.model.taxon.TaxonNode} object.
240
	 * @return a boolean.
241
	 */
242
	public static boolean isDirty(TaxonNode taxonNode){
243

    
244
		for (IEditorReference reference : getActivePage().getEditorReferences()) {
245

    
246
			try {
247
				if (reference.getEditorInput() instanceof TaxonEditorInput) {
248
					TaxonEditorInput editorInput = (TaxonEditorInput) reference.getEditorInput();
249
					if(editorInput.getTaxonNode().equals(taxonNode) && reference.isDirty()){
250
						return true;
251
					}
252
				}
253
			} catch (PartInitException e) {
254
				MessagingUtils.error(NavigationUtil.class, e.getMessage(), e);
255
				throw new RuntimeException(e);
256
			}
257

    
258
		}
259
		return false;
260
	}
261

    
262
	/**
263
	 * <p>selectInNavigator</p>
264
	 *
265
	 * @param element a {@link java.lang.Object} object.
266
	 * @param parentElement a {@link java.lang.Object} object.
267
	 */
268
	public static void selectInNavigator(final Object element, final Object parentElement) {
269
		Display.getDefault().asyncExec(new Runnable(){
270

    
271
			@Override
272
            public void run() {
273
				TaxonNavigator navigator = showNavigator();
274

    
275
				if (navigator != null) {
276
					CommonViewer viewer = navigator.getCommonViewer();
277
					if (viewer != null) {
278
						if (parentElement != null) {
279
							viewer.setExpandedState(parentElement, true);
280
						}
281
						viewer.setSelection(new StructuredSelection(element));
282
					}
283
				}
284
			}
285

    
286
		});
287
	}
288

    
289
	/**
290
	 * <p>openSearch</p>
291
	 *
292
	 * @param selection a {@link java.lang.Object} object.
293
	 */
294
	public static void openSearch(Object selection) {
295
		if(selection instanceof Taxon){
296
			Taxon taxon = (Taxon) selection;
297

    
298
			handleOpeningOfMultipleTaxonNodes(taxon.getTaxonNodes());
299

    
300
		}else if(selection instanceof Synonym){
301
			Synonym synonym = (Synonym) selection;
302

    
303
			Set<Taxon> accTaxa = new HashSet<Taxon>();
304
			if (synonym.getAcceptedTaxon() != null){
305
				accTaxa.add(synonym.getAcceptedTaxon());
306
			}
307
			handleOpeningOfMultipleTaxa(accTaxa);
308

    
309
		}else{
310
			MessagingUtils.warningDialog(NOT_IMPLEMENTED_YET, NavigationUtil.class, Messages.NavigationUtil_ORPHAN_NAME_MESSAGE);
311
		}
312

    
313
	}
314

    
315
	private static void handleOpeningOfMultipleTaxa(Set<Taxon> acceptedTaxa) {
316
		if(acceptedTaxa.size() == 1){
317
			openEditor(acceptedTaxa.iterator().next());
318
		}else if(acceptedTaxa.size() > 1){
319
			// FIXME implement a dialog that shows all possible taxa and let the user choose which he wants to open.
320
			MessagingUtils.warningDialog(NOT_IMPLEMENTED_YET, NavigationUtil.class, Messages.NavigationUtil_MULTI_TREE);
321
		}else if(acceptedTaxa.size() == 0){
322
			// this is an undesired state
323
			MessagingUtils.warningDialog(NOT_IMPLEMENTED_YET, NavigationUtil.class, Messages.NavigationUtil_ORPHAN_TAXON);
324
		}
325
	}
326

    
327
	/**
328
	 * @param taxonNodes
329
	 */
330
	private static void handleOpeningOfMultipleTaxonNodes(
331
			Set<TaxonNode> taxonNodes) {
332

    
333
		if(taxonNodes.size() == 1){
334
			openEditor(taxonNodes.iterator().next());
335
		}else if(taxonNodes.size() > 1){
336
			// FIXME implement a dialog that shows all possible taxa and let the user choose which he wants to open.
337
			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.");
338
		}else if(taxonNodes.size() == 0){
339
			// this is an undesired state
340
			MessagingUtils.warningDialog(Messages.NavigationUtil_INCORRECT_STATE, NavigationUtil.class, Messages.NavigationUtil_INCORRECT_STATE_MESSAGE);
341
		}
342
	}
343

    
344
	/**
345
	 * <p>showNavigator</p>
346
	 *
347
	 * @return the TaxonNavigator instance if present
348
	 */
349
	public static TaxonNavigator showNavigator() {
350
		return (TaxonNavigator) showView(TaxonNavigator.ID);
351
	}
352

    
353
	/**
354
	 * <p>getNavigator</p>
355
	 *
356
	 * @param restore a boolean.
357
	 * @return a {@link eu.etaxonomy.taxeditor.navigation.navigator.TaxonNavigator} object.
358
	 */
359
	public static TaxonNavigator getNavigator(boolean restore) {
360
		return (TaxonNavigator) getView(TaxonNavigator.ID, restore);
361
	}
362

    
363
	/**
364
	 * <p>getOpenEditors</p>
365
	 *
366
	 * @return a {@link java.util.Set} object.
367
	 */
368
	public static Set<IEditorPart> getOpenEditors() {
369
		return EditorUtil.getOpenEditors();
370
	}
371

    
372
	/**
373
	 * <p>getPluginId</p>
374
	 *
375
	 * @return a {@link java.lang.String} object.
376
	 */
377
	public static String getPluginId(){
378
		return TaxeditorNavigationPlugin.PLUGIN_ID;
379
	}
380

    
381
}
(1-1/4)