Project

General

Profile

Download (7.19 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.services.EMenuService;
22
import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
23
import org.eclipse.jface.dialogs.MessageDialog;
24
import org.eclipse.jface.viewers.ArrayContentProvider;
25
import org.eclipse.jface.viewers.ISelectionChangedListener;
26
import org.eclipse.jface.viewers.TableViewer;
27
import org.eclipse.swt.SWT;
28
import org.eclipse.swt.layout.FillLayout;
29
import org.eclipse.swt.layout.GridData;
30
import org.eclipse.swt.layout.GridLayout;
31
import org.eclipse.swt.widgets.Composite;
32

    
33
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
34
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
35
import eu.etaxonomy.cdm.model.common.CdmBase;
36
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
37
import eu.etaxonomy.taxeditor.bulkeditor.BulkEditorQuery;
38
import eu.etaxonomy.taxeditor.bulkeditor.input.AbstractBulkEditorInput;
39
import eu.etaxonomy.taxeditor.model.AbstractUtility;
40
import eu.etaxonomy.taxeditor.model.IDerivedUnitFacadePart;
41
import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
42
import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
43
import eu.etaxonomy.taxeditor.model.IPartContentHasFactualData;
44
import eu.etaxonomy.taxeditor.model.IPartContentHasMedia;
45
import eu.etaxonomy.taxeditor.model.IPartContentHasSupplementalData;
46
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
47
import eu.etaxonomy.taxeditor.store.CdmStore;
48
import eu.etaxonomy.taxeditor.workbench.part.IE4SavablePart;
49

    
50
/**
51
 *
52
 * @author pplitzner
53
 * @since Sep 8, 2017
54
 *
55
 */
56
public class BulkEditorE4 implements IPartContentHasDetails, IConversationEnabled, IPostOperationEnabled,
57
        IDirtyMarkable, IDerivedUnitFacadePart, IPartContentHasFactualData,
58
        IPartContentHasSupplementalData, IPartContentHasMedia, IE4SavablePart {
59

    
60
	@Inject
61
	private MDirtyable dirty;
62

    
63
    private AbstractBulkEditorInput<?> input;
64

    
65
    private Composite topComposite;
66

    
67
    private TableViewer viewer;
68

    
69
    private ConversationHolder conversation;
70

    
71
    @Inject
72
    private ESelectionService selService;
73

    
74
    private ISelectionChangedListener selectionChangedListener;
75

    
76
    @Inject
77
    public BulkEditorE4() {
78
	}
79

    
80
	public void init(AbstractBulkEditorInput<?> input){
81
	    this.input = input;
82
	    AbstractBulkEditorInput<?> bulkEditorInput = input;
83
	    BulkEditorSearchE4 searchBar = new BulkEditorSearchE4(this, topComposite, SWT.NONE);
84
	    //layout needed because the search bar is added after @PostConstuct method
85
	    topComposite.getParent().layout();
86

    
87
	    if(bulkEditorInput.getEntityUuid()!=null){
88
	        performSearch(new BulkEditorQuery(bulkEditorInput.getEntityUuid().toString(), null));
89
	    }
90
	}
91

    
92
	/** {@inheritDoc} */
93
	@PostConstruct
94
	public void createPartControl(Composite parent, EMenuService menuService) {
95
        if (CdmStore.isActive()){
96
            if(conversation == null){
97
                conversation = CdmStore.createConversation();
98
            }
99
        }
100
        else{
101
            return;
102
        }
103

    
104
		parent.setLayout(new GridLayout());
105

    
106
		topComposite = new Composite(parent, SWT.NONE);
107
		topComposite.setLayout(new GridLayout());
108

    
109
		GridData gridData = new GridData(SWT.FILL, SWT.TOP, true, false);
110
		topComposite.setLayoutData(gridData);
111

    
112
		Composite bottomComposite = new Composite(parent, SWT.NONE);
113
		bottomComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
114
		bottomComposite.setLayout(new FillLayout());
115
		viewer = new TableViewer(bottomComposite);
116
		viewer.setContentProvider(new ArrayContentProvider());
117
		viewer.setLabelProvider(new BulkEditorLabelProvider(this));
118

    
119

    
120
        //propagate selection
121
        selectionChangedListener = (event -> selService.setSelection(AbstractUtility.getElementsFromSelectionChangedEvent(event)));
122
        viewer.addSelectionChangedListener(selectionChangedListener);
123

    
124
        //create context menu
125
        menuService.registerContextMenu(viewer.getControl(), "eu.etaxonomy.taxeditor.bulkeditor.popupmenu.bulkeditor");
126

    
127
	}
128

    
129
	@Override
130
    @Persist
131
	public void save(IProgressMonitor monitor) {
132
	    input.saveModel();
133

    
134
	    // commit the conversation and start a new transaction immediately
135
	    conversation.commit(true);
136

    
137
	    Object object = viewer.getInput();
138
	    if(object instanceof List){
139
	        for (Object item: (List)object) {
140
	            CdmBase cdmBase = (CdmBase) item;
141
	            CdmStore.getService(cdmBase).merge(cdmBase, true);
142
            }
143
	    }
144
        dirty.setDirty(false);
145
        viewer.refresh();
146

    
147
	}
148

    
149
	/** {@inheritDoc} */
150
    public void performSearch(BulkEditorQuery query) {
151
        if (query != null) {
152

    
153
            // TODO check if dirty, prompt save
154
            if (isDirty()) {
155
                String[] labels = {"Save (and Search)", "Don't save (and Search)","Cancel"};
156
                MessageDialog dialog =new MessageDialog(topComposite.getShell(), "Save changes", null, "You have made changes that must be saved before this query can be executed. Would you like to proceed?", MessageDialog.QUESTION,labels, 0);
157
                int result = dialog.open();
158
                if (result == 0) {
159
                    save(new NullProgressMonitor());
160
                } else if (result == 2){
161
                    return;
162
                }
163
            }
164

    
165
            getEditorInput().performSearch(query);
166

    
167
            viewer.setInput(getEditorInput().getModel());
168
            refresh();
169
        }
170
    }
171

    
172
    public void refresh() {
173
        refresh(false);
174
    }
175

    
176
    public void refresh(boolean resetInput) {
177
        if(resetInput){
178
            viewer.setInput(getEditorInput().getModel());
179
        }
180
        viewer.refresh();
181
    }
182

    
183
    public TableViewer getViewer() {
184
        return viewer;
185
    }
186

    
187
    public void setDirty(){
188
        dirty.setDirty(true);
189
    }
190

    
191
    public boolean isDirty() {
192
        return dirty.isDirty();
193
    }
194

    
195
    public AbstractBulkEditorInput getEditorInput() {
196
        return input;
197
    }
198

    
199
    /**
200
     * {@inheritDoc}
201
     */
202
    @Override
203
    public void update(CdmDataChangeMap arg0) {
204
    }
205

    
206
    /**
207
     * {@inheritDoc}
208
     */
209
    @Override
210
    public boolean canAttachMedia() {
211
        return true;
212
    }
213

    
214
    /**
215
     * {@inheritDoc}
216
     */
217
    @Override
218
    public void changed(Object element) {
219
        dirty.setDirty(true);
220
    }
221

    
222
    /**
223
     * {@inheritDoc}
224
     */
225
    @Override
226
    public void forceDirty() {
227
        dirty.setDirty(true);
228
    }
229

    
230
    /**
231
     * {@inheritDoc}
232
     */
233
    @Override
234
    public boolean postOperation(CdmBase objectAffectedByOperation) {
235
        return false;
236
    }
237

    
238
    /**
239
     * {@inheritDoc}
240
     */
241
    @Override
242
    public boolean onComplete() {
243
        return false;
244
    }
245

    
246
    /**
247
     * {@inheritDoc}
248
     */
249
    @Override
250
    public ConversationHolder getConversationHolder() {
251
        return conversation;
252
    }
253

    
254
}
(1-1/3)