Project

General

Profile

Download (6.86 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
	    input.saveModel();
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
        viewer.refresh();
142

    
143
	}
144

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

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

    
161
            getEditorInput().performSearch(query);
162

    
163
            viewer.setInput(getEditorInput().getModel());
164
            refresh();
165
        }
166
    }
167

    
168
    public void refresh() {
169
        refresh(false);
170
    }
171

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

    
179
    public TableViewer getViewer() {
180
        return viewer;
181
    }
182

    
183
    public void setDirty(){
184
        dirty.setDirty(true);
185
    }
186

    
187
    public boolean isDirty() {
188
        return dirty.isDirty();
189
    }
190

    
191
    public AbstractBulkEditorInput getEditorInput() {
192
        return input;
193
    }
194

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

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

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

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

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

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

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

    
249
}
(1-1/3)