Project

General

Profile

Download (10.7 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.di.UISynchronize;
25
import org.eclipse.e4.ui.model.application.ui.MDirtyable;
26
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
27
import org.eclipse.e4.ui.services.EMenuService;
28
import org.eclipse.e4.ui.workbench.modeling.EPartService;
29
import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
30
import org.eclipse.jface.util.LocalSelectionTransfer;
31
import org.eclipse.jface.viewers.ISelection;
32
import org.eclipse.jface.viewers.ISelectionChangedListener;
33
import org.eclipse.jface.viewers.IStructuredSelection;
34
import org.eclipse.jface.viewers.SelectionChangedEvent;
35
import org.eclipse.jface.viewers.StructuredSelection;
36
import org.eclipse.jface.viewers.TreeViewer;
37
import org.eclipse.swt.SWT;
38
import org.eclipse.swt.dnd.DND;
39
import org.eclipse.swt.dnd.Transfer;
40
import org.eclipse.swt.events.KeyAdapter;
41
import org.eclipse.swt.events.KeyEvent;
42
import org.eclipse.swt.layout.FillLayout;
43
import org.eclipse.swt.widgets.Composite;
44
import org.eclipse.ui.IMemento;
45

    
46
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
47
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
48
import eu.etaxonomy.cdm.api.service.IFeatureTreeService;
49
import eu.etaxonomy.cdm.model.description.FeatureNode;
50
import eu.etaxonomy.cdm.model.description.FeatureTree;
51
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
52
import eu.etaxonomy.taxeditor.editor.definedterm.TermTransfer;
53
import eu.etaxonomy.taxeditor.featuretree.FeatureNodeTransfer;
54
import eu.etaxonomy.taxeditor.featuretree.FeatureTreeContentProvider;
55
import eu.etaxonomy.taxeditor.featuretree.FeatureTreeLabelProvider;
56
import eu.etaxonomy.taxeditor.featuretree.e4.operation.AddFeatureOperation;
57
import eu.etaxonomy.taxeditor.model.AbstractUtility;
58
import eu.etaxonomy.taxeditor.model.IContextListener;
59
import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
60
import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
61
import eu.etaxonomy.taxeditor.model.IPartContentHasSupplementalData;
62
import eu.etaxonomy.taxeditor.session.ICdmEntitySession;
63
import eu.etaxonomy.taxeditor.store.AppModelId;
64
import eu.etaxonomy.taxeditor.store.CdmStore;
65
import eu.etaxonomy.taxeditor.store.StoreUtil;
66
import eu.etaxonomy.taxeditor.workbench.part.IE4ViewerPart;
67

    
68
/**
69
 *
70
 * @author pplitzner
71
 * @date 06.06.2017
72
 *
73
 */
74
public class FeatureTreeEditor implements IFeatureTreeEditor, ISelectionChangedListener,
75
        IE4ViewerPart, IPartContentHasDetails, IPartContentHasSupplementalData,
76
        IContextListener, IConversationEnabled, IDirtyMarkable {
77

    
78
    private ConversationHolder conversation;
79

    
80
    private ICdmEntitySession cdmEntitySession;
81

    
82
    @Inject
83
    private ESelectionService selService;
84

    
85
    @Inject
86
    private MDirtyable dirty;
87

    
88
    @Inject
89
    private UISynchronize sync;
90

    
91
    @Inject
92
    private MPart thisPart;
93

    
94
    private TreeViewer viewer;
95

    
96
    @Inject
97
    public FeatureTreeEditor() {
98
        CdmStore.getContextManager().addContextListener(this);
99
    }
100

    
101
    @PostConstruct
102
    public void createControl(Composite parent, EMenuService menuService){
103
        if (CdmStore.isActive()){
104
            initSession();
105
        }
106
        else{
107
            return;
108
        }
109
        parent.setLayout(new FillLayout());
110
        viewer = new TreeViewer(parent);
111
        viewer.setContentProvider(new FeatureTreeContentProvider());
112
        viewer.setLabelProvider(new FeatureTreeLabelProvider());
113

    
114
        int ops = DND.DROP_COPY | DND.DROP_MOVE;
115
        Transfer[] transfers = new Transfer[] {
116
                FeatureNodeTransfer.getInstance(),
117
                TermTransfer.getInstance(),
118
                LocalSelectionTransfer.getTransfer()};
119
        viewer.addDragSupport(ops, transfers, new FeatureNodeDragListener(viewer));
120
        viewer.addDropSupport(ops, transfers, new FeatureTreeDropAdapter(this, viewer, sync));
121
        viewer.addSelectionChangedListener(this);
122
        viewer.getTree().addKeyListener(new KeyAdapter() {
123
            @Override
124
            public void keyPressed(KeyEvent e) {
125
                if(e.stateMask == SWT.MOD1 && e.keyCode == 'c'){
126
                    copy(viewer.getStructuredSelection());
127
                }
128
                else if(e.stateMask == SWT.MOD1 && e.keyCode == 'v'){
129
                    paste(viewer.getStructuredSelection());
130
                }
131
            }
132
        });
133

    
134
        List<FeatureTree> trees = CdmStore.getService(IFeatureTreeService.class).list(FeatureTree.class, null, null, null, null);
135
        viewer.setInput(trees);
136

    
137
        //create context menu
138
        menuService.registerContextMenu(viewer.getControl(), AppModelId.POPUPMENU_EU_ETAXONOMY_TAXEDITOR_STORE_POPUPMENU_FEATURETREEEDITOR);
139
    }
140

    
141
    public void paste(IStructuredSelection selection) {
142
        if (StoreUtil.promptCheckIsDirty(this)) {
143
            return;
144
        }
145

    
146
        ISelection clipBoardSelection = LocalSelectionTransfer.getTransfer().getSelection();
147
        Object firstElement = selection.getFirstElement();
148
        FeatureNode parentNode = null;
149
        if(firstElement instanceof FeatureNode){
150
            parentNode = (FeatureNode) firstElement;
151
        }
152
        else if(firstElement instanceof FeatureTree){
153
            parentNode = ((FeatureTree)firstElement).getRoot();
154
        }
155
        if(parentNode!=null){
156
            FeatureNode copiedNode = (FeatureNode) ((IStructuredSelection)clipBoardSelection).getFirstElement();
157

    
158
            AddFeatureOperation operation = new AddFeatureOperation(copiedNode.getFeature(), parentNode, this, this);
159
            AbstractUtility.executeOperation(operation, sync);
160
        }
161

    
162
    }
163

    
164
    public void copy(IStructuredSelection selection) {
165
        LocalSelectionTransfer.getTransfer().setSelection(selection);
166
    }
167

    
168
    private void initSession(){
169
        if(conversation == null){
170
            conversation = CdmStore.createConversation();
171
        }
172
        if(cdmEntitySession==null){
173
            cdmEntitySession = CdmStore.getCurrentSessionManager().newSession(this, true);
174
        }
175
    }
176

    
177
    private void clearSession() {
178
        if(conversation!=null){
179
            conversation.close();
180
            conversation = null;
181
        }
182
        if(cdmEntitySession != null) {
183
            cdmEntitySession.dispose();
184
            cdmEntitySession = null;
185
        }
186
        dirty.setDirty(false);
187
    }
188

    
189
	public void setDirty(boolean isDirty){
190
	    this.dirty.setDirty(isDirty);
191
	}
192

    
193
	@Override
194
    public boolean isDirty(){
195
	    return dirty.isDirty();
196
	}
197

    
198
	@Override
199
	public void selectionChanged(SelectionChangedEvent event) {
200
		//propagate selection
201
		selService.setSelection(event.getSelection());
202
	}
203

    
204
	@Focus
205
	public void focus(){
206
	    if(viewer!=null){
207
	        viewer.getControl().setFocus();
208
	    }
209
        if(conversation!=null && !conversation.isBound()){
210
            conversation.bind();
211
        }
212
        if(cdmEntitySession != null) {
213
            cdmEntitySession.bind();
214
        }
215
	}
216

    
217
	@Override
218
    public void refresh(){
219
	    viewer.refresh();
220
	}
221

    
222
	public TreeViewer getViewer(){
223
	    return viewer;
224
	}
225

    
226
	@Override
227
	public IStructuredSelection getSelection() {
228
	    return (IStructuredSelection) viewer.getSelection();
229
	}
230

    
231
	@Override
232
	public ConversationHolder getConversationHolder() {
233
	    return conversation;
234
	}
235

    
236
	@Override
237
    @Persist
238
	public void save(IProgressMonitor monitor){
239
        if (!conversation.isBound()) {
240
            conversation.bind();
241
        }
242

    
243
        // commit the conversation and start a new transaction immediately
244
        conversation.commit(true);
245

    
246
        CdmStore.getService(IFeatureTreeService.class).saveOrUpdate(getRootEntities());
247

    
248
        initializeTrees();
249

    
250
        this.setDirty(false);
251
	}
252

    
253
    private void initializeTrees() {
254
        Object[] expandedElements = viewer.getExpandedElements();
255
        viewer.getTree().removeAll();
256
        List<FeatureTree> trees = CdmStore.getService(IFeatureTreeService.class).list(FeatureTree.class, null, null, null, null);
257
        viewer.setInput(trees);
258
        viewer.setExpandedElements(expandedElements);
259
    }
260

    
261
	@PreDestroy
262
	public void dispose(){
263
	    selService.setSelection(null);
264
	    clearSession();
265
	}
266

    
267
    @Override
268
    public ICdmEntitySession getCdmEntitySession() {
269
        return cdmEntitySession;
270
    }
271

    
272
    @Override
273
    public Map<Object, List<String>> getPropertyPathsMap() {
274
        List<String> propertyPaths = Arrays.asList(new String[] {
275
                "children", //$NON-NLS-1$
276
                "feature", //$NON-NLS-1$
277
                "featureTree", //$NON-NLS-1$
278
        });
279
        Map<Object, List<String>> propertyPathMap =
280
                new HashMap<Object, List<String>>();
281
        propertyPathMap.put(FeatureNode.class,propertyPaths);
282
        return propertyPathMap;
283
    }
284

    
285
    @Override
286
    public List<FeatureTree> getRootEntities() {
287
        return (List<FeatureTree>) viewer.getInput();
288
    }
289

    
290
    @Override
291
    public void contextAboutToStop(IMemento memento, IProgressMonitor monitor) {
292
    }
293

    
294
    @Override
295
    public void contextStop(IMemento memento, IProgressMonitor monitor) {
296
        //close view when workbench closes
297
        try{
298
            thisPart.getContext().get(EPartService.class).hidePart(thisPart);
299
        }
300
        catch(Exception e){
301
            //nothing
302
        }
303
    }
304

    
305
    @Override
306
    public void contextStart(IMemento memento, IProgressMonitor monitor) {
307
    }
308

    
309
    @Override
310
    public void contextRefresh(IProgressMonitor monitor) {
311
    }
312

    
313
    @Override
314
    public void workbenchShutdown(IMemento memento, IProgressMonitor monitor) {
315
    }
316

    
317
    @Override
318
    public void update(CdmDataChangeMap arg0) {
319
    }
320

    
321
    @Override
322
    public void changed(Object element) {
323
        dirty.setDirty(true);
324
        viewer.refresh();
325
    }
326

    
327
    @Override
328
    public void forceDirty() {
329
        dirty.setDirty(true);
330
    }
331

    
332
    @Override
333
    public boolean postOperation(Object objectAffectedByOperation) {
334
        initializeTrees();
335
        viewer.refresh();
336
        if(objectAffectedByOperation instanceof FeatureNode){
337
            FeatureNode node = (FeatureNode)objectAffectedByOperation;
338
            viewer.expandToLevel(node.getFeatureTree(), 1);
339
            StructuredSelection selection = new StructuredSelection(objectAffectedByOperation);
340
            viewer.setSelection(selection);
341
        }
342
        return true;
343
    }
344

    
345
    @Override
346
    public boolean onComplete() {
347
        return false;
348
    }
349

    
350
}
(3-3/5)