Project

General

Profile

Download (11 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.Set;
13
import java.util.UUID;
14

    
15
import org.eclipse.core.commands.operations.IUndoContext;
16
import org.eclipse.core.commands.operations.UndoContext;
17
import org.eclipse.jface.viewers.StructuredSelection;
18
import org.eclipse.jface.wizard.WizardDialog;
19
import org.eclipse.swt.widgets.Display;
20
import org.eclipse.swt.widgets.Shell;
21
import org.eclipse.ui.IEditorPart;
22
import org.eclipse.ui.IEditorReference;
23
import org.eclipse.ui.IWorkbenchWindow;
24
import org.eclipse.ui.PartInitException;
25
import org.eclipse.ui.PlatformUI;
26
import org.eclipse.ui.navigator.CommonViewer;
27

    
28
import eu.etaxonomy.cdm.api.service.IClassificationService;
29
import eu.etaxonomy.cdm.api.service.INameService;
30
import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
31
import eu.etaxonomy.cdm.api.service.ITaxonService;
32
import eu.etaxonomy.cdm.model.common.ICdmBase;
33
import eu.etaxonomy.cdm.model.description.PolytomousKey;
34
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
35
import eu.etaxonomy.cdm.model.taxon.Classification;
36
import eu.etaxonomy.cdm.model.taxon.Synonym;
37
import eu.etaxonomy.cdm.model.taxon.Taxon;
38
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
39
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
40
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
41
import eu.etaxonomy.taxeditor.editor.EditorUtil;
42
import eu.etaxonomy.taxeditor.editor.TaxonEditorInput;
43
import eu.etaxonomy.taxeditor.editor.internal.TaxeditorEditorPlugin;
44
import eu.etaxonomy.taxeditor.model.AbstractUtility;
45
import eu.etaxonomy.taxeditor.model.MessagingUtils;
46
import eu.etaxonomy.taxeditor.navigation.internal.TaxeditorNavigationPlugin;
47
import eu.etaxonomy.taxeditor.navigation.navigator.TaxonNavigator;
48
import eu.etaxonomy.taxeditor.newWizard.NewClassificationWizard;
49
import eu.etaxonomy.taxeditor.store.CdmStore;
50
import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
51

    
52
/**
53
 * <p>NavigationUtil class.</p>
54
 *
55
 * @author n.hoffmann
56
 * @created 24.03.2009
57
 * @version 1.0
58
 */
59
public class NavigationUtil extends AbstractUtility{
60
	private static IUndoContext defaultUndoContext;
61

    
62
	/**
63
	 * <p>openEditor</p>
64
	 *
65
	 * @param selectedObject a {@link eu.etaxonomy.cdm.model.common.CdmBase} object.
66
	 */
67
	public static void openEditor(UuidAndTitleCache uuidAndTitleCache){
68
	    Class type = uuidAndTitleCache.getType();
69
	    if(type.equals(Classification.class)){
70
	        Classification classification = CdmStore.getService(IClassificationService.class).load(uuidAndTitleCache.getUuid());
71
	        openEditor(classification);
72
	    }
73
	    else if(type.equals(TaxonNode.class)){
74
	        TaxonNode taxonNode = CdmStore.getService(ITaxonNodeService.class).load(uuidAndTitleCache.getUuid());
75
	        openEditor(taxonNode);
76
	    }
77
	    else if(TaxonBase.class.isAssignableFrom(type)){
78
	        TaxonBase taxonBase = CdmStore.getService(ITaxonService.class).load(uuidAndTitleCache.getUuid());
79
	        openEditor(taxonBase);
80
	    }
81
	    else if(type.equals(TaxonNameBase.class)){
82
	        TaxonNameBase nameBase = CdmStore.getService(INameService.class).load(uuidAndTitleCache.getUuid());
83
	        openEditor(nameBase);
84
	    }
85
	    else{
86
	        MessagingUtils.warningDialog("Unknown type", NavigationUtil.class, "There is no editor available to open this object.");
87
	    }
88
	}
89

    
90
	public static void openEditor(ICdmBase selectedObject){
91
		UUID entityUuid = selectedObject.getUuid();
92
		try {
93
			if(selectedObject instanceof Classification){
94
		        NewClassificationWizard classificationWizard = new NewClassificationWizard();
95
		        classificationWizard.init(null, null);
96
		        classificationWizard.setEntity((Classification) selectedObject);
97
		        WizardDialog dialog = new WizardDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), classificationWizard);
98
		        dialog.open();
99
			}
100
			else if(selectedObject instanceof TaxonNode){
101
				EditorUtil.openTaxonNode(entityUuid);
102
			}else if(selectedObject instanceof TaxonBase){
103
				EditorUtil.openTaxonBase(entityUuid);
104
			}else if(selectedObject instanceof TaxonNameBase){
105
				// TODO open bulk editor
106
				MessagingUtils.warningDialog("Not implemented yet", NavigationUtil.class, "You tried to open a name. This is not handled by the software yet. For open a pure name you can use the bulk editor");
107
			}else if(selectedObject instanceof PolytomousKey){
108
				EditorUtil.openPolytomousKey(entityUuid);
109
			}else{
110
				MessagingUtils.warningDialog("Unsupported Type", NavigationUtil.class, "No editor exists for the current selection: " + selectedObject);
111
			}
112
		} catch (PartInitException e) {
113
			MessagingUtils.error(NavigationUtil.class, "Error opening the editor", e);
114
		} catch (Exception e) {
115
		    MessagingUtils.errorDialog("Could not create Taxon",
116
		            NavigationUtil.class,
117
		            e.getMessage(), TaxeditorStorePlugin.PLUGIN_ID,
118
		            e,
119
		            true);
120

    
121
		}
122
	}
