Project

General

Profile

Download (9.91 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.List;
13

    
14
import javax.annotation.PostConstruct;
15
import javax.inject.Inject;
16

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

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

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

    
68
	@Inject
69
	private MDirtyable dirty;
70

    
71
    private AbstractBulkEditorInput<?> input;
72

    
73
    private Composite topComposite;
74

    
75
    private TableViewer viewer;
76

    
77
    private ConversationHolder conversation;
78

    
79
    @Inject
80
    private ESelectionService selService;
81

    
82
    private ISelectionChangedListener selectionChangedListener;
83

    
84
    @Inject
85
    private MPart thisPart;
86

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

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

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

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

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

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

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

    
125

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

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

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

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

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

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

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

    
182
//		createColumns(viewer);
183

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

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

    
191
	}
192

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

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

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

    
205
	    Object object = viewer.getInput();
206
	    if(object instanceof List){
207
	        for (Object item: (List)object) {
208
	            CdmBase cdmBase = (CdmBase) item;
209
	            CdmStore.getService(cdmBase).merge(cdmBase, true);
210
            }
211
	    }
212
        dirty.setDirty(false);
213
        viewer.refresh();
214

    
215
	}
216

    
217
	/** {@inheritDoc} */
218
    public void performSearch(BulkEditorQuery query) {
219
        if (query != null) {
220

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

    
237
            viewer.setInput(getEditorInput().getModel());
238
            refresh();
239
        }
240
    }
241

    
242
    public void refresh() {
243
        refresh(false);
244
    }
245

    
246
    public void refresh(boolean resetInput) {
247
        if(resetInput){
248
            viewer.setInput(getEditorInput().getModel());
249
        }
250
        viewer.refresh();
251
    }
252

    
253
    public TableViewer getViewer() {
254
        return viewer;
255
    }
256

    
257
    public void setDirty(){
258
        dirty.setDirty(true);
259
    }
260

    
261
    public boolean isDirty() {
262
        return dirty.isDirty();
263
    }
264

    
265
    public AbstractBulkEditorInput getEditorInput() {
266
        return input;
267
    }
268

    
269
    /**
270
     * {@inheritDoc}
271
     */
272
    @Override
273
    public void update(CdmDataChangeMap arg0) {
274
    }
275

    
276
    /**
277
     * {@inheritDoc}
278
     */
279
    @Override
280
    public boolean canAttachMedia() {
281
        return true;
282
    }
283

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

    
292
    /**
293
     * {@inheritDoc}
294
     */
295
    @Override
296
    public void forceDirty() {
297
        dirty.setDirty(true);
298
    }
299

    
300
    /**
301
     * {@inheritDoc}
302
     */
303
    @Override
304
    public boolean postOperation(CdmBase objectAffectedByOperation) {
305
        return false;
306
    }
307

    
308
    /**
309
     * {@inheritDoc}
310
     */
311
    @Override
312
    public boolean onComplete() {
313
        return false;
314
    }
315

    
316
    /**
317
     * {@inheritDoc}
318
     */
319
    @Override
320
    public ConversationHolder getConversationHolder() {
321
        return conversation;
322
    }
323

    
324
}
(1-1/3)