Project

General

Profile

Download (6.86 KB) Statistics
| Branch: | Tag: | Revision:
1 364a9123 Patrick Plitzner
/**
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 5cfc8708 Patrick Plitzner
import java.util.List;
13
14 364a9123 Patrick Plitzner
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 a4b31985 Patrick Plitzner
import org.eclipse.e4.ui.services.EMenuService;
20
import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
21 364a9123 Patrick Plitzner
import org.eclipse.jface.dialogs.MessageDialog;
22 a21eea64 Patrick Plitzner
import org.eclipse.jface.viewers.ArrayContentProvider;
23 a4b31985 Patrick Plitzner
import org.eclipse.jface.viewers.ISelectionChangedListener;
24 a21eea64 Patrick Plitzner
import org.eclipse.jface.viewers.TableViewer;
25 364a9123 Patrick Plitzner
import org.eclipse.swt.SWT;
26 a21eea64 Patrick Plitzner
import org.eclipse.swt.layout.FillLayout;
27 364a9123 Patrick Plitzner
import org.eclipse.swt.layout.GridData;
28
import org.eclipse.swt.layout.GridLayout;
29
import org.eclipse.swt.widgets.Composite;
30 a21eea64 Patrick Plitzner
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 364a9123 Patrick Plitzner
import eu.etaxonomy.taxeditor.bulkeditor.BulkEditorQuery;
36
import eu.etaxonomy.taxeditor.bulkeditor.input.AbstractBulkEditorInput;
37 a4b31985 Patrick Plitzner
import eu.etaxonomy.taxeditor.model.AbstractUtility;
38 364a9123 Patrick Plitzner
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 a21eea64 Patrick Plitzner
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
44 364a9123 Patrick Plitzner
import eu.etaxonomy.taxeditor.store.CdmStore;
45
46
/**
47
 *
48
 * @author pplitzner
49
 * @since Sep 8, 2017
50
 *
51
 */
52 a21eea64 Patrick Plitzner
public class BulkEditorE4 implements IPartContentHasDetails, IConversationEnabled, IPostOperationEnabled,
53 364a9123 Patrick Plitzner
        IDirtyMarkable, IDerivedUnitFacadePart, IPartContentHasFactualData, IPartContentHasMedia {
54
55
	private BulkEditorSearchE4 searchBar = null;
56
57
	@Inject
58
	private MDirtyable dirty;
59
60
    private AbstractBulkEditorInput<?> input;
61
62 a21eea64 Patrick Plitzner
    private Composite topComposite;
63 364a9123 Patrick Plitzner
64 a21eea64 Patrick Plitzner
    private TableViewer viewer;
65 364a9123 Patrick Plitzner
66 a21eea64 Patrick Plitzner
    private ConversationHolder conversation;
67 364a9123 Patrick Plitzner
68 a4b31985 Patrick Plitzner
    @Inject
69
    private ESelectionService selService;
70
71
    private ISelectionChangedListener selectionChangedListener;
72
73 a21eea64 Patrick Plitzner
    @Inject
74
    public BulkEditorE4() {
75 364a9123 Patrick Plitzner
	}
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 a21eea64 Patrick Plitzner
	    searchBar = new BulkEditorSearchE4(this, topComposite, SWT.NONE);
84 2bc43991 Patrick Plitzner
	    //layout needed because the search bar is added after @PostConstuct method
85
	    topComposite.getParent().layout();
86 364a9123 Patrick Plitzner
87
	}
88
89
	/** {@inheritDoc} */
90
	@PostConstruct
91 a4b31985 Patrick Plitzner
	public void createPartControl(Composite parent, EMenuService menuService) {
92 364a9123 Patrick Plitzner
        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 a21eea64 Patrick Plitzner
		topComposite = new Composite(parent, SWT.NONE);
104
		topComposite.setLayout(new GridLayout());
105 364a9123 Patrick Plitzner
106
		GridData gridData = new GridData(SWT.FILL, SWT.TOP, true, false);
107 a21eea64 Patrick Plitzner
		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 bce766a1 Patrick Plitzner
		viewer.setLabelProvider(new BulkEditorLabelProvider(this));
115 364a9123 Patrick Plitzner
116 a4b31985 Patrick Plitzner
117
        //propagate selection
118
        selectionChangedListener = (event -> selService.setSelection(AbstractUtility.getElementsFromSelectionChangedEvent(event)));
119
        viewer.addSelectionChangedListener(selectionChangedListener);
120
121 364a9123 Patrick Plitzner
        //create context menu
122 a4b31985 Patrick Plitzner
        menuService.registerContextMenu(viewer.getControl(), "eu.etaxonomy.taxeditor.bulkeditor.popupmenu.bulkeditor");
123 364a9123 Patrick Plitzner
124
	}
