Project

General

Profile

Download (12.1 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 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
        structuresTreeEditorComposite = addFeatureTreeEditor(composite);
122
        initFeatureTreeComposite(structuresTreeEditorComposite, null, null);
123
        structuresTreeEditorComposite.getLabel_title().setText("Structures");
124

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

    
130
        propertiesTreeEditorComposite = addFeatureTreeEditor(composite_1);
131
        initFeatureTreeComposite(propertiesTreeEditorComposite, null, null);
132
        propertiesTreeEditorComposite.getLabel_title().setText("Properties");
133
        //TODO: fix drag and drop
134
//        initFeatureTreeComposite(treeViewerProperties, new CharacterDragListener(treeViewerStructures.getViewer(), treeViewerProperties.getViewer()), null);
135

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

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

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

    
151
        characterTreeEditorComposite = addFeatureTreeEditor(composite_3);
152
        initFeatureTreeComposite(characterTreeEditorComposite, new FeatureNodeDragListener(characterTreeEditorComposite.getViewer()), new FeatureNodeDropAdapter(characterTreeEditorComposite.getViewer()));
153
        characterTreeEditorComposite.getLabel_title().setText("Characters");
154

    
155
        sashForm.setWeights(new int[] {3, 3, 1, 3});
156

    
157
        init();
158
    }
159

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

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

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

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

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

    
215
                setDirty(true);
216
                characterTreeEditorComposite.getViewer().refresh();
217
            }
218
        });
219
    }
220

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

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

    
230
        CdmStore.getService(IFeatureTreeService.class).saveOrUpdate(characterTreeEditorComposite.getFeatureTree());
231

    
232
        this.setDirty(false);
233
    }
234

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

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

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

    
268
    @Override
269
    public ICdmEntitySession getCdmEntitySession() {
270
        return cdmEntitySession;
271
    }
272

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

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

    
290

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

    
300

    
301
    public void setDirty(boolean isDirty) {
302
        dirty.setDirty(isDirty);
303
    }
304

    
305
    private class FeatureTreeChooserListener extends SelectionAdapter{
306
        FeatureTreeEditorComposite featureTreeEditorComposite;
307

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

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

    
322
}
(3-3/3)