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
.CommonNavigator
;
35 import org
.eclipse
.ui
.navigator
.CommonViewer
;
37 import eu
.etaxonomy
.cdm
.model
.common
.IdentifiableEntity
;
38 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
39 import eu
.etaxonomy
.taxeditor
.editor
.EditorUtil
;
40 import eu
.etaxonomy
.taxeditor
.editor
.TaxonEditorInput
;
41 import eu
.etaxonomy
.taxeditor
.editor
.internal
.TaxeditorEditorPlugin
;
42 import eu
.etaxonomy
.taxeditor
.navigation
.internal
.TaxeditorNavigationPlugin
;
43 import eu
.etaxonomy
.taxeditor
.navigation
.navigator
.TaxonNavigator
;
44 import eu
.etaxonomy
.taxeditor
.store
.model
.AbstractUtility
;
51 public class NavigationUtil
extends AbstractUtility
{
52 private static final Logger logger
= Logger
.getLogger(NavigationUtil
.class);
53 private static IStatusLineManager statusLineManager
;
54 private static IUndoContext defaultUndoContext
;
57 * FIXME Introduces a dependency on eu.etaxonomy.taxeditor.editor. This is highly undesirable.
59 * It is not clear at the moment by what mechanics we are going to open the
60 * editor. I would prefer some sort of resource model that has the editor linked
61 * to the resource type of taxonbase. Another reason to switch to CommonNavigatorFamework
65 public static void openEditor(IdentifiableEntity selection
) {
67 UUID entityUuid
= selection
.getUuid();
68 EditorUtil
.open(entityUuid
);
69 } catch (PartInitException e
) {
70 logger
.error("Error opening the editor", e
);
74 public static Shell
getShell() {
75 return getActiveWindow().getShell();
78 public static IWorkbenchWindow
getActiveWindow() {
79 return TaxeditorNavigationPlugin
.getDefault().getWorkbench().
80 getActiveWorkbenchWindow();
83 public static void openEmpty(Taxon parentTaxon
) {
85 UUID parentTaxonUuid
= parentTaxon
== null ?
null : parentTaxon
.getUuid();
87 EditorUtil
.openEmpty(parentTaxonUuid
);
88 } catch (PartInitException e
) {
89 logger
.error("Error opening the editor", e
);
93 public static IOperationHistory
getWorkbenchOperationHistory() {
94 return TaxeditorEditorPlugin
.getDefault().getWorkbench().
95 getOperationSupport().getOperationHistory();
98 public static IUndoContext
getWorkbenchUndoContext() {
99 return TaxeditorEditorPlugin
.getDefault().getWorkbench().
100 getOperationSupport().getUndoContext();
103 public static void executeOperation(IUndoableOperation operation
){
105 IStatus status
= getWorkbenchOperationHistory().execute(operation
, getMonitor(),
106 WorkspaceUndoUtil
.getUIInfoAdapter(getShell()));
107 String statusString
= status
.equals(Status
.OK_STATUS
) ?
"completed" : "cancelled";
108 setStatusLine(operation
.getLabel() + " " + statusString
+ ".");
109 } catch (ExecutionException e
) {
110 logger
.error("Error executing operation: " + operation
.getLabel(), e
);
114 private static IProgressMonitor
getMonitor() {
115 statusLineManager
.setCancelEnabled(false);
116 return statusLineManager
.getProgressMonitor();
119 public static void setStatusLineManager(IStatusLineManager manager
) {
120 statusLineManager
= manager
;
123 public static void setStatusLine(String message
) {
124 statusLineManager
.setMessage(message
);
129 * Returns the selected taxon for referencing in context menus
134 public static Taxon
getCurrentSelection(ExecutionEvent event
){
136 ISelection menuSelection
= HandlerUtil
.getActiveMenuSelection(event
);
138 // The selection should always be a tree selection since we are in the
139 // taxonomic tree view. Just in case this will be used in another spot.
140 if(menuSelection
instanceof TreeSelection
){
141 Taxon taxon
= (Taxon
) ((TreeSelection
) menuSelection
).getFirstElement();
142 logger
.debug("Selected taxon: " + taxon
);
148 public static IUndoContext
getUndoContext() {
149 // FIXME this has to be more specific. Every widget has to have its own undo context
150 // return IOperationHistory.GLOBAL_UNDO_CONTEXT;
152 // Plug-ins that wish their operations to be undoable from workbench views
153 // such as the Navigator or Package Explorer should assign the workbench
154 // undo context to their operations.
155 if (defaultUndoContext
== null) {
156 defaultUndoContext
= new UndoContext();
158 return defaultUndoContext
;
161 public static boolean isDirty(Taxon taxon
){
163 for (IEditorReference reference
: getActivePage().getEditorReferences()) {
164 TaxonEditorInput editorInput
;
166 editorInput
= (TaxonEditorInput
) reference
.getEditorInput();
167 if(editorInput
.getTaxon().equals(taxon
) && reference
.isDirty()){
170 } catch (PartInitException e
) {
178 * @param parentElement
181 public static void selectInNavigator(Object element
, Object parentElement
) {
182 TaxonNavigator navigator
= (TaxonNavigator
) TaxeditorEditorPlugin
.getDefault().getWorkbench().
183 getActiveWorkbenchWindow().getActivePage().findView(TaxonNavigator
.ID
);
184 if (navigator
!= null) {
185 CommonViewer viewer
= navigator
.getCommonViewer();
186 if (viewer
!= null) {
187 if (parentElement
!= null) {
188 viewer
.setExpandedState(parentElement
, true);
190 viewer
.setSelection(new StructuredSelection((Taxon
) element
));