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