Project

General

Profile

Download (8.65 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.swt.widgets.Text;
42
import org.eclipse.ui.IMemento;
43

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

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

    
71
    private ConversationHolder conversation;
72

    
73
    private ICdmEntitySession cdmEntitySession;
74

    
75
    @Inject
76
    private ESelectionService selService;
77

    
78
    @Inject
79
    private MDirtyable dirty;
80

    
81
    private FeatureTreeEditorComposite composite;
82

    
83
    @Inject
84
    private MPart thisPart;
85

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

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

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

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

    
131
	public void setDirty(boolean isDirty){
132
	    this.dirty.setDirty(isDirty);
133
	}
134

    
135
	public boolean isDirty(){
136
	    return dirty.isDirty();
137
	}
138

    
139
    public FeatureTree getSelectedFeatureTree(){
140
        return composite.getFeatureTree();
141
    }
142

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

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

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

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

    
175
	public TreeViewer getViewer(){
176
	    return composite.getViewer();
177
	}
178

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

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

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

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

    
205
        CdmStore.getService(IFeatureTreeService.class).saveOrUpdate(composite.getFeatureTree());
206

    
207
        this.setDirty(false);
208
	}
209

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

    
222
    @Override
223
    public ICdmEntitySession getCdmEntitySession() {
224
        return cdmEntitySession;
225
    }
226

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

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

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

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

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

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

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

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

    
299
}
(3-3/5)