9d02173cbee47aeee0b3d8f500d7f12bca78451e
[taxeditor.git] / taxeditor-navigation / src / main / java / eu / etaxonomy / taxeditor / navigation / NavigationUtil.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
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.
8 */
9
10 package eu.etaxonomy.taxeditor.navigation;
11
12 import java.util.UUID;
13
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;
35
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;
44
45 /**
46 * @author n.hoffmann
47 * @created 24.03.2009
48 * @version 1.0
49 */
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;
54
55 /**
56 * FIXME Introduces a dependency on eu.etaxonomy.taxeditor.editor. This is highly undesirable.
57 *
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
61 *
62 * @param uuid
63 */
64 public static void openEditor(IdentifiableEntity selection) {
65 try {
66 UUID entityUuid = selection.getUuid();
67 EditorUtil.open(entityUuid);
68 } catch (PartInitException e) {
69 logger.error("Error opening the editor", e);
70 }
71 }
72
73 public static Shell getShell() {
74 return getActiveWindow().getShell();
75 }
76
77 public static IWorkbenchWindow getActiveWindow() {
78 return TaxeditorNavigationPlugin.getDefault().getWorkbench().
79 getActiveWorkbenchWindow();
80 }
81
82 public static void openEmpty(Taxon parentTaxon) {
83 try {
84 UUID parentTaxonUuid = parentTaxon == null ? null : parentTaxon.getUuid();
85
86 EditorUtil.openEmpty(parentTaxonUuid);
87 } catch (PartInitException e) {
88 logger.error("Error opening the editor", e);
89 }
90 }
91
92 public static IOperationHistory getWorkbenchOperationHistory() {
93 return TaxeditorEditorPlugin.getDefault().getWorkbench().
94 getOperationSupport().getOperationHistory();
95 }
96
97 public static IUndoContext getWorkbenchUndoContext() {
98 return TaxeditorEditorPlugin.getDefault().getWorkbench().
99 getOperationSupport().getUndoContext();
100 }
101
102 public static void executeOperation(IUndoableOperation operation){
103 try {
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);
110 }
111 }
112
113 private static IProgressMonitor getMonitor() {
114 statusLineManager.setCancelEnabled(false);
115 return statusLineManager.getProgressMonitor();
116 }
117
118 public static void setStatusLineManager(IStatusLineManager manager) {
119 statusLineManager = manager;
120 }
121
122 public static void setStatusLine(String message) {
123 statusLineManager.setMessage(message);
124 }
125
126
127 /**
128 * Returns the selected taxon for referencing in context menus
129 *
130 * @param event
131 * @return
132 */
133 public static Taxon getCurrentSelection(ExecutionEvent event){
134
135 ISelection menuSelection = HandlerUtil.getActiveMenuSelection(event);
136
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);
142 return taxon;
143 }
144 return null;
145 }
146
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;
150
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();
156 }
157 return defaultUndoContext;
158 }
159
160 public static boolean isDirty(Taxon taxon){
161
162 for (IEditorReference reference : getActivePage().getEditorReferences()) {
163 TaxonEditorInput editorInput;
164 try {
165 editorInput = (TaxonEditorInput) reference.getEditorInput();
166 if(editorInput.getTaxon().equals(taxon) && reference.isDirty()){
167 return true;
168 }
169 } catch (PartInitException e) {
170 e.printStackTrace();
171 }
172 }
173 return false;
174 }
175
176 /**
177 * @param parentElement
178 * @param childTaxon
179 */
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);
188 }
189 viewer.setSelection(new StructuredSelection((Taxon) element));
190 }
191 }
192 }
193 }