Project

General

Profile

Download (7.46 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 javax.annotation.PostConstruct;
13
import javax.annotation.PreDestroy;
14
import javax.inject.Inject;
15

    
16
import org.eclipse.core.runtime.IProgressMonitor;
17
import org.eclipse.e4.core.contexts.ContextInjectionFactory;
18
import org.eclipse.e4.core.contexts.IEclipseContext;
19
import org.eclipse.e4.core.di.annotations.Optional;
20
import org.eclipse.e4.core.services.events.IEventBroker;
21
import org.eclipse.e4.ui.di.Focus;
22
import org.eclipse.e4.ui.di.Persist;
23
import org.eclipse.e4.ui.di.UIEventTopic;
24
import org.eclipse.e4.ui.model.application.ui.MDirtyable;
25
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
26
import org.eclipse.jface.viewers.IStructuredSelection;
27
import org.eclipse.jface.viewers.StructuredSelection;
28
import org.eclipse.swt.SWT;
29
import org.eclipse.swt.widgets.Composite;
30

    
31
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
32
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
33
import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade;
34
import eu.etaxonomy.cdm.model.common.CdmBase;
35
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
36
import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
37
import eu.etaxonomy.cdm.model.taxon.Taxon;
38
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
39
import eu.etaxonomy.taxeditor.bulkeditor.BulkEditorQuery;
40
import eu.etaxonomy.taxeditor.bulkeditor.input.AbstractBulkEditorInput;
41
import eu.etaxonomy.taxeditor.editor.ITaxonEditor;
42
import eu.etaxonomy.taxeditor.event.WorkbenchEventConstants;
43
import eu.etaxonomy.taxeditor.model.IDerivedUnitFacadePart;
44
import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
45
import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
46
import eu.etaxonomy.taxeditor.model.IPartContentHasFactualData;
47
import eu.etaxonomy.taxeditor.model.IPartContentHasMedia;
48
import eu.etaxonomy.taxeditor.model.IPartContentHasSupplementalData;
49
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
50
import eu.etaxonomy.taxeditor.workbench.part.IE4SavablePart;
51

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

    
62
    @Inject
63
	private MDirtyable dirty;
64

    
65
    private AbstractBulkEditorInput input;
66

    
67
    private ConversationHolder conversation;
68

    
69
    @Inject
70
    private IEventBroker eventBroker;
71

    
72
    @Inject
73
    IEclipseContext context;
74

    
75
    @Inject
76
    private MPart thisPart;
77

    
78
    private BulkEditorQuery lastQuery = null;
79

    
80
    private BulkEditorE4Composite bulkEditorComposite;
81

    
82
    @Inject
83
    public BulkEditorE4() {
84
	}
85

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

    
92
	    bulkEditorComposite.init(input);
93
	}
94

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

    
102
	@Override
103
	@Persist
104
	public void save(IProgressMonitor monitor) {
105
	    save(monitor, true);
106
	}
107

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

    
114
        IStructuredSelection selection = getSelection();
115

    
116
        dirty.setDirty(false);
117
        input.dispose();
118
        input.bind();
119
        conversation.commit(true);
120

    
121
        if (lastQuery != null){
122
            bulkEditorComposite.performSearch(lastQuery, selection);
123
        }
124
    }
125

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

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

    
140
	@PreDestroy
141
	public void dispose() {
142
	    if(conversation!=null){
143
	        conversation.unregisterForDataStoreChanges(this);
144
	        conversation.close();
145
	    }
146
	    if(input!=null){
147
	        input.dispose();
148
	    }
149
	    dirty.setDirty(false);
150
	}
151

    
152
    @Optional
153
    @Inject
154
    private void updateAfterSearch(@UIEventTopic(WorkbenchEventConstants.BULK_EDITOR_SEARCH_FINISHED)IStructuredSelection selection){
155
        if(selection!=null){
156
            setSelection(selection);
157
        }
158
    }
159

    
160
    public void refresh(){
161
        bulkEditorComposite.refresh();
162
    }
163

    
164
    public IStructuredSelection getSelection(){
165
        return bulkEditorComposite.getSelection();
166
    }
167

    
168
    public void setSelection(IStructuredSelection selection){
169
        bulkEditorComposite.setSelection(selection);
170
    }
171

    
172
    public void setDirty(boolean isDirty){
173
        dirty.setDirty(isDirty);
174
    }
175

    
176
    public void setDirty(){
177
        setDirty(true);
178
    }
179

    
180
    public boolean isDirty() {
181
        return dirty.isDirty();
182
    }
183

    
184
    public AbstractBulkEditorInput getEditorInput() {
185
        return input;
186
    }
187

    
188
    public void copyDataToClipboard() {
189
        bulkEditorComposite.copyDataToClipboard();
190
    }
191

    
192
    @Override
193
    public void update(CdmDataChangeMap arg0) {
194
    }
195

    
196
    @Override
197
    public boolean canAttachMedia() {
198
        return true;
199
    }
200

    
201
    @Override
202
    public void changed(Object element) {
203
        if(element instanceof DerivedUnitFacade){
204
            DerivedUnit derivedUnit = ((DerivedUnitFacade) element).innerDerivedUnit();
205
            if(derivedUnit!=null){
206
                getEditorInput().addSaveCandidate(derivedUnit);
207
            }
208
            FieldUnit fieldUnit = ((DerivedUnitFacade) element).innerFieldUnit();
209
            if(fieldUnit!=null){
210
                getEditorInput().addSaveCandidate(fieldUnit);
211
            }
212
        }
213
        else if (element instanceof CdmBase) {
214
            getEditorInput().addSaveCandidate((CdmBase)element);
215
            input.replaceInModel((CdmBase) element);
216
        }
217
        dirty.setDirty(true);
218
        setSelection(new StructuredSelection(element));
219
    }
220

    
221
    @Override
222
    public void forceDirty() {
223
        dirty.setDirty(true);
224
    }
225

    
226
    @Override
227
    public boolean postOperation(CdmBase objectAffectedByOperation) {
228
        return false;
229
    }
230

    
231
    @Override
232
    public boolean onComplete() {
233
        return false;
234
    }
235

    
236
    @Override
237
    public ConversationHolder getConversationHolder() {
238
        return conversation;
239
    }
240

    
241
    public BulkEditorQuery getLastQuery() {
242
       return lastQuery;
243
    }
244

    
245
    public void setLastQuery(BulkEditorQuery lastQuery) {
246
       this.lastQuery = lastQuery;
247

    
248
    }
249

    
250
    @Override
251
    public Taxon getTaxon() {
252
        IStructuredSelection selection = getSelection();
253
        if(selection.size()==1) {
254
            Object object = selection.iterator().next();
255
            if(object instanceof Taxon){
256
                return (Taxon) object;
257
            }
258
        }
259
        return null;
260
    }
261

    
262
}
(2-2/7)