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
.taxeditor
.editor
.e4
.TaxonEditorInputE4
;
40 import eu
.etaxonomy
.taxeditor
.editor
.group
.authority
.CdmAuthorityEditorInput
;
41 import eu
.etaxonomy
.taxeditor
.editor
.group
.authority
.e4
.CdmAuthorityEditorE4
;
42 import eu
.etaxonomy
.taxeditor
.editor
.internal
.TaxeditorEditorPlugin
;
43 import eu
.etaxonomy
.taxeditor
.editor
.l10n
.Messages
;
44 import eu
.etaxonomy
.taxeditor
.editor
.name
.e4
.TaxonNameEditorE4
;
45 import eu
.etaxonomy
.taxeditor
.editor
.view
.derivate
.DerivateView
;
46 import eu
.etaxonomy
.taxeditor
.editor
.view
.derivate
.DerivateViewEditorInput
;
47 import eu
.etaxonomy
.taxeditor
.editor
.workingSet
.WorkingSetEditor
;
48 import eu
.etaxonomy
.taxeditor
.editor
.workingSet
.matrix
.CharacterMatrixPart
;
49 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
50 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
51 import eu
.etaxonomy
.taxeditor
.workbench
.WorkbenchUtility
;
54 * Utility for the editor package
60 public class EditorUtil
extends AbstractUtility
{
62 private static final String NAME_EDITOR_ID
= "eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditorE4";
63 private static boolean isSaving
= false;
65 public static void openWorkingSetEditor(UUID workingSetUuid
, EModelService modelService
, EPartService partService
, MApplication application
){
66 String partId
= AppModelId
.PARTDESCRIPTOR_EU_ETAXONOMY_TAXEDITOR_EDITOR_VIEW_WORKINGSET_WORKINGSETEDITOR
;
67 MPart part
= showPart(partId
, modelService
, partService
, application
);
68 WorkingSetEditor editor
= (WorkingSetEditor
) part
.getObject();
69 editor
.init(workingSetUuid
);
72 public static void openCharacterMatrix(UUID workingSetUuid
, EModelService modelService
, EPartService partService
, MApplication application
){
73 String partId
= AppModelId
.PARTDESCRIPTOR_EU_ETAXONOMY_TAXEDITOR_EDITOR_WORKINGSET_MATRIX_CHARACTERMATRIXPART
;
74 MPart part
= showPart(partId
, modelService
, partService
, application
);
75 CharacterMatrixPart editor
= (CharacterMatrixPart
) part
.getObject();
76 editor
.init(workingSetUuid
, true);
79 public static void openSpecimenEditor(DerivateViewEditorInput input
, EModelService modelService
, EPartService partService
, MApplication application
){
80 String partId
= AppModelId
.PARTDESCRIPTOR_EU_ETAXONOMY_TAXEDITOR_EDITOR_VIEW_DERIVATE_DERIVATEVIEW
;
81 MPart part
= showPart(partId
, modelService
, partService
, application
);
82 DerivateView derivateView
= (DerivateView
) part
.getObject();
83 derivateView
.init(input
);
86 public static void openRightsEditor(CdmAuthorityEditorInput input
, EModelService modelService
, EPartService partService
, MApplication application
){
87 String partId
= AppModelId
.PARTDESCRIPTOR_EU_ETAXONOMY_TAXEDITOR_EDITOR_GROUP_AUTHORITY_E4_CDMAUTHORITYEDITORE4
;
88 MPart part
= showPart(partId
, modelService
, partService
, application
);
89 CdmAuthorityEditorE4 authorityView
= (CdmAuthorityEditorE4
) part
.getObject();
90 authorityView
.init(input
);
93 private static MPart
showPart(String partId
, EModelService modelService
, EPartService partService
, MApplication application
){
94 MPart part
= partService
.findPart(partId
);
95 if(part
==null || modelService
.getPartDescriptor(partId
).isAllowMultiple()){
96 part
= partService
.createPart(partId
);
98 MPartStack editorAreaPartStack
= WorkbenchUtility
.getEditorAreaPartStack(application
, modelService
);
99 if(editorAreaPartStack
!=null){
100 editorAreaPartStack
.getChildren().add(part
);
102 return partService
.showPart(part
, PartState
.ACTIVATE
);
105 public static void openTaxonNodeE4(UUID taxonNodeUuid
, EModelService modelService
, EPartService partService
, MApplication application
) {
106 TaxonEditorInputE4 input
= TaxonEditorInputE4
.NewInstance(taxonNodeUuid
);
107 openNameEditor_internal(input
, modelService
, partService
, application
);
110 public static void openTaxonBaseE4(UUID taxonBaseUuid
, EModelService modelService
, EPartService partService
, MApplication application
) {
111 TaxonEditorInputE4 input
= TaxonEditorInputE4
.NewInstanceFromTaxonBase(taxonBaseUuid
);
112 openNameEditor_internal(input
, modelService
, partService
, application
);
115 public static void openTaxonBaseE4(UUID taxonBaseUuid
) {
116 //FIXME E4 this can probably be removed when fully migrated
117 TaxonEditorInputE4 input
= TaxonEditorInputE4
.NewInstanceFromTaxonBase(taxonBaseUuid
);
119 EPartService partService
= TaxeditorEditorPlugin
.getDefault().getWorkbench().getActiveWorkbenchWindow().getService(EPartService
.class);
120 EModelService modelService
= TaxeditorEditorPlugin
.getDefault().getWorkbench().getActiveWorkbenchWindow().getService(EModelService
.class);
121 openNameEditor_internal(input
, modelService
, partService
, null);
124 private static void openNameEditor_internal(TaxonEditorInputE4 input
, EModelService modelService
, EPartService partService
, MApplication application
) {
125 TaxonBase taxonBase
= input
.getTaxon();
129 if (taxonBase
.isOrphaned()) {
130 if(taxonBase
.isInstanceOf(Synonym
.class)){
131 MessagingUtils
.warningDialog(Messages
.EditorUtil_ORPHAN_ACCEPTED_TAXON
, TaxonEditorInputE4
.class, Messages
.EditorUtil_ORPHAN_ACCEPTED_TAXON_MESSAGE
);
135 MessagingUtils
.warningDialog(Messages
.EditorUtil_ORPHAN_TAXON
, TaxonEditorInputE4
.class, Messages
.EditorUtil_ORPHAN_TAXON_MESSAGE
);
140 Collection
<MPart
> parts
= partService
.getParts();
141 //check if part is already opened
142 for (MPart part
: parts
) {
143 if(part
.getObject() instanceof TaxonNameEditorE4
144 && ((TaxonNameEditorE4
) part
.getObject()).getTaxon()!=null
145 && ((TaxonNameEditorE4
) part
.getObject()).getTaxon().getUuid().equals(input
.getTaxon().getUuid())){
146 partService
.hidePart(part
);
150 MPart part
= showPart(NAME_EDITOR_ID
, modelService
, partService
, application
);
151 TaxonNameEditorE4 editor
= (TaxonNameEditorE4
) part
.getObject();
156 * An uninitialized taxon is one that hasn't been saved yet. As such, it
157 * should appear in neither the list of recent names nor in the taxonomic
160 * @param parentNodeUuid
161 * a {@link java.util.UUID} object.
163 public static void openEmptyE4(UUID parentNodeUuid
) {
164 TaxonEditorInputE4 input
= TaxonEditorInputE4
165 .NewEmptyInstance(parentNodeUuid
);
166 EPartService partService
= TaxeditorEditorPlugin
.getDefault().getWorkbench().getActiveWorkbenchWindow().getService(EPartService
.class);
167 MPart part
= partService
.createPart(NAME_EDITOR_ID
);
168 part
= partService
.showPart(part
, PartState
.ACTIVATE
);
169 TaxonNameEditorE4 editor
= (TaxonNameEditorE4
) part
.getObject();
181 public static void setSaving(boolean isSaving
) {
182 EditorUtil
.isSaving
= isSaving
;
192 public static boolean isSaving() {
201 * @return a {@link org.eclipse.core.commands.operations.IUndoContext}
204 public static IUndoContext
getUndoContext() {
205 return IOperationHistory
.GLOBAL_UNDO_CONTEXT
;
214 * a {@link org.eclipse.ui.IEditorPart} object.
216 * a {@link org.eclipse.swt.widgets.Shell} object.
219 public static boolean forceUserSave(IEditorPart editor
, Shell shell
) {
220 if (editor
.isDirty()) {
222 boolean doSave
= MessageDialog
223 .openConfirm(shell
, Messages
.EditorUtil_COMFIRM_SAVE
,
224 Messages
.EditorUtil_CONFIRM_SAVE_MESSAGE
);
230 editor
.doSave(AbstractUtility
.getMonitor());
235 public static boolean forceUserSaveE4Editor(TaxonNameEditorE4 editor
, Shell shell
) {
236 if (editor
.isDirty()) {
238 boolean doSave
= MessageDialog
239 .openConfirm(shell
, Messages
.EditorUtil_COMFIRM_SAVE
,
240 Messages
.EditorUtil_CONFIRM_SAVE_MESSAGE
);
246 editor
.save(AbstractUtility
.getMonitor());
257 * a {@link org.eclipse.core.commands.ExecutionEvent} object.
258 * @return a {@link org.eclipse.jface.viewers.IStructuredSelection} object.
260 public static IStructuredSelection
getSelection(ExecutionEvent event
) {
261 IEditorPart activeEditor
= HandlerUtil
.getActiveEditor(event
);
263 return (IStructuredSelection
) activeEditor
.getSite()
264 .getSelectionProvider().getSelection();
272 * @return a {@link java.lang.String} object.
274 public static String
getPluginId() {
275 return TaxeditorEditorPlugin
.PLUGIN_ID
;
279 * Iterates recursively over all originals having the given specimen as a derivate.
280 * The first {@link DerivedUnit} with no more originals or the first {@link FieldUnit} is returned
281 * @param specimen the start element for which the originals are iterated recursively
282 * @return either a FieldUnit or a the topmost DerivedUnit (which can be itself)
284 public static SpecimenOrObservationBase
<?
> getTopMostDerivate(SpecimenOrObservationBase
<?
> specimen
){
288 if(specimen
.isInstanceOf(FieldUnit
.class)){
291 else if(specimen
.isInstanceOf(DerivedUnit
.class)){
292 DerivedUnit derivedUnit
= HibernateProxyHelper
.deproxy(specimen
, DerivedUnit
.class);
293 if(derivedUnit
.getOriginals()!=null
294 && !(derivedUnit
.getOriginals().isEmpty())){
295 for(SpecimenOrObservationBase
<?
> original
:((DerivedUnit
) specimen
).getOriginals()){
296 return getTopMostDerivate(original
);
304 * If the current selection is a single {@link TreeNode} it will be returned.
305 * @param selection the selection to check
306 * @return the selected TreeNode or <code>null</code> if no TreeNode selected
308 public static TreeNode
getTreeNodeOfSelection(ISelection selection
){
309 if(selection
instanceof IStructuredSelection
310 && ((IStructuredSelection
) selection
).size()==1
311 && ((IStructuredSelection
) selection
).getFirstElement() instanceof TreeNode
){
312 return (TreeNode
) ((IStructuredSelection
) selection
).getFirstElement();
318 public static void closeObsoleteEditor(TaxonNode taxonNode
, EPartService partService
){
319 String treeIndex
= taxonNode
.treeIndex();
320 Collection
<MPart
> parts
= partService
.getParts();
321 for (MPart part
: parts
) {
322 Object object
= part
.getObject();
323 if(object
instanceof TaxonNameEditorE4
){
324 TaxonNameEditorE4 taxonEditor
= (TaxonNameEditorE4
)object
;
325 TaxonNode node
= taxonEditor
.getEditorInput().getTaxonNode();
326 if (node
.treeIndex()!= null){
327 if(node
.treeIndex().startsWith(treeIndex
)){
328 partService
.hidePart(part
);
331 logger
.debug("The taxonnode of taxon " + node
.getTaxon().getTitleCache() + " uuid: " + node
.getUuid() + " has no treeindex");;
337 public static void updateEditor(TaxonNode taxonNode
, TaxonNameEditorE4 editor
){
338 String treeIndex
= taxonNode
.treeIndex();
339 TaxonNode node
= editor
.getEditorInput().getTaxonNode();
340 if(node
.treeIndex().equals(treeIndex
)){
341 TaxonEditorInputE4 input
= TaxonEditorInputE4
.NewInstance(node
.getUuid());