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
;
12 import java
.util
.HashSet
;
14 import java
.util
.UUID
;
16 import org
.apache
.log4j
.Logger
;
17 import org
.eclipse
.core
.commands
.ExecutionEvent
;
18 import org
.eclipse
.core
.commands
.operations
.IUndoContext
;
19 import org
.eclipse
.core
.commands
.operations
.UndoContext
;
20 import org
.eclipse
.jface
.viewers
.ISelection
;
21 import org
.eclipse
.jface
.viewers
.StructuredSelection
;
22 import org
.eclipse
.jface
.viewers
.TreeSelection
;
23 import org
.eclipse
.swt
.widgets
.Shell
;
24 import org
.eclipse
.ui
.IEditorReference
;
25 import org
.eclipse
.ui
.IWorkbenchWindow
;
26 import org
.eclipse
.ui
.PartInitException
;
27 import org
.eclipse
.ui
.handlers
.HandlerUtil
;
28 import org
.eclipse
.ui
.navigator
.CommonViewer
;
30 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
31 import eu
.etaxonomy
.cdm
.model
.taxon
.Synonym
;
32 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
33 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
34 import eu
.etaxonomy
.taxeditor
.editor
.EditorUtil
;
35 import eu
.etaxonomy
.taxeditor
.editor
.TaxonEditorInput
;
36 import eu
.etaxonomy
.taxeditor
.editor
.internal
.TaxeditorEditorPlugin
;
37 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
38 import eu
.etaxonomy
.taxeditor
.navigation
.internal
.TaxeditorNavigationPlugin
;
39 import eu
.etaxonomy
.taxeditor
.navigation
.navigator
.TaxonNavigator
;
46 public class NavigationUtil
extends AbstractUtility
{
47 private static final Logger logger
= Logger
.getLogger(NavigationUtil
.class);
48 private static IUndoContext defaultUndoContext
;
51 * FIXME Introduces a dependency on eu.etaxonomy.taxeditor.editor. This is highly undesirable.
53 * It is not clear at the moment by what mechanics we are going to open the
54 * editor. I would prefer some sort of resource model that has the editor linked
55 * to the resource type of taxonbase. Another reason to switch to CommonNavigatorFamework
59 public static void openEditor(TaxonNode selection
) {
61 UUID entityUuid
= selection
.getUuid();
62 EditorUtil
.open(entityUuid
);
63 } catch (PartInitException e
) {
64 logger
.error("Error opening the editor", e
);
68 public static Shell
getShell() {
69 return getActiveWindow().getShell();
72 public static IWorkbenchWindow
getActiveWindow() {
73 return TaxeditorNavigationPlugin
.getDefault().getWorkbench().
74 getActiveWorkbenchWindow();
77 public static void openEmpty(UUID parentNodeUuid
) {
79 EditorUtil
.openEmpty(parentNodeUuid
);
80 } catch (PartInitException e
) {
81 logger
.error("Error opening the editor", e
);
85 public static IUndoContext
getWorkbenchUndoContext() {
86 return TaxeditorEditorPlugin
.getDefault().getWorkbench().
87 getOperationSupport().getUndoContext();
90 public static IUndoContext
getUndoContext() {
91 // FIXME this has to be more specific. Every widget has to have its own undo context
92 // return IOperationHistory.GLOBAL_UNDO_CONTEXT;
94 // Plug-ins that wish their operations to be undoable from workbench views
95 // such as the Navigator or Package Explorer should assign the workbench
96 // undo context to their operations.
97 if (defaultUndoContext
== null) {
98 defaultUndoContext
= new UndoContext();
100 return defaultUndoContext
;
104 * Whether a taxonNode has unsaved changes.
109 public static boolean isDirty(TaxonNode taxonNode
){
111 for (IEditorReference reference
: getActivePage().getEditorReferences()) {
114 if (reference
.getEditorInput() instanceof TaxonEditorInput
) {
115 TaxonEditorInput editorInput
= (TaxonEditorInput
) reference
.getEditorInput();
116 if(editorInput
.getTaxonNode().equals(taxonNode
) && reference
.isDirty()){
120 } catch (PartInitException e
) {
122 throw new RuntimeException(e
);
132 * @param parentElement
134 public static void selectInNavigator(Object element
, Object parentElement
) {
135 TaxonNavigator navigator
= getNavigator();
136 if (navigator
!= null) {
137 CommonViewer viewer
= navigator
.getCommonViewer();
138 if (viewer
!= null) {
139 if (parentElement
!= null) {
140 viewer
.setExpandedState(parentElement
, true);
142 viewer
.setSelection(new StructuredSelection((TaxonNode
) element
));
150 public static void openSearch(Object selection
) {
151 if(selection
instanceof Taxon
){
152 Taxon taxon
= (Taxon
) selection
;
154 handleOpeningOfMultipleTaxonNodes(taxon
.getTaxonNodes());
156 }else if(selection
instanceof Synonym
){
157 Synonym synonym
= (Synonym
) selection
;
159 Set
<TaxonNode
> taxonNodes
= new HashSet
<TaxonNode
>();
160 for (Taxon taxon
: synonym
.getAcceptedTaxa()){
161 taxonNodes
.addAll(taxon
.getTaxonNodes());
163 handleOpeningOfMultipleTaxonNodes(taxonNodes
);
166 warningDialog("Not implemented yet", "You chose to open a name that has no connection to a taxa. The Editor does not support editing of such a content type at the moment.");
167 logger
.warn("Could not open editor for selection. Selection was of type: " + selection
.getClass().getSimpleName());
175 private static void handleOpeningOfMultipleTaxonNodes(
176 Set
<TaxonNode
> taxonNodes
) {
178 if(taxonNodes
.size() == 1){
179 openEditor(taxonNodes
.iterator().next());
180 }else if(taxonNodes
.size() > 1){
181 // FIXME implement a dialog that shows all possible taxa and let the user choose which he wants to open.
182 warningDialog("Not implemented yet", "The accepted taxon is in multiple taxonomic trees. We currently do not know which one you want to open." +
183 " This case is not handled yet by the software.");
184 }else if(taxonNodes
.size() == 0){
185 // this is an undesired state
186 warningDialog("Incorrect state", "The accepted taxon is not in a taxonomic view. This should not have happened.");
191 * @return the TaxonNavigator instance if present
193 public static TaxonNavigator
getNavigator() {
194 return (TaxonNavigator
) TaxeditorEditorPlugin
.getDefault().getWorkbench().
195 getActiveWorkbenchWindow().getActivePage().findView(TaxonNavigator
.ID
);