Project

General

Profile

Download (12.9 KB) Statistics
| Branch: | Tag: | Revision:
1 364a9123 Patrick Plitzner
/**
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 04bb4f0f Katja Luther
import java.util.EventObject;
13 9d542503 Patrick Plitzner
import java.util.List;
14
15 364a9123 Patrick Plitzner
import javax.annotation.PostConstruct;
16 76a8b44f Patrick Plitzner
import javax.annotation.PreDestroy;
17 364a9123 Patrick Plitzner
import javax.inject.Inject;
18
19 5221978c Patrick Plitzner
import org.eclipse.core.runtime.IProgressMonitor;
20
import org.eclipse.core.runtime.NullProgressMonitor;
21 20dd6f72 Patrick Plitzner
import org.eclipse.e4.core.services.events.IEventBroker;
22
import org.eclipse.e4.ui.di.Focus;
23 364a9123 Patrick Plitzner
import org.eclipse.e4.ui.di.Persist;
24
import org.eclipse.e4.ui.model.application.ui.MDirtyable;
25 401bbfa3 Patrick Plitzner
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
26 a4b31985 Patrick Plitzner
import org.eclipse.e4.ui.services.EMenuService;
27
import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
28 364a9123 Patrick Plitzner
import org.eclipse.jface.dialogs.MessageDialog;
29 a21eea64 Patrick Plitzner
import org.eclipse.jface.viewers.ArrayContentProvider;
30 ff742a38 Patrick Plitzner
import org.eclipse.jface.viewers.CellEditor;
31 04bb4f0f Katja Luther
import org.eclipse.jface.viewers.ColumnViewerEditor;
32
import org.eclipse.jface.viewers.ColumnViewerEditorActivationEvent;
33
import org.eclipse.jface.viewers.ColumnViewerEditorActivationStrategy;
34 ff742a38 Patrick Plitzner
import org.eclipse.jface.viewers.ICellModifier;
35 20dd6f72 Patrick Plitzner
import org.eclipse.jface.viewers.ISelection;
36 a4b31985 Patrick Plitzner
import org.eclipse.jface.viewers.ISelectionChangedListener;
37 20dd6f72 Patrick Plitzner
import org.eclipse.jface.viewers.IStructuredSelection;
38 ff742a38 Patrick Plitzner
import org.eclipse.jface.viewers.ITableLabelProvider;
39 a21eea64 Patrick Plitzner
import org.eclipse.jface.viewers.TableViewer;
40 ff742a38 Patrick Plitzner
import org.eclipse.jface.viewers.TableViewerColumn;
41 04bb4f0f Katja Luther
import org.eclipse.jface.viewers.TableViewerEditor;
42
import org.eclipse.jface.viewers.TableViewerFocusCellManager;
43 ff742a38 Patrick Plitzner
import org.eclipse.jface.viewers.TextCellEditor;
44 364a9123 Patrick Plitzner
import org.eclipse.swt.SWT;
45 04bb4f0f Katja Luther
import org.eclipse.swt.events.MouseEvent;
46 a21eea64 Patrick Plitzner
import org.eclipse.swt.layout.FillLayout;
47 364a9123 Patrick Plitzner
import org.eclipse.swt.layout.GridData;
48
import org.eclipse.swt.layout.GridLayout;
49
import org.eclipse.swt.widgets.Composite;
50 ff742a38 Patrick Plitzner
import org.eclipse.swt.widgets.Table;
51 a21eea64 Patrick Plitzner
52
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
53
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
54
import eu.etaxonomy.cdm.model.common.CdmBase;
55 20dd6f72 Patrick Plitzner
import eu.etaxonomy.cdm.model.taxon.Taxon;
56 a21eea64 Patrick Plitzner
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
57 364a9123 Patrick Plitzner
import eu.etaxonomy.taxeditor.bulkeditor.BulkEditorQuery;
58
import eu.etaxonomy.taxeditor.bulkeditor.input.AbstractBulkEditorInput;
59 20dd6f72 Patrick Plitzner
import eu.etaxonomy.taxeditor.editor.ITaxonEditor;
60
import eu.etaxonomy.taxeditor.event.WorkbenchEventConstants;
61 401bbfa3 Patrick Plitzner
import eu.etaxonomy.taxeditor.l10n.Messages;
62 a4b31985 Patrick Plitzner
import eu.etaxonomy.taxeditor.model.AbstractUtility;
63 364a9123 Patrick Plitzner
import eu.etaxonomy.taxeditor.model.IDerivedUnitFacadePart;
64
import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
65
import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
66
import eu.etaxonomy.taxeditor.model.IPartContentHasFactualData;
67
import eu.etaxonomy.taxeditor.model.IPartContentHasMedia;
68 44674f05 Patrick Plitzner
import eu.etaxonomy.taxeditor.model.IPartContentHasSupplementalData;
69 a21eea64 Patrick Plitzner
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
70 5221978c Patrick Plitzner
import eu.etaxonomy.taxeditor.workbench.part.IE4SavablePart;
71 364a9123 Patrick Plitzner
72
/**
73
 *
74
 * @author pplitzner
75
 * @since Sep 8, 2017
76
 *
77
 */
