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
.swt
.widgets
.Display
;
19 import org
.eclipse
.swt
.widgets
.Shell
;
20 import org
.eclipse
.ui
.IEditorPart
;
21 import org
.eclipse
.ui
.IEditorReference
;
22 import org
.eclipse
.ui
.IWorkbenchWindow
;
23 import org
.eclipse
.ui
.PartInitException
;
24 import org
.eclipse
.ui
.navigator
.CommonViewer
;
26 import eu
.etaxonomy
.cdm
.model
.common
.ICdmBase
;
27 import eu
.etaxonomy
.cdm
.model
.description
.PolytomousKey
;
28 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
29 import eu
.etaxonomy
.cdm
.model
.taxon
.Synonym
;
30 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
31 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
32 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
33 import eu
.etaxonomy
.taxeditor
.editor
.EditorUtil
;
34 import eu
.etaxonomy
.taxeditor
.editor
.TaxonEditorInput
;
35 import eu
.etaxonomy
.taxeditor
.editor
.internal
.TaxeditorEditorPlugin
;
36 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
37 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
38 import eu
.etaxonomy
.taxeditor
.navigation
.internal
.TaxeditorNavigationPlugin
;
39 import eu
.etaxonomy
.taxeditor
.navigation
.navigator
.TaxonNavigator
;
40 import eu
.etaxonomy
.taxeditor
.store
.internal
.TaxeditorStorePlugin
;
43 * <p>NavigationUtil class.</p>
49 public class NavigationUtil
extends AbstractUtility
{
50 private static IUndoContext defaultUndoContext
;
55 * @param selectedObject a {@link eu.etaxonomy.cdm.model.common.CdmBase} object.
57 public static void openEditor(ICdmBase selectedObject
){
58 UUID entityUuid
= selectedObject
.getUuid();
60 if(selectedObject
instanceof TaxonNode
){
61 EditorUtil
.openTaxonNode(entityUuid
);
62 }else if(selectedObject
instanceof TaxonBase
){
63 EditorUtil
.openTaxonBase(entityUuid
);
64 }else if(selectedObject
instanceof TaxonNameBase
){
65 // TODO open bulk editor
66 MessagingUtils
.warningDialog("Not implemented yet", NavigationUtil
.class, "You tried to open a name. This is not handled by the software yet.");
67 }else if(selectedObject
instanceof PolytomousKey
){
68 EditorUtil
.openPolytomousKey(entityUuid
);
70 MessagingUtils
.warningDialog("Unsupported Type", NavigationUtil
.class, "No editor exists for the current selection: " + selectedObject
);
72 } catch (PartInitException e
) {
73 MessagingUtils
.error(NavigationUtil
.class, "Error opening the editor", e
);
74 } catch (Exception e
) {
75 MessagingUtils
.errorDialog("Could not create Taxon",
77 e
.getMessage(), TaxeditorStorePlugin
.PLUGIN_ID
,
87 * @param parentNodeUuid a {@link java.util.UUID} object.
89 public static void openEmpty(UUID parentNodeUuid
) {
91 EditorUtil
.openEmpty(parentNodeUuid
);
92 } catch (PartInitException e
) {
93 MessagingUtils
.error(NavigationUtil
.class, "Error opening the editor", e
);
100 * @return a {@link org.eclipse.swt.widgets.Shell} object.
102 public static Shell
getShell() {
103 return getActiveWindow().getShell();
107 * <p>getActiveWindow</p>
109 * @return a {@link org.eclipse.ui.IWorkbenchWindow} object.
111 public static IWorkbenchWindow
getActiveWindow() {
112 return TaxeditorNavigationPlugin
.getDefault().getWorkbench().
113 getActiveWorkbenchWindow();
117 * <p>getWorkbenchUndoContext</p>
119 * @return a {@link org.eclipse.core.commands.operations.IUndoContext} object.
121 public static IUndoContext
getWorkbenchUndoContext() {
122 return TaxeditorEditorPlugin
.getDefault().getWorkbench().
123 getOperationSupport().getUndoContext();
127 * <p>getUndoContext</p>
129 * @return a {@link org.eclipse.core.commands.operations.IUndoContext} object.
131 public static IUndoContext
getUndoContext() {
132 // FIXME this has to be more specific. Every widget has to have its own undo context
133 // return IOperationHistory.GLOBAL_UNDO_CONTEXT;
135 // Plug-ins that wish their operations to be undoable from workbench views
136 // such as the Navigator or Package Explorer should assign the workbench
137 // undo context to their operations.
138 if (defaultUndoContext
== null) {
139 defaultUndoContext
= new UndoContext();
141 return defaultUndoContext
;
145 * Whether a taxonNode has unsaved changes.
147 * @param taxonNode a {@link eu.etaxonomy.cdm.model.taxon.TaxonNode} object.
150 public static boolean isDirty(TaxonNode taxonNode
){
152 for (IEditorReference reference
: getActivePage().getEditorReferences()) {
155 if (reference
.getEditorInput() instanceof TaxonEditorInput
) {
156 TaxonEditorInput editorInput
= (TaxonEditorInput
) reference
.getEditorInput();
157 if(editorInput
.getTaxonNode().equals(taxonNode
) && reference
.isDirty()){
161 } catch (PartInitException e
) {
162 MessagingUtils
.error(NavigationUtil
.class, e
.getMessage(), e
);
163 throw new RuntimeException(e
);
171 * <p>selectInNavigator</p>
173 * @param element a {@link java.lang.Object} object.
174 * @param parentElement a {@link java.lang.Object} object.
176 public static void selectInNavigator(final Object element
, final Object parentElement
) {
177 Display
.getDefault().asyncExec(new Runnable(){
181 TaxonNavigator navigator
= showNavigator();
183 if (navigator
!= null) {
184 CommonViewer viewer
= navigator
.getCommonViewer();
185 if (viewer
!= null) {
186 if (parentElement
!= null) {
187 viewer
.setExpandedState(parentElement
, true);
189 viewer
.setSelection(new StructuredSelection(element
));
200 * @param selection a {@link java.lang.Object} object.
202 public static void openSearch(Object selection
) {
203 if(selection
instanceof Taxon
){
204 Taxon taxon
= (Taxon
) selection
;
206 handleOpeningOfMultipleTaxonNodes(taxon
.getTaxonNodes());
208 }else if(selection
instanceof Synonym
){
209 Synonym synonym
= (Synonym
) selection
;
211 handleOpeningOfMultipleTaxa(synonym
.getAcceptedTaxa());
214 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.");
219 private static void handleOpeningOfMultipleTaxa(Set
<Taxon
> acceptedTaxa
) {
220 if(acceptedTaxa
.size() == 1){
221 openEditor(acceptedTaxa
.iterator().next());
222 }else if(acceptedTaxa
.size() > 1){
223 // FIXME implement a dialog that shows all possible taxa and let the user choose which he wants to open.
224 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." +
225 " This case is not handled yet by the software.");
226 }else if(acceptedTaxa
.size() == 0){
227 // this is an undesired state
228 MessagingUtils
.warningDialog("Not implemented yet", NavigationUtil
.class, "This taxon is not connected to a classification. Currently editing of such taxa is not supported yet.");
235 private static void handleOpeningOfMultipleTaxonNodes(
236 Set
<TaxonNode
> taxonNodes
) {
238 if(taxonNodes
.size() == 1){
239 openEditor(taxonNodes
.iterator().next());
240 }else if(taxonNodes
.size() > 1){
241 // FIXME implement a dialog that shows all possible taxa and let the user choose which he wants to open.
242 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." +
243 " This case is not handled yet by the software.");
244 }else if(taxonNodes
.size() == 0){
245 // this is an undesired state
246 MessagingUtils
.warningDialog("Incorrect state", NavigationUtil
.class, "The accepted taxon is not in a taxonomic view. This should not have happened.");
251 * <p>showNavigator</p>
253 * @return the TaxonNavigator instance if present
255 public static TaxonNavigator
showNavigator() {
256 return (TaxonNavigator
) showView(TaxonNavigator
.ID
);
260 * <p>getNavigator</p>
262 * @param restore a boolean.
263 * @return a {@link eu.etaxonomy.taxeditor.navigation.navigator.TaxonNavigator} object.
265 public static TaxonNavigator
getNavigator(boolean restore
) {
266 return (TaxonNavigator
) getView(TaxonNavigator
.ID
, restore
);
270 * <p>getOpenEditors</p>
272 * @return a {@link java.util.Set} object.
274 public static Set
<IEditorPart
> getOpenEditors() {
275 return EditorUtil
.getOpenEditors();
281 * @return a {@link java.lang.String} object.
283 public static String
getPluginId(){
284 return TaxeditorNavigationPlugin
.PLUGIN_ID
;