Project

General

Profile

Download (12 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.EventObject;
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.ColumnViewerEditor;
30
import org.eclipse.jface.viewers.ColumnViewerEditorActivationEvent;
31
import org.eclipse.jface.viewers.ColumnViewerEditorActivationStrategy;
32
import org.eclipse.jface.viewers.ICellModifier;
33
import org.eclipse.jface.viewers.ISelectionChangedListener;
34
import org.eclipse.jface.viewers.ITableLabelProvider;
35
import org.eclipse.jface.viewers.TableViewer;
36
import org.eclipse.jface.viewers.TableViewerColumn;
37
import org.eclipse.jface.viewers.TableViewerEditor;
38
import org.eclipse.jface.viewers.TableViewerFocusCellManager;
39
import org.eclipse.jface.viewers.TextCellEditor;
40
import org.eclipse.swt.SWT;
41
import org.eclipse.swt.events.MouseEvent;
42
import org.eclipse.swt.layout.FillLayout;
43
import org.eclipse.swt.layout.GridData;
44
import org.eclipse.swt.layout.GridLayout;
45
import org.eclipse.swt.widgets.Composite;
46
import org.eclipse.swt.widgets.Table;
47

    
48
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
49
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
50
import eu.etaxonomy.cdm.model.common.CdmBase;
51
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
52
import eu.etaxonomy.taxeditor.bulkeditor.BulkEditorQuery;
53
import eu.etaxonomy.taxeditor.bulkeditor.input.AbstractBulkEditorInput;
54
import eu.etaxonomy.taxeditor.l10n.Messages;
55
import eu.etaxonomy.taxeditor.model.AbstractUtility;
56
import eu.etaxonomy.taxeditor.model.IDerivedUnitFacadePart;
57
import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
58
import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
59
import eu.etaxonomy.taxeditor.model.IPartContentHasFactualData;
60
import eu.etaxonomy.taxeditor.model.IPartContentHasMedia;
61
import eu.etaxonomy.taxeditor.model.IPartContentHasSupplementalData;
62
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
63
import eu.etaxonomy.taxeditor.store.CdmStore;
64
import eu.etaxonomy.taxeditor.workbench.part.IE4SavablePart;
65

    
66
/**
67
 *
68
 * @author pplitzner
69
 * @since Sep 8, 2017
70
 *
71
 */
72
public class BulkEditorE4 implements IPartContentHasDetails, IConversationEnabled, IPostOperationEnabled,
73
        IDirtyMarkable, IDerivedUnitFacadePart, IPartContentHasFactualData,
74
        IPartContentHasSupplementalData, IPartContentHasMedia, IE4SavablePart {
75

    
76
	@Inject
77
	private MDirtyable dirty;
78

    
79
    private AbstractBulkEditorInput<?> input;
80

    
81
    private Composite topComposite;
82

    
83
    private TableViewer viewer;
84

    
85
    private ConversationHolder conversation;
86

    
87
    @Inject
88
    private ESelectionService selService;
89

    
90
    private ISelectionChangedListener selectionChangedListener;
91

    
92
    @Inject
93
    private MPart thisPart;
94

    
95
    private BulkEditorQuery lastQuery = null;
96

    
97
    @Inject
98
    public BulkEditorE4() {
99
	}
100

    
101
	public void init(AbstractBulkEditorInput<?> input){
102
	    this.input = input;
103
	    this.conversation = input.getConversation();
104

    
105
	    BulkEditorSearchE4 searchBar = new BulkEditorSearchE4(this, topComposite, SWT.NONE);
106
	    //layout needed because the search bar is added after @PostConstuct method
107
	    topComposite.getParent().layout();
108

    
109
	    thisPart.setLabel(input.getName());
110

    
111
	    //create columns
112
	    Table table = viewer.getTable();
113
	    String[] titles = {input.getName(), "Type"};
114
	    int[] bounds = {500, 100};
115

    
116
	    ColumnViewerEditorActivationStrategy activationSupport = new ColumnViewerEditorActivationStrategy(viewer) {
117
            @Override
118
            protected boolean isEditorActivationEvent(ColumnViewerEditorActivationEvent event) {
119
                // Enable editor only with mouse double click
120
                if (event.eventType == ColumnViewerEditorActivationEvent.MOUSE_DOUBLE_CLICK_SELECTION) {
121
                    EventObject source = event.sourceEvent;
122
                    if (source instanceof MouseEvent && ((MouseEvent)source).button == 3) {
123
                        return false;
124
                    }
125

    
126
                    return true;
127
                }
128

    
129
                return false;
130
            }
131
        };
132
        TableViewerFocusCellManager focusCellManager = new TableViewerFocusCellManager(viewer, new BulkEditorFocusCellOwnerDrawHighLighter(viewer));
133

    
134
        TableViewerEditor.create(viewer, focusCellManager, activationSupport, ColumnViewerEditor.TABBING_HORIZONTAL |
135
                ColumnViewerEditor.TABBING_MOVE_TO_ROW_NEIGHBOR |
136
                ColumnViewerEditor.TABBING_VERTICAL |
137
                ColumnViewerEditor.KEYBOARD_ACTIVATION );
138

    
139
	    CellEditor[] editors = new CellEditor[titles.length];
140
	    for (int i = 0; i < titles.length; i++) {
141
	        TableViewerColumn column = new TableViewerColumn(viewer, SWT.NONE);
142
	        editors[i] = new TextCellEditor(table);
143

    
144
	        column.getColumn().setText(titles[i]);
145
	        column.getColumn().setWidth(bounds[i]);
146
	        column.getColumn().setResizable(true);
147
	        column.getColumn().setMoveable(true);
148

    
149
	    }
150

    
151
	    table.setHeaderVisible(true);
152
	    table.setLinesVisible(true);
153

    
154
	    viewer.setCellEditors(editors);
155
	    viewer.setColumnProperties(titles);
156

    
157
	    //content and label provider (NOTE: has to be set AFTER creating cell editors
158
	    viewer.setContentProvider(new ArrayContentProvider());
159
        BulkEditorLabelProvider labelProvider = new BulkEditorLabelProvider(this);
160
        viewer.setLabelProvider(labelProvider);
161

    
162

    
163
        //allow text selection
164
        viewer.setCellModifier(new ICellModifier() {
165
            @Override
166
            public void modify(Object element, String property, Object value) {
167
            }
168
            @Override
169
            public Object getValue(Object element, String property) {
170
                ITableLabelProvider tableLabelProvider = null;
171
                if(viewer.getLabelProvider() instanceof ITableLabelProvider){
172
                    tableLabelProvider = (ITableLabelProvider) viewer.getLabelProvider();
173
                }
174
                Object[] columnProperties = viewer.getColumnProperties();
175
                for (int i=0;i<columnProperties.length;i++) {
176
                    if(columnProperties[i].equals(property) && tableLabelProvider!=null){
177
                        return tableLabelProvider.getColumnText(element, i);
178
                    }
179
                }
180
                return "";
181
            }
182
            @Override
183
            public boolean canModify(Object element, String property) {
184
                return true;
185
            }
186
        });
187

    
188

    
189

    
190
        if(input.getEntityUuid()!=null){
191
            performSearch(new BulkEditorQuery(input.getEntityUuid().toString(), null));
192
        }
193
	}
194

    
195
	/** {@inheritDoc} */
196
	@PostConstruct
197
	public void createPartControl(Composite parent, EMenuService menuService) {
198
		parent.setLayout(new GridLayout());
199

    
200
		topComposite = new Composite(parent, SWT.NONE);
201
		topComposite.setLayout(new GridLayout());
202

    
203
		GridData gridData = new GridData(SWT.FILL, SWT.TOP, true, false);
204
		topComposite.setLayoutData(gridData);
205

    
206
		Composite bottomComposite = new Composite(parent, SWT.NONE);
207
		bottomComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
208
		bottomComposite.setLayout(new FillLayout());
209
		viewer = new TableViewer(bottomComposite, SWT.MULTI | SWT.FULL_SELECTION);
210

    
211
//		createColumns(viewer);
212

    
213
        //propagate selection
214
        selectionChangedListener = (event -> selService.setSelection(AbstractUtility.getElementsFromSelectionChangedEvent(event)));
215
        viewer.addSelectionChangedListener(selectionChangedListener);
216

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

    
220
	}
221

    
222
	@Override
223
	@Persist
224
	public void save(IProgressMonitor monitor) {
225
	    save(monitor, true);
226
	}
227

    
228
	@PreDestroy
229
	public void dispose() {
230
	    if(conversation!=null){
231
	        conversation.unregisterForDataStoreChanges(this);
232
	        conversation.close();
233
	    }
234
	    if(input!=null){
235
	        input.dispose();
236
	    }
237
	}
238

    
239
	public void save(IProgressMonitor monitor, boolean resetMerge) {
240
	    if (!input.getCdmEntitySession().isActive()){
241
            input.getCdmEntitySession().bind();
242
        }
243
	    input.saveModel(resetMerge);
244

    
245
	    List<CdmBase> saveCandidates = getEditorInput().getSaveCandidates();
246

    
247
	    if (!saveCandidates.isEmpty()){
248
	        CdmStore.getService(saveCandidates.get(0)).merge(saveCandidates, true);
249
	    }
250
        dirty.setDirty(false);
251
        getEditorInput().resetSaveCandidates();
252
        getEditorInput().dispose();
253
        getEditorInput().bind();
254
        conversation.commit(true);
255

    
256
        if (lastQuery != null){
257
            performSearch(lastQuery);
258
        }
259
        viewer.refresh();
260

    
261

    
262
	}
263

    
264
	/** {@inheritDoc} */
265
    public void performSearch(BulkEditorQuery query) {
266
        if (query != null) {
267

    
268
            // TODO check if dirty, prompt save
269
            if (isDirty()) {
270
                String[] labels = {Messages.BulkEditorE4_SAVE_AND_SEARCH, Messages.BulkEditorE4_DONT_SAVE,Messages.BulkEditorE4_CANCEL};
271
                MessageDialog dialog =new MessageDialog(topComposite.getShell(), Messages.BulkEditorE4_SAVE_CHANGES_TITLE, null, Messages.BulkEditorE4_SAVE_CHANGES_MESSAGE, MessageDialog.QUESTION,labels, 0);
272
                int result = dialog.open();
273
                if (result == 0) {
274
                    save(new NullProgressMonitor());
275
                } else if (result == 2){
276
                    return;
277
                }
278
            }
279
            dirty.setDirty(false);
280
//            getEditorInput().dispose();
281
//            getEditorInput().bind();
282
            getEditorInput().performSearch(query);
283
            lastQuery = query;
284
            viewer.setInput(getEditorInput().getModel());
285
            refresh();
286
        }
287
    }
288

    
289
    public void refresh() {
290
        refresh(true);
291
    }
292

    
293
    public void refresh(boolean resetInput) {
294
        if(resetInput){
295
            viewer.setInput(getEditorInput().getModel());
296
        }
297
        viewer.refresh();
298
    }
299

    
300
    public TableViewer getViewer() {
301
        return viewer;
302
    }
303

    
304
    public void setDirty(){
305
        dirty.setDirty(true);
306
    }
307

    
308
    public boolean isDirty() {
309
        return dirty.isDirty();
310
    }
311

    
312
    public AbstractBulkEditorInput getEditorInput() {
313
        return input;
314
    }
315

    
316
    /**
317
     * {@inheritDoc}
318
     */
319
    @Override
320
    public void update(CdmDataChangeMap arg0) {
321
    }
322

    
323
    /**
324
     * {@inheritDoc}
325
     */
326
    @Override
327
    public boolean canAttachMedia() {
328
        return true;
329
    }
330

    
331
    /**
332
     * {@inheritDoc}
333
     */
334
    @Override
335
    public void changed(Object element) {
336
        if (element instanceof CdmBase) {
337
            getEditorInput().addSaveCandidate((CdmBase)element);
338
        }
339
        dirty.setDirty(true);
340
    }
341

    
342
    /**
343
     * {@inheritDoc}
344
     */
345
    @Override
346
    public void forceDirty() {
347
        dirty.setDirty(true);
348
    }
349

    
350
    /**
351
     * {@inheritDoc}
352
     */
353
    @Override
354
    public boolean postOperation(CdmBase objectAffectedByOperation) {
355
        return false;
356
    }
357

    
358
    /**
359
     * {@inheritDoc}
360
     */
361
    @Override
362
    public boolean onComplete() {
363
        return false;
364
    }
365

    
366
    /**
367
     * {@inheritDoc}
368
     */
369
    @Override
370
    public ConversationHolder getConversationHolder() {
371
        return conversation;
372
    }
373

    
374
    /**
375
     * @return
376
     */
377
    public BulkEditorQuery getLastQuery() {
378
       return lastQuery;
379
    }
380

    
381
    /**
382
     * @param object
383
     */
384
    public void setLastQuery(BulkEditorQuery lastQuery) {
385
       this.lastQuery = lastQuery;
386

    
387
    }
388

    
389
}
(1-1/4)