Project

General

Profile

Download (10 KB) Statistics
| Branch: | Tag: | Revision:
1 5ce82a07 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.featuretree.e4;
11
12 0343d833 Patrick Plitzner
import java.util.Arrays;
13
import java.util.HashMap;
14
import java.util.List;
15
import java.util.Map;
16 5ce82a07 Patrick Plitzner
17
import javax.annotation.PostConstruct;
18 0343d833 Patrick Plitzner
import javax.annotation.PreDestroy;
19 5ce82a07 Patrick Plitzner
import javax.inject.Inject;
20
21 dbc398cf Patrick Plitzner
import org.eclipse.core.runtime.IProgressMonitor;
22 4d98dfd9 Patrick Plitzner
import org.eclipse.e4.ui.di.Focus;
23 5ce82a07 Patrick Plitzner
import org.eclipse.e4.ui.di.Persist;
24
import org.eclipse.e4.ui.model.application.ui.MDirtyable;
25 dbc398cf Patrick Plitzner
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
26 ff46c6ca Patrick Plitzner
import org.eclipse.e4.ui.services.EMenuService;
27 dbc398cf Patrick Plitzner
import org.eclipse.e4.ui.workbench.modeling.EPartService;
28 4d98dfd9 Patrick Plitzner
import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
29 5ce82a07 Patrick Plitzner
import org.eclipse.jface.viewers.ISelectionChangedListener;
30 ddf52a47 Patrick Plitzner
import org.eclipse.jface.viewers.IStructuredSelection;
31 5ce82a07 Patrick Plitzner
import org.eclipse.jface.viewers.SelectionChangedEvent;
32 ff46c6ca Patrick Plitzner
import org.eclipse.jface.viewers.TreeViewer;
33 6ba01b22 Patrick Plitzner
import org.eclipse.swt.dnd.DND;
34
import org.eclipse.swt.dnd.Transfer;
35
import org.eclipse.swt.layout.FillLayout;
36 5ce82a07 Patrick Plitzner
import org.eclipse.swt.widgets.Composite;
37 dbc398cf Patrick Plitzner
import org.eclipse.ui.IMemento;
38 5ce82a07 Patrick Plitzner
39
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
40 25af2145 Patrick Plitzner
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
41 5ce82a07 Patrick Plitzner
import eu.etaxonomy.cdm.api.service.IFeatureTreeService;
42
import eu.etaxonomy.cdm.model.description.FeatureNode;
43
import eu.etaxonomy.cdm.model.description.FeatureTree;
44 25af2145 Patrick Plitzner
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
45 6ba01b22 Patrick Plitzner
import eu.etaxonomy.taxeditor.editor.definedterm.TermTransfer;
46
import eu.etaxonomy.taxeditor.featuretree.FeatureNodeTransfer;
47
import eu.etaxonomy.taxeditor.featuretree.FeatureTreeContentProvider;
48
import eu.etaxonomy.taxeditor.featuretree.FeatureTreeLabelProvider;
49 f77fd7f6 Patrick Plitzner
import eu.etaxonomy.taxeditor.model.AbstractUtility;
50 dbc398cf Patrick Plitzner
import eu.etaxonomy.taxeditor.model.IContextListener;
51 6ba01b22 Patrick Plitzner
import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
52 e75df01d Patrick Plitzner
import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
53
import eu.etaxonomy.taxeditor.model.IPartContentHasSupplementalData;
54 0343d833 Patrick Plitzner
import eu.etaxonomy.taxeditor.session.ICdmEntitySession;
55
import eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled;
56 5ce82a07 Patrick Plitzner
import eu.etaxonomy.taxeditor.store.CdmStore;
57 99d4f2d2 Patrick Plitzner
import eu.etaxonomy.taxeditor.workbench.part.IE4SavablePart;
58 ddf52a47 Patrick Plitzner
import eu.etaxonomy.taxeditor.workbench.part.IE4ViewerPart;
59 5ce82a07 Patrick Plitzner
60
/**
61
 *
62
 * @author pplitzner
63
 * @date 06.06.2017
64
 *
65
 */
