ref #7086 Allow drag and drop for taxon filter from taxon navigator
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / workingSet / WorkingSetEditor.java
1 /**
2 * Copyright (C) 2017 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
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.
8 */
9 package eu.etaxonomy.taxeditor.editor.workingSet;
10
11 import java.util.Collection;
12 import java.util.HashSet;
13 import java.util.Set;
14 import java.util.UUID;
15
16 import javax.annotation.PostConstruct;
17 import javax.annotation.PreDestroy;
18 import javax.inject.Inject;
19 import javax.inject.Named;
20
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;
38
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;
57
58 /**
59 * @author pplitzner
60 * @since Nov 21, 2017
61 *
62 */
63 public class WorkingSetEditor implements IE4SavablePart{
64
65 private WorkingSetComposite composite;
66
67 private ConversationHolder conversation;
68
69 private WorkingSet workingSet;
70
71 private final int dndOperations = DND.DROP_MOVE;
72
73 @Inject
74 private MDirtyable dirty;
75
76 private ModifyListener labelModifyListener;
77
78 @PostConstruct
79 public void create(Composite parent, IEclipseContext context, @Named(IServiceConstants.ACTIVE_SHELL)Shell shell){
80 if(CdmStore.isActive() && conversation==null){
81 conversation = CdmStore.createConversation();
82 }
83 else{
84 return;
85 }
86 CdmFormFactory cdmFormFactory = new CdmFormFactory(Display.getCurrent());
87 ContextInjectionFactory.inject(cdmFormFactory, context);
88
89 composite = new WorkingSetComposite(parent, SWT.NONE);
90
91 labelModifyListener = new ModifyListener() {
92 @Override
93 public void modifyText(ModifyEvent e) {
94 dirty.setDirty(true);
95 }
96 };
97 composite.getRankMin().addSelectionChangedListener(event->dirty.setDirty(true));
98 composite.getRankMax().addSelectionChangedListener(event->dirty.setDirty(true));
99
100 composite.getTextAreaText().addListener(SWT.MouseDown, event-> {
101 NamedArea area = NamedAreaSelectionDialog.select(shell, null, null);
102 if(area!=null){
103 composite.setArea(area);
104 }
105 dirty.setDirty(true);
106 });
107
108 //add drag'n'drop support
109 Transfer[] transfers = new Transfer[] {LocalSelectionTransfer.getTransfer()};
110 composite.getTaxonNodeTree().addDropSupport(dndOperations, transfers, new TaxonNodeDropAdapter(this));
111
112 }
113
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());
118 }
119 if(workingSet.getDescriptiveSystem()!=null){
120 composite.setCharacters(workingSet.getDescriptiveSystem());
121 }
122 Rank maxRank = workingSet.getMaxRank();
123 if(maxRank!=null){
124 composite.setRankMax(maxRank);
125 }
126 Rank minRank = workingSet.getMinRank();
127 if(minRank!=null){
128 composite.setRankMin(minRank);
129 }
130 Set<NamedArea> geoFilter = workingSet.getGeoFilter();
131 if(geoFilter!=null && !geoFilter.isEmpty()){
132 composite.setArea(geoFilter.iterator().next());
133 }
134
135 composite.getTxt_label().addModifyListener(labelModifyListener);
136 }
137
138 /**
139 * {@inheritDoc}
140 */
141 @Persist
142 @Override
143 public void save(IProgressMonitor monitor) {
144 NamedArea area = composite.getArea();
145 Set<NamedArea> areas = new HashSet<>();
146 if(area!=null){
147 areas.add(area);
148 }
149 Object input = composite.getTaxonNodeTree().getInput();
150 if(input!=null){
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);
164 }
165 }
166 }
167 }
168 }
169 }
170 FeatureTree characters = composite.getCharacters();
171 DefinedTermBase rankMaxSelection = composite.getRankMax().getSelection();
172 Rank rankMax = null;
173 if(rankMaxSelection instanceof Rank){
174 rankMax = (Rank) rankMaxSelection;
175 }
176 DefinedTermBase rankMinSelection = composite.getRankMin().getSelection();
177 Rank rankMin = null;
178 if(rankMinSelection instanceof Rank){
179 rankMin = (Rank) rankMinSelection;
180 }
181
182 workingSet.setMaxRank(rankMax);
183 workingSet.setMinRank(rankMin);
184 workingSet.setDescriptiveSystem(characters);
185 workingSet.setGeoFilter(areas);
186
187 conversation.commit();
188 CdmStore.getService(IWorkingSetService.class).merge(workingSet, true);
189
190 dirty.setDirty(false);
191 }
192
193 @Focus
194 public void setFocus(){
195 if(conversation!=null){
196 conversation.bind();
197 }
198 }
199
200 @PreDestroy
201 public void dispose(){
202 if(conversation!=null){
203 conversation.close();
204 conversation = null;
205 }
206 }
207
208 public Viewer getTaxonTreeViewer() {
209 return composite.getTaxonNodeTree();
210 }
211
212 }