Project

General

Profile

Download (10 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.inject.Inject;
17

    
18
import org.eclipse.core.runtime.IProgressMonitor;
19
import org.eclipse.core.runtime.NullProgressMonitor;
20
import org.eclipse.e4.ui.di.Persist;
21
import org.eclipse.e4.ui.model.application.ui.MDirtyable;
22
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
23
import org.eclipse.e4.ui.services.EMenuService;
24
import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
25
import org.eclipse.jface.dialogs.MessageDialog;
26
import org.eclipse.jface.viewers.ArrayContentProvider;
27
import org.eclipse.jface.viewers.CellEditor;
28
import org.eclipse.jface.viewers.ICellModifier;
29
import org.eclipse.jface.viewers.ISelectionChangedListener;
30
import org.eclipse.jface.viewers.ITableLabelProvider;
31
import org.eclipse.jface.viewers.TableViewer;
32
import org.eclipse.jface.viewers.TableViewerColumn;
33
import org.eclipse.jface.viewers.TextCellEditor;
34
import org.eclipse.swt.SWT;
35
import org.eclipse.swt.layout.FillLayout;
36
import org.eclipse.swt.layout.GridData;
37
import org.eclipse.swt.layout.GridLayout;
38
import org.eclipse.swt.widgets.Composite;
39
import org.eclipse.swt.widgets.Table;
40

    
41
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
42
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
43
import eu.etaxonomy.cdm.model.common.CdmBase;
44
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
45
import eu.etaxonomy.taxeditor.bulkeditor.BulkEditorQuery;
46
import eu.etaxonomy.taxeditor.bulkeditor.input.AbstractBulkEditorInput;
47
import eu.etaxonomy.taxeditor.l10n.Messages;
48
import eu.etaxonomy.taxeditor.model.AbstractUtility;
49
import eu.etaxonomy.taxeditor.model.IDerivedUnitFacadePart;
50
import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
51
import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
52
import eu.etaxonomy.taxeditor.model.IPartContentHasFactualData;
53
import eu.etaxonomy.taxeditor.model.IPartContentHasMedia;
54
import eu.etaxonomy.taxeditor.model.IPartContentHasSupplementalData;
55
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
56
import eu.etaxonomy.taxeditor.store.CdmStore;
57
import eu.etaxonomy.taxeditor.workbench.part.IE4SavablePart;
58

    
59
/**
60
 *
61
 * @author pplitzner
62
 * @since Sep 8, 2017
63
 *
64
 */
65
public class BulkEditorE4 implements IPartContentHasDetails, IConversationEnabled, IPostOperationEnabled,
66
        IDirtyMarkable, IDerivedUnitFacadePart, IPartContentHasFactualData,
67
        IPartContentHasSupplementalData, IPartContentHasMedia, IE4SavablePart {
68

    
69
	@Inject
70
	private MDirtyable dirty;
71

    
72
    private AbstractBulkEditorInput<?> input;
73

    
74
    private Composite topComposite;
75

    
76
    private TableViewer viewer;
77

    
78
    private ConversationHolder conversation;
79

    
80
    @Inject
81
    private ESelectionService selService;
82

    
83
    private ISelectionChangedListener selectionChangedListener;
84

    
85
    @Inject
86
    private MPart thisPart;
87

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

    
92
	public void init(AbstractBulkEditorInput<?> input){
93
	    this.input = input;
94
	    AbstractBulkEditorInput<?> bulkEditorInput = input;
95
	    BulkEditorSearchE4 searchBar = new BulkEditorSearchE4(this, topComposite, SWT.NONE);
96
	    //layout needed because the search bar is added after @PostConstuct method
97
	    topComposite.getParent().layout();
98

    
99
	    thisPart.setLabel(input.getName());
100

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

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

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

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

    
126

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

    
152
        if(bulkEditorInput.getEntityUuid()!=null){
153
            performSearch(new BulkEditorQuery(bulkEditorInput.getEntityUuid().toString(), null));
154
        }
155
	}
156

    
157
	/** {@inheritDoc} */
158
	@PostConstruct
159
	public void createPartControl(Composite parent, EMenuService menuService) {
160
        if (CdmStore.isActive()){
161
            if(conversation == null){
162
                conversation = CdmStore.createConversation();
163
            }
164
        }
165
        else{
166
            return;
167
        }
168

    
169
		parent.setLayout(new GridLayout());
170

    
171
		topComposite = new Composite(parent, SWT.NONE);
172
		topComposite.setLayout(new GridLayout());
173

    
174
		GridData gridData = new GridData(SWT.FILL, SWT.TOP, true, false);
175
		topComposite.setLayoutData(gridData);
176

    
177
		Composite bottomComposite = new Composite(parent, SWT.NONE);
178
		bottomComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
179
		bottomComposite.setLayout(new FillLayout());
180
		viewer = new TableViewer(bottomComposite, SWT.MULTI | SWT.H_SCROLL
181
                | SWT.V_SCROLL | SWT.FULL_SELECTION);
182

    
183
//		createColumns(viewer);
184

    
185
        //propagate selection
186
        selectionChangedListener = (event -> selService.setSelection(AbstractUtility.getElementsFromSelectionChangedEvent(event)));
187
        viewer.addSelectionChangedListener(selectionChangedListener);
188

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

    
192
	}
193

    
194
	@Override
195
	@Persist
196
	public void save(IProgressMonitor monitor) {
197
	    save(monitor, true);
198
	}
199

    
200
	public void save(IProgressMonitor monitor, boolean resetMerge) {
201
	    input.saveModel(resetMerge);
202

    
203
	    // commit the conversation and start a new transaction immediately
204
	    conversation.commit(true);
205

    
206
	    Object object = viewer.getInput();
207
	    List<CdmBase> cdmBaseList = new ArrayList<>();;
208
	    if(object instanceof List){
209
	        for (Object item: (List)object) {
210
	            CdmBase cdmBase = (CdmBase) item;
211
	            cdmBaseList.add(cdmBase);
212
            }
213
	    }
214
	    CdmStore.getService(cdmBaseList.get(0)).merge(cdmBaseList, true);
215
        dirty.setDirty(false);
216
        viewer.refresh();
217

    
218
	}
219

    
220
	/** {@inheritDoc} */
221
    public void performSearch(BulkEditorQuery query) {
222
        if (query != null) {
223

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

    
240
            viewer.setInput(getEditorInput().getModel());
241
            refresh();
242
        }
243
    }
244

    
245
    public void refresh() {
246
        refresh(true);
247
    }
248

    
249
    public void refresh(boolean resetInput) {
250
        if(resetInput){
251
            viewer.setInput(getEditorInput().getModel());
252
        }
253
        viewer.refresh();
254
    }
255

    
256
    public TableViewer getViewer() {
257
        return viewer;
258
    }
259

    
260
    public void setDirty(){
261
        dirty.setDirty(true);
262
    }
263

    
264
    public boolean isDirty() {
265
        return dirty.isDirty();
266
    }
267

    
268
    public AbstractBulkEditorInput getEditorInput() {
269
        return input;
270
    }
271

    
272
    /**
273
     * {@inheritDoc}
274
     */
275
    @Override
276
    public void update(CdmDataChangeMap arg0) {
277
    }
278

    
279
    /**
280
     * {@inheritDoc}
281
     */
282
    @Override
283
    public boolean canAttachMedia() {
284
        return true;
285
    }
286

    
287
    /**
288
     * {@inheritDoc}
289
     */
290
    @Override
291
    public void changed(Object element) {
292
        dirty.setDirty(true);
293
    }
294

    
295
    /**
296
     * {@inheritDoc}
297
     */
298
    @Override
299
    public void forceDirty() {
300
        dirty.setDirty(true);
301
    }
302

    
303
    /**
304
     * {@inheritDoc}
305
     */
306
    @Override
307
    public boolean postOperation(CdmBase objectAffectedByOperation) {
308
        return false;
309
    }
310

    
311
    /**
312
     * {@inheritDoc}
313
     */
314
    @Override
315
    public boolean onComplete() {
316
        return false;
317
    }
318

    
319
    /**
320
     * {@inheritDoc}
321
     */
322
    @Override
323
    public ConversationHolder getConversationHolder() {
324
        return conversation;
325
    }
326

    
327
}
(1-1/3)