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