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
.editor
;
12 import java
.util
.HashSet
;
14 import java
.util
.UUID
;
16 import org
.eclipse
.core
.commands
.ExecutionEvent
;
17 import org
.eclipse
.core
.commands
.operations
.IOperationHistory
;
18 import org
.eclipse
.core
.commands
.operations
.IUndoContext
;
19 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
20 import org
.eclipse
.jface
.viewers
.ISelection
;
21 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
22 import org
.eclipse
.jface
.viewers
.TreeNode
;
23 import org
.eclipse
.swt
.widgets
.Shell
;
24 import org
.eclipse
.ui
.IEditorInput
;
25 import org
.eclipse
.ui
.IEditorPart
;
26 import org
.eclipse
.ui
.IEditorReference
;
27 import org
.eclipse
.ui
.IWorkbenchPage
;
28 import org
.eclipse
.ui
.PartInitException
;
29 import org
.eclipse
.ui
.handlers
.HandlerUtil
;
31 import eu
.etaxonomy
.cdm
.api
.service
.ITaxonService
;
32 import eu
.etaxonomy
.cdm
.model
.common
.ITreeNode
;
33 import eu
.etaxonomy
.cdm
.model
.occurrence
.DerivedUnit
;
34 import eu
.etaxonomy
.cdm
.model
.occurrence
.FieldUnit
;
35 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationBase
;
36 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
37 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
38 import eu
.etaxonomy
.taxeditor
.editor
.group
.authority
.CdmAuthorityEditor
;
39 import eu
.etaxonomy
.taxeditor
.editor
.group
.authority
.CdmAuthorityEditorInput
;
40 import eu
.etaxonomy
.taxeditor
.editor
.internal
.TaxeditorEditorPlugin
;
41 import eu
.etaxonomy
.taxeditor
.editor
.key
.KeyEditor
;
42 import eu
.etaxonomy
.taxeditor
.editor
.key
.polytomous
.PolytomousKeyEditorInput
;
43 import eu
.etaxonomy
.taxeditor
.editor
.view
.checklist
.ChecklistEditor
;
44 import eu
.etaxonomy
.taxeditor
.editor
.view
.checklist
.ChecklistEditorInput
;
45 import eu
.etaxonomy
.taxeditor
.editor
.view
.dataimport
.BioCaseEditorInput
;
46 import eu
.etaxonomy
.taxeditor
.editor
.view
.dataimport
.DataImportEditor
;
47 import eu
.etaxonomy
.taxeditor
.editor
.view
.dataimport
.DataImportEditorInput
;
48 import eu
.etaxonomy
.taxeditor
.editor
.view
.dataimport
.GbifImportEditor
;
49 import eu
.etaxonomy
.taxeditor
.editor
.view
.dataimport
.GbifImportEditorInput
;
50 import eu
.etaxonomy
.taxeditor
.editor
.view
.dataimport
.SpecimenImportEditor
;
51 import eu
.etaxonomy
.taxeditor
.editor
.view
.derivate
.DerivateView
;
52 import eu
.etaxonomy
.taxeditor
.editor
.view
.derivate
.DerivateViewEditorInput
;
53 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
54 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
55 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
58 * Utility for the editor package
64 public class EditorUtil
extends AbstractUtility
{
66 private static boolean isSaving
= false;
69 * Opens a new editor window with the given input
75 * @throws PartInitException
77 private static IEditorPart
open(final IEditorInput input
,
78 final String editorId
) throws PartInitException
{
79 return getActivePage().openEditor(input
, editorId
);
83 * Opens a new editor window with the given TaxonEditorInput
86 * a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput}
88 * @throws org.eclipse.ui.PartInitException
91 public static void open(TaxonEditorInput input
) throws PartInitException
{
92 open(input
, MultiPageTaxonEditor
.ID
);
95 public static void open(PolytomousKeyEditorInput input
)
96 throws PartInitException
{
97 open(input
, KeyEditor
.ID
);
100 public static void open(CdmAuthorityEditorInput input
)
101 throws PartInitException
{
102 open(input
, CdmAuthorityEditor
.ID
);
106 * Opens a new DerivateView for the given input
107 * @param input a {@link DerivateViewEditorInput} representing the selected derivate
108 * @throws PartInitException
110 public static void open(DerivateViewEditorInput input
)
111 throws PartInitException
{
112 open(input
, DerivateView
.ID
);
117 * Opens a new ChecklistView for the given input
118 * @param input a {@link ChecklistEditorInput} representing the selected checklist
119 * @throws PartInitException
121 public static void open(ChecklistEditorInput input
)
122 throws PartInitException
{
123 open(input
, ChecklistEditor
.ID
);
127 * Opens a new {@link DataImportEditor} for the given input
128 * @param input a {@link DataImportEditorInput}
129 * @throws PartInitException
131 public static void open(DataImportEditorInput
<?
> input
)
132 throws PartInitException
{
133 if(input
instanceof BioCaseEditorInput
){
134 open(input
, SpecimenImportEditor
.ID
);
136 else if(input
instanceof GbifImportEditorInput
){
137 open(input
, GbifImportEditor
.ID
);
142 * Taxon Editors may be opened by supplying a taxon node uuid. Session gets
143 * initialised here and is passed to the editor
145 * @param taxonNodeUuid
146 * a {@link java.util.UUID} object.
147 * @throws java.lang.Exception
150 public static void openTaxonNode(UUID taxonNodeUuid
) throws Exception
{
151 TaxonEditorInput input
= TaxonEditorInput
.NewInstance(taxonNodeUuid
);
160 * @param taxonBaseUuid
161 * a {@link java.util.UUID} object.
162 * @throws org.eclipse.ui.PartInitException
165 public static void openTaxonBase(UUID taxonBaseUuid
)
166 throws PartInitException
{
167 TaxonBase taxonBase
= CdmStore
.getService(ITaxonService
.class).find(taxonBaseUuid
);
168 if (taxonBase
!= null && taxonBase
.isOrphaned()) {
169 MessagingUtils
.warningDialog("Orphaned Taxon", TaxonEditorInput
.class, "This is an orphaned taxon i.e. a taxon that is not connected to a classification and not having any taxonomic relationships. Editing of orphaned taxon is currently not supported.");
172 TaxonEditorInput input
= TaxonEditorInput
173 .NewInstanceFromTaxonBase(taxonBaseUuid
);
179 * findEditorByTaxonNodeUuid
182 * @param taxonNodeUuid
183 * a {@link java.util.UUID} object.
184 * @return a {@link org.eclipse.ui.IEditorPart} object.
185 * @throws java.lang.Exception
188 public static IEditorPart
findEditorByTaxonNodeUuid(UUID taxonNodeUuid
)
190 IEditorInput input
= TaxonEditorInput
.NewInstance(taxonNodeUuid
);
191 return getActivePage().findEditor(input
);
195 * An uninitialized taxon is one that hasn't been saved yet. As such, it
196 * should appear in neither the list of recent names nor in the taxonomic
199 * @throws org.eclipse.ui.PartInitException
201 * @param parentNodeUuid
202 * a {@link java.util.UUID} object.
204 public static void openEmpty(UUID parentNodeUuid
) throws PartInitException
{
205 TaxonEditorInput input
= TaxonEditorInput
206 .NewEmptyInstance(parentNodeUuid
);
207 open(input
, MultiPageTaxonEditor
.ID
);
209 getActiveMultiPageTaxonEditor().changed(null);
221 public static void setSaving(boolean isSaving
) {
222 EditorUtil
.isSaving
= isSaving
;
232 public static boolean isSaving() {
237 * Returns a set of all currently open <code>MultiPageTaxonEditor</code>s.
239 * @return a {@link java.util.Set} object.
241 public static Set
<IEditorPart
> getOpenEditors() {
242 Set
<IEditorPart
> taxonEditors
= new HashSet
<IEditorPart
>();
244 if (getActivePage() != null) {
245 for (IEditorReference reference
: getActivePage()
246 .getEditorReferences()) {
247 IEditorPart editor
= reference
.getEditor(false);
248 if (editor
instanceof MultiPageTaxonEditor
) {
249 taxonEditors
.add(editor
);
258 * Returns the currently active taxon editor
260 * @return the taxon editor that has focus
262 public static MultiPageTaxonEditor
getActiveMultiPageTaxonEditor() {
263 IEditorPart editorPart
= getActiveEditor();
264 if (editorPart
!= null && editorPart
instanceof MultiPageTaxonEditor
) {
265 MultiPageTaxonEditor editor
= (MultiPageTaxonEditor
) editorPart
;
266 editor
.getConversationHolder().bind();
274 * getActiveEditorPage
278 * a {@link eu.etaxonomy.taxeditor.editor.Page} object.
279 * @return a {@link org.eclipse.ui.IEditorPart} object.
281 public static IEditorPart
getActiveEditorPage(Page page
) {
282 MultiPageTaxonEditor editor
= getActiveMultiPageTaxonEditor();
284 return editor
!= null ? editor
.getPage(page
) : null;
288 * Returns the selection of the currently active taxon editor
290 * @return a {@link org.eclipse.jface.viewers.ISelection} object.
292 public static ISelection
getCurrentSelection() {
293 if (getActiveMultiPageTaxonEditor() == null) {
296 return getActiveMultiPageTaxonEditor().getSite()
297 .getSelectionProvider().getSelection();
307 * a {@link eu.etaxonomy.taxeditor.editor.MultiPageTaxonEditor}
309 * @return a {@link org.eclipse.core.commands.operations.IUndoContext}
312 public static IUndoContext
getUndoContext(MultiPageTaxonEditor editor
) {
313 return editor
.getUndoContext();
321 * @return a {@link org.eclipse.core.commands.operations.IUndoContext}
324 public static IUndoContext
getUndoContext() {
325 return IOperationHistory
.GLOBAL_UNDO_CONTEXT
;
334 * a {@link org.eclipse.ui.IEditorPart} object.
336 * a {@link org.eclipse.swt.widgets.Shell} object.
339 public static boolean forceUserSave(IEditorPart editor
, Shell shell
) {
340 if (editor
.isDirty()) {
342 boolean doSave
= MessageDialog
343 .openConfirm(shell
, "Confirm save",
344 "Warning - this operation will save the editor. This will also save all other unsaved changes " +
345 "in your working editor to the database. Please 'Cancel' if you are not ready to do this.");
351 editor
.doSave(AbstractUtility
.getMonitor());
362 * a {@link org.eclipse.core.commands.ExecutionEvent} object.
363 * @return a {@link org.eclipse.jface.viewers.IStructuredSelection} object.
365 public static IStructuredSelection
getSelection(ExecutionEvent event
) {
366 IEditorPart activeEditor
= HandlerUtil
.getActiveEditor(event
);
368 return (IStructuredSelection
) activeEditor
.getSite()
369 .getSelectionProvider().getSelection();
377 * @return a {@link java.lang.String} object.
379 public static String
getPluginId() {
380 return TaxeditorEditorPlugin
.PLUGIN_ID
;
383 public static void openPolytomousKey(UUID polytomousKeyUuid
)
385 PolytomousKeyEditorInput input
= PolytomousKeyEditorInput
386 .NewInstance(polytomousKeyUuid
);
390 // public static void openPolytomousKeyEditor(UUID polytomousKeyUuid, String name)
391 // throws Exception {
392 // PolytomousKeyEditorInput input = new PolytomousKeyEditorInput(polytomousKeyUuid, name);
396 public static void openCdmAuthorities(UUID groupUuid
)
398 CdmAuthorityEditorInput input
= CdmAuthorityEditorInput
.NewInstance(groupUuid
);
403 * Iterates recursively over all originals having the given specimen as a derivate.
404 * The first {@link DerivedUnit} with no more originals or the first {@link FieldUnit} is returned
405 * @param specimen the start element for which the originals are iterated recursively
406 * @return either a FieldUnit or a the topmost DerivedUnit (which can be itself)
408 public static SpecimenOrObservationBase
<?
> getTopMostDerivate(SpecimenOrObservationBase
<?
> specimen
){
409 if(specimen
.isInstanceOf(FieldUnit
.class)){
412 else if(specimen
instanceof DerivedUnit
413 && ((DerivedUnit
) specimen
).getOriginals()!=null
414 && !((DerivedUnit
) specimen
).getOriginals().isEmpty()){
415 for(SpecimenOrObservationBase
<?
> original
:((DerivedUnit
) specimen
).getOriginals()){
416 return getTopMostDerivate(original
);
418 //needed to add this for compilation although this is unreachable
427 * Iterates recursively over all originals having the given specimen as a derivate.
428 * If a {@link FieldUnit} is found it is returned
429 * @param specimen the start element for which the originals are iterated recursively
430 * @return the FieldUnit if found, <code>null</code> otherwise
432 public static FieldUnit
getFieldUnit(SpecimenOrObservationBase
<?
> specimen
){
433 SpecimenOrObservationBase
<?
> topMostDerivate
= getTopMostDerivate(specimen
);
434 if(topMostDerivate
instanceof FieldUnit
) {
435 return (FieldUnit
) topMostDerivate
;
441 * If the current selection is a single {@link TreeNode} it will be returned.
442 * @param selection the selection to check
443 * @return the selected TreeNode or <code>null</code> if no TreeNode selected
445 public static TreeNode
getTreeNodeOfSelection(ISelection selection
){
446 if(selection
instanceof IStructuredSelection
447 && ((IStructuredSelection
) selection
).size()==1
448 && ((IStructuredSelection
) selection
).getFirstElement() instanceof TreeNode
){
449 return (TreeNode
) ((IStructuredSelection
) selection
).getFirstElement();
456 * Opens a taxon editor for the given object if the given object is a valid input.
457 * @param object the object for which the editor will be opened
458 * @throws PartInitException
460 public static void openTaxonEditor(Object object
) throws PartInitException
{
461 if(object
instanceof TaxonBase
<?
>){
462 openTaxonBase(((TaxonBase
<?
>) object
).getUuid());
466 public static boolean closeObsoleteEditor(TaxonNode taxonNode
, IWorkbenchPage activePage
){
467 boolean result
= true;
468 for (IEditorReference ref
: activePage
.getEditorReferences()) {
470 String treeIndex
= ((ITreeNode
)taxonNode
).treeIndex();
473 IEditorInput input
= ref
.getEditorInput();
474 if (input
instanceof TaxonEditorInput
) {
475 TaxonNode node
= ((TaxonEditorInput
) input
).getTaxonNode();
476 //if node is a child of taxonNode then close the editor
477 if( ((ITreeNode
) node
).treeIndex().startsWith(treeIndex
)){
478 //if (taxonNode.equals(node)) {
479 result
&= activePage
.closeEditor(ref
.getEditor(false), true);
483 } catch (PartInitException e
) {