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
.Collection
;
13 import java
.util
.UUID
;
15 import org
.eclipse
.core
.commands
.ExecutionEvent
;
16 import org
.eclipse
.core
.commands
.operations
.IOperationHistory
;
17 import org
.eclipse
.core
.commands
.operations
.IUndoContext
;
18 import org
.eclipse
.e4
.ui
.model
.application
.ui
.basic
.MPart
;
19 import org
.eclipse
.e4
.ui
.workbench
.modeling
.EPartService
;
20 import org
.eclipse
.e4
.ui
.workbench
.modeling
.EPartService
.PartState
;
21 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
22 import org
.eclipse
.jface
.viewers
.ISelection
;
23 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
24 import org
.eclipse
.jface
.viewers
.TreeNode
;
25 import org
.eclipse
.swt
.widgets
.Shell
;
26 import org
.eclipse
.ui
.IEditorInput
;
27 import org
.eclipse
.ui
.IEditorPart
;
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
.occurrence
.DerivedUnit
;
33 import eu
.etaxonomy
.cdm
.model
.occurrence
.FieldUnit
;
34 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationBase
;
35 import eu
.etaxonomy
.cdm
.model
.taxon
.Synonym
;
36 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
37 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
38 import eu
.etaxonomy
.taxeditor
.editor
.e4
.TaxonEditorInputE4
;
39 import eu
.etaxonomy
.taxeditor
.editor
.group
.authority
.CdmAuthorityEditor
;
40 import eu
.etaxonomy
.taxeditor
.editor
.group
.authority
.CdmAuthorityEditorInput
;
41 import eu
.etaxonomy
.taxeditor
.editor
.internal
.TaxeditorEditorPlugin
;
42 import eu
.etaxonomy
.taxeditor
.editor
.key
.KeyEditor
;
43 import eu
.etaxonomy
.taxeditor
.editor
.key
.polytomous
.PolytomousKeyEditorInput
;
44 import eu
.etaxonomy
.taxeditor
.editor
.l10n
.Messages
;
45 import eu
.etaxonomy
.taxeditor
.editor
.name
.e4
.TaxonNameEditorE4
;
46 import eu
.etaxonomy
.taxeditor
.editor
.view
.checklist
.ChecklistEditor
;
47 import eu
.etaxonomy
.taxeditor
.editor
.view
.checklist
.ChecklistEditorInput
;
48 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
49 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
50 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
53 * Utility for the editor package
59 public class EditorUtil
extends AbstractUtility
{
61 private static boolean isSaving
= false;
64 * Opens a new editor window with the given input
70 * @throws PartInitException
72 private static IEditorPart
open(final IEditorInput input
,
73 final String editorId
) throws PartInitException
{
74 IEditorPart editor
= getActivePage().openEditor(input
, editorId
);
76 editor
.getEditorInput() != null &&
77 input
!= editor
.getEditorInput() &&
78 input
instanceof CdmEntitySessionInput
) {
79 ((CdmEntitySessionInput
)input
).dispose();
84 public static void open(PolytomousKeyEditorInput input
)
85 throws PartInitException
{
86 open(input
, KeyEditor
.ID
);
89 public static void open(CdmAuthorityEditorInput input
)
90 throws PartInitException
{
91 open(input
, CdmAuthorityEditor
.ID
);
95 * Opens a new ChecklistView for the given input
96 * @param input a {@link ChecklistEditorInput} representing the selected checklist
97 * @throws PartInitException
99 public static void open(ChecklistEditorInput input
)
100 throws PartInitException
{
101 open(input
, ChecklistEditor
.ID
);
105 * Opens a new {@link DataImportEditor} for the given input
106 * @param input a {@link DataImportEditorInput}
107 * @throws PartInitException
109 // public static void open(DataImportEditorInput<?> input)
110 // throws PartInitException {
111 // if(input instanceof BioCaseEditorInput){
112 // open(input, SpecimenImportEditor.ID);
114 // else if(input instanceof GbifImportEditorInput){
115 // open(input, GbifImportEditor.ID);
120 * Taxon Editors may be opened by supplying a taxon node uuid. Session gets
121 * initialised here and is passed to the editor
123 * @param taxonNodeUuid
124 * a {@link java.util.UUID} object.
125 * @throws java.lang.Exception
128 public static void openTaxonNodeE4(UUID taxonNodeUuid
) throws Exception
{
129 //FIXME E4 this can probably be removed when fully migrated
130 TaxonEditorInputE4 input
= TaxonEditorInputE4
.NewInstance(taxonNodeUuid
);
131 EPartService partService
= TaxeditorEditorPlugin
.getDefault().getWorkbench().getActiveWorkbenchWindow().getService(EPartService
.class);
132 MPart part
= partService
.createPart("eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditorE4");
133 partService
.showPart(part
, PartState
.ACTIVATE
);
134 TaxonNameEditorE4 editor
= (TaxonNameEditorE4
) part
.getObject();
138 public static void openTaxonBaseE4(UUID taxonBaseUuid
) throws PartInitException
{
139 //FIXME E4 this can probably be removed when fully migrated
140 TaxonBase taxonBase
= CdmStore
.getService(ITaxonService
.class).find(taxonBaseUuid
);
142 if (taxonBase
!= null && taxonBase
.isOrphaned()) {
143 if(taxonBase
.isInstanceOf(Synonym
.class)){
144 MessagingUtils
.warningDialog(Messages
.EditorUtil_ORPHAN_ACCEPTED_TAXON
, TaxonEditorInputE4
.class, Messages
.EditorUtil_ORPHAN_ACCEPTED_TAXON_MESSAGE
);
148 MessagingUtils
.warningDialog(Messages
.EditorUtil_ORPHAN_TAXON
, TaxonEditorInputE4
.class, Messages
.EditorUtil_ORPHAN_TAXON_MESSAGE
);
153 TaxonEditorInputE4 input
= TaxonEditorInputE4
.NewInstanceFromTaxonBase(taxonBaseUuid
);
154 EPartService partService
= TaxeditorEditorPlugin
.getDefault().getWorkbench().getActiveWorkbenchWindow().getService(EPartService
.class);
155 MPart part
= partService
.createPart("eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditorE4");
156 partService
.showPart(part
, PartState
.ACTIVATE
);
157 TaxonNameEditorE4 editor
= (TaxonNameEditorE4
) part
.getObject();
163 * findEditorByTaxonNodeUuid
166 * @param taxonNodeUuid
167 * a {@link java.util.UUID} object.
168 * @return a {@link org.eclipse.ui.IEditorPart} object.
169 * @throws java.lang.Exception
172 public static IEditorPart
findEditorByTaxonNodeUuid(UUID taxonNodeUuid
)
174 IEditorInput input
= TaxonEditorInput
.NewInstance(taxonNodeUuid
);
175 return getActivePage().findEditor(input
);
179 * An uninitialized taxon is one that hasn't been saved yet. As such, it
180 * should appear in neither the list of recent names nor in the taxonomic
183 * @throws org.eclipse.ui.PartInitException
185 * @param parentNodeUuid
186 * a {@link java.util.UUID} object.
188 public static void openEmptyE4(UUID parentNodeUuid
) throws PartInitException
{
189 TaxonEditorInputE4 input
= TaxonEditorInputE4
190 .NewEmptyInstance(parentNodeUuid
);
191 EPartService partService
= TaxeditorEditorPlugin
.getDefault().getWorkbench().getActiveWorkbenchWindow().getService(EPartService
.class);
192 MPart part
= partService
.createPart("eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditorE4");
193 partService
.showPart(part
, PartState
.ACTIVATE
);
194 TaxonNameEditorE4 editor
= (TaxonNameEditorE4
) part
.getObject();
206 public static void setSaving(boolean isSaving
) {
207 EditorUtil
.isSaving
= isSaving
;
217 public static boolean isSaving() {
226 * @return a {@link org.eclipse.core.commands.operations.IUndoContext}
229 public static IUndoContext
getUndoContext() {
230 return IOperationHistory
.GLOBAL_UNDO_CONTEXT
;
239 * a {@link org.eclipse.ui.IEditorPart} object.
241 * a {@link org.eclipse.swt.widgets.Shell} object.
244 public static boolean forceUserSave(IEditorPart editor
, Shell shell
) {
245 if (editor
.isDirty()) {
247 boolean doSave
= MessageDialog
248 .openConfirm(shell
, Messages
.EditorUtil_COMFIRM_SAVE
,
249 Messages
.EditorUtil_CONFIRM_SAVE_MESSAGE
);
255 editor
.doSave(AbstractUtility
.getMonitor());
260 public static boolean forceUserSaveE4Editor(TaxonNameEditorE4 editor
, Shell shell
) {
261 if (editor
.isDirty()) {
263 boolean doSave
= MessageDialog
264 .openConfirm(shell
, Messages
.EditorUtil_COMFIRM_SAVE
,
265 Messages
.EditorUtil_CONFIRM_SAVE_MESSAGE
);
271 editor
.save(AbstractUtility
.getMonitor());
282 * a {@link org.eclipse.core.commands.ExecutionEvent} object.
283 * @return a {@link org.eclipse.jface.viewers.IStructuredSelection} object.
285 public static IStructuredSelection
getSelection(ExecutionEvent event
) {
286 IEditorPart activeEditor
= HandlerUtil
.getActiveEditor(event
);
288 return (IStructuredSelection
) activeEditor
.getSite()
289 .getSelectionProvider().getSelection();
297 * @return a {@link java.lang.String} object.
299 public static String
getPluginId() {
300 return TaxeditorEditorPlugin
.PLUGIN_ID
;
303 public static void openPolytomousKey(UUID polytomousKeyUuid
)
305 PolytomousKeyEditorInput input
= PolytomousKeyEditorInput
306 .NewInstance(polytomousKeyUuid
);
310 // public static void openPolytomousKeyEditor(UUID polytomousKeyUuid, String name)
311 // throws Exception {
312 // PolytomousKeyEditorInput input = new PolytomousKeyEditorInput(polytomousKeyUuid, name);
316 public static void openCdmAuthorities(UUID groupUuid
)
318 CdmAuthorityEditorInput input
= CdmAuthorityEditorInput
.NewInstance(groupUuid
);
323 * Iterates recursively over all originals having the given specimen as a derivate.
324 * The first {@link DerivedUnit} with no more originals or the first {@link FieldUnit} is returned
325 * @param specimen the start element for which the originals are iterated recursively
326 * @return either a FieldUnit or a the topmost DerivedUnit (which can be itself)
328 public static SpecimenOrObservationBase
<?
> getTopMostDerivate(SpecimenOrObservationBase
<?
> specimen
){
329 if(specimen
.isInstanceOf(FieldUnit
.class)){
332 else if(specimen
instanceof DerivedUnit
333 && ((DerivedUnit
) specimen
).getOriginals()!=null
334 && !((DerivedUnit
) specimen
).getOriginals().isEmpty()){
335 for(SpecimenOrObservationBase
<?
> original
:((DerivedUnit
) specimen
).getOriginals()){
336 return getTopMostDerivate(original
);
338 //needed to add this for compilation although this is unreachable
347 * If the current selection is a single {@link TreeNode} it will be returned.
348 * @param selection the selection to check
349 * @return the selected TreeNode or <code>null</code> if no TreeNode selected
351 public static TreeNode
getTreeNodeOfSelection(ISelection selection
){
352 if(selection
instanceof IStructuredSelection
353 && ((IStructuredSelection
) selection
).size()==1
354 && ((IStructuredSelection
) selection
).getFirstElement() instanceof TreeNode
){
355 return (TreeNode
) ((IStructuredSelection
) selection
).getFirstElement();
361 public static void closeObsoleteEditor(TaxonNode taxonNode
, EPartService partService
){
362 String treeIndex
= taxonNode
.treeIndex();
363 Collection
<MPart
> parts
= partService
.getParts();
364 for (MPart part
: parts
) {
365 Object object
= part
.getObject();
366 if(object
instanceof TaxonNameEditorE4
){
367 TaxonNameEditorE4 taxonEditor
= (TaxonNameEditorE4
)object
;
368 TaxonNode node
= taxonEditor
.getEditorInput().getTaxonNode();
369 if(node
.treeIndex().startsWith(treeIndex
)){
370 partService
.hidePart(part
, true);