Project

General

Profile

Download (11.9 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
* Copyright (C) 2017 EDIT
4
* European Distributed Institute of Taxonomy
5
* http://www.e-taxonomy.eu
6
*
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
9
*/
10
package eu.etaxonomy.taxeditor.editor.workingSet;
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
import javax.inject.Named;
22

    
23
import org.eclipse.e4.core.di.annotations.Optional;
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.services.IServiceConstants;
28
import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
29
import org.eclipse.jface.viewers.ISelection;
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.TreeSelection;
34
import org.eclipse.swt.SWT;
35
import org.eclipse.swt.custom.SashForm;
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.layout.GridData;
41
import org.eclipse.swt.layout.GridLayout;
42
import org.eclipse.swt.widgets.Button;
43
import org.eclipse.swt.widgets.Composite;
44
import org.eclipse.swt.widgets.Display;
45
import org.eclipse.swt.widgets.Shell;
46
import org.eclipse.ui.forms.widgets.FormToolkit;
47

    
48
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
49
import eu.etaxonomy.cdm.api.service.IFeatureNodeService;
50
import eu.etaxonomy.cdm.api.service.IFeatureTreeService;
51
import eu.etaxonomy.cdm.model.description.Character;
52
import eu.etaxonomy.cdm.model.description.FeatureNode;
53
import eu.etaxonomy.cdm.model.description.FeatureTree;
54
import eu.etaxonomy.taxeditor.featuretree.FeatureNodeDragListener;
55
import eu.etaxonomy.taxeditor.featuretree.FeatureNodeDropAdapter;
56
import eu.etaxonomy.taxeditor.featuretree.e4.FeatureTreeEditorComposite;
57
import eu.etaxonomy.taxeditor.model.MessagingUtils;
58
import eu.etaxonomy.taxeditor.session.ICdmEntitySession;
59
import eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled;
60
import eu.etaxonomy.taxeditor.store.CdmStore;
61
import eu.etaxonomy.taxeditor.ui.dialog.selection.FeatureTreeSelectionDialog;
62

    
63
/**
64
 * @author pplitzner
65
 * @date 24.05.2017
66
 *
67
 */
68
public class CharacterEditor implements ICdmEntitySessionEnabled, ISelectionChangedListener, ModifyListener{
69

    
70

    
71
    private FeatureTreeEditorComposite characterTreeEditorComposite;
72
    private FeatureTreeEditorComposite treeViewerProperties;
73
    private FeatureTreeEditorComposite treeViewerStructures;
74
    private final FormToolkit formToolkit = new FormToolkit(Display.getDefault());
75
    private Button btnAddCharacter;
76

    
77
    @Inject
78
    private ESelectionService selService;
79

    
80
    @Inject
81
    private MDirtyable dirty;
82

    
83
    private Shell shell;
84

    
85
    private ConversationHolder conversation;
86

    
87
    private ICdmEntitySession cdmEntitySession;
88
    private Composite composite;
89

    
90
    public CharacterEditor() {
91
    }
92

    
93
    /**
94
     * Create contents of the view part.
95
     */
96
    @PostConstruct
97
    public void createControls(Composite parent) {
98
        if (CdmStore.isActive()){
99
            if(conversation == null){
100
                conversation = CdmStore.createConversation();
101
            }
102
            if(cdmEntitySession!=null){
103
                cdmEntitySession = CdmStore.getCurrentSessionManager().newSession(this, true);
104
            }
105
        }
106
        else{
107
            return;
108
        }
109
        parent.setLayout(new GridLayout(1, false));
110

    
111
        SashForm sashForm = new SashForm(parent, SWT.NONE);
112
        sashForm.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
113
        formToolkit.adapt(sashForm);
114
        formToolkit.paintBordersFor(sashForm);
115

    
116
        composite = new Composite(sashForm, SWT.NONE);
117
        formToolkit.adapt(composite);
118
        formToolkit.paintBordersFor(composite);
119
        composite.setLayout(new GridLayout(1, false));
120

    
121
        treeViewerStructures = addFeatureTreeEditor(composite);
122
        initFeatureTreeComposite(treeViewerStructures, null, null);
123

    
124
        Composite composite_1 = new Composite(sashForm, SWT.NONE);
125
        formToolkit.adapt(composite_1);
126
        formToolkit.paintBordersFor(composite_1);
127
        composite_1.setLayout(new GridLayout(1, false));
128

    
129
        treeViewerProperties = addFeatureTreeEditor(composite_1);
130
        initFeatureTreeComposite(treeViewerProperties, null, null);
131
        //TODO: fix drag and drop
132
//        initFeatureTreeComposite(treeViewerProperties, new CharacterDragListener(treeViewerStructures.getViewer(), treeViewerProperties.getViewer()), null);
133

    
134
        Composite composite_2 = new Composite(sashForm, SWT.NONE);
135
        formToolkit.adapt(composite_2);
136
        formToolkit.paintBordersFor(composite_2);
137
        composite_2.setLayout(new GridLayout(1, false));
138

    
139
        btnAddCharacter = new Button(composite_2, SWT.NONE);
140
        btnAddCharacter.setText(">>");
141
        btnAddCharacter.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, true, 1, 1));
142
        formToolkit.adapt(btnAddCharacter, true, true);
143

    
144
        Composite composite_3 = new Composite(sashForm, SWT.NONE);
145
        formToolkit.adapt(composite_3);
146
        formToolkit.paintBordersFor(composite_3);
147
        composite_3.setLayout(new GridLayout(1, false));
148

    
149
        characterTreeEditorComposite = addFeatureTreeEditor(composite_3);
150
        initFeatureTreeComposite(characterTreeEditorComposite, new FeatureNodeDragListener(characterTreeEditorComposite.getViewer()), new FeatureNodeDropAdapter(characterTreeEditorComposite.getViewer()));
151

    
152
        sashForm.setWeights(new int[] {3, 3, 1, 3});
153

    
154
        init();
155
    }
