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
.IClassificationService
;
29 import eu
.etaxonomy
.cdm
.api
.service
.INameService
;
30 import eu
.etaxonomy
.cdm
.api
.service
.ITaxonNodeService
;
31 import eu
.etaxonomy
.cdm
.api
.service
.ITaxonService
;
32 import eu
.etaxonomy
.cdm
.model
.common
.ICdmBase
;
33 import eu
.etaxonomy
.cdm
.model
.description
.PolytomousKey
;
34 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
35 import eu
.etaxonomy
.cdm
.model
.taxon
.Classification
;
36 import eu
.etaxonomy
.cdm
.model
.taxon
.Synonym
;
37 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
38 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
39 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
40 import eu
.etaxonomy
.cdm
.persistence
.dto
.UuidAndTitleCache
;
41 import eu
.etaxonomy
.taxeditor
.editor
.EditorUtil
;
42 import eu
.etaxonomy
.taxeditor
.editor
.TaxonEditorInput
;
43 import eu
.etaxonomy
.taxeditor
.editor
.internal
.TaxeditorEditorPlugin
;
44 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
45 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
46 import eu
.etaxonomy
.taxeditor
.navigation
.internal
.TaxeditorNavigationPlugin
;
47 import eu
.etaxonomy
.taxeditor
.navigation
.navigator
.TaxonNavigator
;
48 import eu
.etaxonomy
.taxeditor
.newWizard
.NewClassificationWizard
;
49 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
50 import eu
.etaxonomy
.taxeditor
.store
.internal
.TaxeditorStorePlugin
;
53 * <p>NavigationUtil class.</p>
59 public class NavigationUtil
extends AbstractUtility
{
60 private static IUndoContext defaultUndoContext
;
65 * @param selectedObject a {@link eu.etaxonomy.cdm.model.common.CdmBase} object.
67 public static void openEditor(UuidAndTitleCache uuidAndTitleCache
){
68 Class type
= uuidAndTitleCache
.getType();
69 if(type
.equals(Classification
.class)){
70 Classification classification
= CdmStore
.getService(IClassificationService
.class).load(uuidAndTitleCache
.getUuid());
71 openEditor(classification
);
73 else if(type
.equals(TaxonNode
.class)){
74 TaxonNode taxonNode
= CdmStore
.getService(ITaxonNodeService
.class).load(uuidAndTitleCache
.getUuid());
75 openEditor(taxonNode
);
77 else if(type
.equals(TaxonBase
.class) || type
.getSuperclass().equals(TaxonBase
.class)){
78 TaxonBase taxonBase
= CdmStore
.getService(ITaxonService
.class).load(uuidAndTitleCache
.getUuid());
79 openEditor(taxonBase
);
81 else if(type
.equals(TaxonNameBase
.class)){
82 TaxonNameBase nameBase
= CdmStore
.getService(INameService
.class).load(uuidAndTitleCache
.getUuid());
86 MessagingUtils
.warningDialog("Unknown type", NavigationUtil
.class, "There is no editor available to open this object.");
90 public static void openEditor(ICdmBase selectedObject
){
91 UUID entityUuid
= selectedObject
.getUuid();
93 if(selectedObject
instanceof Classification
){
94 NewClassificationWizard classificationWizard
= new NewClassificationWizard();
95 classificationWizard
.init(null, null);
96 classificationWizard
.setEntity((Classification
) selectedObject
);
97 WizardDialog dialog
= new WizardDialog(PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getShell(), classificationWizard
);
100 else if(selectedObject
instanceof TaxonNode
){
101 EditorUtil
.openTaxonNode(entityUuid
);
102 }else if(selectedObject
instanceof TaxonBase
){
103 EditorUtil
.openTaxonBase(entityUuid
);
104 }else if(selectedObject
instanceof TaxonNameBase
){
105 // TODO open bulk editor
106 MessagingUtils
.warningDialog("Not implemented yet", NavigationUtil
.class, "You tried to open a name. This is not handled by the software yet. For open a pure name you can use the bulk editor");
107 }else if(selectedObject
instanceof PolytomousKey
){
108 EditorUtil
.openPolytomousKey(entityUuid
);
110 MessagingUtils
.warningDialog("Unsupported Type", NavigationUtil
.class, "No editor exists for the current selection: " + selectedObject
);
112 } catch (PartInitException e
) {
113 MessagingUtils
.error(NavigationUtil
.class, "Error opening the editor", e
);
114 } catch (Exception e
) {
115 MessagingUtils
.errorDialog("Could not create Taxon",
116 NavigationUtil
.class,
117 e
.getMessage(), TaxeditorStorePlugin
.PLUGIN_ID
,
127 * @param parentNodeUuid a {@link java.util.UUID} object.
129 public static void openEmpty(UUID parentNodeUuid
) {
131 EditorUtil
.openEmpty(parentNodeUuid
);
132 } catch (PartInitException e
) {
133 MessagingUtils
.error(NavigationUtil
.class, "Error opening the editor", e
);
140 * @return a {@link org.eclipse.swt.widgets.Shell} object.
142 public static Shell
getShell() {
143 return getActiveWindow().getShell();
147 * <p>getActiveWindow</p>
149 * @return a {@link org.eclipse.ui.IWorkbenchWindow} object.
151 public static IWorkbenchWindow
getActiveWindow() {
152 return TaxeditorNavigationPlugin
.getDefault().getWorkbench().
153 getActiveWorkbenchWindow();
157 * <p>getWorkbenchUndoContext</p>
159 * @return a {@link org.eclipse.core.commands.operations.IUndoContext} object.
161 public static IUndoContext
getWorkbenchUndoContext() {
162 return TaxeditorEditorPlugin
.getDefault().getWorkbench().
163 getOperationSupport().getUndoContext();
167 * <p>getUndoContext</p>
169 * @return a {@link org.eclipse.core.commands.operations.IUndoContext} object.
171 public static IUndoContext
getUndoContext() {
172 // FIXME this has to be more specific. Every widget has to have its own undo context
173 // return IOperationHistory.GLOBAL_UNDO_CONTEXT;
175 // Plug-ins that wish their operations to be undoable from workbench views
176 // such as the Navigator or Package Explorer should assign the workbench
177 // undo context to their operations.
178 if (defaultUndoContext
== null) {
179 defaultUndoContext
= new UndoContext();
181 return defaultUndoContext
;
185 * Whether a taxonNode has unsaved changes.
187 * @param taxonNode a {@link eu.etaxonomy.cdm.model.taxon.TaxonNode} object.
190 public static boolean isDirty(TaxonNode taxonNode
){
192 for (IEditorReference reference
: getActivePage().getEditorReferences()) {
195 if (reference
.getEditorInput() instanceof TaxonEditorInput
) {
196 TaxonEditorInput editorInput
= (TaxonEditorInput
) reference
.getEditorInput();
197 if(editorInput
.getTaxonNode().equals(taxonNode
) && reference
.isDirty()){
201 } catch (PartInitException e
) {
202 MessagingUtils
.error(NavigationUtil
.class, e
.getMessage(), e
);
203 throw new RuntimeException(e
);
211 * <p>selectInNavigator</p>
213 * @param element a {@link java.lang.Object} object.
214 * @param parentElement a {@link java.lang.Object} object.
216 public static void selectInNavigator(final Object element
, final Object parentElement
) {
217 Display
.getDefault().asyncExec(new Runnable(){
221 TaxonNavigator navigator
= showNavigator();
223 if (navigator
!= null) {
224 CommonViewer viewer
= navigator
.getCommonViewer();
225 if (viewer
!= null) {
226 if (parentElement
!= null) {
227 viewer
.setExpandedState(parentElement
, true);
229 viewer
.setSelection(new StructuredSelection(element
));
240 * @param selection a {@link java.lang.Object} object.
242 public static void openSearch(Object selection
) {
243 if(selection
instanceof Taxon
){
244 Taxon taxon
= (Taxon
) selection
;
246 handleOpeningOfMultipleTaxonNodes(taxon
.getTaxonNodes());
248 }else if(selection
instanceof Synonym
){
249 Synonym synonym
= (Synonym
) selection
;
251 handleOpeningOfMultipleTaxa(synonym
.getAcceptedTaxa());
254 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.");
259 private static void handleOpeningOfMultipleTaxa(Set
<Taxon
> acceptedTaxa
) {
260 if(acceptedTaxa
.size() == 1){
261 openEditor(acceptedTaxa
.iterator().next());
262 }else if(acceptedTaxa
.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(acceptedTaxa
.size() == 0){
267 // this is an undesired state
268 MessagingUtils
.warningDialog("Not implemented yet", NavigationUtil
.class, "This taxon is not connected to a classification. Currently editing of such taxa is not supported yet.");
275 private static void handleOpeningOfMultipleTaxonNodes(
276 Set
<TaxonNode
> taxonNodes
) {
278 if(taxonNodes
.size() == 1){
279 openEditor(taxonNodes
.iterator().next());
280 }else if(taxonNodes
.size() > 1){
281 // FIXME implement a dialog that shows all possible taxa and let the user choose which he wants to open.
282 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." +
283 " This case is not handled yet by the software.");
284 }else if(taxonNodes
.size() == 0){
285 // this is an undesired state
286 MessagingUtils
.warningDialog("Incorrect state", NavigationUtil
.class, "The accepted taxon is not in a taxonomic view. This should not have happened.");
291 * <p>showNavigator</p>
293 * @return the TaxonNavigator instance if present
295 public static TaxonNavigator
showNavigator() {
296 return (TaxonNavigator
) showView(TaxonNavigator
.ID
);
300 * <p>getNavigator</p>
302 * @param restore a boolean.
303 * @return a {@link eu.etaxonomy.taxeditor.navigation.navigator.TaxonNavigator} object.
305 public static TaxonNavigator
getNavigator(boolean restore
) {
306 return (TaxonNavigator
) getView(TaxonNavigator
.ID
, restore
);
310 * <p>getOpenEditors</p>
312 * @return a {@link java.util.Set} object.
314 public static Set
<IEditorPart
> getOpenEditors() {
315 return EditorUtil
.getOpenEditors();
321 * @return a {@link java.lang.String} object.
323 public static String
getPluginId(){
324 return TaxeditorNavigationPlugin
.PLUGIN_ID
;