78 a21eea64 Patrick Plitzner
public class BulkEditorE4 implements IPartContentHasDetails, IConversationEnabled, IPostOperationEnabled,
79 44674f05 Patrick Plitzner
        IDirtyMarkable, IDerivedUnitFacadePart, IPartContentHasFactualData,
80 20dd6f72 Patrick Plitzner
        IPartContentHasSupplementalData, IPartContentHasMedia, IE4SavablePart, ITaxonEditor {
81 364a9123 Patrick Plitzner
82
	@Inject
83
	private MDirtyable dirty;
84
85
    private AbstractBulkEditorInput<?> input;
86
87 a21eea64 Patrick Plitzner
    private Composite topComposite;
88 364a9123 Patrick Plitzner
89 a21eea64 Patrick Plitzner
    private TableViewer viewer;
90 364a9123 Patrick Plitzner
91 a21eea64 Patrick Plitzner
    private ConversationHolder conversation;
92 364a9123 Patrick Plitzner
93 a4b31985 Patrick Plitzner
    @Inject
94
    private ESelectionService selService;
95
96 20dd6f72 Patrick Plitzner
    @Inject
97
    private IEventBroker eventBroker;
98
99 a4b31985 Patrick Plitzner
    private ISelectionChangedListener selectionChangedListener;
100
101 401bbfa3 Patrick Plitzner
    @Inject
102
    private MPart thisPart;
103
104 e78c8323 Katja Luther
    private BulkEditorQuery lastQuery = null;
105
106 a21eea64 Patrick Plitzner
    @Inject
107
    public BulkEditorE4() {
108 364a9123 Patrick Plitzner
	}
109
110
	public void init(AbstractBulkEditorInput<?> input){
111
	    this.input = input;
112 76a8b44f Patrick Plitzner
	    this.conversation = input.getConversation();
113
114 5221978c Patrick Plitzner
	    BulkEditorSearchE4 searchBar = new BulkEditorSearchE4(this, topComposite, SWT.NONE);
115 2bc43991 Patrick Plitzner
	    //layout needed because the search bar is added after @PostConstuct method
116
	    topComposite.getParent().layout();
117 364a9123 Patrick Plitzner
118 401bbfa3 Patrick Plitzner
	    thisPart.setLabel(input.getName());
119 ff742a38 Patrick Plitzner
120
	    //create columns
121
	    Table table = viewer.getTable();
122
	    String[] titles = {input.getName(), "Type"};
123
	    int[] bounds = {500, 100};
124
125 04bb4f0f Katja Luther
	    ColumnViewerEditorActivationStrategy activationSupport = new ColumnViewerEditorActivationStrategy(viewer) {
126
            @Override
127
            protected boolean isEditorActivationEvent(ColumnViewerEditorActivationEvent event) {
128
                // Enable editor only with mouse double click
129
                if (event.eventType == ColumnViewerEditorActivationEvent.MOUSE_DOUBLE_CLICK_SELECTION) {
130
                    EventObject source = event.sourceEvent;
131
                    if (source instanceof MouseEvent && ((MouseEvent)source).button == 3) {
132
                        return false;
133
                    }
134
135
                    return true;
136
                }
137
138
                return false;
139
            }
140
        };
141 63dd81ea Katja Luther
        TableViewerFocusCellManager focusCellManager = new TableViewerFocusCellManager(viewer, new BulkEditorFocusCellOwnerDrawHighLighter(viewer));
142 04bb4f0f Katja Luther
143
        TableViewerEditor.create(viewer, focusCellManager, activationSupport, ColumnViewerEditor.TABBING_HORIZONTAL |
144
                ColumnViewerEditor.TABBING_MOVE_TO_ROW_NEIGHBOR |
145
                ColumnViewerEditor.TABBING_VERTICAL |
146 63dd81ea Katja Luther
                ColumnViewerEditor.KEYBOARD_ACTIVATION );
147 04bb4f0f Katja Luther
148 ff742a38 Patrick Plitzner
	    CellEditor[] editors = new CellEditor[titles.length];
149
	    for (int i = 0; i < titles.length; i++) {
150
	        TableViewerColumn column = new TableViewerColumn(viewer, SWT.NONE);
151
	        editors[i] = new TextCellEditor(table);
152 2160ead6 Katja Luther
153 ff742a38 Patrick Plitzner
	        column.getColumn().setText(titles[i]);
154
	        column.getColumn().setWidth(bounds[i]);
155
	        column.getColumn().setResizable(true);
156
	        column.getColumn().setMoveable(true);
157 63dd81ea Katja Luther
158 ff742a38 Patrick Plitzner
	    }
159 04bb4f0f Katja Luther
160 ff742a38 Patrick Plitzner
	    table.setHeaderVisible(true);
161
	    table.setLinesVisible(true);
162 41a6a18d Katja Luther
163 ff742a38 Patrick Plitzner
	    viewer.setCellEditors(editors);
164
	    viewer.setColumnProperties(titles);
165
166
	    //content and label provider (NOTE: has to be set AFTER creating cell editors
167
	    viewer.setContentProvider(new ArrayContentProvider());
168
        BulkEditorLabelProvider labelProvider = new BulkEditorLabelProvider(this);
169
        viewer.setLabelProvider(labelProvider);
170
171
172
        //allow text selection
173
        viewer.setCellModifier(new ICellModifier() {
174
            @Override
175
            public void modify(Object element, String property, Object value) {
176
            }
177
            @Override
178
            public Object getValue(Object element, String property) {
179
                ITableLabelProvider tableLabelProvider = null;
180
                if(viewer.getLabelProvider() instanceof ITableLabelProvider){
181
                    tableLabelProvider = (ITableLabelProvider) viewer.getLabelProvider();
182
                }
183
                Object[] columnProperties = viewer.getColumnProperties();
184
                for (int i=0;i<columnProperties.length;i++) {
185
                    if(columnProperties[i].equals(property) && tableLabelProvider!=null){
186
                        return tableLabelProvider.getColumnText(element, i);
187
                    }
188
                }
189
                return "";
190
            }
191
            @Override
192
            public boolean canModify(Object element, String property) {
193 6d457a88 Patrick Plitzner
                return true;
194 ff742a38 Patrick Plitzner
            }
195
        });
196 9f70389a Patrick Plitzner
197 04bb4f0f Katja Luther
198
199 76a8b44f Patrick Plitzner
        if(input.getEntityUuid()!=null){
200
            performSearch(new BulkEditorQuery(input.getEntityUuid().toString(), null));
201 9f70389a Patrick Plitzner
        }
202 364a9123 Patrick Plitzner
	}
203
204
	/** {@inheritDoc} */
205
	@PostConstruct
206 a4b31985 Patrick Plitzner
	public void createPartControl(Composite parent, EMenuService menuService) {
207 364a9123 Patrick Plitzner
		parent.setLayout(new GridLayout());
208
209 a21eea64 Patrick Plitzner
		topComposite = new Composite(parent, SWT.NONE);
210
		topComposite.setLayout(new GridLayout());
211 364a9123 Patrick Plitzner
212
		GridData gridData = new GridData(SWT.FILL, SWT.TOP, true, false);
213 a21eea64 Patrick Plitzner
		topComposite.setLayoutData(gridData);
214
215
		Composite bottomComposite = new Composite(parent, SWT.NONE);
216
		bottomComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
217
		bottomComposite.setLayout(new FillLayout());
218 63dd81ea Katja Luther
		viewer = new TableViewer(bottomComposite, SWT.MULTI | SWT.FULL_SELECTION);
219 364a9123 Patrick Plitzner
220 ff742a38 Patrick Plitzner
//		createColumns(viewer);
221 a4b31985 Patrick Plitzner
222
        //propagate selection
223
        selectionChangedListener = (event -> selService.setSelection(AbstractUtility.getElementsFromSelectionChangedEvent(event)));
224
        viewer.addSelectionChangedListener(selectionChangedListener);
225
226 364a9123 Patrick Plitzner
        //create context menu
227 401bbfa3 Patrick Plitzner
        menuService.registerContextMenu(viewer.getControl(), "eu.etaxonomy.taxeditor.bulkeditor.popupmenu.bulkeditor"); //$NON-NLS-1$
228 364a9123 Patrick Plitzner
229
	}
230
231 5221978c Patrick Plitzner
	@Override
232 35ee4164 Patrick Plitzner
	@Persist
233 5221978c Patrick Plitzner
	public void save(IProgressMonitor monitor) {
234 35ee4164 Patrick Plitzner
	    save(monitor, true);
235
	}
236
237 20dd6f72 Patrick Plitzner
	@Focus
238
	public void setFocus() {
239
	    //make sure to bind again if maybe in another view the conversation was unbound
240
	    eventBroker.post(WorkbenchEventConstants.CURRENT_ACTIVE_EDITOR, this);
241
	}
242
243 76a8b44f Patrick Plitzner
	@PreDestroy
244
	public void dispose() {
245
	    if(conversation!=null){
246
	        conversation.unregisterForDataStoreChanges(this);
247
	        conversation.close();
248
	    }
249
	    if(input!=null){
250
	        input.dispose();
251
	    }
252
	}
253
254 35ee4164 Patrick Plitzner
	public void save(IProgressMonitor monitor, boolean resetMerge) {
255 da333f17 Katja Luther
	    if (!input.getCdmEntitySession().isActive()){
256
            input.getCdmEntitySession().bind();
257
        }
258 35ee4164 Patrick Plitzner
	    input.saveModel(resetMerge);
259 bce766a1 Patrick Plitzner
260 bcf8e025 Katja Luther
	    List<CdmBase> saveCandidates = getEditorInput().getSaveCandidates();
261 41a6a18d Katja Luther
262 c0fb722d Katja Luther
263 a2a04809 Patrick Plitzner
        dirty.setDirty(false);
264 c486ba7d Katja Luther
        getEditorInput().resetSaveCandidates();
265 63dd81ea Katja Luther
        getEditorInput().dispose();
266
        getEditorInput().bind();
267
        conversation.commit(true);
268 e78c8323 Katja Luther
269
        if (lastQuery != null){
270
            performSearch(lastQuery);
271
        }
272 fd7d64e9 Patrick Plitzner
        viewer.refresh();
273
274 e78c8323 Katja Luther
275 364a9123 Patrick Plitzner
	}
276
277
	/** {@inheritDoc} */
278 a21eea64 Patrick Plitzner
    public void performSearch(BulkEditorQuery query) {
279
        if (query != null) {
280
281
            // TODO check if dirty, prompt save
282
            if (isDirty()) {
283 401bbfa3 Patrick Plitzner
                String[] labels = {Messages.BulkEditorE4_SAVE_AND_SEARCH, Messages.BulkEditorE4_DONT_SAVE,Messages.BulkEditorE4_CANCEL};
284
                MessageDialog dialog =new MessageDialog(topComposite.getShell(), Messages.BulkEditorE4_SAVE_CHANGES_TITLE, null, Messages.BulkEditorE4_SAVE_CHANGES_MESSAGE, MessageDialog.QUESTION,labels, 0);
285 a21eea64 Patrick Plitzner
                int result = dialog.open();
286
                if (result == 0) {
287 5221978c Patrick Plitzner
                    save(new NullProgressMonitor());
288 a21eea64 Patrick Plitzner
                } else if (result == 2){
289
                    return;
290
                }
291
            }
292 2160ead6 Katja Luther
            dirty.setDirty(false);
293 41a6a18d Katja Luther
//            getEditorInput().dispose();
294
//            getEditorInput().bind();
295 a21eea64 Patrick Plitzner
            getEditorInput().performSearch(query);
296 e78c8323 Katja Luther
            lastQuery = query;
297 bce766a1 Patrick Plitzner
            viewer.setInput(getEditorInput().getModel());
298 a21eea64 Patrick Plitzner
            refresh();
299
        }
300
    }
301 364a9123 Patrick Plitzner
302 a380926d Patrick Plitzner
    public void refresh() {
303 58f15314 Katja Luther
        refresh(true);
304 a2a04809 Patrick Plitzner
    }
305
306
    public void refresh(boolean resetInput) {
307
        if(resetInput){
308 bce766a1 Patrick Plitzner
            viewer.setInput(getEditorInput().getModel());
309 a2a04809 Patrick Plitzner
        }
310 a21eea64 Patrick Plitzner
        viewer.refresh();
311
    }
312 364a9123 Patrick Plitzner
313 a2a04809 Patrick Plitzner
    public TableViewer getViewer() {
314
        return viewer;
315
    }
316
317
    public void setDirty(){
318
        dirty.setDirty(true);
319
    }
320
321 a380926d Patrick Plitzner
    public boolean isDirty() {
322 a21eea64 Patrick Plitzner
        return dirty.isDirty();
323
    }
324 364a9123 Patrick Plitzner
325 a21eea64 Patrick Plitzner
    public AbstractBulkEditorInput getEditorInput() {
326
        return input;
327
    }
328 364a9123 Patrick Plitzner
329 a21eea64 Patrick Plitzner
    /**
330
     * {@inheritDoc}
331
     */
332
    @Override
333
    public void update(CdmDataChangeMap arg0) {
334
    }
335 364a9123 Patrick Plitzner
336 a21eea64 Patrick Plitzner
    /**
337
     * {@inheritDoc}
338
     */
339
    @Override
340
    public boolean canAttachMedia() {
341
        return true;
342
    }
343 364a9123 Patrick Plitzner
344 a21eea64 Patrick Plitzner
    /**
345
     * {@inheritDoc}
346
     */
347
    @Override
348
    public void changed(Object element) {
349 bcf8e025 Katja Luther
        if (element instanceof CdmBase) {
350
            getEditorInput().addSaveCandidate((CdmBase)element);
351
        }
352 5221978c Patrick Plitzner
        dirty.setDirty(true);
353 a21eea64 Patrick Plitzner
    }
354 364a9123 Patrick Plitzner
355 a21eea64 Patrick Plitzner
    /**
356
     * {@inheritDoc}
357
     */
358
    @Override
359
    public void forceDirty() {
360
        dirty.setDirty(true);
361
    }
362 364a9123 Patrick Plitzner
363 a21eea64 Patrick Plitzner
    /**
364
     * {@inheritDoc}
365
     */
366
    @Override
367
    public boolean postOperation(CdmBase objectAffectedByOperation) {
368
        return false;
369
    }
370 364a9123 Patrick Plitzner
371 a21eea64 Patrick Plitzner
    /**
372
     * {@inheritDoc}
373
     */
374
    @Override
375
    public boolean onComplete() {
376
        return false;
377 364a9123 Patrick Plitzner
    }
378
379 a21eea64 Patrick Plitzner
    /**
380
     * {@inheritDoc}
381
     */
382
    @Override
383
    public ConversationHolder getConversationHolder() {
384
        return conversation;
385
    }
386 364a9123 Patrick Plitzner
387 e78c8323 Katja Luther
    /**
388
     * @return
389
     */
390
    public BulkEditorQuery getLastQuery() {
391
       return lastQuery;
392
    }
393
394
    /**
395
     * @param object
396
     */
397
    public void setLastQuery(BulkEditorQuery lastQuery) {
398
       this.lastQuery = lastQuery;
399
400
    }
401
402 20dd6f72 Patrick Plitzner
    /**
403
     * {@inheritDoc}
404
     */
405
    @Override
406
    public Taxon getTaxon() {
407
        ISelection selection = viewer.getSelection();
408
        if(selection instanceof IStructuredSelection
409
                && ((IStructuredSelection) selection).size()==1) {
410
            Object object = ((IStructuredSelection) selection).iterator().next();
411
            if(object instanceof Taxon){
412
                return (Taxon) object;
413
            }
414
        }
415
        return null;
416
    }
417
418 364a9123 Patrick Plitzner
}