0c6ed7b160bf56a54ed4890072ad69fd16c41eda
[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(Object selectedObject){
106 if (selectedObject instanceof UuidAndTitleCache){
107 Class type = ((UuidAndTitleCache) selectedObject).getType();
108 if(type == Taxon.class || type == Synonym.class){
109 try {
110 EditorUtil.openTaxonBase(((UuidAndTitleCache) selectedObject).getUuid());
111 } catch (PartInitException e) {
112 NavigationUtil.error(NavigationUtil.class, "Error opening the editor", e);
113 }
114 }
115 }
116 }
117
118 /**
119 * <p>openEmpty</p>
120 *
121 * @param parentNodeUuid a {@link java.util.UUID} object.
122 */
123 public static void openEmpty(UUID parentNodeUuid) {
124 try {
125 EditorUtil.openEmpty(parentNodeUuid);
126 } catch (PartInitException e) {
127 NavigationUtil.error(NavigationUtil.class, "Error opening the editor", e);
128 }
129 }
130
131 /**
132 * <p>openEditor</p>
133 *
134 * @param configuration a eu.etaxonomy.taxeditor.editor.OpenEditorConfiguration object.
135 */
136 public static void openEditor(OpenEditorConfiguration configuration) {
137
138 }
139
140 /**
141 * <p>getShell</p>
142 *
143 * @return a {@link org.eclipse.swt.widgets.Shell} object.
144 */
145 public static Shell getShell() {
146 return getActiveWindow().getShell();
147 }
148
149 /**
150 * <p>getActiveWindow</p>
151 *
152 * @return a {@link org.eclipse.ui.IWorkbenchWindow} object.
153 */
154 public static IWorkbenchWindow getActiveWindow() {
155 return TaxeditorNavigationPlugin.getDefault().getWorkbench().
156 getActiveWorkbenchWindow();
157 }
158
159 /**
160 * <p>getWorkbenchUndoContext</p>
161 *
162 * @return a {@link org.eclipse.core.commands.operations.IUndoContext} object.
163 */
164 public static IUndoContext getWorkbenchUndoContext() {
165 return TaxeditorEditorPlugin.getDefault().getWorkbench().
166 getOperationSupport().getUndoContext();
167 }
168
169 /**
170 * <p>getUndoContext</p>
171 *
172 * @return a {@link org.eclipse.core.commands.operations.IUndoContext} object.
173 */
174 public static IUndoContext getUndoContext() {
175 // FIXME this has to be more specific. Every widget has to have its own undo context
176 // return IOperationHistory.GLOBAL_UNDO_CONTEXT;
177
178 // Plug-ins that wish their operations to be undoable from workbench views
179 // such as the Navigator or Package Explorer should assign the workbench
180 // undo context to their operations.
181 if (defaultUndoContext == null) {
182 defaultUndoContext = new UndoContext();
183 }
184 return defaultUndoContext;
185 }
186
187 /**
188 * Whether a taxonNode has unsaved changes.
189 *
190 * @param taxonNode a {@link eu.etaxonomy.cdm.model.taxon.TaxonNode} object.
191 * @return a boolean.
192 */
193 public static boolean isDirty(TaxonNode taxonNode){
194
195 for (IEditorReference reference : getActivePage().getEditorReferences()) {
196
197 try {
198 if (reference.getEditorInput() instanceof TaxonEditorInput) {
199 TaxonEditorInput editorInput = (TaxonEditorInput) reference.getEditorInput();
200 if(editorInput.getTaxonNode().equals(taxonNode) && reference.isDirty()){
201 return true;
202 }
203 }
204 } catch (PartInitException e) {
205 NavigationUtil.error(NavigationUtil.class, e.getMessage(), e);
206 throw new RuntimeException(e);
207 }
208
209 }
210 return false;
211 }
212
213 /**
214 * <p>selectInNavigator</p>
215 *
216 * @param element a {@link java.lang.Object} object.
217 * @param parentElement a {@link java.lang.Object} object.
218 */
219 public static void selectInNavigator(final Object element, final Object parentElement) {
220 Display.getDefault().asyncExec(new Runnable(){
221
222 public void run() {
223 TaxonNavigator navigator = showNavigator();
224
225 if (navigator != null) {
226 CommonViewer viewer = navigator.getCommonViewer();
227 if (viewer != null) {
228 if (parentElement != null) {
229 viewer.setExpandedState(parentElement, true);
230 }
231 viewer.setSelection(new StructuredSelection((TaxonNode) element));
232 }
233 }
234 }
235
236 });
237 }
238
239 /**
240 * <p>openSearch</p>
241 *
242 * @param selection a {@link java.lang.Object} object.
243 */
244 public static void openSearch(Object selection) {
245 if(selection instanceof Taxon){
246 Taxon taxon = (Taxon) selection;
247
248 handleOpeningOfMultipleTaxonNodes(taxon.getTaxonNodes());
249
250 }else if(selection instanceof Synonym){
251 Synonym synonym = (Synonym) selection;
252
253 handleOpeningOfMultipleTaxa(synonym.getAcceptedTaxa());
254
255 }else{
256 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.");
257 }
258
259 }
260
261 private static void handleOpeningOfMultipleTaxa(Set<Taxon> acceptedTaxa) {
262 if(acceptedTaxa.size() == 1){
263 openEditor(acceptedTaxa.iterator().next());
264 }else if(acceptedTaxa.size() > 1){
265 // FIXME implement a dialog that shows all possible taxa and let the user choose which he wants to open.
266 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." +
267 " This case is not handled yet by the software.");
268 }else if(acceptedTaxa.size() == 0){
269 // this is an undesired state
270 warningDialog("Incorrect state", NavigationUtil.class, "The accepted taxon is not in a taxonomic view. This should not have happened.");
271 }
272 }
273
274 /**
275 * @param taxonNodes
276 */
277 private static void handleOpeningOfMultipleTaxonNodes(
278 Set<TaxonNode> taxonNodes) {
279
280 if(taxonNodes.size() == 1){
281 openEditor(taxonNodes.iterator().next());
282 }else if(taxonNodes.size() > 1){
283 // FIXME implement a dialog that shows all possible taxa and let the user choose which he wants to open.
284 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." +
285 " This case is not handled yet by the software.");
286 }else if(taxonNodes.size() == 0){
287 // this is an undesired state
288 warningDialog("Incorrect state", NavigationUtil.class, "The accepted taxon is not in a taxonomic view. This should not have happened.");
289 }
290 }
291
292 /**
293 * <p>showNavigator</p>
294 *
295 * @return the TaxonNavigator instance if present
296 */
297 public static TaxonNavigator showNavigator() {
298 return (TaxonNavigator) showView(TaxonNavigator.ID);
299 }
300
301 /**
302 * <p>getNavigator</p>
303 *
304 * @param restore a boolean.
305 * @return a {@link eu.etaxonomy.taxeditor.navigation.navigator.TaxonNavigator} object.
306 */
307 public static TaxonNavigator getNavigator(boolean restore) {
308 return (TaxonNavigator) getView(TaxonNavigator.ID, restore);
309 }
310
311 /**
312 * <p>getOpenEditors</p>
313 *
314 * @return a {@link java.util.Set} object.
315 */
316 public static Set<IEditorPart> getOpenEditors() {
317 return EditorUtil.getOpenEditors();
318 }
319
320 /**
321 * <p>getPluginId</p>
322 *
323 * @return a {@link java.lang.String} object.
324 */
325 protected static String getPluginId(){
326 return TaxeditorNavigationPlugin.PLUGIN_ID;
327 }
328
329 }