2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.taxeditor
.navigation
;
13 import java
.util
.UUID
;
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
;
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
;
50 * <p>NavigationUtil class.</p>
56 public class NavigationUtil
extends AbstractUtility
{
57 private static IUndoContext defaultUndoContext
;
62 * @param selectedObject a {@link eu.etaxonomy.cdm.model.common.CdmBase} object.
64 public static void openEditor(UuidAndTitleCache uuidAndTitleCache
){
65 TaxonBase taxonBase
= CdmStore
.getService(ITaxonService
.class).load(uuidAndTitleCache
.getUuid());
67 MessagingUtils
.warn(NavigationUtil
.class, "No taxon stored with UUID "+uuidAndTitleCache
.getUuid());
70 openEditor(taxonBase
);
72 public static void openEditor(ICdmBase selectedObject
){
73 UUID entityUuid
= selectedObject
.getUuid();
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
);
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
);
92 MessagingUtils
.warningDialog("Unsupported Type", NavigationUtil
.class, "No editor exists for the current selection: " + selectedObject
);
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",
99 e
.getMessage(), TaxeditorStorePlugin
.PLUGIN_ID
,
109 * @param parentNodeUuid a {@link java.util.UUID} object.
111 public static void openEmpty(UUID parentNodeUuid
) {
113 EditorUtil
.openEmpty(parentNodeUuid
);
114 } catch (PartInitException e
) {
115 MessagingUtils
.error(NavigationUtil
.class, "Error opening the editor", e
);
122 * @return a {@link org.eclipse.swt.widgets.Shell} object.
124 public static Shell
getShell() {
125 return getActiveWindow().getShell();
129 * <p>getActiveWindow</p>
131 * @return a {@link org.eclipse.ui.IWorkbenchWindow} object.
133 public static IWorkbenchWindow
getActiveWindow() {
134 return TaxeditorNavigationPlugin
.getDefault().getWorkbench().
135 getActiveWorkbenchWindow();
139 * <p>getWorkbenchUndoContext</p>
141 * @return a {@link org.eclipse.core.commands.operations.IUndoContext} object.
143 public static IUndoContext
getWorkbenchUndoContext() {
144 return TaxeditorEditorPlugin
.getDefault().getWorkbench().
145 getOperationSupport().getUndoContext();
149 * <p>getUndoContext</p>
151 * @return a {@link org.eclipse.core.commands.operations.IUndoContext} object.
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;
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();
163 return defaultUndoContext
;
167 * Whether a taxonNode has unsaved changes.
169 * @param taxonNode a {@link eu.etaxonomy.cdm.model.taxon.TaxonNode} object.
172 public static boolean isDirty(TaxonNode taxonNode
){
174 for (IEditorReference reference
: getActivePage().getEditorReferences()) {
177 if (reference
.getEditorInput() instanceof TaxonEditorInput
) {
178 TaxonEditorInput editorInput
= (TaxonEditorInput
) reference
.getEditorInput();
179 if(editorInput
.getTaxonNode().equals(taxonNode
) && reference
.isDirty()){
183 } catch (PartInitException e
) {
184 MessagingUtils
.error(NavigationUtil
.class, e
.getMessage(), e
);
185 throw new RuntimeException(e
);
193 * <p>selectInNavigator</p>
195 * @param element a {@link java.lang.Object} object.
196 * @param parentElement a {@link java.lang.Object} object.
198 public static void selectInNavigator(final Object element
, final Object parentElement
) {
199 Display
.getDefault().asyncExec(new Runnable(){
203 TaxonNavigator navigator
= showNavigator();
205 if (navigator
!= null) {
206 CommonViewer viewer
= navigator
.getCommonViewer();
207 if (viewer
!= null) {
208 if (parentElement
!= null) {
209 viewer
.setExpandedState(parentElement
, true);
211 viewer
.setSelection(new StructuredSelection(element
));
222 * @param selection a {@link java.lang.Object} object.
224 public static void openSearch(Object selection
) {
225 if(selection
instanceof Taxon
){
226 Taxon taxon
= (Taxon
) selection
;
228 handleOpeningOfMultipleTaxonNodes(taxon
.getTaxonNodes());
230 }else if(selection
instanceof Synonym
){
231 Synonym synonym
= (Synonym
) selection
;
233 handleOpeningOfMultipleTaxa(synonym
.getAcceptedTaxa());
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.");
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.");
257 private static void handleOpeningOfMultipleTaxonNodes(
258 Set
<TaxonNode
> taxonNodes
) {
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.");
273 * <p>showNavigator</p>
275 * @return the TaxonNavigator instance if present
277 public static TaxonNavigator
showNavigator() {
278 return (TaxonNavigator
) showView(TaxonNavigator
.ID
);
282 * <p>getNavigator</p>
284 * @param restore a boolean.
285 * @return a {@link eu.etaxonomy.taxeditor.navigation.navigator.TaxonNavigator} object.
287 public static TaxonNavigator
getNavigator(boolean restore
) {
288 return (TaxonNavigator
) getView(TaxonNavigator
.ID
, restore
);
292 * <p>getOpenEditors</p>
294 * @return a {@link java.util.Set} object.
296 public static Set
<IEditorPart
> getOpenEditors() {
297 return EditorUtil
.getOpenEditors();
303 * @return a {@link java.lang.String} object.
305 public static String
getPluginId(){
306 return TaxeditorNavigationPlugin
.PLUGIN_ID
;