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