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