123

    
124
	/**
125
	 * <p>openEmpty</p>
126
	 *
127
	 * @param parentNodeUuid a {@link java.util.UUID} object.
128
	 */
129
	public static void openEmpty(UUID parentNodeUuid) {
130
		try {
131
			EditorUtil.openEmpty(parentNodeUuid);
132
		} catch (PartInitException e) {
133
			MessagingUtils.error(NavigationUtil.class, "Error opening the editor", e);
134
		}
135
	}
136

    
137
	/**
138
	 * <p>getShell</p>
139
	 *
140
	 * @return a {@link org.eclipse.swt.widgets.Shell} object.
141
	 */
142
	public static Shell getShell() {
143
		return getActiveWindow().getShell();
144
	}
145

    
146
	/**
147
	 * <p>getActiveWindow</p>
148
	 *
149
	 * @return a {@link org.eclipse.ui.IWorkbenchWindow} object.
150
	 */
151
	public static IWorkbenchWindow getActiveWindow() {
152
		return TaxeditorNavigationPlugin.getDefault().getWorkbench().
153
				getActiveWorkbenchWindow();
154
	}
155

    
156
	/**
157
	 * <p>getWorkbenchUndoContext</p>
158
	 *
159
	 * @return a {@link org.eclipse.core.commands.operations.IUndoContext} object.
160
	 */
161
	public static IUndoContext getWorkbenchUndoContext() {
162
		return TaxeditorEditorPlugin.getDefault().getWorkbench().
163
					getOperationSupport().getUndoContext();
164
	}
165

    
166
	/**
167
	 * <p>getUndoContext</p>
168
	 *
169
	 * @return a {@link org.eclipse.core.commands.operations.IUndoContext} object.
170
	 */
171
	public static IUndoContext getUndoContext() {
172
		// FIXME this has to be more specific. Every widget has to have its own undo context
173
//		return IOperationHistory.GLOBAL_UNDO_CONTEXT;
174

    
175
		// Plug-ins that wish their operations to be undoable from workbench views
176
		// such as the Navigator or Package Explorer should assign the workbench
177
		// undo context to their operations.
178
		if (defaultUndoContext == null) {
179
			defaultUndoContext = new UndoContext();
180
		}
181
		return defaultUndoContext;
182
	}
183

    
184
	/**
185
	 * Whether a taxonNode has unsaved changes.
186
	 *
187
	 * @param taxonNode a {@link eu.etaxonomy.cdm.model.taxon.TaxonNode} object.
188
	 * @return a boolean.
189
	 */
190
	public static boolean isDirty(TaxonNode taxonNode){
191

    
192
		for (IEditorReference reference : getActivePage().getEditorReferences()) {
193

    
194
			try {
195
				if (reference.getEditorInput() instanceof TaxonEditorInput) {
196
					TaxonEditorInput editorInput = (TaxonEditorInput) reference.getEditorInput();
197
					if(editorInput.getTaxonNode().equals(taxonNode) && reference.isDirty()){
198
						return true;
199
					}
200
				}
201
			} catch (PartInitException e) {
202
				MessagingUtils.error(NavigationUtil.class, e.getMessage(), e);
203
				throw new RuntimeException(e);
204
			}
205

    
206
		}
207
		return false;
208
	}
