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
.ExecutionException
;
19 import org
.eclipse
.core
.commands
.operations
.IOperationHistory
;
20 import org
.eclipse
.core
.commands
.operations
.IUndoContext
;
21 import org
.eclipse
.core
.commands
.operations
.IUndoableOperation
;
22 import org
.eclipse
.core
.commands
.operations
.UndoContext
;
23 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
24 import org
.eclipse
.core
.runtime
.IStatus
;
25 import org
.eclipse
.core
.runtime
.Status
;
26 import org
.eclipse
.jface
.action
.IStatusLineManager
;
27 import org
.eclipse
.jface
.viewers
.ISelection
;
28 import org
.eclipse
.jface
.viewers
.StructuredSelection
;
29 import org
.eclipse
.jface
.viewers
.TreeSelection
;
30 import org
.eclipse
.swt
.widgets
.Shell
;
31 import org
.eclipse
.ui
.IEditorReference
;
32 import org
.eclipse
.ui
.IWorkbenchWindow
;
33 import org
.eclipse
.ui
.PartInitException
;
34 import org
.eclipse
.ui
.handlers
.HandlerUtil
;
35 import org
.eclipse
.ui
.ide
.undo
.WorkspaceUndoUtil
;
36 import org
.eclipse
.ui
.navigator
.CommonViewer
;
38 import eu
.etaxonomy
.cdm
.model
.taxon
.Synonym
;
39 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
40 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
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
.navigation
.internal
.TaxeditorNavigationPlugin
;
46 import eu
.etaxonomy
.taxeditor
.navigation
.navigator
.TaxonNavigator
;
53 public class NavigationUtil
extends AbstractUtility
{
54 private static final Logger logger
= Logger
.getLogger(NavigationUtil
.class);
55 private static IStatusLineManager statusLineManager
;
56 private static IUndoContext defaultUndoContext
;
59 * FIXME Introduces a dependency on eu.etaxonomy.taxeditor.editor. This is highly undesirable.
61 * It is not clear at the moment by what mechanics we are going to open the
62 * editor. I would prefer some sort of resource model that has the editor linked
63 * to the resource type of taxonbase. Another reason to switch to CommonNavigatorFamework
67 public static void openEditor(TaxonNode selection
) {
69 UUID entityUuid
= selection
.getUuid();
70 EditorUtil
.open(entityUuid
);
71 } catch (PartInitException e
) {
72 logger
.error("Error opening the editor", e
);
76 public static Shell
getShell() {
77 return getActiveWindow().getShell();
80 public static IWorkbenchWindow
getActiveWindow() {
81 return TaxeditorNavigationPlugin
.getDefault().getWorkbench().
82 getActiveWorkbenchWindow();
85 public static void openEmpty(TaxonNode parentTaxonNode
) {
87 UUID parentTaxonNodeUuid
= parentTaxonNode
== null ?
null : parentTaxonNode
.getUuid();
89 EditorUtil
.openEmpty(parentTaxonNodeUuid
);
90 } catch (PartInitException e
) {
91 logger
.error("Error opening the editor", e
);
95 public static IOperationHistory
getWorkbenchOperationHistory() {
96 return TaxeditorEditorPlugin
.getDefault().getWorkbench().
97 getOperationSupport().getOperationHistory();
100 public static IUndoContext
getWorkbenchUndoContext() {
101 return TaxeditorEditorPlugin
.getDefault().getWorkbench().
102 getOperationSupport().getUndoContext();
105 public static void executeOperation(IUndoableOperation operation
){
107 IStatus status
= getWorkbenchOperationHistory().execute(operation
, getMonitor(),
108 WorkspaceUndoUtil
.getUIInfoAdapter(getShell()));
109 String statusString
= status
.equals(Status
.OK_STATUS
) ?
"completed" : "cancelled";
110 setStatusLine(operation
.getLabel() + " " + statusString
+ ".");
111 } catch (ExecutionException e
) {
112 logger
.error("Error executing operation: " + operation
.getLabel(), e
);
116 private static IProgressMonitor
getMonitor() {
117 statusLineManager
.setCancelEnabled(false);
118 return statusLineManager
.getProgressMonitor();
121 public static void setStatusLineManager(IStatusLineManager manager
) {
122 statusLineManager
= manager
;
125 public static void setStatusLine(String message
) {
126 statusLineManager
.setMessage(message
);
131 * Returns the selected taxon for referencing in context menus
136 public static TaxonNode
getCurrentSelection(ExecutionEvent event
){
138 ISelection menuSelection
= HandlerUtil
.getActiveMenuSelection(event
);
140 // The selection should always be a tree selection since we are in the
141 // taxonomic tree view. Just in case this will be used in another spot.
142 if(menuSelection
instanceof TreeSelection
){
143 TaxonNode taxonNode
= (TaxonNode
) ((TreeSelection
) menuSelection
).getFirstElement();
144 logger
.debug("Selected taxon: " + taxonNode
);
150 public static IUndoContext
getUndoContext() {
151 // FIXME this has to be more specific. Every widget has to have its own undo context
152 // return IOperationHistory.GLOBAL_UNDO_CONTEXT;
154 // Plug-ins that wish their operations to be undoable from workbench views
155 // such as the Navigator or Package Explorer should assign the workbench
156 // undo context to their operations.
157 if (defaultUndoContext
== null) {
158 defaultUndoContext
= new UndoContext();
160 return defaultUndoContext
;
164 * Whether a taxonNode has unsaved changes.
169 public static boolean isDirty(TaxonNode taxonNode
){
171 for (IEditorReference reference
: getActivePage().getEditorReferences()) {
172 TaxonEditorInput editorInput
;
174 editorInput
= (TaxonEditorInput
) reference
.getEditorInput();
175 if(editorInput
.getTaxonNode().equals(taxonNode
) && reference
.isDirty()){
178 } catch (PartInitException e
) {
188 * @param parentElement
190 public static void selectInNavigator(Object element
, Object parentElement
) {
191 TaxonNavigator navigator
= (TaxonNavigator
) TaxeditorEditorPlugin
.getDefault().getWorkbench().
192 getActiveWorkbenchWindow().getActivePage().findView(TaxonNavigator
.ID
);
193 if (navigator
!= null) {
194 CommonViewer viewer
= navigator
.getCommonViewer();
195 if (viewer
!= null) {
196 if (parentElement
!= null) {
197 viewer
.setExpandedState(parentElement
, true);
199 viewer
.setSelection(new StructuredSelection((TaxonNode
) element
));
207 public static void openSearch(Object selection
) {
208 if(selection
instanceof Taxon
){
209 Taxon taxon
= (Taxon
) selection
;
211 handleOpeningOfMultipleTaxonNodes(taxon
.getTaxonNodes());
213 }else if(selection
instanceof Synonym
){
214 Synonym synonym
= (Synonym
) selection
;
216 Set
<TaxonNode
> taxonNodes
= new HashSet
<TaxonNode
>();
217 for (Taxon taxon
: synonym
.getAcceptedTaxa()){
218 taxonNodes
.addAll(taxon
.getTaxonNodes());
220 handleOpeningOfMultipleTaxonNodes(taxonNodes
);
223 warningDialog("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.");
224 logger
.warn("Could not open editor for selection. Selection was of type: " + selection
.getClass().getSimpleName());
232 private static void handleOpeningOfMultipleTaxonNodes(
233 Set
<TaxonNode
> taxonNodes
) {
235 if(taxonNodes
.size() == 1){
236 openEditor(taxonNodes
.iterator().next());
238 // FIXME implement a dialog that shows all possible taxa and let the user choose which he wants to open.
239 warningDialog("The accepted taxon is either in multiple taxonmoic views or the synonym has" +
240 " more than one accepted taxon. This case is not handled yet by the software.");