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