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
.ExecutionException
;
16 import org
.eclipse
.core
.commands
.NotEnabledException
;
17 import org
.eclipse
.core
.commands
.NotHandledException
;
18 import org
.eclipse
.core
.commands
.common
.NotDefinedException
;
19 import org
.eclipse
.core
.commands
.operations
.IUndoContext
;
20 import org
.eclipse
.core
.commands
.operations
.UndoContext
;
21 import org
.eclipse
.jface
.viewers
.StructuredSelection
;
22 import org
.eclipse
.swt
.widgets
.Display
;
23 import org
.eclipse
.swt
.widgets
.Shell
;
24 import org
.eclipse
.ui
.IEditorPart
;
25 import org
.eclipse
.ui
.IEditorReference
;
26 import org
.eclipse
.ui
.IWorkbenchWindow
;
27 import org
.eclipse
.ui
.PartInitException
;
28 import org
.eclipse
.ui
.handlers
.IHandlerService
;
29 import org
.eclipse
.ui
.navigator
.CommonViewer
;
31 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
32 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
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
.taxeditor
.editor
.EditorUtil
;
38 import eu
.etaxonomy
.taxeditor
.editor
.OpenEditorConfiguration
;
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
.navigation
.internal
.TaxeditorNavigationPlugin
;
43 import eu
.etaxonomy
.taxeditor
.navigation
.navigator
.TaxonNavigator
;
46 * <p>NavigationUtil class.</p>
52 public class NavigationUtil
extends AbstractUtility
{
53 private static IUndoContext defaultUndoContext
;
56 * <p>executeEditHandler</p>
58 public static void executeEditHandler(){
60 String commandId
= "eu.etaxonomy.taxeditor.navigation.command.editSelection";
62 IHandlerService handlerService
= (IHandlerService
) NavigationUtil
.getService(IHandlerService
.class);
64 handlerService
.executeCommand(commandId
, null);
65 } catch (ExecutionException e
) {
66 NavigationUtil
.error(NavigationUtil
.class, e
);
67 } catch (NotDefinedException e
) {
68 NavigationUtil
.error(NavigationUtil
.class, e
);
69 } catch (NotEnabledException e
) {
70 NavigationUtil
.error(NavigationUtil
.class, e
);
71 } catch (NotHandledException e
) {
72 NavigationUtil
.error(NavigationUtil
.class, e
);
79 * @param selectedObject a {@link eu.etaxonomy.cdm.model.common.CdmBase} object.
81 public static void openEditor(CdmBase selectedObject
){
82 UUID entityUuid
= selectedObject
.getUuid();
84 if(selectedObject
instanceof TaxonNode
){
85 EditorUtil
.openTaxonNode(entityUuid
);
86 }else if(selectedObject
instanceof TaxonBase
){
87 EditorUtil
.openTaxonBase(entityUuid
);
88 }else if(selectedObject
instanceof TaxonNameBase
){
89 // TODO open bulk editor
90 warningDialog("Not implemented yet", NavigationUtil
.class, "You tried to open a name. This is not handled by the software yet.");
92 warningDialog("Unsupported Type", NavigationUtil
.class, "No editor exists for the current selection: " + selectedObject
);
94 } catch (PartInitException e
) {
95 NavigationUtil
.error(NavigationUtil
.class, "Error opening the editor", e
);
96 } catch (Exception e
) {
97 EditorUtil
.warningDialog("Could not create Taxon", NavigationUtil
.class, e
.getMessage());
104 * @param parentNodeUuid a {@link java.util.UUID} object.
106 public static void openEmpty(UUID parentNodeUuid
) {
108 EditorUtil
.openEmpty(parentNodeUuid
);
109 } catch (PartInitException e
) {
110 NavigationUtil
.error(NavigationUtil
.class, "Error opening the editor", e
);
117 * @param configuration a eu.etaxonomy.taxeditor.editor.OpenEditorConfiguration object.
119 public static void openEditor(OpenEditorConfiguration configuration
) {
120 // TODO Auto-generated method stub
127 * @return a {@link org.eclipse.swt.widgets.Shell} object.
129 public static Shell
getShell() {
130 return getActiveWindow().getShell();
134 * <p>getActiveWindow</p>
136 * @return a {@link org.eclipse.ui.IWorkbenchWindow} object.
138 public static IWorkbenchWindow
getActiveWindow() {
139 return TaxeditorNavigationPlugin
.getDefault().getWorkbench().
140 getActiveWorkbenchWindow();
144 * <p>getWorkbenchUndoContext</p>
146 * @return a {@link org.eclipse.core.commands.operations.IUndoContext} object.
148 public static IUndoContext
getWorkbenchUndoContext() {
149 return TaxeditorEditorPlugin
.getDefault().getWorkbench().
150 getOperationSupport().getUndoContext();
154 * <p>getUndoContext</p>
156 * @return a {@link org.eclipse.core.commands.operations.IUndoContext} object.
158 public static IUndoContext
getUndoContext() {
159 // FIXME this has to be more specific. Every widget has to have its own undo context
160 // return IOperationHistory.GLOBAL_UNDO_CONTEXT;
162 // Plug-ins that wish their operations to be undoable from workbench views
163 // such as the Navigator or Package Explorer should assign the workbench
164 // undo context to their operations.
165 if (defaultUndoContext
== null) {
166 defaultUndoContext
= new UndoContext();
168 return defaultUndoContext
;
172 * Whether a taxonNode has unsaved changes.
174 * @param taxonNode a {@link eu.etaxonomy.cdm.model.taxon.TaxonNode} object.
177 public static boolean isDirty(TaxonNode taxonNode
){
179 for (IEditorReference reference
: getActivePage().getEditorReferences()) {
182 if (reference
.getEditorInput() instanceof TaxonEditorInput
) {
183 TaxonEditorInput editorInput
= (TaxonEditorInput
) reference
.getEditorInput();
184 if(editorInput
.getTaxonNode().equals(taxonNode
) && reference
.isDirty()){
188 } catch (PartInitException e
) {
189 NavigationUtil
.error(NavigationUtil
.class, e
.getMessage(), e
);
190 throw new RuntimeException(e
);
198 * <p>selectInNavigator</p>
200 * @param element a {@link java.lang.Object} object.
201 * @param parentElement a {@link java.lang.Object} object.
203 public static void selectInNavigator(final Object element
, final Object parentElement
) {
204 Display
.getDefault().asyncExec(new Runnable(){
207 TaxonNavigator navigator
= showNavigator();
209 if (navigator
!= null) {
210 CommonViewer viewer
= navigator
.getCommonViewer();
211 if (viewer
!= null) {
212 if (parentElement
!= null) {
213 viewer
.setExpandedState(parentElement
, true);
215 viewer
.setSelection(new StructuredSelection((TaxonNode
) element
));
226 * @param selection a {@link java.lang.Object} object.
228 public static void openSearch(Object selection
) {
229 if(selection
instanceof Taxon
){
230 Taxon taxon
= (Taxon
) selection
;
232 handleOpeningOfMultipleTaxonNodes(taxon
.getTaxonNodes());
234 }else if(selection
instanceof Synonym
){
235 Synonym synonym
= (Synonym
) selection
;
237 handleOpeningOfMultipleTaxa(synonym
.getAcceptedTaxa());
240 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.");
245 private static void handleOpeningOfMultipleTaxa(Set
<Taxon
> acceptedTaxa
) {
246 if(acceptedTaxa
.size() == 1){
247 openEditor(acceptedTaxa
.iterator().next());
248 }else if(acceptedTaxa
.size() > 1){
249 // FIXME implement a dialog that shows all possible taxa and let the user choose which he wants to open.
250 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." +
251 " This case is not handled yet by the software.");
252 }else if(acceptedTaxa
.size() == 0){
253 // this is an undesired state
254 warningDialog("Incorrect state", NavigationUtil
.class, "The accepted taxon is not in a taxonomic view. This should not have happened.");
261 private static void handleOpeningOfMultipleTaxonNodes(
262 Set
<TaxonNode
> taxonNodes
) {
264 if(taxonNodes
.size() == 1){
265 openEditor(taxonNodes
.iterator().next());
266 }else if(taxonNodes
.size() > 1){
267 // FIXME implement a dialog that shows all possible taxa and let the user choose which he wants to open.
268 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." +
269 " This case is not handled yet by the software.");
270 }else if(taxonNodes
.size() == 0){
271 // this is an undesired state
272 warningDialog("Incorrect state", NavigationUtil
.class, "The accepted taxon is not in a taxonomic view. This should not have happened.");
277 * <p>showNavigator</p>
279 * @return the TaxonNavigator instance if present
281 public static TaxonNavigator
showNavigator() {
282 return (TaxonNavigator
) showView(TaxonNavigator
.ID
);
286 * <p>getNavigator</p>
288 * @param restore a boolean.
289 * @return a {@link eu.etaxonomy.taxeditor.navigation.navigator.TaxonNavigator} object.
291 public static TaxonNavigator
getNavigator(boolean restore
) {
292 return (TaxonNavigator
) getView(TaxonNavigator
.ID
, restore
);
296 * <p>getOpenEditors</p>
298 * @return a {@link java.util.Set} object.
300 public static Set
<IEditorPart
> getOpenEditors() {
301 return EditorUtil
.getOpenEditors();
307 * @return a {@link java.lang.String} object.
309 protected static String
getPluginId(){
310 return TaxeditorNavigationPlugin
.PLUGIN_ID
;