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