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
;
12 import java
.util
.HashMap
;
13 import java
.util
.HashSet
;
16 import java
.util
.UUID
;
18 import org
.eclipse
.core
.commands
.Command
;
19 import org
.eclipse
.core
.commands
.ParameterizedCommand
;
20 import org
.eclipse
.core
.commands
.common
.NotDefinedException
;
21 import org
.eclipse
.core
.commands
.operations
.IUndoContext
;
22 import org
.eclipse
.core
.commands
.operations
.UndoContext
;
23 import org
.eclipse
.jface
.viewers
.StructuredSelection
;
24 import org
.eclipse
.jface
.wizard
.WizardDialog
;
25 import org
.eclipse
.swt
.widgets
.Display
;
26 import org
.eclipse
.swt
.widgets
.Shell
;
27 import org
.eclipse
.ui
.IEditorPart
;
28 import org
.eclipse
.ui
.IEditorReference
;
29 import org
.eclipse
.ui
.IWorkbenchWindow
;
30 import org
.eclipse
.ui
.PartInitException
;
31 import org
.eclipse
.ui
.PlatformUI
;
32 import org
.eclipse
.ui
.commands
.ICommandService
;
33 import org
.eclipse
.ui
.handlers
.IHandlerService
;
34 import org
.eclipse
.ui
.navigator
.CommonViewer
;
36 import eu
.etaxonomy
.cdm
.api
.service
.IClassificationService
;
37 import eu
.etaxonomy
.cdm
.api
.service
.INameService
;
38 import eu
.etaxonomy
.cdm
.api
.service
.ITaxonNodeService
;
39 import eu
.etaxonomy
.cdm
.api
.service
.ITaxonService
;
40 import eu
.etaxonomy
.cdm
.model
.common
.ICdmBase
;
41 import eu
.etaxonomy
.cdm
.model
.description
.PolytomousKey
;
42 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
43 import eu
.etaxonomy
.cdm
.model
.taxon
.Classification
;
44 import eu
.etaxonomy
.cdm
.model
.taxon
.Synonym
;
45 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
46 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
47 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
48 import eu
.etaxonomy
.cdm
.persistence
.dto
.UuidAndTitleCache
;
49 import eu
.etaxonomy
.taxeditor
.editor
.EditorUtil
;
50 import eu
.etaxonomy
.taxeditor
.editor
.TaxonEditorInput
;
51 import eu
.etaxonomy
.taxeditor
.editor
.internal
.TaxeditorEditorPlugin
;
52 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
53 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
54 import eu
.etaxonomy
.taxeditor
.navigation
.internal
.TaxeditorNavigationPlugin
;
55 import eu
.etaxonomy
.taxeditor
.navigation
.l10n
.Messages
;
56 import eu
.etaxonomy
.taxeditor
.navigation
.navigator
.TaxonNavigator
;
57 import eu
.etaxonomy
.taxeditor
.newWizard
.NewClassificationWizard
;
58 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
59 import eu
.etaxonomy
.taxeditor
.store
.internal
.TaxeditorStorePlugin
;
62 * <p>NavigationUtil class.</p>
68 public class NavigationUtil
extends AbstractUtility
{
70 private static final String NOT_IMPLEMENTED_YET
= Messages
.NavigationUtil_NOT_IMPLEMENTED
;
71 private static final String ERROR_OPENING_THE_EDITOR
= Messages
.NavigationUtil_OPEN_ERROR
;
72 private static IUndoContext defaultUndoContext
;
77 * @param selectedObject a {@link eu.etaxonomy.cdm.model.common.CdmBase} object.
79 public static void openEditor(UuidAndTitleCache uuidAndTitleCache
){
80 Class type
= uuidAndTitleCache
.getType();
81 ICdmBase cdmBase
= null;
82 if(type
.equals(Classification
.class)){
83 cdmBase
= CdmStore
.getService(IClassificationService
.class).load(uuidAndTitleCache
.getUuid());
85 else if(type
.equals(TaxonNode
.class)){
86 cdmBase
= CdmStore
.getService(ITaxonNodeService
.class).load(uuidAndTitleCache
.getUuid());
88 else if(TaxonBase
.class.isAssignableFrom(type
)){
89 cdmBase
= CdmStore
.getService(ITaxonService
.class).load(uuidAndTitleCache
.getUuid());
91 else if(type
.equals(TaxonNameBase
.class)){
92 cdmBase
= CdmStore
.getService(INameService
.class).load(uuidAndTitleCache
.getUuid());
95 MessagingUtils
.warningDialog(Messages
.NavigationUtil_UNKNOWN_TYPE
, NavigationUtil
.class, Messages
.NavigationUtil_UNKNOWN_TYPE_MESSAGE
);
101 MessagingUtils
.warningDialog(Messages
.NavigationUtil_NOT_FOUND
, NavigationUtil
.class, Messages
.NavigationUtil_NOT_FOUND_MESSAGE
);
105 public static void openEditor(ICdmBase selectedObject
){
106 UUID entityUuid
= selectedObject
.getUuid();
108 if(selectedObject
instanceof Classification
){
109 NewClassificationWizard classificationWizard
= new NewClassificationWizard();
110 classificationWizard
.init(null, null);
111 classificationWizard
.setEntity((Classification
) selectedObject
);
112 WizardDialog dialog
= new WizardDialog(PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getShell(), classificationWizard
);
115 else if(selectedObject
instanceof TaxonNode
){
116 EditorUtil
.openTaxonNode(entityUuid
);
117 }else if(selectedObject
instanceof TaxonBase
){
118 TaxonBase taxonBase
= (TaxonBase
)selectedObject
;
119 if(taxonBase
.isOrphaned()){
120 openInBulkEditor(taxonBase
);
123 EditorUtil
.openTaxonBase(entityUuid
);
125 }else if(selectedObject
instanceof TaxonNameBase
){
126 openInBulkEditor(selectedObject
);
127 }else if(selectedObject
instanceof PolytomousKey
){
128 EditorUtil
.openPolytomousKey(entityUuid
);
130 MessagingUtils
.warningDialog(Messages
.NavigationUtil_UNSUPPORTED_TYPE
, NavigationUtil
.class, Messages
.NavigationUtil_UNSUPPORTED_TYPE_MESSAGE
+ selectedObject
);
132 } catch (PartInitException e
) {
133 MessagingUtils
.error(NavigationUtil
.class, ERROR_OPENING_THE_EDITOR
, e
);
134 } catch (Exception e
) {
135 MessagingUtils
.errorDialog(Messages
.NavigationUtil_CREATE_FAILED
,
136 NavigationUtil
.class,
137 e
.getMessage(), TaxeditorStorePlugin
.PLUGIN_ID
,
144 private static void openInBulkEditor(ICdmBase selectedObject
) {
145 ICommandService commandService
= (ICommandService
)PlatformUI
.getWorkbench().getService(ICommandService
.class);
146 IHandlerService handlerService
= (IHandlerService
)PlatformUI
.getWorkbench().getService(IHandlerService
.class);
147 String openInBulkEditorCommand
= "eu.etaxonomy.taxeditor.bulkeditor.openBulkEditorForIdentifiableEntity"; //$NON-NLS-1$
148 Command command
= commandService
.getCommand(openInBulkEditorCommand
);
149 if(command
.isDefined()){
150 Map
<String
, UUID
> params
= new HashMap
<String
, UUID
>();
151 params
.put(openInBulkEditorCommand
+".uuid", selectedObject
.getUuid()); //$NON-NLS-1$
152 ParameterizedCommand parameterizedCommand
= ParameterizedCommand
.generateCommand(command
, params
);
154 if(parameterizedCommand
!=null){
155 handlerService
.executeCommand(parameterizedCommand
, null);
159 handlerService
.executeCommand(command
.getId(), null);
162 } catch (NotDefinedException nde
) {
163 throw new RuntimeException("Could not find open command: " + command
.getId()); //$NON-NLS-1$
164 } catch (Exception exception
) {
165 MessagingUtils
.error(NavigationUtil
.class, "An exception occured while trying to execute "+command
.getId(), exception
); //$NON-NLS-1$
173 * @param parentNodeUuid a {@link java.util.UUID} object.
175 public static void openEmpty(UUID parentNodeUuid
) {
177 EditorUtil
.openEmpty(parentNodeUuid
);
178 } catch (PartInitException e
) {
179 MessagingUtils
.error(NavigationUtil
.class, ERROR_OPENING_THE_EDITOR
, e
);
186 * @return a {@link org.eclipse.swt.widgets.Shell} object.
188 public static Shell
getShell() {
189 return getActiveWindow().getShell();
193 * <p>getActiveWindow</p>
195 * @return a {@link org.eclipse.ui.IWorkbenchWindow} object.
197 public static IWorkbenchWindow
getActiveWindow() {
198 return TaxeditorNavigationPlugin
.getDefault().getWorkbench().
199 getActiveWorkbenchWindow();
203 * <p>getWorkbenchUndoContext</p>
205 * @return a {@link org.eclipse.core.commands.operations.IUndoContext} object.
207 public static IUndoContext
getWorkbenchUndoContext() {
208 return TaxeditorEditorPlugin
.getDefault().getWorkbench().
209 getOperationSupport().getUndoContext();
213 * <p>getUndoContext</p>
215 * @return a {@link org.eclipse.core.commands.operations.IUndoContext} object.
217 public static IUndoContext
getUndoContext() {
218 // FIXME this has to be more specific. Every widget has to have its own undo context
219 // return IOperationHistory.GLOBAL_UNDO_CONTEXT;
221 // Plug-ins that wish their operations to be undoable from workbench views
222 // such as the Navigator or Package Explorer should assign the workbench
223 // undo context to their operations.
224 if (defaultUndoContext
== null) {
225 defaultUndoContext
= new UndoContext();
227 return defaultUndoContext
;
231 * Whether a taxonNode has unsaved changes.
233 * @param taxonNode a {@link eu.etaxonomy.cdm.model.taxon.TaxonNode} object.
236 public static boolean isDirty(TaxonNode taxonNode
){
238 for (IEditorReference reference
: getActivePage().getEditorReferences()) {
241 if (reference
.getEditorInput() instanceof TaxonEditorInput
) {
242 TaxonEditorInput editorInput
= (TaxonEditorInput
) reference
.getEditorInput();
243 if(editorInput
.getTaxonNode().equals(taxonNode
) && reference
.isDirty()){
247 } catch (PartInitException e
) {
248 MessagingUtils
.error(NavigationUtil
.class, e
.getMessage(), e
);
249 throw new RuntimeException(e
);
257 * <p>selectInNavigator</p>
259 * @param element a {@link java.lang.Object} object.
260 * @param parentElement a {@link java.lang.Object} object.
262 public static void selectInNavigator(final Object element
, final Object parentElement
) {
263 Display
.getDefault().asyncExec(new Runnable(){
267 TaxonNavigator navigator
= showNavigator();
269 if (navigator
!= null) {
270 CommonViewer viewer
= navigator
.getCommonViewer();
271 if (viewer
!= null) {
272 if (parentElement
!= null) {
273 viewer
.setExpandedState(parentElement
, true);
275 viewer
.setSelection(new StructuredSelection(element
));
286 * @param selection a {@link java.lang.Object} object.
288 public static void openSearch(Object selection
) {
289 if(selection
instanceof Taxon
){
290 Taxon taxon
= (Taxon
) selection
;
292 handleOpeningOfMultipleTaxonNodes(taxon
.getTaxonNodes());
294 }else if(selection
instanceof Synonym
){
295 Synonym synonym
= (Synonym
) selection
;
297 Set
<Taxon
> accTaxa
= new HashSet
<Taxon
>();
298 if (synonym
.getAcceptedTaxon() != null){
299 accTaxa
.add(synonym
.getAcceptedTaxon());
301 handleOpeningOfMultipleTaxa(accTaxa
);
304 MessagingUtils
.warningDialog(NOT_IMPLEMENTED_YET
, NavigationUtil
.class, Messages
.NavigationUtil_ORPHAN_NAME_MESSAGE
);
309 private static void handleOpeningOfMultipleTaxa(Set
<Taxon
> acceptedTaxa
) {
310 if(acceptedTaxa
.size() == 1){
311 openEditor(acceptedTaxa
.iterator().next());
312 }else if(acceptedTaxa
.size() > 1){
313 // FIXME implement a dialog that shows all possible taxa and let the user choose which he wants to open.
314 MessagingUtils
.warningDialog(NOT_IMPLEMENTED_YET
, NavigationUtil
.class, Messages
.NavigationUtil_MULTI_TREE
);
315 }else if(acceptedTaxa
.size() == 0){
316 // this is an undesired state
317 MessagingUtils
.warningDialog(NOT_IMPLEMENTED_YET
, NavigationUtil
.class, Messages
.NavigationUtil_ORPHAN_TAXON
);
324 private static void handleOpeningOfMultipleTaxonNodes(
325 Set
<TaxonNode
> taxonNodes
) {
327 if(taxonNodes
.size() == 1){
328 openEditor(taxonNodes
.iterator().next());
329 }else if(taxonNodes
.size() > 1){
330 // FIXME implement a dialog that shows all possible taxa and let the user choose which he wants to open.
331 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. This case is not handled yet by the software.");
332 }else if(taxonNodes
.size() == 0){
333 // this is an undesired state
334 MessagingUtils
.warningDialog(Messages
.NavigationUtil_INCORRECT_STATE
, NavigationUtil
.class, Messages
.NavigationUtil_INCORRECT_STATE_MESSAGE
);
339 * <p>showNavigator</p>
341 * @return the TaxonNavigator instance if present
343 public static TaxonNavigator
showNavigator() {
344 return (TaxonNavigator
) showView(TaxonNavigator
.ID
);
348 * <p>getNavigator</p>
350 * @param restore a boolean.
351 * @return a {@link eu.etaxonomy.taxeditor.navigation.navigator.TaxonNavigator} object.
353 public static TaxonNavigator
getNavigator(boolean restore
) {
354 return (TaxonNavigator
) getView(TaxonNavigator
.ID
, restore
);
358 * <p>getOpenEditors</p>
360 * @return a {@link java.util.Set} object.
362 public static Set
<IEditorPart
> getOpenEditors() {
363 return EditorUtil
.getOpenEditors();
369 * @return a {@link java.lang.String} object.
371 public static String
getPluginId(){
372 return TaxeditorNavigationPlugin
.PLUGIN_ID
;