base implementation and configuration for securing the editor - #4055 (action enablem...
[taxeditor.git] / eu.etaxonomy.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.Set;
13 import java.util.UUID;
14
15 import org.eclipse.core.commands.ExecutionException;
16 import org.eclipse.core.commands.NotEnabledException;
17 import org.eclipse.core.commands.NotHandledException;
18 import org.eclipse.core.commands.common.NotDefinedException;
19 import org.eclipse.core.commands.operations.IUndoContext;
20 import org.eclipse.core.commands.operations.UndoContext;
21 import org.eclipse.jface.viewers.StructuredSelection;
22 import org.eclipse.swt.widgets.Display;
23 import org.eclipse.swt.widgets.Shell;
24 import org.eclipse.ui.IEditorPart;
25 import org.eclipse.ui.IEditorReference;
26 import org.eclipse.ui.IWorkbenchWindow;
27 import org.eclipse.ui.PartInitException;
28 import org.eclipse.ui.handlers.IHandlerService;
29 import org.eclipse.ui.navigator.CommonViewer;
30
31 import eu.etaxonomy.cdm.model.common.ICdmBase;
32 import eu.etaxonomy.cdm.model.common.UuidAndTitleCache;
33 import eu.etaxonomy.cdm.model.description.PolytomousKey;
34 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
35 import eu.etaxonomy.cdm.model.taxon.Synonym;
36 import eu.etaxonomy.cdm.model.taxon.Taxon;
37 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
38 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
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.model.AbstractUtility;
43 import eu.etaxonomy.taxeditor.navigation.internal.TaxeditorNavigationPlugin;
44 import eu.etaxonomy.taxeditor.navigation.navigator.TaxonNavigator;
45
46 /**
47 * <p>NavigationUtil class.</p>
48 *
49 * @author n.hoffmann
50 * @created 24.03.2009
51 * @version 1.0
52 */
53 public class NavigationUtil extends AbstractUtility{
54 private static IUndoContext defaultUndoContext;
55
56 /**
57 * <p>executeEditHandler</p>
58 */
59 public static void executeEditHandler(){
60
61 String commandId = "eu.etaxonomy.taxeditor.navigation.command.update.editSelection";
62
63 IHandlerService handlerService = (IHandlerService) AbstractUtility.getService(IHandlerService.class);
64 try {
65 handlerService.executeCommand(commandId, null);
66 } catch (ExecutionException e) {
67 AbstractUtility.error(NavigationUtil.class, e);
68 } catch (NotDefinedException e) {
69 AbstractUtility.error(NavigationUtil.class, e);
70 } catch (NotEnabledException e) {
71 AbstractUtility.error(NavigationUtil.class, e);
72 } catch (NotHandledException e) {
73 AbstractUtility.error(NavigationUtil.class, e);
74 }
75 }
76
77 /**
78 * <p>openEditor</p>
79 *
80 * @param selectedObject a {@link eu.etaxonomy.cdm.model.common.CdmBase} object.
81 */
82 public static void openEditor(ICdmBase selectedObject){
83 UUID entityUuid = selectedObject.getUuid();
84 try {
85 if(selectedObject instanceof TaxonNode){
86 EditorUtil.openTaxonNode(entityUuid);
87 }else if(selectedObject instanceof TaxonBase){
88 EditorUtil.openTaxonBase(entityUuid);
89 }else if(selectedObject instanceof TaxonNameBase){
90 // TODO open bulk editor
91 warningDialog("Not implemented yet", NavigationUtil.class, "You tried to open a name. This is not handled by the software yet.");
92 }else if(selectedObject instanceof PolytomousKey){
93 EditorUtil.openPolytomousKey(entityUuid);
94 }else{
95 warningDialog("Unsupported Type", NavigationUtil.class, "No editor exists for the current selection: " + selectedObject);
96 }
97 } catch (PartInitException e) {
98 AbstractUtility.error(NavigationUtil.class, "Error opening the editor", e);
99 } catch (Exception e) {
100 AbstractUtility.warningDialog("Could not create Taxon", NavigationUtil.class, e.getMessage());
101 }
102 }
103
104 public static void openEditor(Object selectedObject){
105 if (selectedObject instanceof UuidAndTitleCache){
106 Class type = ((UuidAndTitleCache) selectedObject).getType();
107 if(type == Taxon.class || type == Synonym.class){
108 try {
109 EditorUtil.openTaxonBase(((UuidAndTitleCache) selectedObject).getUuid());
110 } catch (PartInitException e) {
111 AbstractUtility.error(NavigationUtil.class, "Error opening the editor", e);
112 }
113 }
114 }else if(selectedObject instanceof ICdmBase){
115 openEditor((ICdmBase) selectedObject);
116 }else{
117 AbstractUtility.error(NavigationUtil.class, new IllegalArgumentException("Selected object is not supported: " + selectedObject));
118 }
119 }
120
121 /**
122 * <p>openEmpty</p>
123 *
124 * @param parentNodeUuid a {@link java.util.UUID} object.
125 */
126 public static void openEmpty(UUID parentNodeUuid) {
127 try {
128 EditorUtil.openEmpty(parentNodeUuid);
129 } catch (PartInitException e) {
130 AbstractUtility.error(NavigationUtil.class, "Error opening the editor", e);
131 }
132 }
133
134 /**
135 * <p>getShell</p>
136 *
137 * @return a {@link org.eclipse.swt.widgets.Shell} object.
138 */
139 public static Shell getShell() {
140 return getActiveWindow().getShell();
141 }
142
143 /**
144 * <p>getActiveWindow</p>
145 *
146 * @return a {@link org.eclipse.ui.IWorkbenchWindow} object.
147 */
148 public static IWorkbenchWindow getActiveWindow() {
149 return TaxeditorNavigationPlugin.getDefault().getWorkbench().
150 getActiveWorkbenchWindow();
151 }
152
153 /**
154 * <p>getWorkbenchUndoContext</p>
155 *
156 * @return a {@link org.eclipse.core.commands.operations.IUndoContext} object.
157 */
158 public static IUndoContext getWorkbenchUndoContext() {
159 return TaxeditorEditorPlugin.getDefault().getWorkbench().
160 getOperationSupport().getUndoContext();
161 }
162
163 /**
164 * <p>getUndoContext</p>
165 *
166 * @return a {@link org.eclipse.core.commands.operations.IUndoContext} object.
167 */
168 public static IUndoContext getUndoContext() {
169 // FIXME this has to be more specific. Every widget has to have its own undo context
170 // return IOperationHistory.GLOBAL_UNDO_CONTEXT;
171
172 // Plug-ins that wish their operations to be undoable from workbench views
173 // such as the Navigator or Package Explorer should assign the workbench
174 // undo context to their operations.
175 if (defaultUndoContext == null) {
176 defaultUndoContext = new UndoContext();
177 }
178 return defaultUndoContext;
179 }
180
181 /**
182 * Whether a taxonNode has unsaved changes.
183 *
184 * @param taxonNode a {@link eu.etaxonomy.cdm.model.taxon.TaxonNode} object.
185 * @return a boolean.
186 */
187 public static boolean isDirty(TaxonNode taxonNode){
188
189 for (IEditorReference reference : getActivePage().getEditorReferences()) {
190
191 try {
192 if (reference.getEditorInput() instanceof TaxonEditorInput) {
193 TaxonEditorInput editorInput = (TaxonEditorInput) reference.getEditorInput();
194 if(editorInput.getTaxonNode().equals(taxonNode) && reference.isDirty()){
195 return true;
196 }
197 }
198 } catch (PartInitException e) {
199 AbstractUtility.error(NavigationUtil.class, e.getMessage(), e);
200 throw new RuntimeException(e);
201 }
202
203 }
204 return false;
205 }
206
207 /**
208 * <p>selectInNavigator</p>
209 *
210 * @param element a {@link java.lang.Object} object.
211 * @param parentElement a {@link java.lang.Object} object.
212 */
213 public static void selectInNavigator(final Object element, final Object parentElement) {
214 Display.getDefault().asyncExec(new Runnable(){
215
216 @Override
217 public void run() {
218 TaxonNavigator navigator = showNavigator();
219
220 if (navigator != null) {
221 CommonViewer viewer = navigator.getCommonViewer();
222 if (viewer != null) {
223 if (parentElement != null) {
224 viewer.setExpandedState(parentElement, true);
225 }
226 viewer.setSelection(new StructuredSelection(element));
227 }
228 }
229 }
230
231 });
232 }
233
234 /**
235 * <p>openSearch</p>
236 *
237 * @param selection a {@link java.lang.Object} object.
238 */
239 public static void openSearch(Object selection) {
240 if(selection instanceof Taxon){
241 Taxon taxon = (Taxon) selection;
242
243 handleOpeningOfMultipleTaxonNodes(taxon.getTaxonNodes());
244
245 }else if(selection instanceof Synonym){
246 Synonym synonym = (Synonym) selection;
247
248 handleOpeningOfMultipleTaxa(synonym.getAcceptedTaxa());
249
250 }else{
251 warningDialog("Not implemented yet", NavigationUtil.class, "You chose to open a name that has no connection to a taxon. The Editor does not support editing of such a content type at the moment.");
252 }
253
254 }
255
256 private static void handleOpeningOfMultipleTaxa(Set<Taxon> acceptedTaxa) {
257 if(acceptedTaxa.size() == 1){
258 openEditor(acceptedTaxa.iterator().next());
259 }else if(acceptedTaxa.size() > 1){
260 // FIXME implement a dialog that shows all possible taxa and let the user choose which he wants to open.
261 warningDialog("Not implemented yet", NavigationUtil.class, "The accepted taxon is in multiple taxonomic trees. We currently do not know which one you want to open." +
262 " This case is not handled yet by the software.");
263 }else if(acceptedTaxa.size() == 0){
264 // this is an undesired state
265 warningDialog("Not implemented yet", NavigationUtil.class, "This taxon is not connected to a classification. Currently editing of such taxa is not supported yet.");
266 }
267 }
268
269 /**
270 * @param taxonNodes
271 */
272 private static void handleOpeningOfMultipleTaxonNodes(
273 Set<TaxonNode> taxonNodes) {
274
275 if(taxonNodes.size() == 1){
276 openEditor(taxonNodes.iterator().next());
277 }else if(taxonNodes.size() > 1){
278 // FIXME implement a dialog that shows all possible taxa and let the user choose which he wants to open.
279 warningDialog("Not implemented yet", NavigationUtil.class, "The accepted taxon is in multiple taxonomic trees. We currently do not know which one you want to open." +
280 " This case is not handled yet by the software.");
281 }else if(taxonNodes.size() == 0){
282 // this is an undesired state
283 warningDialog("Incorrect state", NavigationUtil.class, "The accepted taxon is not in a taxonomic view. This should not have happened.");
284 }
285 }
286
287 /**
288 * <p>showNavigator</p>
289 *
290 * @return the TaxonNavigator instance if present
291 */
292 public static TaxonNavigator showNavigator() {
293 return (TaxonNavigator) showView(TaxonNavigator.ID);
294 }
295
296 /**
297 * <p>getNavigator</p>
298 *
299 * @param restore a boolean.
300 * @return a {@link eu.etaxonomy.taxeditor.navigation.navigator.TaxonNavigator} object.
301 */
302 public static TaxonNavigator getNavigator(boolean restore) {
303 return (TaxonNavigator) getView(TaxonNavigator.ID, restore);
304 }
305
306 /**
307 * <p>getOpenEditors</p>
308 *
309 * @return a {@link java.util.Set} object.
310 */
311 public static Set<IEditorPart> getOpenEditors() {
312 return EditorUtil.getOpenEditors();
313 }
314
315 /**
316 * <p>getPluginId</p>
317 *
318 * @return a {@link java.lang.String} object.
319 */
320 public static String getPluginId(){
321 return TaxeditorNavigationPlugin.PLUGIN_ID;
322 }
323
324 }