Project

General

Profile

Download (8.81 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.featuretree.e4;
11

    
12
import java.util.Arrays;
13
import java.util.HashMap;
14
import java.util.List;
15
import java.util.Map;
16

    
17
import javax.annotation.PostConstruct;
18
import javax.annotation.PreDestroy;
19
import javax.inject.Inject;
20

    
21
import org.eclipse.core.runtime.IProgressMonitor;
22
import org.eclipse.e4.ui.di.Focus;
23
import org.eclipse.e4.ui.di.Persist;
24
import org.eclipse.e4.ui.model.application.ui.MDirtyable;
25
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
26
import org.eclipse.e4.ui.services.EMenuService;
27
import org.eclipse.e4.ui.workbench.modeling.EPartService;
28
import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
29
import org.eclipse.jface.util.LocalSelectionTransfer;
30
import org.eclipse.jface.viewers.ISelectionChangedListener;
31
import org.eclipse.jface.viewers.IStructuredSelection;
32
import org.eclipse.jface.viewers.SelectionChangedEvent;
33
import org.eclipse.jface.viewers.StructuredSelection;
34
import org.eclipse.jface.viewers.TreeViewer;
35
import org.eclipse.swt.dnd.DND;
36
import org.eclipse.swt.dnd.Transfer;
37
import org.eclipse.swt.layout.FillLayout;
38
import org.eclipse.swt.widgets.Composite;
39
import org.eclipse.ui.IMemento;
40

    
41
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
42
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
43
import eu.etaxonomy.cdm.api.service.IFeatureTreeService;
44
import eu.etaxonomy.cdm.model.description.FeatureNode;
45
import eu.etaxonomy.cdm.model.description.FeatureTree;
46
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
47
import eu.etaxonomy.taxeditor.editor.definedterm.TermTransfer;
48
import eu.etaxonomy.taxeditor.featuretree.FeatureNodeTransfer;
49
import eu.etaxonomy.taxeditor.featuretree.FeatureTreeContentProvider;
50
import eu.etaxonomy.taxeditor.featuretree.FeatureTreeLabelProvider;
51
import eu.etaxonomy.taxeditor.model.IContextListener;
52
import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
53
import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
54
import eu.etaxonomy.taxeditor.model.IPartContentHasSupplementalData;
55
import eu.etaxonomy.taxeditor.session.ICdmEntitySession;
56
import eu.etaxonomy.taxeditor.store.CdmStore;
57
import eu.etaxonomy.taxeditor.workbench.part.IE4ViewerPart;
58

    
59
/**
60
 *
61
 * @author pplitzner
62
 * @date 06.06.2017
63
 *
64
 */
65
public class FeatureTreeEditor implements IFeatureTreeEditor, ISelectionChangedListener,
66
        IE4ViewerPart, IPartContentHasDetails, IPartContentHasSupplementalData,
67
        IContextListener, IConversationEnabled, IDirtyMarkable {
68

    
69
    private ConversationHolder conversation;
70

    
71
    private ICdmEntitySession cdmEntitySession;
72

    
73
    @Inject
74
    private ESelectionService selService;
75

    
76
    @Inject
77
    private MDirtyable dirty;
78

    
79
    @Inject
80
    private MPart thisPart;
81

    
82
    private TreeViewer viewer;
83

    
84
    @Inject
85
    public FeatureTreeEditor() {
86
        CdmStore.getContextManager().addContextListener(this);
87
    }
88

    
89
    @PostConstruct
90
    public void createControl(Composite parent, EMenuService menuService){
91
        if (CdmStore.isActive()){
92
            initSession();
93
        }
94
        else{
95
            return;
96
        }
97
        parent.setLayout(new FillLayout());
98
        viewer = new TreeViewer(parent);
99
        viewer.setContentProvider(new FeatureTreeContentProvider());
100
        viewer.setLabelProvider(new FeatureTreeLabelProvider());
101

    
102
        int ops = DND.DROP_COPY | DND.DROP_MOVE;
103
        Transfer[] transfers = new Transfer[] {
104
                FeatureNodeTransfer.getInstance(),
105
                TermTransfer.getInstance(),
106
                LocalSelectionTransfer.getTransfer()};
107
        viewer.addDragSupport(ops, transfers, new FeatureNodeDragListener(viewer));
108
        viewer.addDropSupport(ops, transfers, new FeatureNodeDropAdapter(dirty, this, viewer));
109
        viewer.addSelectionChangedListener(this);
110

    
111
        List<FeatureTree> trees = CdmStore.getService(IFeatureTreeService.class).list(FeatureTree.class, null, null, null, null);
112
        viewer.setInput(trees);
113

    
114
        //create context menu
115
        menuService.registerContextMenu(viewer.getControl(), "eu.etaxonomy.taxeditor.store.popupmenu.featureTreeEditor");
116
    }
117

    
118
    private void initSession(){
119
        if(conversation == null){
120
            conversation = CdmStore.createConversation();
121
        }
122
        if(cdmEntitySession==null){
123
            cdmEntitySession = CdmStore.getCurrentSessionManager().newSession(this, true);
124
        }
125
    }
126

    
127
    private void clearSession() {
128
        if(conversation!=null){
129
            conversation.close();
130
            conversation = null;
131
        }
132
        if(cdmEntitySession != null) {
133
            cdmEntitySession.dispose();
134
            cdmEntitySession = null;
135
        }
136
        dirty.setDirty(false);
137
    }
138

    
139
	public void setDirty(boolean isDirty){
140
	    this.dirty.setDirty(isDirty);
141
	}
142

    
143
	@Override
144
    public boolean isDirty(){
145
	    return dirty.isDirty();
146
	}
147

    
148
	@Override
149
	public void selectionChanged(SelectionChangedEvent event) {
150
		//propagate selection
151
		selService.setSelection(event.getSelection());
152
	}
153

    
154
	@Focus
155
	public void focus(){
156
	    if(viewer!=null){
157
	        viewer.getControl().setFocus();
158
	    }
159
        if(conversation!=null && !conversation.isBound()){
160
            conversation.bind();
161
        }
162
        if(cdmEntitySession != null) {
163
            cdmEntitySession.bind();
164
        }
165
	}
166

    
167
	@Override
168
    public void refresh(){
169
	    viewer.refresh();
170
	}
171

    
172
	public TreeViewer getViewer(){
173
	    return viewer;
174
	}
175

    
176
	@Override
177
	public IStructuredSelection getSelection() {
178
	    return (IStructuredSelection) viewer.getSelection();
179
	}
180

    
181
	@Override
182
	public ConversationHolder getConversationHolder() {
183
	    return conversation;
184
	}
185

    
186
	@Override
187
    @Persist
188
	public void save(IProgressMonitor monitor){
189
        if (!conversation.isBound()) {
190
            conversation.bind();
191
        }
192

    
193
        // commit the conversation and start a new transaction immediately
194
        conversation.commit(true);
195

    
196
        CdmStore.getService(IFeatureTreeService.class).saveOrUpdate(getRootEntities());
197

    
198
        initialiseRootEntities();
199

    
200
        this.setDirty(false);
201
	}
202

    
203
    private void initialiseRootEntities() {
204
        Object[] expandedElements = viewer.getExpandedElements();
205
        viewer.getTree().removeAll();
206
        List<FeatureTree> trees = CdmStore.getService(IFeatureTreeService.class).list(FeatureTree.class, null, null, null, null);
207
        viewer.setInput(trees);
208
        viewer.setExpandedElements(expandedElements);
209
    }
210

    
211
	@PreDestroy
212
	public void dispose(){
213
	    selService.setSelection(null);
214
	    clearSession();
215
	}
216

    
217
    @Override
218
    public ICdmEntitySession getCdmEntitySession() {
219
        return cdmEntitySession;
220
    }
221

    
222
    @Override
223
    public Map<Object, List<String>> getPropertyPathsMap() {
224
        List<String> propertyPaths = Arrays.asList(new String[] {
225
                "children", //$NON-NLS-1$
226
                "feature", //$NON-NLS-1$
227
                "featureTree", //$NON-NLS-1$
228
        });
229
        Map<Object, List<String>> propertyPathMap =
230
                new HashMap<Object, List<String>>();
231
        propertyPathMap.put(FeatureNode.class,propertyPaths);
232
        return propertyPathMap;
233
    }
234

    
235
    @Override
236
    public List<FeatureTree> getRootEntities() {
237
        return (List<FeatureTree>) viewer.getInput();
238
    }
239

    
240
    @Override
241
    public void contextAboutToStop(IMemento memento, IProgressMonitor monitor) {
242
    }
243

    
244
    @Override
245
    public void contextStop(IMemento memento, IProgressMonitor monitor) {
246
        //close view when workbench closes
247
        try{
248
            thisPart.getContext().get(EPartService.class).hidePart(thisPart);
249
        }
250
        catch(Exception e){
251
            //nothing
252
        }
253
    }
254

    
255
    @Override
256
    public void contextStart(IMemento memento, IProgressMonitor monitor) {
257
    }
258

    
259
    @Override
260
    public void contextRefresh(IProgressMonitor monitor) {
261
    }
262

    
263
    @Override
264
    public void workbenchShutdown(IMemento memento, IProgressMonitor monitor) {
265
    }
266

    
267
    @Override
268
    public void update(CdmDataChangeMap arg0) {
269
    }
270

    
271
    @Override
272
    public void changed(Object element) {
273
        dirty.setDirty(true);
274
        viewer.refresh();
275
    }
276

    
277
    @Override
278
    public void forceDirty() {
279
        dirty.setDirty(true);
280
    }
281

    
282
    @Override
283
    public boolean postOperation(Object objectAffectedByOperation) {
284
        initialiseRootEntities();
285
        viewer.refresh();
286
        if(objectAffectedByOperation instanceof FeatureNode){
287
            FeatureNode node = (FeatureNode)objectAffectedByOperation;
288
            viewer.expandToLevel(node.getFeatureTree(), 1);
289
            StructuredSelection selection = new StructuredSelection(objectAffectedByOperation);
290
            viewer.setSelection(selection);
291
        }
292
        return true;
293
    }
294

    
295
    @Override
296
    public boolean onComplete() {
297
        return false;
298
    }
299

    
300
}
(3-3/5)