Project

General

Profile

Download (12 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 propertiesTreeEditorComposite;
73
    private FeatureTreeEditorComposite structuresTreeEditorComposite;
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 ConversationHolder conversation;
84

    
85
    private ICdmEntitySession cdmEntitySession;
86
    private Composite composite;
87

    
88
    public CharacterEditor() {
89
    }
90

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

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

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

    
119
        structuresTreeEditorComposite = addFeatureTreeEditor(composite);
120
        initFeatureTreeComposite(structuresTreeEditorComposite, null, null);
121
        structuresTreeEditorComposite.getLabel_title().setText("Structures");
122

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

    
128
        propertiesTreeEditorComposite = addFeatureTreeEditor(composite_1);
129
        initFeatureTreeComposite(propertiesTreeEditorComposite, null, null);
130
        propertiesTreeEditorComposite.getLabel_title().setText("Properties");
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
        characterTreeEditorComposite.getLabel_title().setText("Characters");
152

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

    
155
        init();
156
    }
157

    
158
    private FeatureTreeEditorComposite addFeatureTreeEditor(Composite composite_3) {
159
        FeatureTreeEditorComposite featureTreeEditorComposite = new FeatureTreeEditorComposite(composite_3, SWT.BORDER);
160
        featureTreeEditorComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
161
        featureTreeEditorComposite.getComposite_buttons().setVisible(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(){
176
        if(characterTreeEditorComposite!=null){
177
            characterTreeEditorComposite.getViewer().getControl().setFocus();
178
        }
179
        if(conversation!=null && !conversation.isBound()){
180
            conversation.bind();
181
        }
182
        if(cdmEntitySession != null) {
183
            cdmEntitySession.bind();
184
        }
185
    }
186

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

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

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

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

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

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

    
229
        this.setDirty(false);
230
    }
231

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

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

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

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

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

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

    
287

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

    
297

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

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

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

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

    
319
}
(3-3/3)