Project

General

Profile

Download (7.4 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.util.List;
13

    
14
import javax.annotation.PostConstruct;
15
import javax.inject.Inject;
16

    
17
import org.eclipse.core.runtime.IProgressMonitor;
18
import org.eclipse.core.runtime.NullProgressMonitor;
19
import org.eclipse.e4.ui.di.Persist;
20
import org.eclipse.e4.ui.model.application.ui.MDirtyable;
21
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
22
import org.eclipse.e4.ui.services.EMenuService;
23
import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
24
import org.eclipse.jface.dialogs.MessageDialog;
25
import org.eclipse.jface.viewers.ArrayContentProvider;
26
import org.eclipse.jface.viewers.ISelectionChangedListener;
27
import org.eclipse.jface.viewers.TableViewer;
28
import org.eclipse.swt.SWT;
29
import org.eclipse.swt.layout.FillLayout;
30
import org.eclipse.swt.layout.GridData;
31
import org.eclipse.swt.layout.GridLayout;
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.model.common.CdmBase;
37
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
38
import eu.etaxonomy.taxeditor.bulkeditor.BulkEditorQuery;
39
import eu.etaxonomy.taxeditor.bulkeditor.input.AbstractBulkEditorInput;
40
import eu.etaxonomy.taxeditor.l10n.Messages;
41
import eu.etaxonomy.taxeditor.model.AbstractUtility;
42
import eu.etaxonomy.taxeditor.model.IDerivedUnitFacadePart;
43
import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
44
import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
45
import eu.etaxonomy.taxeditor.model.IPartContentHasFactualData;
46
import eu.etaxonomy.taxeditor.model.IPartContentHasMedia;
47
import eu.etaxonomy.taxeditor.model.IPartContentHasSupplementalData;
48
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
49
import eu.etaxonomy.taxeditor.store.CdmStore;
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 {
61

    
62
	@Inject
63
	private MDirtyable dirty;
64

    
65
    private AbstractBulkEditorInput<?> input;
66

    
67
    private Composite topComposite;
68

    
69
    private TableViewer viewer;
70

    
71
    private ConversationHolder conversation;
72

    
73
    @Inject
74
    private ESelectionService selService;
75

    
76
    private ISelectionChangedListener selectionChangedListener;
77

    
78
    @Inject
79
    private MPart thisPart;
80

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

    
85
	public void init(AbstractBulkEditorInput<?> input){
86
	    this.input = input;
87
	    AbstractBulkEditorInput<?> bulkEditorInput = input;
88
	    BulkEditorSearchE4 searchBar = new BulkEditorSearchE4(this, topComposite, SWT.NONE);
89
	    //layout needed because the search bar is added after @PostConstuct method
90
	    topComposite.getParent().layout();
91

    
92
	    if(bulkEditorInput.getEntityUuid()!=null){
93
	        performSearch(new BulkEditorQuery(bulkEditorInput.getEntityUuid().toString(), null));
94
	    }
95
	    thisPart.setLabel(input.getName());
96
	}
97

    
98
	/** {@inheritDoc} */
99
	@PostConstruct
100
	public void createPartControl(Composite parent, EMenuService menuService) {
101
        if (CdmStore.isActive()){
102
            if(conversation == null){
103
                conversation = CdmStore.createConversation();
104
            }
105
        }
106
        else{
107
            return;
108
        }
109

    
110
		parent.setLayout(new GridLayout());
111

    
112
		topComposite = new Composite(parent, SWT.NONE);
113
		topComposite.setLayout(new GridLayout());
114

    
115
		GridData gridData = new GridData(SWT.FILL, SWT.TOP, true, false);
116
		topComposite.setLayoutData(gridData);
117

    
118
		Composite bottomComposite = new Composite(parent, SWT.NONE);
119
		bottomComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
120
		bottomComposite.setLayout(new FillLayout());
121
		viewer = new TableViewer(bottomComposite);
122
		viewer.setContentProvider(new ArrayContentProvider());
123
		viewer.setLabelProvider(new BulkEditorLabelProvider(this));
124

    
125

    
126
        //propagate selection
127
        selectionChangedListener = (event -> selService.setSelection(AbstractUtility.getElementsFromSelectionChangedEvent(event)));
128
        viewer.addSelectionChangedListener(selectionChangedListener);
129

    
130
        //create context menu
131
        menuService.registerContextMenu(viewer.getControl(), "eu.etaxonomy.taxeditor.bulkeditor.popupmenu.bulkeditor"); //$NON-NLS-1$
132

    
133
	}
134

    
135
	@Override
136
    @Persist
137
	public void save(IProgressMonitor monitor) {
138
	    input.saveModel();
139

    
140
	    // commit the conversation and start a new transaction immediately
141
	    conversation.commit(true);
142

    
143
	    Object object = viewer.getInput();
144
	    if(object instanceof List){
145
	        for (Object item: (List)object) {
146
	            CdmBase cdmBase = (CdmBase) item;
147
	            CdmStore.getService(cdmBase).merge(cdmBase, true);
148
            }
149
	    }
150
        dirty.setDirty(false);
151
        viewer.refresh();
152

    
153
	}
154

    
155
	/** {@inheritDoc} */
156
    public void performSearch(BulkEditorQuery query) {
157
        if (query != null) {
158

    
159
            // TODO check if dirty, prompt save
160
            if (isDirty()) {
161
                String[] labels = {Messages.BulkEditorE4_SAVE_AND_SEARCH, Messages.BulkEditorE4_DONT_SAVE,Messages.BulkEditorE4_CANCEL};
162
                MessageDialog dialog =new MessageDialog(topComposite.getShell(), Messages.BulkEditorE4_SAVE_CHANGES_TITLE, null, Messages.BulkEditorE4_SAVE_CHANGES_MESSAGE, MessageDialog.QUESTION,labels, 0);
163
                int result = dialog.open();
164
                if (result == 0) {
165
                    save(new NullProgressMonitor());
166
                } else if (result == 2){
167
                    return;
168
                }
169
            }
170

    
171
            getEditorInput().performSearch(query);
172

    
173
            viewer.setInput(getEditorInput().getModel());
174
            refresh();
175
        }
176
    }
177

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

    
182
    public void refresh(boolean resetInput) {
183
        if(resetInput){
184
            viewer.setInput(getEditorInput().getModel());
185
        }
186
        viewer.refresh();
187
    }
188

    
189
    public TableViewer getViewer() {
190
        return viewer;
191
    }
192

    
193
    public void setDirty(){
194
        dirty.setDirty(true);
195
    }
196

    
197
    public boolean isDirty() {
198
        return dirty.isDirty();
199
    }
200

    
201
    public AbstractBulkEditorInput getEditorInput() {
202
        return input;
203
    }
204

    
205
    /**
206
     * {@inheritDoc}
207
     */
208
    @Override
209
    public void update(CdmDataChangeMap arg0) {
210
    }
211

    
212
    /**
213
     * {@inheritDoc}
214
     */
215
    @Override
216
    public boolean canAttachMedia() {
217
        return true;
218
    }
219

    
220
    /**
221
     * {@inheritDoc}
222
     */
223
    @Override
224
    public void changed(Object element) {
225
        dirty.setDirty(true);
226
    }
227

    
228
    /**
229
     * {@inheritDoc}
230
     */
231
    @Override
232
    public void forceDirty() {
233
        dirty.setDirty(true);
234
    }
235

    
236
    /**
237
     * {@inheritDoc}
238
     */
239
    @Override
240
    public boolean postOperation(CdmBase objectAffectedByOperation) {
241
        return false;
242
    }
243

    
244
    /**
245
     * {@inheritDoc}
246
     */
247
    @Override
248
    public boolean onComplete() {
249
        return false;
250
    }
251

    
252
    /**
253
     * {@inheritDoc}
254
     */
255
    @Override
256
    public ConversationHolder getConversationHolder() {
257
        return conversation;
258
    }
259

    
260
}
(1-1/3)