125
126 a21eea64 Patrick Plitzner
	@Persist
127
	public void save() {
128 fd7d64e9 Patrick Plitzner
	    input.saveModel();
129 bce766a1 Patrick Plitzner
130 a2a04809 Patrick Plitzner
	    // commit the conversation and start a new transaction immediately
131
	    conversation.commit(true);
132 364a9123 Patrick Plitzner
133 a2a04809 Patrick Plitzner
	    Object object = viewer.getInput();
134
	    if(object instanceof List){
135
	        for (Object item: (List)object) {
136 bce766a1 Patrick Plitzner
	            CdmBase cdmBase = (CdmBase) item;
137
	            CdmStore.getService(cdmBase).merge(cdmBase, true);
138 a2a04809 Patrick Plitzner
            }
139
	    }
140
        dirty.setDirty(false);
141 fd7d64e9 Patrick Plitzner
        viewer.refresh();
142
143 364a9123 Patrick Plitzner
	}
144
145
	/** {@inheritDoc} */
146 a21eea64 Patrick Plitzner
    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 364a9123 Patrick Plitzner
161 a21eea64 Patrick Plitzner
            getEditorInput().performSearch(query);
162 364a9123 Patrick Plitzner
163 bce766a1 Patrick Plitzner
            viewer.setInput(getEditorInput().getModel());
164 a21eea64 Patrick Plitzner
            refresh();
165
        }
166
    }
167 364a9123 Patrick Plitzner
168 a380926d Patrick Plitzner
    public void refresh() {
169 a2a04809 Patrick Plitzner
        refresh(false);
170
    }
171
172
    public void refresh(boolean resetInput) {
173
        if(resetInput){
174 bce766a1 Patrick Plitzner
            viewer.setInput(getEditorInput().getModel());
175 a2a04809 Patrick Plitzner
        }
176 a21eea64 Patrick Plitzner
        viewer.refresh();
177
    }
178 364a9123 Patrick Plitzner
179 a2a04809 Patrick Plitzner
    public TableViewer getViewer() {
180
        return viewer;
181
    }
182
183
    public void setDirty(){
184
        dirty.setDirty(true);
185
    }
186
187 a380926d Patrick Plitzner
    public boolean isDirty() {
188 a21eea64 Patrick Plitzner
        return dirty.isDirty();
189
    }
190 364a9123 Patrick Plitzner
191 a21eea64 Patrick Plitzner
    public AbstractBulkEditorInput getEditorInput() {
192
        return input;
193
    }
194 364a9123 Patrick Plitzner
195 a21eea64 Patrick Plitzner
    /**
196
     * {@inheritDoc}
197
     */
198
    @Override
199
    public void update(CdmDataChangeMap arg0) {
200
    }
201 364a9123 Patrick Plitzner
202 a21eea64 Patrick Plitzner
    /**
203
     * {@inheritDoc}
204
     */
205
    @Override
206
    public boolean canAttachMedia() {
207
        return true;
208
    }
209 364a9123 Patrick Plitzner
210 a21eea64 Patrick Plitzner
    /**
211
     * {@inheritDoc}
212
     */
213
    @Override
214
    public void changed(Object element) {
215
    }
216 364a9123 Patrick Plitzner
217 a21eea64 Patrick Plitzner
    /**
218
     * {@inheritDoc}
219
     */
220
    @Override
221
    public void forceDirty() {
222
        dirty.setDirty(true);
223
    }
224 364a9123 Patrick Plitzner
225 a21eea64 Patrick Plitzner
    /**
226
     * {@inheritDoc}
227
     */
228
    @Override
229
    public boolean postOperation(CdmBase objectAffectedByOperation) {
230
        return false;
231
    }
232 364a9123 Patrick Plitzner
233 a21eea64 Patrick Plitzner
    /**
234
     * {@inheritDoc}
235
     */
236
    @Override
237
    public boolean onComplete() {
238
        return false;
239 364a9123 Patrick Plitzner
    }
240
241 a21eea64 Patrick Plitzner
    /**
242
     * {@inheritDoc}
243
     */
244
    @Override
245
    public ConversationHolder getConversationHolder() {
246
        return conversation;
247
    }
248 364a9123 Patrick Plitzner
249
}