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
.MApplication
;
19 import org
.eclipse
.e4
.ui
.model
.application
.ui
.basic
.MPart
;
20 import org
.eclipse
.e4
.ui
.model
.application
.ui
.basic
.MPartStack
;
21 import org
.eclipse
.e4
.ui
.workbench
.modeling
.EModelService
;
22 import org
.eclipse
.e4
.ui
.workbench
.modeling
.EPartService
;
23 import org
.eclipse
.e4
.ui
.workbench
.modeling
.EPartService
.PartState
;
24 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
25 import org
.eclipse
.jface
.viewers
.ISelection
;
26 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
27 import org
.eclipse
.jface
.viewers
.TreeNode
;
28 import org
.eclipse
.swt
.widgets
.Shell
;
29 import org
.eclipse
.ui
.IEditorPart
;
30 import org
.eclipse
.ui
.handlers
.HandlerUtil
;
32 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
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
.Synonym
;
37 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
38 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
39 import eu
.etaxonomy
.cdm
.persistence
.dto
.TaxonNodeDto
;
40 import eu
.etaxonomy
.taxeditor
.editor
.descriptiveDataSet
.DescriptiveDataSetEditor
;
41 import eu
.etaxonomy
.taxeditor
.editor
.descriptiveDataSet
.matrix
.CharacterMatrixPart
;
42 import eu
.etaxonomy
.taxeditor
.editor
.e4
.TaxonEditorInputE4
;
43 import eu
.etaxonomy
.taxeditor
.editor
.group
.authority
.CdmAuthorityEditorInput
;
44 import eu
.etaxonomy
.taxeditor
.editor
.group
.authority
.e4
.CdmAuthorityEditorE4
;
45 import eu
.etaxonomy
.taxeditor
.editor
.internal
.TaxeditorEditorPlugin
;
46 import eu
.etaxonomy
.taxeditor
.editor
.l10n
.Messages
;
47 import eu
.etaxonomy
.taxeditor
.editor
.name
.e4
.TaxonNameEditorE4
;
48 import eu
.etaxonomy
.taxeditor
.editor
.view
.checklist
.e4
.DistributionEditorPart
;
49 import eu
.etaxonomy
.taxeditor
.editor
.view
.derivate
.DerivateView
;
50 import eu
.etaxonomy
.taxeditor
.editor
.view
.derivate
.DerivateViewEditorInput
;
51 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
52 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
53 import eu
.etaxonomy
.taxeditor
.workbench
.WorkbenchUtility
;
56 * Utility for the editor package
62 public class EditorUtil
extends AbstractUtility
{
64 private static final String NAME_EDITOR_ID
= "eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditorE4";
66 public static void openDescriptiveDataSetEditor(UUID descriptiveDataSetUuid
, EModelService modelService
, EPartService partService
, MApplication application
){
67 String partId
= AppModelId
.PARTDESCRIPTOR_EU_ETAXONOMY_TAXEDITOR_EDITOR_VIEW_DESCRIPTIVEDATASET_DESCRIPTIVEDATASETEDITOR
;
68 MPart part
= showPart(partId
, modelService
, partService
, application
);
69 DescriptiveDataSetEditor editor
= (DescriptiveDataSetEditor
) part
.getObject();
70 editor
.init(descriptiveDataSetUuid
);
73 public static void openCharacterMatrix(UUID descriptiveDataSetUuid
, EModelService modelService
, EPartService partService
, MApplication application
){
74 Collection
<MPart
> parts
= partService
.getParts();
75 for (MPart part
: parts
) {
76 if(part
.getObject() instanceof CharacterMatrixPart
77 && ((CharacterMatrixPart
) part
.getObject()).getDescriptiveDataSet().getUuid().equals(descriptiveDataSetUuid
)){
78 partService
.showPart(part
, PartState
.ACTIVATE
);
82 String partId
= AppModelId
.PARTDESCRIPTOR_EU_ETAXONOMY_TAXEDITOR_EDITOR_DESCRIPTIVEDATASET_MATRIX_CHARACTERMATRIXPART
;
83 MPart part
= showPart(partId
, modelService
, partService
, application
);
84 CharacterMatrixPart editor
= (CharacterMatrixPart
) part
.getObject();
85 editor
.init(descriptiveDataSetUuid
, true);
88 public static void openDistributionEditor(UUID parentTaxonUuid
, EModelService modelService
, EPartService partService
, MApplication application
){
89 Collection
<MPart
> parts
= partService
.getParts();
90 String partId
= AppModelId
.PARTDESCRIPTOR_EU_ETAXONOMY_TAXEDITOR_EDITOR_VIEW_CHECKLIST_E4_DISTRIBUTIONEDITORPART
;
91 MPart part
= showPart(partId
, modelService
, partService
, application
);
92 DistributionEditorPart editor
= (DistributionEditorPart
) part
.getObject();
93 editor
.init(parentTaxonUuid
);
97 public static void openSpecimenEditor(DerivateViewEditorInput input
, EModelService modelService
, EPartService partService
, MApplication application
){
98 String partId
= AppModelId
.PARTDESCRIPTOR_EU_ETAXONOMY_TAXEDITOR_EDITOR_VIEW_DERIVATE_DERIVATEVIEW
;
99 MPart part
= showPart(partId
, modelService
, partService
, application
);
100 DerivateView derivateView
= (DerivateView
) part
.getObject();
101 derivateView
.init(input
);
104 public static void openRightsEditor(CdmAuthorityEditorInput input
, EModelService modelService
, EPartService partService
, MApplication application
){
105 Collection
<MPart
> parts
= partService
.getParts();
106 for (MPart part
: parts
) {
107 if(part
.getObject() instanceof CdmAuthorityEditorE4
108 && ((CdmAuthorityEditorE4
) part
.getObject()).getInput().getGroup().equals(input
.getGroup())){
109 partService
.showPart(part
, PartState
.ACTIVATE
);
113 String partId
= AppModelId
.PARTDESCRIPTOR_EU_ETAXONOMY_TAXEDITOR_EDITOR_GROUP_AUTHORITY_E4_CDMAUTHORITYEDITORE4
;
114 MPart part
= showPart(partId
, modelService
, partService
, application
);
115 CdmAuthorityEditorE4 authorityView
= (CdmAuthorityEditorE4
) part
.getObject();
116 authorityView
.init(input
);
119 public static MPart
showPart(String partId
, EModelService modelService
, EPartService partService
, MApplication application
){
120 MPart part
= partService
.findPart(partId
);
121 if(part
==null || modelService
.getPartDescriptor(partId
).isAllowMultiple()){
122 part
= partService
.createPart(partId
);
124 MPartStack editorAreaPartStack
= WorkbenchUtility
.getEditorAreaPartStack(application
, modelService
);
125 if(editorAreaPartStack
!=null){
126 editorAreaPartStack
.getChildren().add(part
);
128 return partService
.showPart(part
, PartState
.ACTIVATE
);
131 public static void openTaxonNodeE4(UUID taxonNodeUuid
, EModelService modelService
, EPartService partService
, MApplication application
) {
132 TaxonEditorInputE4 input
= TaxonEditorInputE4
.NewInstance(taxonNodeUuid
);
133 openNameEditor_internal(input
, modelService
, partService
, application
);
136 public static void openTaxonBaseE4(UUID taxonBaseUuid
, EModelService modelService
, EPartService partService
, MApplication application
) {
137 TaxonEditorInputE4 input
= TaxonEditorInputE4
.NewInstanceFromTaxonBase(taxonBaseUuid
);
138 openNameEditor_internal(input
, modelService
, partService
, application
);
141 public static void openTaxonBaseE4(UUID taxonBaseUuid
) {
142 //FIXME E4 this can probably be removed when fully migrated
143 TaxonEditorInputE4 input
= TaxonEditorInputE4
.NewInstanceFromTaxonBase(taxonBaseUuid
);
145 EPartService partService
= TaxeditorEditorPlugin
.getDefault().getWorkbench().getActiveWorkbenchWindow().getService(EPartService
.class);
146 EModelService modelService
= TaxeditorEditorPlugin
.getDefault().getWorkbench().getActiveWorkbenchWindow().getService(EModelService
.class);
147 openNameEditor_internal(input
, modelService
, partService
, null);
150 private static void openNameEditor_internal(TaxonEditorInputE4 input
, EModelService modelService
, EPartService partService
, MApplication application
) {
151 TaxonBase taxonBase
= input
.getTaxon();
155 if (taxonBase
.isOrphaned()) {
156 if(taxonBase
.isInstanceOf(Synonym
.class)){
157 MessagingUtils
.warningDialog(Messages
.EditorUtil_ORPHAN_ACCEPTED_TAXON
, TaxonEditorInputE4
.class, Messages
.EditorUtil_ORPHAN_ACCEPTED_TAXON_MESSAGE
);
161 MessagingUtils
.warningDialog(Messages
.EditorUtil_ORPHAN_TAXON
, TaxonEditorInputE4
.class, Messages
.EditorUtil_ORPHAN_TAXON_MESSAGE
);
166 Collection
<MPart
> parts
= partService
.getParts();
167 //check if part is already opened
168 for (MPart part
: parts
) {
169 if(part
.getObject() instanceof TaxonNameEditorE4
170 && ((TaxonNameEditorE4
) part
.getObject()).getTaxon()!=null
171 && ((TaxonNameEditorE4
) part
.getObject()).getTaxon().getUuid().equals(input
.getTaxon().getUuid())){
173 forceUserSaveE4Editor(((TaxonNameEditorE4
) part
.getObject()), getShell());
175 partService
.hidePart(part
);
179 MPart part
= showPart(NAME_EDITOR_ID
, modelService
, partService
, application
);
181 TaxonNameEditorE4 editor
= (TaxonNameEditorE4
) part
.getObject();
185 public static MPart
checkForChanges(UUID taxonUUID
, EPartService partService
){
186 Collection
<MPart
> parts
= partService
.getParts();
187 //check if part is already opened
188 boolean isDirty
= false;
189 for (MPart part
: parts
) {
190 if(part
.getObject() instanceof TaxonNameEditorE4
191 && ((TaxonNameEditorE4
) part
.getObject()).getTaxon()!=null
192 && ((TaxonNameEditorE4
) part
.getObject()).getTaxon().getUuid().equals(taxonUUID
)){
204 * An uninitialized taxon is one that hasn't been saved yet. As such, it
205 * should appear in neither the list of recent names nor in the taxonomic
208 * @param parentNodeUuid
209 * a {@link java.util.UUID} object.
211 public static void openEmptyE4(UUID parentNodeUuid
) {
212 TaxonEditorInputE4 input
= TaxonEditorInputE4
213 .NewEmptyInstance(parentNodeUuid
);
214 EPartService partService
= TaxeditorEditorPlugin
.getDefault().getWorkbench().getActiveWorkbenchWindow().getService(EPartService
.class);
215 MPart part
= partService
.createPart(NAME_EDITOR_ID
);
216 part
= partService
.showPart(part
, PartState
.ACTIVATE
);
217 TaxonNameEditorE4 editor
= (TaxonNameEditorE4
) part
.getObject();
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
;
304 * Iterates recursively over all originals having the given specimen as a derivate.
305 * The first {@link DerivedUnit} with no more originals or the first {@link FieldUnit} is returned
306 * @param specimen the start element for which the originals are iterated recursively
307 * @return either a FieldUnit or a the topmost DerivedUnit (which can be itself)
309 public static SpecimenOrObservationBase
<?
> getTopMostDerivate(SpecimenOrObservationBase
<?
> specimen
){
313 if(specimen
.isInstanceOf(FieldUnit
.class)){
316 else if(specimen
.isInstanceOf(DerivedUnit
.class)){
317 DerivedUnit derivedUnit
= HibernateProxyHelper
.deproxy(specimen
, DerivedUnit
.class);
318 if(derivedUnit
.getOriginals()!=null
319 && !(derivedUnit
.getOriginals().isEmpty())){
320 for(SpecimenOrObservationBase
<?
> original
:((DerivedUnit
) specimen
).getOriginals()){
321 return getTopMostDerivate(original
);
329 * If the current selection is a single {@link TreeNode} it will be returned.
330 * @param selection the selection to check
331 * @return the selected TreeNode or <code>null</code> if no TreeNode selected
333 public static TreeNode
getTreeNodeOfSelection(ISelection selection
){
334 if(selection
instanceof IStructuredSelection
335 && ((IStructuredSelection
) selection
).size()==1
336 && ((IStructuredSelection
) selection
).getFirstElement() instanceof TreeNode
){
337 return (TreeNode
) ((IStructuredSelection
) selection
).getFirstElement();
343 public static void closeObsoleteEditor(TaxonNodeDto taxonNode
, EPartService partService
){
344 String treeIndex
= taxonNode
.getTreeIndex();
345 Collection
<MPart
> parts
= partService
.getParts();
346 for (MPart part
: parts
) {
347 Object object
= part
.getObject();
348 if(object
instanceof TaxonNameEditorE4
){
349 TaxonNameEditorE4 taxonEditor
= (TaxonNameEditorE4
)object
;
350 TaxonNode node
= taxonEditor
.getEditorInput().getTaxonNode();
351 if (node
.treeIndex()!= null){
352 if(node
.treeIndex().startsWith(treeIndex
)){
354 forceUserSaveE4Editor(taxonEditor
, getShell());
356 partService
.hidePart(part
);
359 logger
.debug("The taxonnode of taxon " + node
.getTaxon().getTitleCache() + " uuid: " + node
.getUuid() + " has no treeindex");;
365 public static void updateEditor(TaxonNode taxonNode
, TaxonNameEditorE4 editor
){
366 String treeIndex
= taxonNode
.treeIndex();
367 TaxonNode node
= editor
.getEditorInput().getTaxonNode();
368 if(node
.treeIndex().equals(treeIndex
)){
369 TaxonEditorInputE4 input
= TaxonEditorInputE4
.NewInstance(node
.getUuid());