Did some code cleanup. Removed obsolete and deprecated classes.
[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.HashSet;
13 import java.util.Set;
14 import java.util.UUID;
15
16 import org.apache.log4j.Logger;
17 import org.eclipse.core.commands.ExecutionEvent;
18 import org.eclipse.core.commands.operations.IUndoContext;
19 import org.eclipse.core.commands.operations.UndoContext;
20 import org.eclipse.jface.viewers.ISelection;
21 import org.eclipse.jface.viewers.StructuredSelection;
22 import org.eclipse.jface.viewers.TreeSelection;
23 import org.eclipse.swt.widgets.Shell;
24 import org.eclipse.ui.IEditorReference;
25 import org.eclipse.ui.IWorkbenchWindow;
26 import org.eclipse.ui.PartInitException;
27 import org.eclipse.ui.handlers.HandlerUtil;
28 import org.eclipse.ui.navigator.CommonViewer;
29
30 import eu.etaxonomy.cdm.model.common.CdmBase;
31 import eu.etaxonomy.cdm.model.taxon.Synonym;
32 import eu.etaxonomy.cdm.model.taxon.Taxon;
33 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
34 import eu.etaxonomy.taxeditor.editor.EditorUtil;
35 import eu.etaxonomy.taxeditor.editor.TaxonEditorInput;
36 import eu.etaxonomy.taxeditor.editor.internal.TaxeditorEditorPlugin;
37 import eu.etaxonomy.taxeditor.model.AbstractUtility;
38 import eu.etaxonomy.taxeditor.navigation.internal.TaxeditorNavigationPlugin;
39 import eu.etaxonomy.taxeditor.navigation.navigator.TaxonNavigator;
40
41 /**
42 * @author n.hoffmann
43 * @created 24.03.2009
44 * @version 1.0
45 */
46 public class NavigationUtil extends AbstractUtility{
47 private static final Logger logger = Logger.getLogger(NavigationUtil.class);
48 private static IUndoContext defaultUndoContext;
49
50 /**
51 * FIXME Introduces a dependency on eu.etaxonomy.taxeditor.editor. This is highly undesirable.
52 *
53 * It is not clear at the moment by what mechanics we are going to open the
54 * editor. I would prefer some sort of resource model that has the editor linked
55 * to the resource type of taxonbase. Another reason to switch to CommonNavigatorFamework
56 *
57 * @param uuid
58 */
59 public static void openEditor(TaxonNode selection) {
60 try {
61 UUID entityUuid = selection.getUuid();
62 EditorUtil.open(entityUuid);
63 } catch (PartInitException e) {
64 logger.error("Error opening the editor", e);
65 }
66 }
67
68 public static Shell getShell() {
69 return getActiveWindow().getShell();
70 }
71
72 public static IWorkbenchWindow getActiveWindow() {
73 return TaxeditorNavigationPlugin.getDefault().getWorkbench().
74 getActiveWorkbenchWindow();
75 }
76
77 public static void openEmpty(UUID parentNodeUuid) {
78 try {
79 EditorUtil.openEmpty(parentNodeUuid);
80 } catch (PartInitException e) {
81 logger.error("Error opening the editor", e);
82 }
83 }
84
85 public static IUndoContext getWorkbenchUndoContext() {
86 return TaxeditorEditorPlugin.getDefault().getWorkbench().
87 getOperationSupport().getUndoContext();
88 }
89
90 public static IUndoContext getUndoContext() {
91 // FIXME this has to be more specific. Every widget has to have its own undo context
92 // return IOperationHistory.GLOBAL_UNDO_CONTEXT;
93
94 // Plug-ins that wish their operations to be undoable from workbench views
95 // such as the Navigator or Package Explorer should assign the workbench
96 // undo context to their operations.
97 if (defaultUndoContext == null) {
98 defaultUndoContext = new UndoContext();
99 }
100 return defaultUndoContext;
101 }
102
103 /**
104 * Whether a taxonNode has unsaved changes.
105 *
106 * @param taxonNode
107 * @return
108 */
109 public static boolean isDirty(TaxonNode taxonNode){
110
111 for (IEditorReference reference : getActivePage().getEditorReferences()) {
112
113 try {
114 if (reference.getEditorInput() instanceof TaxonEditorInput) {
115 TaxonEditorInput editorInput = (TaxonEditorInput) reference.getEditorInput();
116 if(editorInput.getTaxonNode().equals(taxonNode) && reference.isDirty()){
117 return true;
118 }
119 }
120 } catch (PartInitException e) {
121 logger.error(e);
122 throw new RuntimeException(e);
123 }
124
125 }
126 return false;
127 }
128
129 /**
130 *
131 * @param element
132 * @param parentElement
133 */
134 public static void selectInNavigator(Object element, Object parentElement) {
135 TaxonNavigator navigator = getNavigator();
136 if (navigator != null) {
137 CommonViewer viewer = navigator.getCommonViewer();
138 if (viewer != null) {
139 if (parentElement != null) {
140 viewer.setExpandedState(parentElement, true);
141 }
142 viewer.setSelection(new StructuredSelection((TaxonNode) element));
143 }
144 }
145 }
146
147 /**
148 * @param selection
149 */
150 public static void openSearch(Object selection) {
151 if(selection instanceof Taxon){
152 Taxon taxon = (Taxon) selection;
153
154 handleOpeningOfMultipleTaxonNodes(taxon.getTaxonNodes());
155
156 }else if(selection instanceof Synonym){
157 Synonym synonym = (Synonym) selection;
158
159 Set<TaxonNode> taxonNodes = new HashSet<TaxonNode>();
160 for (Taxon taxon : synonym.getAcceptedTaxa()){
161 taxonNodes.addAll(taxon.getTaxonNodes());
162 }
163 handleOpeningOfMultipleTaxonNodes(taxonNodes);
164
165 }else{
166 warningDialog("Not implemented yet", "You chose to open a name that has no connection to a taxa. The Editor does not support editing of such a content type at the moment.");
167 logger.warn("Could not open editor for selection. Selection was of type: " + selection.getClass().getSimpleName());
168 }
169
170 }
171
172 /**
173 * @param taxonNodes
174 */
175 private static void handleOpeningOfMultipleTaxonNodes(
176 Set<TaxonNode> taxonNodes) {
177
178 if(taxonNodes.size() == 1){
179 openEditor(taxonNodes.iterator().next());
180 }else if(taxonNodes.size() > 1){
181 // FIXME implement a dialog that shows all possible taxa and let the user choose which he wants to open.
182 warningDialog("Not implemented yet", "The accepted taxon is in multiple taxonomic trees. We currently do not know which one you want to open." +
183 " This case is not handled yet by the software.");
184 }else if(taxonNodes.size() == 0){
185 // this is an undesired state
186 warningDialog("Incorrect state", "The accepted taxon is not in a taxonomic view. This should not have happened.");
187 }
188 }
189
190 /**
191 * @return the TaxonNavigator instance if present
192 */
193 public static TaxonNavigator getNavigator() {
194 return (TaxonNavigator) TaxeditorEditorPlugin.getDefault().getWorkbench().
195 getActiveWorkbenchWindow().getActivePage().findView(TaxonNavigator.ID);
196 }
197 }