Project

General

Profile

Download (11.1 KB) Statistics
| Branch: | Tag: | Revision:
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.IPostOperationEnabled;
63
import eu.etaxonomy.taxeditor.workbench.part.IE4SavablePart;
64

    
65
/**
66
 *
67
 * @author pplitzner
68
 * @since Sep 8, 2017
69
 *
70
 */
71
public class BulkEditorE4 implements IPartContentHasDetails, IConversationEnabled, IPostOperationEnabled,
72
        IDirtyMarkable, IDerivedUnitFacadePart, IPartContentHasFactualData,
73
        IPartContentHasSupplementalData, IPartContentHasMedia, IE4SavablePart, ITaxonEditor {
74

    
75
    @Inject
76
	private MDirtyable dirty;
77

    
78
    private AbstractBulkEditorInput input;
79

    
80
    private ConversationHolder conversation;
81

    
82
    @Inject
83
    private IEventBroker eventBroker;
84

    
85
    @Inject
86
    IEclipseContext context;
87

    
88
    @Inject
89
    private MPart thisPart;
90

    
91
    private BulkEditorQuery lastQuery = null;
92

    
93
    private BulkEditorE4Composite bulkEditorComposite;
94

    
95
    @Inject
96
    public BulkEditorE4() {
97
	}
98

    
99
	@SuppressWarnings("unused")
100
    public void init(AbstractBulkEditorInput<?> input){
101
	    this.input = input;
102
	    this.conversation = input.getConversation();
103
	    thisPart.setLabel(input.getEditorName());
104

    
105
	    bulkEditorComposite.init(input);
106
	}
107

    
108
	/** {@inheritDoc} */
109
	@PostConstruct
110
	public void createPartControl(Composite parent, IEclipseContext context) {
111
	    bulkEditorComposite = new BulkEditorE4Composite(this, parent, SWT.NONE);
112
	    ContextInjectionFactory.inject(bulkEditorComposite, context);
113
	}
114

    
115
	@Override
116
	@Persist
117
	public void save(IProgressMonitor monitor) {
118
	    save(monitor, true);
119
	}
120

    
121
    public void save(IProgressMonitor monitor, boolean resetMerge) {
122
        if (!input.getCdmEntitySession().isActive()){
123
            input.getCdmEntitySession().bind();
124
        }
125
        input.saveModel(resetMerge);
126

    
127
        IStructuredSelection selection = getSelection();
128

    
129
        dirty.setDirty(false);
130
        input.dispose();
131
        input.bind();
132
        conversation.commit(true);
133

    
134
        if (lastQuery != null){
135
            bulkEditorComposite.performSearch(lastQuery, selection);
136
        }
137
    }
138

    
139

    
140
	@Focus
141
	public void setFocus() {
142
        //make sure to bind again if maybe in another view the conversation was unbound
143
        if(conversation!=null && !conversation.isBound()){
144
            conversation.bind();
145
        }
146
        if(input!=null && input.getCdmEntitySession()!= null) {
147
            input.getCdmEntitySession().bind();
148
        }
149

    
150
	    //make sure to bind again if maybe in another view the conversation was unbound
151
	    eventBroker.post(WorkbenchEventConstants.CURRENT_ACTIVE_EDITOR, this);
152
	}
153

    
154
	@PreDestroy
155
	public void dispose() {
156
	    if(conversation!=null){
157
	        conversation.unregisterForDataStoreChanges(this);
158
	        conversation.close();
159
	    }
160
	    if(input!=null){
161
	        input.dispose();
162
	    }
163
	    dirty.setDirty(false);
164
	    //save table settings
165
	    if(bulkEditorComposite.getNatTableState()!=null){
166
            try (FileOutputStream tableStateStream =
167
                    new FileOutputStream(bulkEditorComposite.getStatePropertiesFile())) {
168
                bulkEditorComposite.getNatTableState().store(tableStateStream, null);
169
            } catch (IOException ioe) {
170
                ioe.printStackTrace();
171
            }
172
        }
173
	}
174

    
175
    @Optional
176
    @Inject
177
    private void updateAfterSearch(@UIEventTopic(WorkbenchEventConstants.BULK_EDITOR_SEARCH_FINISHED)IStructuredSelection selection){
178
        if(selection!=null){
179
            setSelection(selection);
180
        }
181
    }
182

    
183
    public void refresh(){
184
        bulkEditorComposite.refresh();
185
    }
186

    
187
    public IStructuredSelection getSelection(){
188
        return bulkEditorComposite.getSelection();
189
    }
190

    
191
    public void setSelection(IStructuredSelection selection){
192
        bulkEditorComposite.setSelection(selection);
193
    }
194

    
195
    public void setDirty(boolean isDirty){
196
        dirty.setDirty(isDirty);
197
    }
198

    
199
    public void setDirty(){
200
        setDirty(true);
201
    }
202

    
203
    @Override
204
    public boolean isDirty() {
205
        return dirty.isDirty();
206
    }
207

    
208
    public AbstractBulkEditorInput getEditorInput() {
209
        return input;
210
    }
211

    
212
    public void copyDataToClipboard() {
213
        bulkEditorComposite.copyDataToClipboard();
214
    }
215

    
216
    @Override
217
    public void update(CdmDataChangeMap arg0) {
218
    }
219

    
220
    @Override
221
    public boolean canAttachMedia() {
222
        return true;
223
    }
224

    
225
    @Override
226
    public void changed(Object element) {
227
        if(element instanceof DerivedUnitFacade){
228
            DerivedUnit derivedUnit = ((DerivedUnitFacade) element).innerDerivedUnit();
229
            if(derivedUnit!=null){
230
                getEditorInput().addSaveCandidate(derivedUnit);
231
            }
232
            FieldUnit fieldUnit = ((DerivedUnitFacade) element).innerFieldUnit();
233
            if(fieldUnit!=null){
234
                getEditorInput().addSaveCandidate(fieldUnit);
235
            }
236
        }
237
        else if (element instanceof CdmBase) {
238
            if (element instanceof DescriptionBase){
239
                if (element instanceof TaxonNameDescription){
240
                    TaxonName changedName = ((TaxonNameDescription)element).getTaxonName();
241
                    getEditorInput().addSaveCandidate(changedName);
242
                    input.replaceInModel(changedName);
243
                }else if (element instanceof TaxonDescription){
244
                    Taxon changedTaxon = ((TaxonDescription)element).getTaxon();
245
                    getEditorInput().addSaveCandidate(changedTaxon);
246
                    input.replaceInModel(changedTaxon);
247
                }
248
            }else if (element instanceof DescriptionElementBase){
249
                if (((DescriptionElementBase)element).getInDescription() instanceof TaxonNameDescription){
250
                    TaxonName changedName = ((TaxonNameDescription)((DescriptionElementBase)element).getInDescription()).getTaxonName();
251
                    getEditorInput().addSaveCandidate(changedName);
252
                    input.replaceInModel(changedName);
253
                }else if (((DescriptionElementBase)element).getInDescription() instanceof TaxonDescription){
254
                    Taxon changedTaxon = ((TaxonDescription)((DescriptionElementBase)element).getInDescription()).getTaxon();
255
                    getEditorInput().addSaveCandidate(changedTaxon);
256
                    input.replaceInModel(changedTaxon);
257
                }
258
            }else if(element instanceof Group){
259
                 Group oldGroup = ((GroupEditorInput)input).getEntityFromModel((Group)element);
260
                 ((GroupEditorInput)input).getSaveUserCandidates().addAll(oldGroup.getMembers());
261
                 getEditorInput().addSaveCandidate((Group)element);
262
                 input.replaceInModel((CdmBase) element);
263
            }else{
264
                getEditorInput().addSaveCandidate((CdmBase)element);
265
                input.replaceInModel((CdmBase) element);
266
            }
267
        }
268
        dirty.setDirty(true);
269
        setSelection(new StructuredSelection(element));
270
    }
271

    
272
    @Override
273
    public void forceDirty() {
274
        dirty.setDirty(true);
275
    }
276

    
277
    @Override
278
    public boolean postOperation(Object objectAffectedByOperation) {
279
        return false;
280
    }
281

    
282
    @Override
283
    public boolean onComplete() {
284
        return false;
285
    }
286

    
287
    @Override
288
    public ConversationHolder getConversationHolder() {
289
        return conversation;
290
    }
291

    
292
    public BulkEditorQuery getLastQuery() {
293
       return lastQuery;
294
    }
295

    
296
    public void setLastQuery(BulkEditorQuery lastQuery) {
297
       this.lastQuery = lastQuery;
298

    
299
    }
300

    
301
    @Override
302
    public Taxon getTaxon() {
303
        IStructuredSelection selection = getSelection();
304
        if(selection.size()==1) {
305
            Object object = selection.iterator().next();
306
            if(object instanceof Taxon){
307
                return (Taxon) object;
308
            }
309
        }
310
        return null;
311
    }
312

    
313
    /**
314
     * {@inheritDoc}
315
     */
316
    @Override
317
    public void update() {
318
        input.performSearch(lastQuery, getSelection());
319

    
320
    }
321

    
322
    @Inject
323
    @Optional
324
    private void updateView(@UIEventTopic(WorkbenchEventConstants.REFRESH_NAME_EDITOR) List<CdmBase> cdmBases) {
325
        if (getEditorInput() instanceof TaxonEditorInput){
326
            for (CdmBase cdmBase: cdmBases){
327
                if (getEditorInput().getModel().contains(cdmBase)){
328
                    input.performSearch(lastQuery, getSelection());
329
                    break;
330
                }
331
            }
332
        }
333
    }
334

    
335
    @Inject
336
    @Optional
337
    private void updatefromDelete(@UIEventTopic(WorkbenchEventConstants.REMOVE_USER) User user) {
338
        if (input instanceof GroupEditorInput){
339
            ((GroupEditorInput)input).getSaveUserCandidates().add(user);
340
        }
341
    }
342

    
343
}
(3-3/9)