2 * Copyright (C) 2017 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.
9 package eu
.etaxonomy
.taxeditor
.editor
.workingSet
;
11 import java
.util
.Collection
;
12 import java
.util
.HashSet
;
14 import java
.util
.UUID
;
16 import javax
.annotation
.PostConstruct
;
17 import javax
.annotation
.PreDestroy
;
18 import javax
.inject
.Inject
;
19 import javax
.inject
.Named
;
21 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
22 import org
.eclipse
.e4
.core
.contexts
.ContextInjectionFactory
;
23 import org
.eclipse
.e4
.core
.contexts
.IEclipseContext
;
24 import org
.eclipse
.e4
.ui
.di
.Focus
;
25 import org
.eclipse
.e4
.ui
.di
.Persist
;
26 import org
.eclipse
.e4
.ui
.model
.application
.ui
.MDirtyable
;
27 import org
.eclipse
.e4
.ui
.services
.IServiceConstants
;
28 import org
.eclipse
.jface
.util
.LocalSelectionTransfer
;
29 import org
.eclipse
.jface
.viewers
.Viewer
;
30 import org
.eclipse
.swt
.SWT
;
31 import org
.eclipse
.swt
.dnd
.DND
;
32 import org
.eclipse
.swt
.dnd
.Transfer
;
33 import org
.eclipse
.swt
.events
.ModifyEvent
;
34 import org
.eclipse
.swt
.events
.ModifyListener
;
35 import org
.eclipse
.swt
.widgets
.Composite
;
36 import org
.eclipse
.swt
.widgets
.Display
;
37 import org
.eclipse
.swt
.widgets
.Shell
;
39 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
40 import eu
.etaxonomy
.cdm
.api
.service
.IOccurrenceService
;
41 import eu
.etaxonomy
.cdm
.api
.service
.IWorkingSetService
;
42 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
43 import eu
.etaxonomy
.cdm
.model
.common
.DefinedTermBase
;
44 import eu
.etaxonomy
.cdm
.model
.description
.FeatureTree
;
45 import eu
.etaxonomy
.cdm
.model
.description
.SpecimenDescription
;
46 import eu
.etaxonomy
.cdm
.model
.description
.WorkingSet
;
47 import eu
.etaxonomy
.cdm
.model
.location
.NamedArea
;
48 import eu
.etaxonomy
.cdm
.model
.name
.Rank
;
49 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationBase
;
50 import eu
.etaxonomy
.cdm
.model
.taxon
.ITaxonTreeNode
;
51 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
52 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
53 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
54 import eu
.etaxonomy
.taxeditor
.ui
.dialog
.selection
.NamedAreaSelectionDialog
;
55 import eu
.etaxonomy
.taxeditor
.ui
.element
.CdmFormFactory
;
56 import eu
.etaxonomy
.taxeditor
.workbench
.part
.IE4SavablePart
;
63 public class WorkingSetEditor
implements IE4SavablePart
{
65 private WorkingSetComposite composite
;
67 private ConversationHolder conversation
;
69 private WorkingSet workingSet
;
71 private final int dndOperations
= DND
.DROP_MOVE
;
74 private MDirtyable dirty
;
76 private ModifyListener labelModifyListener
;
79 public void create(Composite parent
, IEclipseContext context
, @Named(IServiceConstants
.ACTIVE_SHELL
)Shell shell
){
80 if(CdmStore
.isActive() && conversation
==null){
81 conversation
= CdmStore
.createConversation();
86 CdmFormFactory cdmFormFactory
= new CdmFormFactory(Display
.getCurrent());
87 ContextInjectionFactory
.inject(cdmFormFactory
, context
);
89 composite
= new WorkingSetComposite(parent
, SWT
.NONE
);
91 labelModifyListener
= new ModifyListener() {
93 public void modifyText(ModifyEvent e
) {
97 composite
.getRankMin().addSelectionChangedListener(event
->dirty
.setDirty(true));
98 composite
.getRankMax().addSelectionChangedListener(event
->dirty
.setDirty(true));
100 composite
.getTextAreaText().addListener(SWT
.MouseDown
, event
-> {
101 NamedArea area
= NamedAreaSelectionDialog
.select(shell
, null, null);
103 composite
.setArea(area
);
105 dirty
.setDirty(true);
108 //add drag'n'drop support
109 Transfer
[] transfers
= new Transfer
[] {LocalSelectionTransfer
.getTransfer()};
110 composite
.getTaxonNodeTree().addDropSupport(dndOperations
, transfers
, new TaxonNodeDropAdapter(this));
114 public void init(UUID workingSetUuid
) {
115 this.workingSet
= CdmStore
.getService(IWorkingSetService
.class).load(workingSetUuid
);
116 if(workingSet
.getTitleCache()!=null){
117 composite
.getTxt_label().setText(workingSet
.getTitleCache());
119 if(workingSet
.getDescriptiveSystem()!=null){
120 composite
.setCharacters(workingSet
.getDescriptiveSystem());
122 Rank maxRank
= workingSet
.getMaxRank();
124 composite
.setRankMax(maxRank
);
126 Rank minRank
= workingSet
.getMinRank();
128 composite
.setRankMin(minRank
);
130 Set
<NamedArea
> geoFilter
= workingSet
.getGeoFilter();
131 if(geoFilter
!=null && !geoFilter
.isEmpty()){
132 composite
.setArea(geoFilter
.iterator().next());
135 composite
.getTxt_label().addModifyListener(labelModifyListener
);
143 public void save(IProgressMonitor monitor
) {
144 NamedArea area
= composite
.getArea();
145 Set
<NamedArea
> areas
= new HashSet
<>();
149 Object input
= composite
.getTaxonNodeTree().getInput();
151 Collection
<ITaxonTreeNode
> taxonNodes
= (Collection
<ITaxonTreeNode
>) input
;
152 for (ITaxonTreeNode taxonTreeNode
: taxonNodes
) {
153 if(taxonTreeNode
instanceof TaxonNode
){
154 TaxonNode taxonNode
= (TaxonNode
)taxonTreeNode
;
155 workingSet
.addTaxonSubtree(taxonNode
);
156 Set
<NamedArea
> descriptions
;
157 if(taxonNode
!=null && taxonNode
.getTaxon()!=null){
158 Taxon taxon
= HibernateProxyHelper
.deproxy(taxonNode
.getTaxon(), Taxon
.class);
159 Collection
<SpecimenOrObservationBase
> specimens
= CdmStore
.getService(IOccurrenceService
.class).listByAssociatedTaxon(SpecimenOrObservationBase
.class, null, taxon
, null, null, null, null, null);
160 for (SpecimenOrObservationBase specimen
: specimens
) {
161 Set
<SpecimenDescription
> specimenDescriptions
= specimen
.getSpecimenDescriptions();
162 for (SpecimenDescription specimenDescription
: specimenDescriptions
) {
163 workingSet
.addDescription(specimenDescription
);
170 FeatureTree characters
= composite
.getCharacters();
171 DefinedTermBase rankMaxSelection
= composite
.getRankMax().getSelection();
173 if(rankMaxSelection
instanceof Rank
){
174 rankMax
= (Rank
) rankMaxSelection
;
176 DefinedTermBase rankMinSelection
= composite
.getRankMin().getSelection();
178 if(rankMinSelection
instanceof Rank
){
179 rankMin
= (Rank
) rankMinSelection
;
182 workingSet
.setMaxRank(rankMax
);
183 workingSet
.setMinRank(rankMin
);
184 workingSet
.setDescriptiveSystem(characters
);
185 workingSet
.setGeoFilter(areas
);
187 conversation
.commit();
188 CdmStore
.getService(IWorkingSetService
.class).merge(workingSet
, true);
190 dirty
.setDirty(false);
194 public void setFocus(){
195 if(conversation
!=null){
201 public void dispose(){
202 if(conversation
!=null){
203 conversation
.close();
208 public Viewer
getTaxonTreeViewer() {
209 return composite
.getTaxonNodeTree();