Project

General

Profile

Download (7.92 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.occurrence.DerivedUnit;
39
import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
40
import eu.etaxonomy.cdm.model.taxon.Taxon;
41
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
42
import eu.etaxonomy.taxeditor.bulkeditor.BulkEditorQuery;
43
import eu.etaxonomy.taxeditor.bulkeditor.input.AbstractBulkEditorInput;
44
import eu.etaxonomy.taxeditor.editor.ITaxonEditor;
45
import eu.etaxonomy.taxeditor.event.WorkbenchEventConstants;
46
import eu.etaxonomy.taxeditor.model.IDerivedUnitFacadePart;
47
import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
48
import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
49
import eu.etaxonomy.taxeditor.model.IPartContentHasFactualData;
50
import eu.etaxonomy.taxeditor.model.IPartContentHasMedia;
51
import eu.etaxonomy.taxeditor.model.IPartContentHasSupplementalData;
52
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
53
import eu.etaxonomy.taxeditor.workbench.part.IE4SavablePart;
54

    
55
/**
56
 *
57
 * @author pplitzner
58
 * @since Sep 8, 2017
59
 *
60
 */
61
public class BulkEditorE4 implements IPartContentHasDetails, IConversationEnabled, IPostOperationEnabled,
62
        IDirtyMarkable, IDerivedUnitFacadePart, IPartContentHasFactualData,
63
        IPartContentHasSupplementalData, IPartContentHasMedia, IE4SavablePart, ITaxonEditor {
64

    
65
    @Inject
66
	private MDirtyable dirty;
67

    
68
    private AbstractBulkEditorInput input;
69

    
70
    private ConversationHolder conversation;
71

    
72
    @Inject
73
    private IEventBroker eventBroker;
74

    
75
    @Inject
76
    IEclipseContext context;
77

    
78
    @Inject
79
    private MPart thisPart;
80

    
81
    private BulkEditorQuery lastQuery = null;
82

    
83
    private BulkEditorE4Composite bulkEditorComposite;
84

    
85
    @Inject
86
    public BulkEditorE4() {
87
	}
88

    
89
	@SuppressWarnings("unused")
90
    public void init(AbstractBulkEditorInput<?> input){
91
	    this.input = input;
92
	    this.conversation = input.getConversation();
93
	    thisPart.setLabel(input.getEditorName());
94

    
95
	    bulkEditorComposite.init(input);
96
	}
97

    
98
	/** {@inheritDoc} */
99
	@PostConstruct
100
	public void createPartControl(Composite parent, IEclipseContext context) {
101
	    bulkEditorComposite = new BulkEditorE4Composite(this, parent, SWT.NONE);
102
	    ContextInjectionFactory.inject(bulkEditorComposite, context);
103
	}
104

    
105
	@Override
106
	@Persist
107
	public void save(IProgressMonitor monitor) {
108
	    save(monitor, true);
109
	}
110

    
111
    public void save(IProgressMonitor monitor, boolean resetMerge) {
112
        if (!input.getCdmEntitySession().isActive()){
113
            input.getCdmEntitySession().bind();
114
        }
115
        input.saveModel(resetMerge);
116

    
117
        IStructuredSelection selection = getSelection();
118

    
119
        dirty.setDirty(false);
120
        input.dispose();
121
        input.bind();
122
        conversation.commit(true);
123

    
124
        if (lastQuery != null){
125
            bulkEditorComposite.performSearch(lastQuery, selection);
126
        }
127
    }
128

    
129
	@Focus
130
	public void setFocus() {
131
        //make sure to bind again if maybe in another view the conversation was unbound
132
        if(conversation!=null && !conversation.isBound()){
133
            conversation.bind();
134
        }
135
        if(input!=null && input.getCdmEntitySession()!= null) {
136
            input.getCdmEntitySession().bind();
137
        }
138

    
139
	    //make sure to bind again if maybe in another view the conversation was unbound
140
	    eventBroker.post(WorkbenchEventConstants.CURRENT_ACTIVE_EDITOR, this);
141
	}
142

    
143
	@PreDestroy
144
	public void dispose() {
145
	    if(conversation!=null){
146
	        conversation.unregisterForDataStoreChanges(this);
147
	        conversation.close();
148
	    }
149
	    if(input!=null){
150
	        input.dispose();
151
	    }
152
	    dirty.setDirty(false);
153
	    //save table settings
154
	    if(bulkEditorComposite.getNatTableState()!=null){
155
            try (FileOutputStream tableStateStream =
156
                    new FileOutputStream(bulkEditorComposite.getStatePropertiesFile())) {
157
                bulkEditorComposite.getNatTableState().store(tableStateStream, null);
158
            } catch (IOException ioe) {
159
                ioe.printStackTrace();
160
            }
161
        }
162
	}
163

    
164
    @Optional
165
    @Inject
166
    private void updateAfterSearch(@UIEventTopic(WorkbenchEventConstants.BULK_EDITOR_SEARCH_FINISHED)IStructuredSelection selection){
167
        if(selection!=null){
168
            setSelection(selection);
169
        }
170
    }
171

    
172
    public void refresh(){
173
        bulkEditorComposite.refresh();
174
    }
175

    
176
    public IStructuredSelection getSelection(){
177
        return bulkEditorComposite.getSelection();
178
    }
179

    
180
    public void setSelection(IStructuredSelection selection){
181
        bulkEditorComposite.setSelection(selection);
182
    }
183

    
184
    public void setDirty(boolean isDirty){
185
        dirty.setDirty(isDirty);
186
    }
187

    
188
    public void setDirty(){
189
        setDirty(true);
190
    }
191

    
192
    public boolean isDirty() {
193
        return dirty.isDirty();
194
    }
195

    
196
    public AbstractBulkEditorInput getEditorInput() {
197
        return input;
198
    }
199

    
200
    public void copyDataToClipboard() {
201
        bulkEditorComposite.copyDataToClipboard();
202
    }
203

    
204
    @Override
205
    public void update(CdmDataChangeMap arg0) {
206
    }
207

    
208
    @Override
209
    public boolean canAttachMedia() {
210
        return true;
211
    }
212

    
213
    @Override
214
    public void changed(Object element) {
215
        if(element instanceof DerivedUnitFacade){
216
            DerivedUnit derivedUnit = ((DerivedUnitFacade) element).innerDerivedUnit();
217
            if(derivedUnit!=null){
218
                getEditorInput().addSaveCandidate(derivedUnit);
219
            }
220
            FieldUnit fieldUnit = ((DerivedUnitFacade) element).innerFieldUnit();
221
            if(fieldUnit!=null){
222
                getEditorInput().addSaveCandidate(fieldUnit);
223
            }
224
        }
225
        else if (element instanceof CdmBase) {
226
            getEditorInput().addSaveCandidate((CdmBase)element);
227
            input.replaceInModel((CdmBase) element);
228
        }
229
        dirty.setDirty(true);
230
        setSelection(new StructuredSelection(element));
231
    }
232

    
233
    @Override
234
    public void forceDirty() {
235
        dirty.setDirty(true);
236
    }
237

    
238
    @Override
239
    public boolean postOperation(CdmBase objectAffectedByOperation) {
240
        return false;
241
    }
242

    
243
    @Override
244
    public boolean onComplete() {
245
        return false;
246
    }
247

    
248
    @Override
249
    public ConversationHolder getConversationHolder() {
250
        return conversation;
251
    }
252

    
253
    public BulkEditorQuery getLastQuery() {
254
       return lastQuery;
255
    }
256

    
257
    public void setLastQuery(BulkEditorQuery lastQuery) {
258
       this.lastQuery = lastQuery;
259

    
260
    }
261

    
262
    @Override
263
    public Taxon getTaxon() {
264
        IStructuredSelection selection = getSelection();
265
        if(selection.size()==1) {
266
            Object object = selection.iterator().next();
267
            if(object instanceof Taxon){
268
                return (Taxon) object;
269
            }
270
        }
271
        return null;
272
    }
273

    
274
}
(3-3/8)