Project

General

Profile

Download (8.07 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.Collection;
15
import java.util.HashMap;
16
import java.util.List;
17
import java.util.Map;
18

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

    
23
import org.eclipse.core.runtime.IStatus;
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.workbench.modeling.ESelectionService;
28
import org.eclipse.jface.viewers.ISelectionChangedListener;
29
import org.eclipse.jface.viewers.IStructuredSelection;
30
import org.eclipse.jface.viewers.SelectionChangedEvent;
31
import org.eclipse.jface.wizard.WizardDialog;
32
import org.eclipse.swt.SWT;
33
import org.eclipse.swt.events.ModifyEvent;
34
import org.eclipse.swt.events.ModifyListener;
35
import org.eclipse.swt.events.SelectionAdapter;
36
import org.eclipse.swt.events.SelectionEvent;
37
import org.eclipse.swt.widgets.Composite;
38

    
39
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
40
import eu.etaxonomy.cdm.api.service.IFeatureNodeService;
41
import eu.etaxonomy.cdm.api.service.IFeatureTreeService;
42
import eu.etaxonomy.cdm.api.service.config.FeatureNodeDeletionConfigurator;
43
import eu.etaxonomy.cdm.model.description.Feature;
44
import eu.etaxonomy.cdm.model.description.FeatureNode;
45
import eu.etaxonomy.cdm.model.description.FeatureTree;
46
import eu.etaxonomy.taxeditor.featuretree.AvailableFeaturesWizard;
47
import eu.etaxonomy.taxeditor.model.AbstractUtility;
48
import eu.etaxonomy.taxeditor.model.MessagingUtils;
49
import eu.etaxonomy.taxeditor.session.ICdmEntitySession;
50
import eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled;
51
import eu.etaxonomy.taxeditor.store.CdmStore;
52
import eu.etaxonomy.taxeditor.ui.dialog.selection.FeatureTreeSelectionDialog;
53

    
54
/**
55
 *
56
 * @author pplitzner
57
 * @date 06.06.2017
58
 *
59
 */
60
public class FeatureTreeEditor implements ICdmEntitySessionEnabled,
61
		ModifyListener, ISelectionChangedListener {
62

    
63
    private ConversationHolder conversation;
64

    
65
    private ICdmEntitySession cdmEntitySession;
66

    
67
    @Inject
68
    private ESelectionService selService;
69

    
70
    @Inject
71
    private MDirtyable dirty;
72

    
73
    private FeatureTreeEditorComposite composite;
74

    
75
    @Inject
76
    public FeatureTreeEditor() {
77
    }
78

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

    
106
                        }
107
                        FeatureTree tree = FeatureTreeSelectionDialog.select(composite.getDisplay().getActiveShell(), conversation, null);
108
                        if (tree != null) {
109
                            composite.setSelectedTree(tree, FeatureTreeEditor.this);
110
                        }
111
                    }
112
                }, new AddButtonListener(), new RemoveSelectionListener(), new FeatureTreeExportListener(composite.getDisplay().getActiveShell(), composite));
113
    }
114

    
115
	public void setDirty(boolean isDirty){
116
	    this.dirty.setDirty(isDirty);
117
	}
118

    
119
	public boolean isDirty(){
120
	    return dirty.isDirty();
121
	}
122

    
123
    public FeatureTree getSelectedFeatureTree(){
124
        return composite.getFeatureTree();
125
    }
126

    
127
	/** {@inheritDoc} */
128
	@Override
129
	public void modifyText(ModifyEvent e) {
130
	    composite.getFeatureTree().setTitleCache(composite.getText_title().getText(), true);
131
		setDirty(true);
132
	}
133

    
134
	/** {@inheritDoc} */
135
	@Override
136
	public void selectionChanged(SelectionChangedEvent event) {
137
		IStructuredSelection selection = (IStructuredSelection) event
138
				.getSelection();
139

    
140
		composite.getBtnAdd().setEnabled(selection.size() <= 1);
141
		composite.getBtnRemove().setEnabled(selection.size() > 0);
142
		//propagate selection
143
		selService.setSelection(AbstractUtility.getElementsFromSelectionChangedEvent(event));
144
	}