66 6ba01b22 Patrick Plitzner
public class FeatureTreeEditor implements ICdmEntitySessionEnabled, ISelectionChangedListener,
67
        IE4ViewerPart, IE4SavablePart, IPartContentHasDetails, IPartContentHasSupplementalData,
68
        IContextListener, IConversationEnabled, IDirtyMarkable {
69 5ce82a07 Patrick Plitzner
70
    private ConversationHolder conversation;
71 4d98dfd9 Patrick Plitzner
72 0343d833 Patrick Plitzner
    private ICdmEntitySession cdmEntitySession;
73
74 4d98dfd9 Patrick Plitzner
    @Inject
75
    private ESelectionService selService;
76
77 5ce82a07 Patrick Plitzner
    @Inject
78
    private MDirtyable dirty;
79
80 6ba01b22 Patrick Plitzner
//    private FeatureTreeEditorComposite composite;
81 34fd7a1c Patrick Plitzner
82 dbc398cf Patrick Plitzner
    @Inject
83
    private MPart thisPart;
84
85 6ba01b22 Patrick Plitzner
    private TreeViewer viewer;
86
87 5ce82a07 Patrick Plitzner
    @Inject
88 efbcc6bb Patrick Plitzner
    public FeatureTreeEditor() {
89 dbc398cf Patrick Plitzner
        CdmStore.getContextManager().addContextListener(this);
90 5ce82a07 Patrick Plitzner
    }
91
92
	/** {@inheritDoc} */
93 a4a18917 Patrick Plitzner
    @PostConstruct
94 ff46c6ca Patrick Plitzner
    public void createControl(Composite parent, EMenuService menuService){
95 efbcc6bb Patrick Plitzner
        if (CdmStore.isActive()){
96 dd627134 Patrick Plitzner
            initSession();
97 efbcc6bb Patrick Plitzner
        }
98
        else{
99
            return;
100
        }
101 6ba01b22 Patrick Plitzner
        parent.setLayout(new FillLayout());
102
        viewer = new TreeViewer(parent);
103
        viewer.setContentProvider(new FeatureTreeContentProvider());
104
        viewer.setLabelProvider(new FeatureTreeLabelProvider());
105
106
        int ops = DND.DROP_COPY | DND.DROP_MOVE;
107
        Transfer[] transfers = new Transfer[] { FeatureNodeTransfer
108
                .getInstance(), TermTransfer.getInstance() };
109
        viewer.addDragSupport(ops, transfers, new FeatureNodeDragListener(viewer));
110
        viewer.addDropSupport(ops, transfers, new FeatureNodeDropAdapter(dirty, viewer));
111
        viewer.addSelectionChangedListener(this);
112
113
        List<FeatureTree> trees = CdmStore.getService(IFeatureTreeService.class).list(FeatureTree.class, null, null, null, null);
114
        viewer.setInput(trees);
115
116
//        composite = new FeatureTreeEditorComposite(parent, SWT.NULL);
117
//        composite.init(new FeatureNodeDragListener(composite.getViewer()),
118
//                new FeatureNodeDropAdapter(dirty, composite.getViewer()), this, new SelectionAdapter() {
119
//            @Override
120
//            public void widgetSelected(SelectionEvent e) {
121
//                if(isDirty()){
122
//                    MessageDialog dialog;
123
//                    String[] buttonLables = {IDialogConstants.YES_LABEL, IDialogConstants.NO_LABEL, IDialogConstants.CANCEL_LABEL};
124
//                    dialog = new MessageDialog(null, "Unsaved changes", null, "You have unsaved changes. Do you want to save?",
125
//                            MessageDialog.QUESTION_WITH_CANCEL, 0, buttonLables);
126
//                    int returnCode = dialog.open();
127
//
128
//                    if (returnCode == 0){
129
//                        //YES
130
//                        save(new NullProgressMonitor());
131
//                    } else if (returnCode == 1){
132
//                        //NO
133
//                        if(CdmStore.isActive()){
134
//                            clearSession();
135
//                            initSession();
136
//                            composite.setSelectedTree(null);
137
//                        }
138
//                    } else if (returnCode == 2){
139
//                        //CANCEL
140
//                        return;
141
//                    }
142
//                }
143
//                FeatureTree tree = FeatureTreeSelectionDialog.select(composite.getDisplay().getActiveShell(), //conversation,
144
//                        null);
145
//                if (tree != null) {
146
//                    composite.setSelectedTree(tree);
147
//                }
148
//            }
149
//        }, this);
150 ff46c6ca Patrick Plitzner
151
        //create context menu
152 6ba01b22 Patrick Plitzner
        menuService.registerContextMenu(viewer.getControl(), "eu.etaxonomy.taxeditor.store.popupmenu.featureTreeEditor");
153 a4a18917 Patrick Plitzner
    }
154 5ce82a07 Patrick Plitzner
155 dd627134 Patrick Plitzner
    private void initSession(){
156
        if(conversation == null){
157
            conversation = CdmStore.createConversation();
158
        }
159
        if(cdmEntitySession!=null){
160
            cdmEntitySession = CdmStore.getCurrentSessionManager().newSession(this, true);
161
        }
162
    }
163
164
    private void clearSession() {
165
        if(conversation!=null){
166
            conversation.close();
167
            conversation = null;
168
        }
169
        if(cdmEntitySession != null) {
170
            cdmEntitySession.dispose();
171
            cdmEntitySession = null;
172
        }
173
        dirty.setDirty(false);
174
    }
175
176 34fd7a1c Patrick Plitzner
	public void setDirty(boolean isDirty){
177
	    this.dirty.setDirty(isDirty);
178
	}
179 5ce82a07 Patrick Plitzner
180 50c9803d Patrick Plitzner
	public boolean isDirty(){
181
	    return dirty.isDirty();
182
	}
183
184 5ce82a07 Patrick Plitzner
	/** {@inheritDoc} */
185
	@Override
186
	public void selectionChanged(SelectionChangedEvent event) {
187 4d98dfd9 Patrick Plitzner
		//propagate selection
188 f77fd7f6 Patrick Plitzner
		selService.setSelection(AbstractUtility.getElementsFromSelectionChangedEvent(event));
189 4d98dfd9 Patrick Plitzner
	}
190
191
	@Focus
192 22b4b462 Patrick Plitzner
	public void focus(){
193 6ba01b22 Patrick Plitzner
	    if(viewer!=null){
194
	        viewer.getControl().setFocus();
195 efbcc6bb Patrick Plitzner
	    }
196 4d98dfd9 Patrick Plitzner
        if(conversation!=null && !conversation.isBound()){
197
            conversation.bind();
198
        }
199 0343d833 Patrick Plitzner
        if(cdmEntitySession != null) {
200
            cdmEntitySession.bind();
201
        }
202 5ce82a07 Patrick Plitzner
	}
203
204 ddf52a47 Patrick Plitzner
	@Override
205
    public void refresh(){
206 6ba01b22 Patrick Plitzner
	    viewer.refresh();
207 ff46c6ca Patrick Plitzner
	}
208
209
	public TreeViewer getViewer(){
210 6ba01b22 Patrick Plitzner
	    return viewer;
211 ff46c6ca Patrick Plitzner
	}
212
213 ddf52a47 Patrick Plitzner
	/**
214
	 * {@inheritDoc}
215
	 */
216
	@Override
217
	public IStructuredSelection getSelection() {
218 6ba01b22 Patrick Plitzner
	    return (IStructuredSelection) viewer.getSelection();
219 ddf52a47 Patrick Plitzner
	}
220
221 25af2145 Patrick Plitzner
	/**
222
	 * {@inheritDoc}
223
	 */
224
	@Override
225
	public ConversationHolder getConversationHolder() {
226
	    return conversation;
227
	}
228
229 99d4f2d2 Patrick Plitzner
	@Override
230
    @Persist
231
	public void save(IProgressMonitor monitor){
232 0343d833 Patrick Plitzner
        if (!conversation.isBound()) {
233
            conversation.bind();
234
        }
235
236
        // commit the conversation and start a new transaction immediately
237
        conversation.commit(true);
238
239 6ba01b22 Patrick Plitzner
        List<FeatureTree> trees = (List<FeatureTree>) viewer.getInput();
240
        CdmStore.getService(IFeatureTreeService.class).merge(trees);
241 0343d833 Patrick Plitzner
242
        this.setDirty(false);
243 5ce82a07 Patrick Plitzner
	}
244
245 0343d833 Patrick Plitzner
	@PreDestroy
246
	public void dispose(){
247 dd627134 Patrick Plitzner
	    clearSession();
248 5ce82a07 Patrick Plitzner
	}
249
250 0343d833 Patrick Plitzner
    @Override
251
    public ICdmEntitySession getCdmEntitySession() {
252
        return cdmEntitySession;
253
    }
254
255
    @Override
256
    public Map<Object, List<String>> getPropertyPathsMap() {
257
        List<String> propertyPaths = Arrays.asList(new String[] {
258
                "children", //$NON-NLS-1$
259
                "feature", //$NON-NLS-1$
260
                "featureTree", //$NON-NLS-1$
261
        });
262
        Map<Object, List<String>> propertyPathMap =
263
                new HashMap<Object, List<String>>();
264
        propertyPathMap.put(FeatureNode.class,propertyPaths);
265
        return propertyPathMap;
266
    }
267
268
    /**
269
     * {@inheritDoc}
270
     */
271
    @Override
272
    public List<FeatureTree> getRootEntities() {
273 6ba01b22 Patrick Plitzner
        return (List<FeatureTree>) viewer.getInput();
274 2b559c4e Patrick Plitzner
    }
275
276 dbc398cf Patrick Plitzner
    /**
277
     * {@inheritDoc}
278
     */
279
    @Override
280
    public void contextAboutToStop(IMemento memento, IProgressMonitor monitor) {
281
    }
282
283
    /**
284
     * {@inheritDoc}
285
     */
286
    @Override
287
    public void contextStop(IMemento memento, IProgressMonitor monitor) {
288
        //close view when workbench closes
289
        try{
290
            thisPart.getContext().get(EPartService.class).hidePart(thisPart);
291
        }
292
        catch(Exception e){
293
            //nothing
294
        }
295
    }
296
297
    /**
298
     * {@inheritDoc}
299
     */
300
    @Override
301
    public void contextStart(IMemento memento, IProgressMonitor monitor) {
302
    }
303
304
    /**
305
     * {@inheritDoc}
306
     */
307
    @Override
308
    public void contextRefresh(IProgressMonitor monitor) {
309
    }
310
311
    /**
312
     * {@inheritDoc}
313
     */
314
    @Override
315
    public void workbenchShutdown(IMemento memento, IProgressMonitor monitor) {
316
    }
317
318 25af2145 Patrick Plitzner
    /**
319
     * {@inheritDoc}
320
     */
321
    @Override
322
    public void update(CdmDataChangeMap arg0) {
323
    }
324
325 6ba01b22 Patrick Plitzner
    /**
326
     * {@inheritDoc}
327
     */
328
    @Override
329
    public void changed(Object element) {
330
        dirty.setDirty(true);
331
    }
332
333
    /**
334
     * {@inheritDoc}
335
     */
336
    @Override
337
    public void forceDirty() {
338
        dirty.setDirty(true);
339
    }
340
341 5ce82a07 Patrick Plitzner
}