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
.UUID
;
14 import org
.apache
.log4j
.Logger
;
15 import org
.eclipse
.core
.commands
.ExecutionEvent
;
16 import org
.eclipse
.core
.commands
.ExecutionException
;
17 import org
.eclipse
.core
.commands
.operations
.IOperationHistory
;
18 import org
.eclipse
.core
.commands
.operations
.IUndoContext
;
19 import org
.eclipse
.core
.commands
.operations
.IUndoableOperation
;
20 import org
.eclipse
.core
.commands
.operations
.UndoContext
;
21 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
22 import org
.eclipse
.core
.runtime
.IStatus
;
23 import org
.eclipse
.core
.runtime
.Status
;
24 import org
.eclipse
.jface
.action
.IStatusLineManager
;
25 import org
.eclipse
.jface
.viewers
.ISelection
;
26 import org
.eclipse
.jface
.viewers
.StructuredSelection
;
27 import org
.eclipse
.jface
.viewers
.TreeSelection
;
28 import org
.eclipse
.swt
.widgets
.Shell
;
29 import org
.eclipse
.ui
.IEditorReference
;
30 import org
.eclipse
.ui
.IWorkbenchWindow
;
31 import org
.eclipse
.ui
.PartInitException
;
32 import org
.eclipse
.ui
.handlers
.HandlerUtil
;
33 import org
.eclipse
.ui
.ide
.undo
.WorkspaceUndoUtil
;
34 import org
.eclipse
.ui
.navigator
.CommonViewer
;
36 import eu
.etaxonomy
.cdm
.model
.common
.IdentifiableEntity
;
37 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
38 import eu
.etaxonomy
.taxeditor
.editor
.EditorUtil
;
39 import eu
.etaxonomy
.taxeditor
.editor
.TaxonEditorInput
;
40 import eu
.etaxonomy
.taxeditor
.editor
.internal
.TaxeditorEditorPlugin
;
41 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
42 import eu
.etaxonomy
.taxeditor
.navigation
.internal
.TaxeditorNavigationPlugin
;
43 import eu
.etaxonomy
.taxeditor
.navigation
.navigator
.TaxonNavigator
;
50 public class NavigationUtil
extends AbstractUtility
{
51 private static final Logger logger
= Logger
.getLogger(NavigationUtil
.class);
52 private static IStatusLineManager statusLineManager
;
53 private static IUndoContext defaultUndoContext
;
56 * FIXME Introduces a dependency on eu.etaxonomy.taxeditor.editor. This is highly undesirable.
58 * It is not clear at the moment by what mechanics we are going to open the
59 * editor. I would prefer some sort of resource model that has the editor linked
60 * to the resource type of taxonbase. Another reason to switch to CommonNavigatorFamework
64 public static void openEditor(IdentifiableEntity selection
) {
66 UUID entityUuid
= selection
.getUuid();
67 EditorUtil
.open(entityUuid
);
68 } catch (PartInitException e
) {
69 logger
.error("Error opening the editor", e
);
73 public static Shell
getShell() {
74 return getActiveWindow().getShell();
77 public static IWorkbenchWindow
getActiveWindow() {
78 return TaxeditorNavigationPlugin
.getDefault().getWorkbench().
79 getActiveWorkbenchWindow();
82 public static void openEmpty(Taxon parentTaxon
) {
84 UUID parentTaxonUuid
= parentTaxon
== null ?
null : parentTaxon
.getUuid();
86 EditorUtil
.openEmpty(parentTaxonUuid
);
87 } catch (PartInitException e
) {
88 logger
.error("Error opening the editor", e
);
92 public static IOperationHistory
getWorkbenchOperationHistory() {
93 return TaxeditorEditorPlugin
.getDefault().getWorkbench().
94 getOperationSupport().getOperationHistory();
97 public static IUndoContext
getWorkbenchUndoContext() {
98 return TaxeditorEditorPlugin
.getDefault().getWorkbench().
99 getOperationSupport().getUndoContext();
102 public static void executeOperation(IUndoableOperation operation
){
104 IStatus status
= getWorkbenchOperationHistory().execute(operation
, getMonitor(),
105 WorkspaceUndoUtil
.getUIInfoAdapter(getShell()));
106 String statusString
= status
.equals(Status
.OK_STATUS
) ?
"completed" : "cancelled";
107 setStatusLine(operation
.getLabel() + " " + statusString
+ ".");
108 } catch (ExecutionException e
) {
109 logger
.error("Error executing operation: " + operation
.getLabel(), e
);
113 private static IProgressMonitor
getMonitor() {
114 statusLineManager
.setCancelEnabled(false);
115 return statusLineManager
.getProgressMonitor();
118 public static void setStatusLineManager(IStatusLineManager manager
) {
119 statusLineManager
= manager
;
122 public static void setStatusLine(String message
) {
123 statusLineManager
.setMessage(message
);
128 * Returns the selected taxon for referencing in context menus
133 public static Taxon
getCurrentSelection(ExecutionEvent event
){
135 ISelection menuSelection
= HandlerUtil
.getActiveMenuSelection(event
);
137 // The selection should always be a tree selection since we are in the
138 // taxonomic tree view. Just in case this will be used in another spot.
139 if(menuSelection
instanceof TreeSelection
){
140 Taxon taxon
= (Taxon
) ((TreeSelection
) menuSelection
).getFirstElement();
141 logger
.debug("Selected taxon: " + taxon
);
147 public static IUndoContext
getUndoContext() {
148 // FIXME this has to be more specific. Every widget has to have its own undo context
149 // return IOperationHistory.GLOBAL_UNDO_CONTEXT;
151 // Plug-ins that wish their operations to be undoable from workbench views
152 // such as the Navigator or Package Explorer should assign the workbench
153 // undo context to their operations.
154 if (defaultUndoContext
== null) {
155 defaultUndoContext
= new UndoContext();
157 return defaultUndoContext
;
160 public static boolean isDirty(Taxon taxon
){
162 for (IEditorReference reference
: getActivePage().getEditorReferences()) {
163 TaxonEditorInput editorInput
;
165 editorInput
= (TaxonEditorInput
) reference
.getEditorInput();
166 if(editorInput
.getTaxon().equals(taxon
) && reference
.isDirty()){
169 } catch (PartInitException e
) {
177 * @param parentElement
180 public static void selectInNavigator(Object element
, Object parentElement
) {
181 TaxonNavigator navigator
= (TaxonNavigator
) TaxeditorEditorPlugin
.getDefault().getWorkbench().
182 getActiveWorkbenchWindow().getActivePage().findView(TaxonNavigator
.ID
);
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((Taxon
) element
));