156

    
157
    private FeatureTreeEditorComposite addFeatureTreeEditor(Composite composite_3) {
158
        FeatureTreeEditorComposite featureTreeEditorComposite = new FeatureTreeEditorComposite(composite_3, SWT.BORDER);
159
        featureTreeEditorComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
160
        featureTreeEditorComposite.getComposite_buttons().setVisible(false);
161
        featureTreeEditorComposite.getText_title().setEnabled(false);
162
        return featureTreeEditorComposite;
163
    }
164

    
165
    private void initFeatureTreeComposite(FeatureTreeEditorComposite featureTreeEditorComposite,
166
            FeatureNodeDragListener featureNodeDragListener, FeatureNodeDropAdapter featureNodeDropAdapter) {
167
        featureTreeEditorComposite.init(featureNodeDragListener, featureNodeDropAdapter, this,
168
                new FeatureTreeChooserListener(featureTreeEditorComposite), new SelectionAdapter() {
169
                }, new SelectionAdapter() {
170
                }, new SelectionAdapter() {
171
                });
172
    }
173

    
174
    @Focus
175
    public void focus(@Optional@Named(IServiceConstants.ACTIVE_SHELL)Shell shell){
176
        this.shell = shell;
177
        if(characterTreeEditorComposite!=null){
178
            characterTreeEditorComposite.getViewer().getControl().setFocus();
179
        }
180
        if(conversation!=null && !conversation.isBound()){
181
            conversation.bind();
182
        }
183
        if(cdmEntitySession != null) {
184
            cdmEntitySession.bind();
185
        }
186
    }
187

    
188
    private void init(){
189
        btnAddCharacter.addSelectionListener(new SelectionAdapter() {
190
            /**
191
             * {@inheritDoc}
192
             */
193
            @Override
194
            public void widgetSelected(SelectionEvent e) {
195
                ISelection structureTreeSelection = treeViewerStructures.getViewer().getSelection();
196
                ISelection propertyTreeSelection = treeViewerProperties.getViewer().getSelection();
197
                if(structureTreeSelection==null || propertyTreeSelection==null || characterTreeEditorComposite.getFeatureTree()==null){
198
                    MessagingUtils.warningDialog("Cannot perform action", CharacterEditor.this,
199
                            "You have to select a structure, a property and a feature tree to perform this action.");
200
                    return;
201
                }
202
                //get selected structure and property
203
                FeatureNode structureNode = (FeatureNode) ((TreeSelection)structureTreeSelection).getFirstElement();
204
                FeatureNode propertyNode = (FeatureNode) ((TreeSelection)propertyTreeSelection).getFirstElement();
205
                //create new Character
206
                String label = structureNode.getFeature().toString()+" "+propertyNode.getFeature().toString();
207
                Character character = Character.NewInstance(structureNode, propertyNode, null, label, label);
208

    
209
                //add new Character to feature tree
210
                FeatureNode parent = ((FeatureTree) characterTreeEditorComposite.getViewer().getInput()).getRoot();
211
                CdmStore.getService(IFeatureNodeService.class).addChildFeaturNode(parent.getUuid(), character.getUuid());
212

    
213
                setDirty(true);
214
                characterTreeEditorComposite.getViewer().refresh();
215
            }
216
        });
217
    }
