Project

General

Profile

Download (6.98 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.e4.ui.di.Persist;
18
import org.eclipse.e4.ui.model.application.ui.MDirtyable;
19
import org.eclipse.e4.ui.services.EMenuService;
20
import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
21
import org.eclipse.jface.dialogs.MessageDialog;
22
import org.eclipse.jface.viewers.ArrayContentProvider;
23
import org.eclipse.jface.viewers.ISelectionChangedListener;
24
import org.eclipse.jface.viewers.TableViewer;
25
import org.eclipse.swt.SWT;
26
import org.eclipse.swt.layout.FillLayout;
27
import org.eclipse.swt.layout.GridData;
28
import org.eclipse.swt.layout.GridLayout;
29
import org.eclipse.swt.widgets.Composite;
30

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

    
46
/**
47
 *
48
 * @author pplitzner
49
 * @since Sep 8, 2017
50
 *
51
 */
52
public class BulkEditorE4 implements IPartContentHasDetails, IConversationEnabled, IPostOperationEnabled,
53
        IDirtyMarkable, IDerivedUnitFacadePart, IPartContentHasFactualData, IPartContentHasMedia {
54

    
55
	private BulkEditorSearchE4 searchBar = null;
56

    
57
	@Inject
58
	private MDirtyable dirty;
59

    
60
    private AbstractBulkEditorInput<?> input;
61

    
62
    private Composite topComposite;
63

    
64
    private TableViewer viewer;
65

    
66
    private ConversationHolder conversation;
67

    
68
    @Inject
69
    private ESelectionService selService;
70

    
71
    private ISelectionChangedListener selectionChangedListener;
72

    
73
    @Inject
74
    public BulkEditorE4() {
75
	}
76

    
77
	public void init(AbstractBulkEditorInput<?> input){
78
	    this.input = input;
79
	    AbstractBulkEditorInput<?> bulkEditorInput = input;
80
	    if(bulkEditorInput.getEntityUuid()!=null){
81
	        bulkEditorInput.performSearch(new BulkEditorQuery(bulkEditorInput.getEntityUuid().toString(), null));
82
	    }
83
	    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
	}
88

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

    
101
		parent.setLayout(new GridLayout());
102

    
103
		topComposite = new Composite(parent, SWT.NONE);
104
		topComposite.setLayout(new GridLayout());
105

    
106
		GridData gridData = new GridData(SWT.FILL, SWT.TOP, true, false);
107
		topComposite.setLayoutData(gridData);
108

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

    
116

    
117
        //propagate selection
118
        selectionChangedListener = (event -> selService.setSelection(AbstractUtility.getElementsFromSelectionChangedEvent(event)));
119
        viewer.addSelectionChangedListener(selectionChangedListener);
120

    
121
        //create context menu
122
        menuService.registerContextMenu(viewer.getControl(), "eu.etaxonomy.taxeditor.bulkeditor.popupmenu.bulkeditor");
123

    
124
	}
125

    
126
	@Persist
127
	public void save() {
128
	    //FIXME E4 do the potential merging of groups
129

    
130
	    // commit the conversation and start a new transaction immediately
131
	    conversation.commit(true);
132

    
133
	    Object object = viewer.getInput();
134
	    if(object instanceof List){
135
	        for (Object item: (List)object) {
136
	            CdmBase cdmBase = (CdmBase) item;
137
	            CdmStore.getService(cdmBase).merge(cdmBase, true);
138
            }
139
	    }
140
        dirty.setDirty(false);
141
	}
142

    
143
	/** {@inheritDoc} */
144
    public void performSearch(BulkEditorQuery query) {
145
        if (query != null) {
146

    
147
            // TODO check if dirty, prompt save
148
            if (isDirty()) {
149
                String[] labels = {"Save (and Search)", "Don't save (and Search)","Cancel"};
150
                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);
151
                int result = dialog.open();
152
                if (result == 0) {
153
                    save();
154
                } else if (result == 2){
155
                    return;
156
                }
157
            }
158

    
159
            getEditorInput().performSearch(query);
160

    
161
            viewer.setInput(getEditorInput().getModel());
162
            refresh();
163
        }
164
    }
165

    
166
    public void refresh() {
167
        refresh(false);
168
    }
169

    
170
    public void refresh(boolean resetInput) {
171
        if(resetInput){
172
            viewer.setInput(getEditorInput().getModel());
173
        }
174
        viewer.refresh();
175
    }
176

    
177
    public TableViewer getViewer() {
178
        return viewer;
179
    }
180

    
181
    public void setDirty(){
182
        dirty.setDirty(true);
183
    }
184

    
185
    public boolean isDirty() {
186
        return dirty.isDirty();
187
    }
188

    
189
    public AbstractBulkEditorInput getEditorInput() {
190
        return input;
191
    }
192

    
193
    public void removeAllAnnotations(){
194
        input.removeMergeTarget();
195
        input.removeMergeCandidates();
196
    }
197

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

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

    
213
    /**
214
     * {@inheritDoc}
215
     */
216
    @Override
217
    public void changed(Object element) {
218
    }
219

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

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

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

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

    
252
}
(1-1/3)