Project

General

Profile

Download (8.35 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.ArrayList;
13
import java.util.Arrays;
14
import java.util.HashMap;
15
import java.util.List;
16
import java.util.Map;
17

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

    
22
import org.eclipse.core.runtime.IProgressMonitor;
23
import org.eclipse.e4.ui.di.Focus;
24
import org.eclipse.e4.ui.di.Persist;
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.viewers.ISelectionChangedListener;
31
import org.eclipse.jface.viewers.IStructuredSelection;
32
import org.eclipse.jface.viewers.SelectionChangedEvent;
33
import org.eclipse.jface.viewers.TreeViewer;
34
import org.eclipse.swt.SWT;
35
import org.eclipse.swt.events.ModifyEvent;
36
import org.eclipse.swt.events.ModifyListener;
37
import org.eclipse.swt.events.SelectionAdapter;
38
import org.eclipse.swt.events.SelectionEvent;
39
import org.eclipse.swt.widgets.Composite;
40
import org.eclipse.ui.IMemento;
41

    
42
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
43
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
44
import eu.etaxonomy.cdm.api.service.IFeatureTreeService;
45
import eu.etaxonomy.cdm.model.description.FeatureNode;
46
import eu.etaxonomy.cdm.model.description.FeatureTree;
47
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
48
import eu.etaxonomy.taxeditor.model.AbstractUtility;
49
import eu.etaxonomy.taxeditor.model.IContextListener;
50
import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
51
import eu.etaxonomy.taxeditor.model.IPartContentHasSupplementalData;
52
import eu.etaxonomy.taxeditor.model.MessagingUtils;
53
import eu.etaxonomy.taxeditor.session.ICdmEntitySession;
54
import eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled;
55
import eu.etaxonomy.taxeditor.store.CdmStore;
56
import eu.etaxonomy.taxeditor.ui.dialog.selection.FeatureTreeSelectionDialog;
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 ICdmEntitySessionEnabled, ModifyListener, ISelectionChangedListener,
66
        IE4ViewerPart, IPartContentHasDetails, IPartContentHasSupplementalData, IContextListener, IConversationEnabled {
67

    
68
    private ConversationHolder conversation;
69

    
70
    private ICdmEntitySession cdmEntitySession;
71

    
72
    @Inject
73
    private ESelectionService selService;
74

    
75
    @Inject
76
    private MDirtyable dirty;
77

    
78
    private FeatureTreeEditorComposite composite;
79

    
80
    @Inject
81
    private MPart thisPart;
82

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

    
88
	/** {@inheritDoc} */
89
    @PostConstruct
90
    public void createControl(Composite parent, EMenuService menuService){
91
        if (CdmStore.isActive()){
92
            if(conversation == null){
93
                conversation = CdmStore.createConversation();
94
            }
95
            if(cdmEntitySession!=null){
96
                cdmEntitySession = CdmStore.getCurrentSessionManager().newSession(this, true);
97
            }
98
        }
99
        else{
100
            return;
101
        }
102
        composite = new FeatureTreeEditorComposite(parent, SWT.NULL);
103
        composite.init(new FeatureNodeDragListener(composite.getViewer()),
104
                new FeatureNodeDropAdapter(dirty, composite.getViewer()), this, new SelectionAdapter() {
105
                    @Override
106
                    public void widgetSelected(SelectionEvent e) {
107
                        if(isDirty()){
108
                            if(MessagingUtils.confirmDialog("Editor has to be saved", "You have to save before loading another feature tree. Save now?")){
109
                                save();
110
                            }
111
                            else{
112
                                return;
113
                            }
114

    
115
                        }
116
                        FeatureTree tree = FeatureTreeSelectionDialog.select(composite.getDisplay().getActiveShell(), conversation, null);
117
                        if (tree != null) {
118
                            composite.setSelectedTree(tree, FeatureTreeEditor.this);
119
                        }
120
                    }
121
                });
122

    
123
        //create context menu
124
        menuService.registerContextMenu(composite.getViewer().getControl(), "eu.etaxonomy.taxeditor.store.popupmenu.featureTreeEditor");
125
    }
126

    
127
	public void setDirty(boolean isDirty){
128
	    this.dirty.setDirty(isDirty);
129
	}
130

    
131
	public boolean isDirty(){
132
	    return dirty.isDirty();
133
	}
134

    
135
    public FeatureTree getSelectedFeatureTree(){
136
        return composite.getFeatureTree();
137
    }
138

    
139
	/** {@inheritDoc} */
140
	@Override
141
	public void modifyText(ModifyEvent e) {
142
	    composite.getFeatureTree().setTitleCache(composite.getText_title().getText(), true);
143
		setDirty(true);
144
	}
145

    
146
	/** {@inheritDoc} */
147
	@Override
148
	public void selectionChanged(SelectionChangedEvent event) {
149
		//propagate selection
150
		selService.setSelection(AbstractUtility.getElementsFromSelectionChangedEvent(event));
151
	}
152

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

    
166
	@Override
167
    public void refresh(){
168
	    composite.getViewer().refresh();
169
	}
170

    
171
	public TreeViewer getViewer(){
172
	    return composite.getViewer();
173
	}
174

    
175
	/**
176
	 * {@inheritDoc}
177
	 */
178
	@Override
179
	public IStructuredSelection getSelection() {
180
	    return (IStructuredSelection) composite.getViewer().getSelection();
181
	}
182

    
183
	/**
184
	 * {@inheritDoc}
185
	 */
186
	@Override
187
	public ConversationHolder getConversationHolder() {
188
	    return conversation;
189
	}
190

    
191
	@Persist
192
	public void save(){
193
        if (!conversation.isBound()) {
194
            conversation.bind();
195
        }
196

    
197
        // commit the conversation and start a new transaction immediately
198
        conversation.commit(true);
199

    
200
        CdmStore.getService(IFeatureTreeService.class).saveOrUpdate(composite.getFeatureTree());
201

    
202
        this.setDirty(false);
203
	}
204

    
205
	@PreDestroy
206
	public void dispose(){
207
        if(conversation!=null){
208
            conversation.close();
209
        }
210
        if(cdmEntitySession != null) {
211
            cdmEntitySession.dispose();
212
        }
213
	}
214

    
215
    @Override
216
    public ICdmEntitySession getCdmEntitySession() {
217
        return cdmEntitySession;
218
    }
219

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

    
233
    /**
234
     * {@inheritDoc}
235
     */
236
    @Override
237
    public List<FeatureTree> getRootEntities() {
238
        List<FeatureTree> root = new ArrayList<>();
239
        root.add(composite.getFeatureTree());
240
        return root;
241
    }
242

    
243
    /**
244
     * {@inheritDoc}
245
     */
246
    @Override
247
    public void contextAboutToStop(IMemento memento, IProgressMonitor monitor) {
248
    }
249

    
250
    /**
251
     * {@inheritDoc}
252
     */
253
    @Override
254
    public void contextStop(IMemento memento, IProgressMonitor monitor) {
255
        //close view when workbench closes
256
        try{
257
            thisPart.getContext().get(EPartService.class).hidePart(thisPart);
258
        }
259
        catch(Exception e){
260
            //nothing
261
        }
262
    }
263

    
264
    /**
265
     * {@inheritDoc}
266
     */
267
    @Override
268
    public void contextStart(IMemento memento, IProgressMonitor monitor) {
269
    }
270

    
271
    /**
272
     * {@inheritDoc}
273
     */
274
    @Override
275
    public void contextRefresh(IProgressMonitor monitor) {
276
    }
277

    
278
    /**
279
     * {@inheritDoc}
280
     */
281
    @Override
282
    public void workbenchShutdown(IMemento memento, IProgressMonitor monitor) {
283
    }
284

    
285
    /**
286
     * {@inheritDoc}
287
     */
288
    @Override
289
    public void update(CdmDataChangeMap arg0) {
290
    }
291

    
292
}
(3-3/5)