145

    
146
	@Focus
147
	public void focus(){
148
	    if(composite!=null){
149
	        composite.getViewer().getControl().setFocus();
150
	    }
151
        if(conversation!=null && !conversation.isBound()){
152
            conversation.bind();
153
        }
154
        if(cdmEntitySession != null) {
155
            cdmEntitySession.bind();
156
        }
157
	}
158

    
159
	@Persist
160
	public void save(){
161
        if (!conversation.isBound()) {
162
            conversation.bind();
163
        }
164

    
165
        // commit the conversation and start a new transaction immediately
166
        conversation.commit(true);
167

    
168
        CdmStore.getService(IFeatureTreeService.class).saveOrUpdate(composite.getFeatureTree());
169

    
170
        this.setDirty(false);
171
	}
172

    
173
	@PreDestroy
174
	public void dispose(){
175
        if(conversation!=null){
176
            conversation.close();
177
        }
178
        if(cdmEntitySession != null) {
179
            cdmEntitySession.dispose();
180
        }
181
	}
182

    
183
    @Override
184
    public ICdmEntitySession getCdmEntitySession() {
185
        return cdmEntitySession;
186
    }
187

    
188
    @Override
189
    public Map<Object, List<String>> getPropertyPathsMap() {
190
        List<String> propertyPaths = Arrays.asList(new String[] {
191
                "children", //$NON-NLS-1$
192
                "feature", //$NON-NLS-1$
193
                "featureTree", //$NON-NLS-1$
194
        });
195
        Map<Object, List<String>> propertyPathMap =
196
                new HashMap<Object, List<String>>();
197
        propertyPathMap.put(FeatureNode.class,propertyPaths);
198
        return propertyPathMap;
199
    }
200

    
201
    /**
202
     * {@inheritDoc}
203
     */
204
    @Override
205
    public List<FeatureTree> getRootEntities() {
206
        List<FeatureTree> root = new ArrayList<>();
207
        root.add(composite.getFeatureTree());
208
        return root;
209
    }
210

    
211
	private class AddButtonListener extends SelectionAdapter {
212
		@Override
213
		public void widgetSelected(SelectionEvent e) {
214
			AvailableFeaturesWizard wizard = new AvailableFeaturesWizard();
215
			WizardDialog dialog = new WizardDialog(e.widget.getDisplay().getActiveShell(), wizard);
216

    
217
			if (dialog.open() == IStatus.OK) {
218
                FeatureNode parent = ((FeatureTree) composite.getViewer().getInput()).getRoot();
219
                Collection<Feature> additionalFeatures = wizard.getAdditionalFeatures();
220
                for (Feature feature : additionalFeatures) {
221
                    if(!getSelectedFeatureTree().getDistinctFeatures().contains(feature)){
222
                        CdmStore.getService(IFeatureNodeService.class).addChildFeatureNode(parent.getUuid(), feature.getUuid());
223
                    }
224
				}
225
				setDirty(true);
226
				composite.getViewer().refresh();
227
				composite.getViewer().expandToLevel(parent, 1);
228
			}
229
		}
230

    
231
	}
232

    
233
	private class RemoveSelectionListener extends SelectionAdapter {
234
		@Override
235
		public void widgetSelected(SelectionEvent e) {
236
			IStructuredSelection selection = (IStructuredSelection) composite.getViewer()
237
					.getSelection();
238

    
239
			for (Object selectedObject : selection.toArray()) {
240
				FeatureNode featureNode = (FeatureNode) selectedObject;
241
				CdmStore.getService(IFeatureNodeService.class).deleteFeatureNode(featureNode.getUuid(), new FeatureNodeDeletionConfigurator());
242

    
243
			}
244
			setDirty(true);
245
			composite.getViewer().refresh();
246
		}
247
	}
248

    
249
}
(3-3/5)