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