Merge branch 'release/5.5.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.bulkeditor / src / main / java / eu / etaxonomy / taxeditor / bulkeditor / e4 / BulkEditorE4.java
1 /**
2 * Copyright (C) 2007 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
10 package eu.etaxonomy.taxeditor.bulkeditor.e4;
11
12 import java.io.FileOutputStream;
13 import java.io.IOException;
14
15 import javax.annotation.PostConstruct;
16 import javax.annotation.PreDestroy;
17 import javax.inject.Inject;
18
19 import org.eclipse.core.runtime.IProgressMonitor;
20 import org.eclipse.e4.core.contexts.ContextInjectionFactory;
21 import org.eclipse.e4.core.contexts.IEclipseContext;
22 import org.eclipse.e4.core.di.annotations.Optional;
23 import org.eclipse.e4.core.services.events.IEventBroker;
24 import org.eclipse.e4.ui.di.Focus;
25 import org.eclipse.e4.ui.di.Persist;
26 import org.eclipse.e4.ui.di.UIEventTopic;
27 import org.eclipse.e4.ui.model.application.ui.MDirtyable;
28 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
29 import org.eclipse.jface.viewers.IStructuredSelection;
30 import org.eclipse.jface.viewers.StructuredSelection;
31 import org.eclipse.swt.SWT;
32 import org.eclipse.swt.widgets.Composite;
33
34 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
35 import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
36 import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade;
37 import eu.etaxonomy.cdm.model.common.CdmBase;
38 import eu.etaxonomy.cdm.model.description.DescriptionBase;
39 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
40 import eu.etaxonomy.cdm.model.description.TaxonDescription;
41 import eu.etaxonomy.cdm.model.description.TaxonNameDescription;
42 import eu.etaxonomy.cdm.model.name.TaxonName;
43 import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
44 import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
45 import eu.etaxonomy.cdm.model.taxon.Taxon;
46 import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
47 import eu.etaxonomy.taxeditor.bulkeditor.BulkEditorQuery;
48 import eu.etaxonomy.taxeditor.bulkeditor.input.AbstractBulkEditorInput;
49 import eu.etaxonomy.taxeditor.editor.ITaxonEditor;
50 import eu.etaxonomy.taxeditor.event.WorkbenchEventConstants;
51 import eu.etaxonomy.taxeditor.model.IDerivedUnitFacadePart;
52 import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
53 import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
54 import eu.etaxonomy.taxeditor.model.IPartContentHasFactualData;
55 import eu.etaxonomy.taxeditor.model.IPartContentHasMedia;
56 import eu.etaxonomy.taxeditor.model.IPartContentHasSupplementalData;
57 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
58 import eu.etaxonomy.taxeditor.workbench.part.IE4SavablePart;
59
60 /**
61 *
62 * @author pplitzner
63 * @since Sep 8, 2017
64 *
65 */
66 public class BulkEditorE4 implements IPartContentHasDetails, IConversationEnabled, IPostOperationEnabled,
67 IDirtyMarkable, IDerivedUnitFacadePart, IPartContentHasFactualData,
68 IPartContentHasSupplementalData, IPartContentHasMedia, IE4SavablePart, ITaxonEditor {
69
70 @Inject
71 private MDirtyable dirty;
72
73 private AbstractBulkEditorInput input;
74
75 private ConversationHolder conversation;
76
77 @Inject
78 private IEventBroker eventBroker;
79
80 @Inject
81 IEclipseContext context;
82
83 @Inject
84 private MPart thisPart;
85
86 private BulkEditorQuery lastQuery = null;
87
88 private BulkEditorE4Composite bulkEditorComposite;
89
90 @Inject
91 public BulkEditorE4() {
92 }
93
94 @SuppressWarnings("unused")
95 public void init(AbstractBulkEditorInput<?> input){
96 this.input = input;
97 this.conversation = input.getConversation();
98 thisPart.setLabel(input.getEditorName());
99
100 bulkEditorComposite.init(input);
101 }
102
103 /** {@inheritDoc} */
104 @PostConstruct
105 public void createPartControl(Composite parent, IEclipseContext context) {
106 bulkEditorComposite = new BulkEditorE4Composite(this, parent, SWT.NONE);
107 ContextInjectionFactory.inject(bulkEditorComposite, context);
108 }
109
110 @Override
111 @Persist
112 public void save(IProgressMonitor monitor) {
113 save(monitor, true);
114 }
115
116 public void save(IProgressMonitor monitor, boolean resetMerge) {
117 if (!input.getCdmEntitySession().isActive()){
118 input.getCdmEntitySession().bind();
119 }
120 input.saveModel(resetMerge);
121
122 IStructuredSelection selection = getSelection();
123
124 dirty.setDirty(false);
125 input.dispose();
126 input.bind();
127 conversation.commit(true);
128
129 if (lastQuery != null){
130 bulkEditorComposite.performSearch(lastQuery, selection);
131 }
132 }
133
134
135 @Focus
136 public void setFocus() {
137 //make sure to bind again if maybe in another view the conversation was unbound
138 if(conversation!=null && !conversation.isBound()){
139 conversation.bind();
140 }
141 if(input!=null && input.getCdmEntitySession()!= null) {
142 input.getCdmEntitySession().bind();
143 }
144
145 //make sure to bind again if maybe in another view the conversation was unbound
146 eventBroker.post(WorkbenchEventConstants.CURRENT_ACTIVE_EDITOR, this);
147 }
148
149 @PreDestroy
150 public void dispose() {
151 if(conversation!=null){
152 conversation.unregisterForDataStoreChanges(this);
153 conversation.close();
154 }
155 if(input!=null){
156 input.dispose();
157 }
158 dirty.setDirty(false);
159 //save table settings
160 if(bulkEditorComposite.getNatTableState()!=null){
161 try (FileOutputStream tableStateStream =
162 new FileOutputStream(bulkEditorComposite.getStatePropertiesFile())) {
163 bulkEditorComposite.getNatTableState().store(tableStateStream, null);
164 } catch (IOException ioe) {
165 ioe.printStackTrace();
166 }
167 }
168 }
169
170 @Optional
171 @Inject
172 private void updateAfterSearch(@UIEventTopic(WorkbenchEventConstants.BULK_EDITOR_SEARCH_FINISHED)IStructuredSelection selection){
173
174
175 if(selection!=null){
176 setSelection(selection);
177 }
178 }
179
180 public void refresh(){
181 bulkEditorComposite.refresh();
182 }
183
184 public IStructuredSelection getSelection(){
185 return bulkEditorComposite.getSelection();
186 }
187
188 public void setSelection(IStructuredSelection selection){
189 bulkEditorComposite.setSelection(selection);
190 }
191
192 public void setDirty(boolean isDirty){
193 dirty.setDirty(isDirty);
194 }
195
196 public void setDirty(){
197 setDirty(true);
198 }
199
200 @Override
201 public boolean isDirty() {
202 return dirty.isDirty();
203 }
204
205 public AbstractBulkEditorInput getEditorInput() {
206 return input;
207 }
208
209 public void copyDataToClipboard() {
210 bulkEditorComposite.copyDataToClipboard();
211 }
212
213 @Override
214 public void update(CdmDataChangeMap arg0) {
215 }
216
217 @Override
218 public boolean canAttachMedia() {
219 return true;
220 }
221
222 @Override
223 public void changed(Object element) {
224 if(element instanceof DerivedUnitFacade){
225 DerivedUnit derivedUnit = ((DerivedUnitFacade) element).innerDerivedUnit();
226 if(derivedUnit!=null){
227 getEditorInput().addSaveCandidate(derivedUnit);
228 }
229 FieldUnit fieldUnit = ((DerivedUnitFacade) element).innerFieldUnit();
230 if(fieldUnit!=null){
231 getEditorInput().addSaveCandidate(fieldUnit);
232 }
233 }
234 else if (element instanceof CdmBase) {
235 if (element instanceof DescriptionBase){
236 if (element instanceof TaxonNameDescription){
237 TaxonName changedName = ((TaxonNameDescription)element).getTaxonName();
238 getEditorInput().addSaveCandidate(changedName);
239 input.replaceInModel(changedName);
240 }else if (element instanceof TaxonDescription){
241 Taxon changedTaxon = ((TaxonDescription)element).getTaxon();
242 getEditorInput().addSaveCandidate(changedTaxon);
243 input.replaceInModel(changedTaxon);
244 }
245 }else if (element instanceof DescriptionElementBase){
246 if (((DescriptionElementBase)element).getInDescription() instanceof TaxonNameDescription){
247 TaxonName changedName = ((TaxonNameDescription)((DescriptionElementBase)element).getInDescription()).getTaxonName();
248 getEditorInput().addSaveCandidate(changedName);
249 input.replaceInModel(changedName);
250 }else if (((DescriptionElementBase)element).getInDescription() instanceof TaxonDescription){
251 Taxon changedTaxon = ((TaxonDescription)((DescriptionElementBase)element).getInDescription()).getTaxon();
252 getEditorInput().addSaveCandidate(changedTaxon);
253 input.replaceInModel(changedTaxon);
254 }
255 }else{
256 getEditorInput().addSaveCandidate((CdmBase)element);
257 input.replaceInModel((CdmBase) element);
258 }
259 }
260 dirty.setDirty(true);
261 setSelection(new StructuredSelection(element));
262 }
263
264 @Override
265 public void forceDirty() {
266 dirty.setDirty(true);
267 }
268
269 @Override
270 public boolean postOperation(Object objectAffectedByOperation) {
271 return false;
272 }
273
274 @Override
275 public boolean onComplete() {
276 return false;
277 }
278
279 @Override
280 public ConversationHolder getConversationHolder() {
281 return conversation;
282 }
283
284 public BulkEditorQuery getLastQuery() {
285 return lastQuery;
286 }
287
288 public void setLastQuery(BulkEditorQuery lastQuery) {
289 this.lastQuery = lastQuery;
290
291 }
292
293 @Override
294 public Taxon getTaxon() {
295 IStructuredSelection selection = getSelection();
296 if(selection.size()==1) {
297 Object object = selection.iterator().next();
298 if(object instanceof Taxon){
299 return (Taxon) object;
300 }
301 }
302 return null;
303 }
304
305 }