Project

General

Profile

Download (10.3 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.ArrayList;
13
import java.util.List;
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.core.runtime.NullProgressMonitor;
21
import org.eclipse.e4.ui.di.Persist;
22
import org.eclipse.e4.ui.model.application.ui.MDirtyable;
23
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
24
import org.eclipse.e4.ui.services.EMenuService;
25
import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
26
import org.eclipse.jface.dialogs.MessageDialog;
27
import org.eclipse.jface.viewers.ArrayContentProvider;
28
import org.eclipse.jface.viewers.CellEditor;
29
import org.eclipse.jface.viewers.ICellModifier;
30
import org.eclipse.jface.viewers.ISelectionChangedListener;
31
import org.eclipse.jface.viewers.ITableLabelProvider;
32
import org.eclipse.jface.viewers.TableViewer;
33
import org.eclipse.jface.viewers.TableViewerColumn;
34
import org.eclipse.jface.viewers.TextCellEditor;
35
import org.eclipse.swt.SWT;
36
import org.eclipse.swt.layout.FillLayout;
37
import org.eclipse.swt.layout.GridData;
38
import org.eclipse.swt.layout.GridLayout;
39
import org.eclipse.swt.widgets.Composite;
40
import org.eclipse.swt.widgets.Table;
41

    
42
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
43
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
44
import eu.etaxonomy.cdm.model.common.CdmBase;
45
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
46
import eu.etaxonomy.taxeditor.bulkeditor.BulkEditorQuery;
47
import eu.etaxonomy.taxeditor.bulkeditor.input.AbstractBulkEditorInput;
48
import eu.etaxonomy.taxeditor.l10n.Messages;
49
import eu.etaxonomy.taxeditor.model.AbstractUtility;
50
import eu.etaxonomy.taxeditor.model.IDerivedUnitFacadePart;
51
import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
52
import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
53
import eu.etaxonomy.taxeditor.model.IPartContentHasFactualData;
54
import eu.etaxonomy.taxeditor.model.IPartContentHasMedia;
55
import eu.etaxonomy.taxeditor.model.IPartContentHasSupplementalData;
56
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
57
import eu.etaxonomy.taxeditor.store.CdmStore;
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 {
69

    
70
	@Inject
71
	private MDirtyable dirty;
72

    
73
    private AbstractBulkEditorInput<?> input;
74

    
75
    private Composite topComposite;
76

    
77
    private TableViewer viewer;
78

    
79
    private ConversationHolder conversation;
80

    
81
    @Inject
82
    private ESelectionService selService;
83

    
84
    private ISelectionChangedListener selectionChangedListener;
85

    
86
    @Inject
87
    private MPart thisPart;
88

    
89
    @Inject
90
    public BulkEditorE4() {
91
	}
92

    
93
	public void init(AbstractBulkEditorInput<?> input){
94
	    this.input = input;
95
	    this.conversation = input.getConversation();
96

    
97
	    BulkEditorSearchE4 searchBar = new BulkEditorSearchE4(this, topComposite, SWT.NONE);
98
	    //layout needed because the search bar is added after @PostConstuct method
99
	    topComposite.getParent().layout();
100

    
101
	    thisPart.setLabel(input.getName());
102

    
103
	    //create columns
104
	    Table table = viewer.getTable();
105
	    String[] titles = {input.getName(), "Type"};
106
	    int[] bounds = {500, 100};
107

    
108
	    CellEditor[] editors = new CellEditor[titles.length];
109
	    for (int i = 0; i < titles.length; i++) {
110
	        TableViewerColumn column = new TableViewerColumn(viewer, SWT.NONE);
111
	        editors[i] = new TextCellEditor(table);
112

    
113
	        column.getColumn().setText(titles[i]);
114
	        column.getColumn().setWidth(bounds[i]);
115
	        column.getColumn().setResizable(true);
116
	        column.getColumn().setMoveable(true);
117
	    }
118
	    table.setHeaderVisible(true);
119
	    table.setLinesVisible(true);
120
	    viewer.setCellEditors(editors);
121
	    viewer.setColumnProperties(titles);
122

    
123
	    //content and label provider (NOTE: has to be set AFTER creating cell editors
124
	    viewer.setContentProvider(new ArrayContentProvider());
125
        BulkEditorLabelProvider labelProvider = new BulkEditorLabelProvider(this);
126
        viewer.setLabelProvider(labelProvider);
127

    
128

    
129
        //allow text selection
130
        viewer.setCellModifier(new ICellModifier() {
131
            @Override
132
            public void modify(Object element, String property, Object value) {
133
            }
134
            @Override
135
            public Object getValue(Object element, String property) {
136
                ITableLabelProvider tableLabelProvider = null;
137
                if(viewer.getLabelProvider() instanceof ITableLabelProvider){
138
                    tableLabelProvider = (ITableLabelProvider) viewer.getLabelProvider();
139
                }
140
                Object[] columnProperties = viewer.getColumnProperties();
141
                for (int i=0;i<columnProperties.length;i++) {
142
                    if(columnProperties[i].equals(property) && tableLabelProvider!=null){
143
                        return tableLabelProvider.getColumnText(element, i);
144
                    }
145
                }
146
                return "";
147
            }
148
            @Override
149
            public boolean canModify(Object element, String property) {
150
                return true;
151
            }
152
        });
153

    
154
        if(input.getEntityUuid()!=null){
155
            performSearch(new BulkEditorQuery(input.getEntityUuid().toString(), null));
156
        }
157
	}
158

    
159
	/** {@inheritDoc} */
160
	@PostConstruct
161
	public void createPartControl(Composite parent, EMenuService menuService) {
162
		parent.setLayout(new GridLayout());
163

    
164
		topComposite = new Composite(parent, SWT.NONE);
165
		topComposite.setLayout(new GridLayout());
166

    
167
		GridData gridData = new GridData(SWT.FILL, SWT.TOP, true, false);
168
		topComposite.setLayoutData(gridData);
169

    
170
		Composite bottomComposite = new Composite(parent, SWT.NONE);
171
		bottomComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
172
		bottomComposite.setLayout(new FillLayout());
173
		viewer = new TableViewer(bottomComposite, SWT.MULTI | SWT.H_SCROLL
174
                | SWT.V_SCROLL | SWT.FULL_SELECTION);
175

    
176
//		createColumns(viewer);
177

    
178
        //propagate selection
179
        selectionChangedListener = (event -> selService.setSelection(AbstractUtility.getElementsFromSelectionChangedEvent(event)));
180
        viewer.addSelectionChangedListener(selectionChangedListener);
181

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

    
185
	}
186

    
187
	@Override
188
	@Persist
189
	public void save(IProgressMonitor monitor) {
190
	    save(monitor, true);
191
	}
192

    
193
	@PreDestroy
194
	public void dispose() {
195
	    if(conversation!=null){
196
	        conversation.unregisterForDataStoreChanges(this);
197
	        conversation.close();
198
	    }
199
	    if(input!=null){
200
	        input.dispose();
201
	    }
202
	}
203

    
204
	public void save(IProgressMonitor monitor, boolean resetMerge) {
205
	    input.saveModel(resetMerge);
206

    
207
	    // commit the conversation and start a new transaction immediately
208
	    conversation.commit(true);
209

    
210
	    Object object = viewer.getInput();
211
	    List<CdmBase> cdmBaseList = new ArrayList<>();
212
	    List<CdmBase> saveCandidates = getEditorInput().getSaveCandidates();
213
//	    if(saveCandidates != null && !saveCandidates.isEmpty()){
214
//	        for (CdmBase item: saveCandidates) {
215
//
216
//	            cdmBaseList.add(item);
217
//            }
218
//	    }
219
	    if (!cdmBaseList.isEmpty()){
220
	        CdmStore.getService(saveCandidates.get(0)).merge(saveCandidates, true);
221
	    }
222
        dirty.setDirty(false);
223
        viewer.refresh();
224

    
225
	}
226

    
227
	/** {@inheritDoc} */
228
    public void performSearch(BulkEditorQuery query) {
229
        if (query != null) {
230

    
231
            // TODO check if dirty, prompt save
232
            if (isDirty()) {
233
                String[] labels = {Messages.BulkEditorE4_SAVE_AND_SEARCH, Messages.BulkEditorE4_DONT_SAVE,Messages.BulkEditorE4_CANCEL};
234
                MessageDialog dialog =new MessageDialog(topComposite.getShell(), Messages.BulkEditorE4_SAVE_CHANGES_TITLE, null, Messages.BulkEditorE4_SAVE_CHANGES_MESSAGE, MessageDialog.QUESTION,labels, 0);
235
                int result = dialog.open();
236
                if (result == 0) {
237
                    save(new NullProgressMonitor());
238
                } else if (result == 2){
239
                    return;
240
                }
241
            }
242
            dirty.setDirty(false);
243
            getEditorInput().dispose();
244
            getEditorInput().bind();
245
            getEditorInput().performSearch(query);
246

    
247
            viewer.setInput(getEditorInput().getModel());
248
            refresh();
249
        }
250
    }
251

    
252
    public void refresh() {
253
        refresh(true);
254
    }
255

    
256
    public void refresh(boolean resetInput) {
257
        if(resetInput){
258
            viewer.setInput(getEditorInput().getModel());
259
        }
260
        viewer.refresh();
261
    }
262

    
263
    public TableViewer getViewer() {
264
        return viewer;
265
    }
266

    
267
    public void setDirty(){
268
        dirty.setDirty(true);
269
    }
270

    
271
    public boolean isDirty() {
272
        return dirty.isDirty();
273
    }
274

    
275
    public AbstractBulkEditorInput getEditorInput() {
276
        return input;
277
    }
278

    
279
    /**
280
     * {@inheritDoc}
281
     */
282
    @Override
283
    public void update(CdmDataChangeMap arg0) {
284
    }
285

    
286
    /**
287
     * {@inheritDoc}
288
     */
289
    @Override
290
    public boolean canAttachMedia() {
291
        return true;
292
    }
293

    
294
    /**
295
     * {@inheritDoc}
296
     */
297
    @Override
298
    public void changed(Object element) {
299
        if (element instanceof CdmBase) {
300
            getEditorInput().addSaveCandidate((CdmBase)element);
301
        }
302
        dirty.setDirty(true);
303
    }
304

    
305
    /**
306
     * {@inheritDoc}
307
     */
308
    @Override
309
    public void forceDirty() {
310
        dirty.setDirty(true);
311
    }
312

    
313
    /**
314
     * {@inheritDoc}
315
     */
316
    @Override
317
    public boolean postOperation(CdmBase objectAffectedByOperation) {
318
        return false;
319
    }
320

    
321
    /**
322
     * {@inheritDoc}
323
     */
324
    @Override
325
    public boolean onComplete() {
326
        return false;
327
    }
328

    
329
    /**
330
     * {@inheritDoc}
331
     */
332
    @Override
333
    public ConversationHolder getConversationHolder() {
334
        return conversation;
335
    }
336

    
337
}
(1-1/3)