Project

General

Profile

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

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

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

    
56
	private BulkEditorSearchE4 searchBar = null;
57

    
58
	@Inject
59
	private MDirtyable dirty;
60

    
61
    private AbstractBulkEditorInput<?> input;
62

    
63
    private Composite topComposite;
64

    
65
    private TableViewer viewer;
66

    
67
    private ConversationHolder conversation;
68

    
69
    @Inject
70
    private ESelectionService selService;
71

    
72
    private ISelectionChangedListener selectionChangedListener;
73

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

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

    
88
	}
89

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

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

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

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

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

    
117

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

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

    
125
	}
126

    
127
	@Persist
128
	public void save() {
129
	    dirty.setDirty(false);
130

    
131
	    this.searchBar.updateEditorInput();
132

    
133
	}
134

    
135
	/** {@inheritDoc} */
136
    public void performSearch(BulkEditorQuery query) {
137
        if (query != null) {
138

    
139
            // TODO check if dirty, prompt save
140
            if (isDirty()) {
141
                String[] labels = {"Save (and Search)", "Don't save (and Search)","Cancel"};
142
                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);
143
                int result = dialog.open();
144
                if (result == 0) {
145
                    save();
146
                } else if (result == 2){
147
                    return;
148
                }
149
            }
150

    
151
            getEditorInput().performSearch(query);
152

    
153
            viewer.setInput(getEditorInput().getWrappedModel());
154
            refresh();
155
        }
156
    }
157

    
158
    public void refresh() {
159
        viewer.refresh();
160
    }
161

    
162
    public boolean isDirty() {
163
        return dirty.isDirty();
164
    }
165

    
166
    public AbstractBulkEditorInput getEditorInput() {
167
        return input;
168
    }
169

    
170
    public List<AnnotatedTableItem> getMergeCandidates(){
171
        List<AnnotatedTableItem> mergeCandidates = new ArrayList<>();
172
        for(AnnotatedTableItem item: (List<AnnotatedTableItem>)viewer.getInput()){
173
            if(item.isMergeCandidate()){
174
                mergeCandidates.add(item);
175
            }
176
        }
177
        return mergeCandidates;
178
    }
179

    
180
    public AnnotatedTableItem getMergeTarget(){
181
        for(AnnotatedTableItem item: (List<AnnotatedTableItem>)viewer.getInput()){
182
            if(item.isMergeTarget()){
183
                return item;
184
            }
185
        }
186
        return null;
187
    }
188

    
189
    public void removeAllAnnotations(){
190
        for(AnnotatedTableItem item: (List<AnnotatedTableItem>)viewer.getInput()){
191
            item.setMergeCandidate(false);
192
            item.setMergeTarget(false);
193
        }
194
    }
195

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

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

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

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

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

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

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

    
250
}
(2-2/4)