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