2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.taxeditor
.navigation
;
13 import java
.util
.UUID
;
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
;
28 import eu
.etaxonomy
.cdm
.model
.common
.ICdmBase
;
29 import eu
.etaxonomy
.cdm
.model
.description
.PolytomousKey
;
30 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
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
.taxeditor
.editor
.EditorUtil
;
37 import eu
.etaxonomy
.taxeditor
.editor
.TaxonEditorInput
;
38 import eu
.etaxonomy
.taxeditor
.editor
.internal
.TaxeditorEditorPlugin
;
39 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
40 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
41 import eu
.etaxonomy
.taxeditor
.navigation
.internal
.TaxeditorNavigationPlugin
;
42 import eu
.etaxonomy
.taxeditor
.navigation
.navigator
.TaxonNavigator
;
43 import eu
.etaxonomy
.taxeditor
.newWizard
.NewClassificationWizard
;
44 import eu
.etaxonomy
.taxeditor
.store
.internal
.TaxeditorStorePlugin
;
47 * <p>NavigationUtil class.</p>
53 public class NavigationUtil
extends AbstractUtility
{
54 private static IUndoContext defaultUndoContext
;
59 * @param selectedObject a {@link eu.etaxonomy.cdm.model.common.CdmBase} object.
61 public static void openEditor(ICdmBase selectedObject
){
62 UUID entityUuid
= selectedObject
.getUuid();
64 if(selectedObject
instanceof Classification
){
65 NewClassificationWizard classificationWizard
= new NewClassificationWizard();
66 classificationWizard
.init(null, null);
67 classificationWizard
.setEntity((Classification
) selectedObject
);
68 WizardDialog dialog
= new WizardDialog(PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getShell(), classificationWizard
);
71 else if(selectedObject
instanceof TaxonNode
){
72 EditorUtil
.openTaxonNode(entityUuid
);
73 }else if(selectedObject
instanceof TaxonBase
){
74 EditorUtil
.openTaxonBase(entityUuid
);
75 }else if(selectedObject
instanceof TaxonNameBase
){
76 // TODO open bulk editor
77 MessagingUtils
.warningDialog("Not implemented yet", NavigationUtil
.class, "You tried to open a name. This is not handled by the software yet.");
78 }else if(selectedObject
instanceof PolytomousKey
){
79 EditorUtil
.openPolytomousKey(entityUuid
);
81 MessagingUtils
.warningDialog("Unsupported Type", NavigationUtil
.class, "No editor exists for the current selection: " + selectedObject
);
83 } catch (PartInitException e
) {
84 MessagingUtils
.error(NavigationUtil
.class, "Error opening the editor", e
);
85 } catch (Exception e
) {
86 MessagingUtils
.errorDialog("Could not create Taxon",
88 e
.getMessage(), TaxeditorStorePlugin
.PLUGIN_ID
,
98 * @param parentNodeUuid a {@link java.util.UUID} object.
100 public static void openEmpty(UUID parentNodeUuid
) {
102 EditorUtil
.openEmpty(parentNodeUuid
);
103 } catch (PartInitException e
) {
104 MessagingUtils
.error(NavigationUtil
.class, "Error opening the editor", e
);
111 * @return a {@link org.eclipse.swt.widgets.Shell} object.
113 public static Shell
getShell() {
114 return getActiveWindow().getShell();
118 * <p>getActiveWindow</p>
120 * @return a {@link org.eclipse.ui.IWorkbenchWindow} object.
122 public static IWorkbenchWindow
getActiveWindow() {
123 return TaxeditorNavigationPlugin
.getDefault().getWorkbench().
124 getActiveWorkbenchWindow();
128 * <p>getWorkbenchUndoContext</p>
130 * @return a {@link org.eclipse.core.commands.operations.IUndoContext} object.
132 public static IUndoContext
getWorkbenchUndoContext() {
133 return TaxeditorEditorPlugin
.getDefault().getWorkbench().
134 getOperationSupport().getUndoContext();
138 * <p>getUndoContext</p>
140 * @return a {@link org.eclipse.core.commands.operations.IUndoContext} object.
142 public static IUndoContext
getUndoContext() {
143 // FIXME this has to be more specific. Every widget has to have its own undo context
144 // return IOperationHistory.GLOBAL_UNDO_CONTEXT;
146 // Plug-ins that wish their operations to be undoable from workbench views
147 // such as the Navigator or Package Explorer should assign the workbench
148 // undo context to their operations.
149 if (defaultUndoContext
== null) {
150 defaultUndoContext
= new UndoContext();
152 return defaultUndoContext
;
156 * Whether a taxonNode has unsaved changes.
158 * @param taxonNode a {@link eu.etaxonomy.cdm.model.taxon.TaxonNode} object.
161 public static boolean isDirty(TaxonNode taxonNode
){
163 for (IEditorReference reference
: getActivePage().getEditorReferences()) {
166 if (reference
.getEditorInput() instanceof TaxonEditorInput
) {
167 TaxonEditorInput editorInput
= (TaxonEditorInput
) reference
.getEditorInput();
168 if(editorInput
.getTaxonNode().equals(taxonNode
) && reference
.isDirty()){
172 } catch (PartInitException e
) {
173 MessagingUtils
.error(NavigationUtil
.class, e
.getMessage(), e
);
174 throw new RuntimeException(e
);
182 * <p>selectInNavigator</p>
184 * @param element a {@link java.lang.Object} object.
185 * @param parentElement a {@link java.lang.Object} object.
187 public static void selectInNavigator(final Object element
, final Object parentElement
) {
188 Display
.getDefault().asyncExec(new Runnable(){
192 TaxonNavigator navigator
= showNavigator();
194 if (navigator
!= null) {
195 CommonViewer viewer
= navigator
.getCommonViewer();
196 if (viewer
!= null) {
197 if (parentElement
!= null) {
198 viewer
.setExpandedState(parentElement
, true);
200 viewer
.setSelection(new StructuredSelection(element
));
211 * @param selection a {@link java.lang.Object} object.
213 public static void openSearch(Object selection
) {
214 if(selection
instanceof Taxon
){
215 Taxon taxon
= (Taxon
) selection
;
217 handleOpeningOfMultipleTaxonNodes(taxon
.getTaxonNodes());
219 }else if(selection
instanceof Synonym
){
220 Synonym synonym
= (Synonym
) selection
;
222 handleOpeningOfMultipleTaxa(synonym
.getAcceptedTaxa());
225 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.");
230 private static void handleOpeningOfMultipleTaxa(Set
<Taxon
> acceptedTaxa
) {
231 if(acceptedTaxa
.size() == 1){
232 openEditor(acceptedTaxa
.iterator().next());
233 }else if(acceptedTaxa
.size() > 1){
234 // FIXME implement a dialog that shows all possible taxa and let the user choose which he wants to open.
235 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." +
236 " This case is not handled yet by the software.");
237 }else if(acceptedTaxa
.size() == 0){
238 // this is an undesired state
239 MessagingUtils
.warningDialog("Not implemented yet", NavigationUtil
.class, "This taxon is not connected to a classification. Currently editing of such taxa is not supported yet.");
246 private static void handleOpeningOfMultipleTaxonNodes(
247 Set
<TaxonNode
> taxonNodes
) {
249 if(taxonNodes
.size() == 1){
250 openEditor(taxonNodes
.iterator().next());
251 }else if(taxonNodes
.size() > 1){
252 // FIXME implement a dialog that shows all possible taxa and let the user choose which he wants to open.
253 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." +
254 " This case is not handled yet by the software.");
255 }else if(taxonNodes
.size() == 0){
256 // this is an undesired state
257 MessagingUtils
.warningDialog("Incorrect state", NavigationUtil
.class, "The accepted taxon is not in a taxonomic view. This should not have happened.");
262 * <p>showNavigator</p>
264 * @return the TaxonNavigator instance if present
266 public static TaxonNavigator
showNavigator() {
267 return (TaxonNavigator
) showView(TaxonNavigator
.ID
);
271 * <p>getNavigator</p>
273 * @param restore a boolean.
274 * @return a {@link eu.etaxonomy.taxeditor.navigation.navigator.TaxonNavigator} object.
276 public static TaxonNavigator
getNavigator(boolean restore
) {
277 return (TaxonNavigator
) getView(TaxonNavigator
.ID
, restore
);
281 * <p>getOpenEditors</p>
283 * @return a {@link java.util.Set} object.
285 public static Set
<IEditorPart
> getOpenEditors() {
286 return EditorUtil
.getOpenEditors();
292 * @return a {@link java.lang.String} object.
294 public static String
getPluginId(){
295 return TaxeditorNavigationPlugin
.PLUGIN_ID
;