218

    
219
    @Persist
220
    public void save(){
221
        if (!conversation.isBound()) {
222
            conversation.bind();
223
        }
224

    
225
        // commit the conversation and start a new transaction immediately
226
        conversation.commit(true);
227

    
228
        CdmStore.getService(IFeatureTreeService.class).saveOrUpdate(characterTreeEditorComposite.getFeatureTree());
229

    
230
        this.setDirty(false);
231
    }
232

    
233
    @PreDestroy
234
    public void dispose(){
235
        if(conversation!=null){
236
            conversation.close();
237
        }
238
        if(cdmEntitySession != null) {
239
            cdmEntitySession.dispose();
240
        }
241
    }
242

    
243
    @Override
244
    public Map<Object, List<String>> getPropertyPathsMap() {
245
        List<String> propertyPaths = Arrays.asList(new String[] {
246
                "children", //$NON-NLS-1$
247
                "feature", //$NON-NLS-1$
248
                "featureTree", //$NON-NLS-1$
249
        });
250
        Map<Object, List<String>> propertyPathMap =
251
                new HashMap<Object, List<String>>();
252
        propertyPathMap.put(FeatureNode.class,propertyPaths);
253
        return propertyPathMap;
254
    }
255

    
256
    /**
257
     * {@inheritDoc}
258
     */
259
    @Override
260
    public List<FeatureTree> getRootEntities() {
261
        List<FeatureTree> root = new ArrayList<>();
262
        root.add(characterTreeEditorComposite.getFeatureTree());
263
        return root;
264
    }
265

    
266
    @Override
267
    public ICdmEntitySession getCdmEntitySession() {
268
        return cdmEntitySession;
269
    }
270

    
271
    @Focus
272
    public void setFocus() {
273
        if(conversation!=null && !conversation.isBound()){
274
            conversation.bind();
275
        }
276
    }
277

    
278
    /**
279
     * {@inheritDoc}
280
     */
281
    @Override
282
    public void selectionChanged(SelectionChangedEvent event) {
283
        //propagate selection
284
        IStructuredSelection isel = (IStructuredSelection) event.getSelection();
285
        selService.setSelection((isel.size() == 1 ? isel.getFirstElement() : isel.toArray()));
286
    }
287

    
288

    
289
    /**
290
     * {@inheritDoc}
291
     */
292
    @Override
293
    public void modifyText(ModifyEvent e) {
294
        characterTreeEditorComposite.getFeatureTree().setTitleCache(characterTreeEditorComposite.getText_title().getText(), true);
295
        setDirty(true);
296
    }
297

    
298

    
299
    public void setDirty(boolean isDirty) {
300
        dirty.setDirty(isDirty);
301
    }
302

    
303
    private class FeatureTreeChooserListener extends SelectionAdapter{
304
        FeatureTreeEditorComposite featureTreeEditorComposite;
305

    
306
        public FeatureTreeChooserListener(FeatureTreeEditorComposite featureTreeEditorComposite) {
307
            super();
308
            this.featureTreeEditorComposite = featureTreeEditorComposite;
309
        }
310

    
311
        @Override
312
        public void widgetSelected(SelectionEvent e) {
313
            FeatureTree tree = FeatureTreeSelectionDialog.select(shell, conversation, null);
314
            if(tree!=null){
315
                featureTreeEditorComposite.setSelectedTree(tree, CharacterEditor.this);
316
            }
317
        }
318
    }
319

    
320
}
(3-3/3)