Project

General

Profile

Download (10.1 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
	    if(object instanceof List){
213
	        for (Object item: (List)object) {
214
	            CdmBase cdmBase = (CdmBase) item;
215
	            cdmBaseList.add(cdmBase);
216
            }
217
	    }
218
	    if (!cdmBaseList.isEmpty()){
219
	        CdmStore.getService(cdmBaseList.get(0)).merge(cdmBaseList, true);
220
	    }
221
        dirty.setDirty(false);
222
        viewer.refresh();
223

    
224
	}
225

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

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

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

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

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

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

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

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

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

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

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

    
293
    /**
294
     * {@inheritDoc}
295
     */
296
    @Override
297
    public void changed(Object element) {
298
        dirty.setDirty(true);
299
    }
300

    
301
    /**
302
     * {@inheritDoc}
303
     */
304
    @Override
305
    public void forceDirty() {
306
        dirty.setDirty(true);
307
    }
308

    
309
    /**
310
     * {@inheritDoc}
311
     */
312
    @Override
313
    public boolean postOperation(CdmBase objectAffectedByOperation) {
314
        return false;
315
    }
316

    
317
    /**
318
     * {@inheritDoc}
319
     */
320
    @Override
321
    public boolean onComplete() {
322
        return false;
323
    }
324

    
325
    /**
326
     * {@inheritDoc}
327
     */
328
    @Override
329
    public ConversationHolder getConversationHolder() {
330
        return conversation;
331
    }
332

    
333
}
(1-1/3)