209

    
210
	/**
211
	 * <p>selectInNavigator</p>
212
	 *
213
	 * @param element a {@link java.lang.Object} object.
214
	 * @param parentElement a {@link java.lang.Object} object.
215
	 */
216
	public static void selectInNavigator(final Object element, final Object parentElement) {
217
		Display.getDefault().asyncExec(new Runnable(){
218

    
219
			@Override
220
            public void run() {
221
				TaxonNavigator navigator = showNavigator();
222

    
223
				if (navigator != null) {
224
					CommonViewer viewer = navigator.getCommonViewer();
225
					if (viewer != null) {
226
						if (parentElement != null) {
227
							viewer.setExpandedState(parentElement, true);
228
						}
229
						viewer.setSelection(new StructuredSelection(element));
230
					}
231
				}
232
			}
233

    
234
		});
235
	}
236

    
237
	/**
238
	 * <p>openSearch</p>
239
	 *
240
	 * @param selection a {@link java.lang.Object} object.
241
	 */
242
	public static void openSearch(Object selection) {
243
		if(selection instanceof Taxon){
244
			Taxon taxon = (Taxon) selection;
245

    
246
			handleOpeningOfMultipleTaxonNodes(taxon.getTaxonNodes());
247

    
248
		}else if(selection instanceof Synonym){
249
			Synonym synonym = (Synonym) selection;
250

    
251
			handleOpeningOfMultipleTaxa(synonym.getAcceptedTaxa());
252

    
253
		}else{
254
			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.");
255
		}
256

    
257
	}
258

    
259
	private static void handleOpeningOfMultipleTaxa(Set<Taxon> acceptedTaxa) {
260
		if(acceptedTaxa.size() == 1){
261
			openEditor(acceptedTaxa.iterator().next());
262
		}else if(acceptedTaxa.size() > 1){
263
			// FIXME implement a dialog that shows all possible taxa and let the user choose which he wants to open.
264
			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." +
265
					" This case is not handled yet by the software.");
266
		}else if(acceptedTaxa.size() == 0){
267
			// this is an undesired state
268
			MessagingUtils.warningDialog("Not implemented yet", NavigationUtil.class, "This taxon is not connected to a classification. Currently editing of such taxa is not supported yet.");
269
		}
270
	}
271

    
272
	/**
273
	 * @param taxonNodes
274
	 */
275
	private static void handleOpeningOfMultipleTaxonNodes(
276
			Set<TaxonNode> taxonNodes) {
277

    
278
		if(taxonNodes.size() == 1){
279
			openEditor(taxonNodes.iterator().next());
280
		}else if(taxonNodes.size() > 1){
281
			// FIXME implement a dialog that shows all possible taxa and let the user choose which he wants to open.
282
			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." +
283
					" This case is not handled yet by the software.");
284
		}else if(taxonNodes.size() == 0){
285
			// this is an undesired state
286
			MessagingUtils.warningDialog("Incorrect state", NavigationUtil.class, "The accepted taxon is not in a taxonomic view. This should not have happened.");
287
		}
288
	}
289

    
290
	/**
291
	 * <p>showNavigator</p>
292
	 *
293
	 * @return the TaxonNavigator instance if present
294
	 */
295
	public static TaxonNavigator showNavigator() {
296
		return (TaxonNavigator) showView(TaxonNavigator.ID);
297
	}
298

    
299
	/**
300
	 * <p>getNavigator</p>
301
	 *
302
	 * @param restore a boolean.
303
	 * @return a {@link eu.etaxonomy.taxeditor.navigation.navigator.TaxonNavigator} object.
304
	 */
305
	public static TaxonNavigator getNavigator(boolean restore) {
306
		return (TaxonNavigator) getView(TaxonNavigator.ID, restore);
307
	}
308

    
309
	/**
310
	 * <p>getOpenEditors</p>
311
	 *
312
	 * @return a {@link java.util.Set} object.
313
	 */
314
	public static Set<IEditorPart> getOpenEditors() {
315
		return EditorUtil.getOpenEditors();
316
	}
317

    
318
	/**
319
	 * <p>getPluginId</p>
320
	 *
321
	 * @return a {@link java.lang.String} object.
322
	 */
323
	public static String getPluginId(){
324
		return TaxeditorNavigationPlugin.PLUGIN_ID;
325
	}
326

    
327
}
(1-1/4)