ref #9448: remove E4 from file names - bulkeditor continue
[taxeditor.git] / eu.etaxonomy.taxeditor.bulkeditor / src / main / java / eu / etaxonomy / taxeditor / bulkeditor / e4 / BulkEditor.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 package eu.etaxonomy.taxeditor.bulkeditor.e4;
10
11 import java.io.FileOutputStream;
12 import java.io.IOException;
13 import java.util.List;
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.SpecimenDescription;
41 import eu.etaxonomy.cdm.model.description.TaxonDescription;
42 import eu.etaxonomy.cdm.model.description.TaxonNameDescription;
43 import eu.etaxonomy.cdm.model.media.Media;
44 import eu.etaxonomy.cdm.model.name.TaxonName;
45 import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
46 import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
47 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
48 import eu.etaxonomy.cdm.model.permission.Group;
49 import eu.etaxonomy.cdm.model.permission.User;
50 import eu.etaxonomy.cdm.model.taxon.Taxon;
51 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
52 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
53 import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
54 import eu.etaxonomy.taxeditor.bulkeditor.BulkEditorQuery;
55 import eu.etaxonomy.taxeditor.bulkeditor.input.AbstractBulkEditorInput;
56 import eu.etaxonomy.taxeditor.bulkeditor.input.GroupEditorInput;
57 import eu.etaxonomy.taxeditor.bulkeditor.input.TaxonEditorInput;
58 import eu.etaxonomy.taxeditor.editor.IBulkEditor;
59 import eu.etaxonomy.taxeditor.editor.ITaxonEditor;
60 import eu.etaxonomy.taxeditor.event.WorkbenchEventConstants;
61 import eu.etaxonomy.taxeditor.model.IDerivedUnitFacadePart;
62 import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
63 import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
64 import eu.etaxonomy.taxeditor.model.IPartContentHasFactualData;
65 import eu.etaxonomy.taxeditor.model.IPartContentHasMedia;
66 import eu.etaxonomy.taxeditor.model.IPartContentHasSupplementalData;
67 import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
68 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
69 import eu.etaxonomy.taxeditor.workbench.part.IE4SavablePart;
70
71 /**
72 * @author pplitzner
73 * @since Sep 8, 2017
74 */
75 public class BulkEditor implements IPartContentHasDetails, IConversationEnabled, IPostOperationEnabled,
76 IDirtyMarkable, IDerivedUnitFacadePart, IPartContentHasFactualData,
77 IPartContentHasSupplementalData, IPartContentHasMedia, IE4SavablePart, ITaxonEditor, IBulkEditor {
78
79 @Inject
80 private MDirtyable dirty;
81
82 private AbstractBulkEditorInput input;
83
84 private ConversationHolder conversation;
85
86 @Inject
87 private IEventBroker eventBroker;
88
89 @Inject
90 IEclipseContext context;
91
92 @Inject
93 private MPart thisPart;
94
95 private BulkEditorQuery lastQuery = null;
96
97 private BulkEditorComposite bulkEditorComposite;
98
99 @Inject
100 public BulkEditor() {
101 }
102
103 public void init(AbstractBulkEditorInput<?> input){
104 this.input = input;
105 this.conversation = input.getConversation();
106 thisPart.setLabel(input.getEditorName());
107
108 bulkEditorComposite.init(input);
109 }
110
111 @PostConstruct
112 public void createPartControl(Composite parent, IEclipseContext context) {
113 bulkEditorComposite = new BulkEditorComposite(this, parent, SWT.NONE);
114 ContextInjectionFactory.inject(bulkEditorComposite, context);
115 }
116
117 @Override
118 @Persist
119 public void save(IProgressMonitor monitor) {
120 save(monitor, true);
121 }
122
123 public void save(IProgressMonitor monitor, boolean resetMerge) {
124 if (!input.getCdmEntitySession().isActive()){
125 input.getCdmEntitySession().bind();
126 }
127 input.saveModel(resetMerge);
128
129 IStructuredSelection selection = getSelection();
130
131 dirty.setDirty(false);
132 input.dispose();
133 input.bind();
134 conversation.commit(true);
135
136 if (lastQuery != null){
137 bulkEditorComposite.performSearch(lastQuery, selection);
138 }
139 }
140
141
142 @Focus
143 public void setFocus() {
144 //make sure to bind again if maybe in another view the conversation was unbound
145 if(conversation!=null && !conversation.isBound()){
146 conversation.bind();
147 }
148 if(input!=null && input.getCdmEntitySession()!= null) {
149 input.getCdmEntitySession().bind();
150 }
151
152 //make sure to bind again if maybe in another view the conversation was unbound
153 eventBroker.post(WorkbenchEventConstants.CURRENT_ACTIVE_EDITOR, this);
154 }
155
156 @PreDestroy
157 public void dispose() {
158 if(conversation!=null){
159 conversation.unregisterForDataStoreChanges(this);
160 conversation.close();
161 }
162 if(input!=null){
163 input.dispose();
164 }
165 dirty.setDirty(false);
166 //save table settings
167 if(bulkEditorComposite.getNatTableState()!=null){
168 try (FileOutputStream tableStateStream =
169 new FileOutputStream(bulkEditorComposite.getStatePropertiesFile())) {
170 bulkEditorComposite.getNatTableState().store(tableStateStream, null);
171 } catch (IOException ioe) {
172 ioe.printStackTrace();
173 }
174 }
175 }
176
177 @Optional
178 @Inject
179 private void updateAfterSearch(@UIEventTopic(WorkbenchEventConstants.BULK_EDITOR_SEARCH_FINISHED)IStructuredSelection selection){
180 if(selection!=null){
181 setSelection(selection);
182 }
183 }
184
185 public void refresh(){
186 bulkEditorComposite.refresh();
187 }
188
189 public IStructuredSelection getSelection(){
190 return bulkEditorComposite.getSelection();
191 }
192
193 public IStructuredSelection getCellSelection(){
194 return bulkEditorComposite.getCellSelection();
195 }
196
197 public void setSelection(IStructuredSelection selection){
198 bulkEditorComposite.setSelection(selection);
199 }
200
201 public void setDirty(boolean isDirty){
202 dirty.setDirty(isDirty);
203 }
204
205 public void setDirty(){
206 setDirty(true);
207 }
208
209 @Override
210 public boolean isDirty() {
211 return dirty.isDirty();
212 }
213
214 public AbstractBulkEditorInput getEditorInput() {
215 return input;
216 }
217
218 public void copyDataToClipboard() {
219 bulkEditorComposite.copyDataToClipboard();
220 }
221
222 @Override
223 public void update(CdmDataChangeMap arg0) {
224 }
225
226 @Override
227 public boolean canAttachMedia() {
228 return true;
229 }
230
231 @Override
232 public void changed(Object element) {
233 if(element instanceof DerivedUnitFacade){
234 DerivedUnit derivedUnit = ((DerivedUnitFacade) element).innerDerivedUnit();
235 if(derivedUnit!=null){
236 getEditorInput().addSaveCandidate(derivedUnit);
237 }
238 FieldUnit fieldUnit = ((DerivedUnitFacade) element).innerFieldUnit();
239 if(fieldUnit!=null){
240 getEditorInput().addSaveCandidate(fieldUnit);
241 }
242 }
243 else if (element instanceof CdmBase) {
244 if (element instanceof DescriptionBase){
245 if (element instanceof TaxonNameDescription){
246 TaxonName changedName = ((TaxonNameDescription)element).getTaxonName();
247 if (getEditorInput() instanceof TaxonEditorInput){
248 IStructuredSelection sel = getSelection();
249 Object firstElement = sel.getFirstElement();
250 if (firstElement instanceof TaxonBase){
251 getEditorInput().addSaveCandidate((TaxonBase)firstElement);
252 }
253 }else{
254 getEditorInput().addSaveCandidate(changedName);
255 }
256
257 input.replaceInModel(changedName);
258 }else if (element instanceof TaxonDescription){
259 Taxon changedTaxon = ((TaxonDescription)element).getTaxon();
260 getEditorInput().addSaveCandidate(changedTaxon);
261 input.replaceInModel(changedTaxon);
262 }
263 }else if (element instanceof DescriptionElementBase){
264 if (((DescriptionElementBase)element).getInDescription() instanceof TaxonNameDescription){
265 TaxonName changedName = ((TaxonNameDescription)((DescriptionElementBase)element).getInDescription()).getTaxonName();
266 //check whether the bulk editor is taxon or name bulk editor
267 if (getEditorInput() instanceof TaxonEditorInput){
268 IStructuredSelection sel = getSelection();
269 Object firstElement = sel.getFirstElement();
270 if (firstElement instanceof TaxonBase){
271 getEditorInput().addSaveCandidate((TaxonBase)firstElement);
272 }
273 }else{
274 getEditorInput().addSaveCandidate(changedName);
275 }
276 input.replaceInModel(changedName);
277 }else if (((DescriptionElementBase)element).getInDescription() instanceof TaxonDescription){
278 Taxon changedTaxon = ((TaxonDescription)((DescriptionElementBase)element).getInDescription()).getTaxon();
279 getEditorInput().addSaveCandidate(changedTaxon);
280 input.replaceInModel(changedTaxon);
281 }else if (((DescriptionElementBase)element).getInDescription() instanceof SpecimenDescription){
282 SpecimenOrObservationBase changedSpecimen = ((SpecimenDescription)((DescriptionElementBase)element).getInDescription()).getDescribedSpecimenOrObservation();
283 getEditorInput().addSaveCandidate(changedSpecimen);
284 input.replaceInModel(changedSpecimen);
285 }
286 }else if (element instanceof Media){
287 IStructuredSelection sel = getSelection();
288 Object firstElement = sel.getFirstElement();
289 if (firstElement instanceof TaxonBase){
290 TaxonBase changedTaxon = (TaxonBase)sel.getFirstElement();
291 getEditorInput().addSaveCandidate(changedTaxon);
292 input.replaceInModel(changedTaxon);
293 }else if (firstElement instanceof SpecimenOrObservationBase){
294 SpecimenOrObservationBase changedSpecimen = (SpecimenOrObservationBase)sel.getFirstElement();
295 getEditorInput().addSaveCandidate(changedSpecimen);
296 input.replaceInModel(changedSpecimen);
297 } else if (firstElement instanceof Media){
298 getEditorInput().addSaveCandidate((Media)element);
299 input.replaceInModel((Media)element);
300 }
301 }else if(element instanceof Group){
302 Group oldGroup = ((GroupEditorInput)input).getEntityFromModel((Group)element);
303 ((GroupEditorInput)input).getSaveUserCandidates().addAll(oldGroup.getMembers());
304 getEditorInput().addSaveCandidate((Group)element);
305 input.replaceInModel((CdmBase) element);
306 }else{
307 getEditorInput().addSaveCandidate((CdmBase)element);
308 input.replaceInModel((CdmBase) element);
309 }
310 }
311 dirty.setDirty(true);
312 setSelection(new StructuredSelection(element));
313 }
314
315 @Override
316 public void forceDirty() {
317 dirty.setDirty(true);
318 }
319
320 @Override
321 public boolean postOperation(Object objectAffectedByOperation) {
322 return false;
323 }
324
325 @Override
326 public boolean onComplete() {
327 return false;
328 }
329
330 @Override
331 public ConversationHolder getConversationHolder() {
332 return conversation;
333 }
334
335 public BulkEditorQuery getLastQuery() {
336 return lastQuery;
337 }
338
339 public void setLastQuery(BulkEditorQuery lastQuery) {
340 this.lastQuery = lastQuery;
341
342 }
343
344 @Override
345 public Taxon getTaxon() {
346 IStructuredSelection selection = getSelection();
347 if(selection.size()==1) {
348 Object object = selection.iterator().next();
349 if(object instanceof Taxon){
350 return (Taxon) object;
351 }
352 }
353 return null;
354 }
355
356 @Override
357 public void update() {
358 input.performSearch(lastQuery, getSelection());
359 }
360
361 @Override
362 public void addOperation(AbstractPostOperation operation) {
363 // operations not yet used for bulk editor
364 }
365
366 @Inject
367 @Optional
368 private void updateView(@UIEventTopic(WorkbenchEventConstants.REFRESH_NAME_EDITOR) List<CdmBase> cdmBases) {
369 if (getEditorInput() instanceof TaxonEditorInput){
370 for (CdmBase cdmBase: cdmBases){
371 if (getEditorInput().getModel().contains(cdmBase)){
372 input.performSearch(lastQuery, getSelection());
373 break;
374 }
375 }
376 }
377 }
378
379 @Inject
380 @Optional
381 private void updatefromDelete(@UIEventTopic(WorkbenchEventConstants.REMOVE_USER) User user) {
382 if (input instanceof GroupEditorInput){
383 ((GroupEditorInput)input).getSaveUserCandidates().add(user);
384 }
385 }
386
387 @Override
388 public TaxonNode getTaxonNode() {
389 return null;
390 }
391
392 }