Project

General

Profile

Download (9.8 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

    
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
        if(selection!=null){
174
            setSelection(selection);
175
        }
176
    }
177

    
178
    public void refresh(){
179
        bulkEditorComposite.refresh();
180
    }
181

    
182
    public IStructuredSelection getSelection(){
183
        return bulkEditorComposite.getSelection();
184
    }
185

    
186
    public void setSelection(IStructuredSelection selection){
187
        bulkEditorComposite.setSelection(selection);
188
    }
189

    
190
    public void setDirty(boolean isDirty){
191
        dirty.setDirty(isDirty);
192
    }
193

    
194
    public void setDirty(){
195
        setDirty(true);
196
    }
197

    
198
    @Override
199
    public boolean isDirty() {
200
        return dirty.isDirty();
201
    }
202

    
203
    public AbstractBulkEditorInput getEditorInput() {
204
        return input;
205
    }
206

    
207
    public void copyDataToClipboard() {
208
        bulkEditorComposite.copyDataToClipboard();
209
    }
210

    
211
    @Override
212
    public void update(CdmDataChangeMap arg0) {
213
    }
214

    
215
    @Override
216
    public boolean canAttachMedia() {
217
        return true;
218
    }
219

    
220
    @Override
221
    public void changed(Object element) {
222
        if(element instanceof DerivedUnitFacade){
223
            DerivedUnit derivedUnit = ((DerivedUnitFacade) element).innerDerivedUnit();
224
            if(derivedUnit!=null){
225
                getEditorInput().addSaveCandidate(derivedUnit);
226
            }
227
            FieldUnit fieldUnit = ((DerivedUnitFacade) element).innerFieldUnit();
228
            if(fieldUnit!=null){
229
                getEditorInput().addSaveCandidate(fieldUnit);
230
            }
231
        }
232
        else if (element instanceof CdmBase) {
233
            if (element instanceof DescriptionBase){
234
                if (element instanceof TaxonNameDescription){
235
                    TaxonName changedName = ((TaxonNameDescription)element).getTaxonName();
236
                    getEditorInput().addSaveCandidate(changedName);
237
                    input.replaceInModel(changedName);
238
                }else if (element instanceof TaxonDescription){
239
                    Taxon changedTaxon = ((TaxonDescription)element).getTaxon();
240
                    getEditorInput().addSaveCandidate(changedTaxon);
241
                    input.replaceInModel(changedTaxon);
242
                }
243
            }else if (element instanceof DescriptionElementBase){
244
                if (((DescriptionElementBase)element).getInDescription() instanceof TaxonNameDescription){
245
                    TaxonName changedName = ((TaxonNameDescription)((DescriptionElementBase)element).getInDescription()).getTaxonName();
246
                    getEditorInput().addSaveCandidate(changedName);
247
                    input.replaceInModel(changedName);
248
                }else if (((DescriptionElementBase)element).getInDescription() instanceof TaxonDescription){
249
                    Taxon changedTaxon = ((TaxonDescription)((DescriptionElementBase)element).getInDescription()).getTaxon();
250
                    getEditorInput().addSaveCandidate(changedTaxon);
251
                    input.replaceInModel(changedTaxon);
252
                }
253
            }else{
254
                getEditorInput().addSaveCandidate((CdmBase)element);
255
                input.replaceInModel((CdmBase) element);
256
            }
257
        }
258
        dirty.setDirty(true);
259
        setSelection(new StructuredSelection(element));
260
    }
261

    
262
    @Override
263
    public void forceDirty() {
264
        dirty.setDirty(true);
265
    }
266

    
267
    @Override
268
    public boolean postOperation(Object objectAffectedByOperation) {
269
        return false;
270
    }
271

    
272
    @Override
273
    public boolean onComplete() {
274
        return false;
275
    }
276

    
277
    @Override
278
    public ConversationHolder getConversationHolder() {
279
        return conversation;
280
    }
281

    
282
    public BulkEditorQuery getLastQuery() {
283
       return lastQuery;
284
    }
285

    
286
    public void setLastQuery(BulkEditorQuery lastQuery) {
287
       this.lastQuery = lastQuery;
288

    
289
    }
290

    
291
    @Override
292
    public Taxon getTaxon() {
293
        IStructuredSelection selection = getSelection();
294
        if(selection.size()==1) {
295
            Object object = selection.iterator().next();
296
            if(object instanceof Taxon){
297
                return (Taxon) object;
298
            }
299
        }
300
        return null;
301
    }
302

    
303
    /**
304
     * {@inheritDoc}
305
     */
306
    @Override
307
    public void update() {
308
        input.performSearch(lastQuery, getSelection());
309

    
310
    }
311

    
312
}
(3-3/9)