Project

General

Profile

Download (8.6 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.core.runtime.NullProgressMonitor;
24
import org.eclipse.e4.ui.di.Focus;
25
import org.eclipse.e4.ui.di.Persist;
26
import org.eclipse.e4.ui.model.application.ui.MDirtyable;
27
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
28
import org.eclipse.e4.ui.services.EMenuService;
29
import org.eclipse.e4.ui.workbench.modeling.EPartService;
30
import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
31
import org.eclipse.jface.viewers.ISelectionChangedListener;
32
import org.eclipse.jface.viewers.IStructuredSelection;
33
import org.eclipse.jface.viewers.SelectionChangedEvent;
34
import org.eclipse.jface.viewers.TreeViewer;
35
import org.eclipse.swt.SWT;
36
import org.eclipse.swt.events.ModifyEvent;
37
import org.eclipse.swt.events.ModifyListener;
38
import org.eclipse.swt.events.SelectionAdapter;
39
import org.eclipse.swt.events.SelectionEvent;
40
import org.eclipse.swt.widgets.Composite;
41
import org.eclipse.ui.IMemento;
42

    
43
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
44
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
45
import eu.etaxonomy.cdm.api.service.IFeatureTreeService;
46
import eu.etaxonomy.cdm.model.description.FeatureNode;
47
import eu.etaxonomy.cdm.model.description.FeatureTree;
48
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
49
import eu.etaxonomy.taxeditor.model.AbstractUtility;
50
import eu.etaxonomy.taxeditor.model.IContextListener;
51
import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
52
import eu.etaxonomy.taxeditor.model.IPartContentHasSupplementalData;
53
import eu.etaxonomy.taxeditor.model.MessagingUtils;
54
import eu.etaxonomy.taxeditor.session.ICdmEntitySession;
55
import eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled;
56
import eu.etaxonomy.taxeditor.store.CdmStore;
57
import eu.etaxonomy.taxeditor.ui.dialog.selection.FeatureTreeSelectionDialog;
58
import eu.etaxonomy.taxeditor.workbench.part.IE4SavablePart;
59
import eu.etaxonomy.taxeditor.workbench.part.IE4ViewerPart;
60

    
61
/**
62
 *
63
 * @author pplitzner
64
 * @date 06.06.2017
65
 *
66
 */
67
public class FeatureTreeEditor implements ICdmEntitySessionEnabled, ModifyListener, ISelectionChangedListener,
68
        IE4ViewerPart, IE4SavablePart, IPartContentHasDetails, IPartContentHasSupplementalData, IContextListener, IConversationEnabled {
69

    
70
    private ConversationHolder conversation;
71

    
72
    private ICdmEntitySession cdmEntitySession;
73

    
74
    @Inject
75
    private ESelectionService selService;
76

    
77
    @Inject
78
    private MDirtyable dirty;
79

    
80
    private FeatureTreeEditorComposite composite;
81

    
82
    @Inject
83
    private MPart thisPart;
84

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

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

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

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

    
129
	public void setDirty(boolean isDirty){
130
	    this.dirty.setDirty(isDirty);
131
	}
132

    
133
	public boolean isDirty(){
134
	    return dirty.isDirty();
135
	}
136

    
137
    public FeatureTree getSelectedFeatureTree(){
138
        return composite.getFeatureTree();
139
    }
140

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

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

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

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

    
173
	public TreeViewer getViewer(){
174
	    return composite.getViewer();
175
	}
176

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

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

    
193
	@Override
194
    @Persist
195
	public void save(IProgressMonitor monitor){
196
        if (!conversation.isBound()) {
197
            conversation.bind();
198
        }
199

    
200
        // commit the conversation and start a new transaction immediately
201
        conversation.commit(true);
202

    
203
        CdmStore.getService(IFeatureTreeService.class).saveOrUpdate(composite.getFeatureTree());
204

    
205
        this.setDirty(false);
206
	}
207

    
208
	@PreDestroy
209
	public void dispose(){
210
        if(conversation!=null){
211
            conversation.close();
212
            conversation = null;
213
        }
214
        if(cdmEntitySession != null) {
215
            cdmEntitySession.dispose();
216
            cdmEntitySession = null;
217
        }
218
	}
219

    
220
    @Override
221
    public ICdmEntitySession getCdmEntitySession() {
222
        return cdmEntitySession;
223
    }
224

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

    
238
    /**
239
     * {@inheritDoc}
240
     */
241
    @Override
242
    public List<FeatureTree> getRootEntities() {
243
        List<FeatureTree> root = new ArrayList<>();
244
        root.add(composite.getFeatureTree());
245
        return root;
246
    }
247

    
248
    /**
249
     * {@inheritDoc}
250
     */
251
    @Override
252
    public void contextAboutToStop(IMemento memento, IProgressMonitor monitor) {
253
    }
254

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

    
269
    /**
270
     * {@inheritDoc}
271
     */
272
    @Override
273
    public void contextStart(IMemento memento, IProgressMonitor monitor) {
274
    }
275

    
276
    /**
277
     * {@inheritDoc}
278
     */
279
    @Override
280
    public void contextRefresh(IProgressMonitor monitor) {
281
    }
282

    
283
    /**
284
     * {@inheritDoc}
285
     */
286
    @Override
287
    public void workbenchShutdown(IMemento memento, IProgressMonitor monitor) {
288
    }
289

    
290
    /**
291
     * {@inheritDoc}
292
     */
293
    @Override
294
    public void update(CdmDataChangeMap arg0) {
295
    }
296

    
297
}
(3-3/5)