Project

General

Profile

Download (7.77 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
import javax.inject.Named;
23

    
24
import org.eclipse.core.runtime.IStatus;
25
import org.eclipse.e4.core.di.annotations.Optional;
26
import org.eclipse.e4.ui.di.Focus;
27
import org.eclipse.e4.ui.di.Persist;
28
import org.eclipse.e4.ui.model.application.ui.MDirtyable;
29
import org.eclipse.e4.ui.services.IServiceConstants;
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.wizard.WizardDialog;
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.Shell;
42

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

    
57
/**
58
 *
59
 * @author pplitzner
60
 * @date 06.06.2017
61
 *
62
 */
63
public class FeatureTreeEditor implements ICdmEntitySessionEnabled,
64
		ModifyListener, ISelectionChangedListener {
65

    
66
    private ConversationHolder conversation;
67

    
68
    private ICdmEntitySession cdmEntitySession;
69

    
70
    @Inject
71
    private ESelectionService selService;
72

    
73
    @Inject
74
    private MDirtyable dirty;
75

    
76
    private FeatureTreeEditorComposite composite;
77

    
78
    @Inject
79
    public FeatureTreeEditor() {
80
    }
81

    
82
	/** {@inheritDoc} */
83
    @PostConstruct
84
    public void createControl(Composite parent, @Optional@Named(IServiceConstants.ACTIVE_SHELL) Shell shell){
85
        if (CdmStore.isActive()){
86
            if(conversation == null){
87
                conversation = CdmStore.createConversation();
88
            }
89
            if(cdmEntitySession!=null){
90
                cdmEntitySession = CdmStore.getCurrentSessionManager().newSession(this, true);
91
            }
92
        }
93
        else{
94
            return;
95
        }
96
        composite = new FeatureTreeEditorComposite(parent, SWT.NULL);
97
        composite.init(new FeatureNodeDragListener(composite.getViewer()),
98
                new FeatureNodeDropAdapter(this, composite.getViewer()), this, new SelectionAdapter() {
99
                    @Override
100
                    public void widgetSelected(SelectionEvent e) {
101
                        FeatureTree tree = FeatureTreeSelectionDialog.select(shell, conversation, null);
102
                        if (tree != null) {
103
                            composite.setSelectedTree(tree, FeatureTreeEditor.this);
104
                        }
105
                    }
106
                }, new AddButtonListener(), new RemoveSelectionListener(), new FeatureTreeExportListener(shell, composite));
107
    }
108

    
109
	public void setDirty(boolean isDirty){
110
	    this.dirty.setDirty(isDirty);
111
	}
112

    
113
	public boolean isDirty(){
114
	    return dirty.isDirty();
115
	}
116

    
117
    public FeatureTree getSelectedFeatureTree(){
118
        return composite.getFeatureTree();
119
    }
120

    
121
	/** {@inheritDoc} */
122
	@Override
123
	public void modifyText(ModifyEvent e) {
124
	    composite.getFeatureTree().setTitleCache(composite.getText_title().getText(), true);
125
		setDirty(true);
126
	}
127

    
128
	/** {@inheritDoc} */
129
	@Override
130
	public void selectionChanged(SelectionChangedEvent event) {
131
		IStructuredSelection selection = (IStructuredSelection) event
132
				.getSelection();
133

    
134
		composite.getBtnAdd().setEnabled(selection.size() <= 1);
135
		composite.getBtnRemove().setEnabled(selection.size() > 0);
136
		//propagate selection
137
		selService.setSelection(AbstractUtility.getElementsFromSelectionChangedEvent(event));
138
	}
139

    
140
	@Focus
141
	public void focus(){
142
	    if(composite!=null){
143
	        composite.getViewer().getControl().setFocus();
144
	    }
145
        if(conversation!=null && !conversation.isBound()){
146
            conversation.bind();
147
        }
148
        if(cdmEntitySession != null) {
149
            cdmEntitySession.bind();
150
        }
151
	}
152

    
153
	@Persist
154
	public void save(){
155
        if (!conversation.isBound()) {
156
            conversation.bind();
157
        }
158

    
159
        // commit the conversation and start a new transaction immediately
160
        conversation.commit(true);
161

    
162
        CdmStore.getService(IFeatureTreeService.class).saveOrUpdate(composite.getFeatureTree());
163

    
164
        this.setDirty(false);
165
	}
166

    
167
	@PreDestroy
168
	public void dispose(){
169
        if(conversation!=null){
170
            conversation.close();
171
        }
172
        if(cdmEntitySession != null) {
173
            cdmEntitySession.dispose();
174
        }
175
	}
176

    
177
    @Override
178
    public ICdmEntitySession getCdmEntitySession() {
179
        return cdmEntitySession;
180
    }
181

    
182
    @Override
183
    public Map<Object, List<String>> getPropertyPathsMap() {
184
        List<String> propertyPaths = Arrays.asList(new String[] {
185
                "children", //$NON-NLS-1$
186
                "feature", //$NON-NLS-1$
187
                "featureTree", //$NON-NLS-1$
188
        });
189
        Map<Object, List<String>> propertyPathMap =
190
                new HashMap<Object, List<String>>();
191
        propertyPathMap.put(FeatureNode.class,propertyPaths);
192
        return propertyPathMap;
193
    }
194

    
195
    /**
196
     * {@inheritDoc}
197
     */
198
    @Override
199
    public List<FeatureTree> getRootEntities() {
200
        List<FeatureTree> root = new ArrayList<>();
201
        root.add(composite.getFeatureTree());
202
        return root;
203
    }
204

    
205
	private class AddButtonListener extends SelectionAdapter {
206
		@Override
207
		public void widgetSelected(SelectionEvent e) {
208
			AvailableFeaturesWizard wizard = new AvailableFeaturesWizard();
209
			WizardDialog dialog = new WizardDialog(e.widget.getDisplay().getActiveShell(), wizard);
210

    
211
			if (dialog.open() == IStatus.OK) {
212
                FeatureNode parent = ((FeatureTree) composite.getViewer().getInput()).getRoot();
213
                Collection<Feature> additionalFeatures = wizard.getAdditionalFeatures();
214
                for (Feature feature : additionalFeatures) {
215
                    if(!getSelectedFeatureTree().getDistinctFeatures().contains(feature)){
216
                        CdmStore.getService(IFeatureNodeService.class).addChildFeaturNode(parent, feature);
217
                    }
218
				}
219
				setDirty(true);
220
				composite.getViewer().refresh();
221
				composite.getViewer().expandToLevel(parent, 1);
222
			}
223
		}
224

    
225
	}
226

    
227
	private class RemoveSelectionListener extends SelectionAdapter {
228
		@Override
229
		public void widgetSelected(SelectionEvent e) {
230
			IStructuredSelection selection = (IStructuredSelection) composite.getViewer()
231
					.getSelection();
232

    
233
			for (Object selectedObject : selection.toArray()) {
234
				FeatureNode featureNode = (FeatureNode) selectedObject;
235
				CdmStore.getService(IFeatureNodeService.class).deleteFeatureNode(featureNode.getUuid(), new FeatureNodeDeletionConfigurator());
236

    
237
			}
238
			setDirty(true);
239
			composite.getViewer().refresh();
240
		}
241
	}
242

    
243
}
(3-3/5)