Project

General

Profile

Download (9.12 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 * Copyright (C) 2017 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
package eu.etaxonomy.taxeditor.editor.descriptiveDataSet.matrix;
10

    
11
import java.util.Arrays;
12
import java.util.Collection;
13
import java.util.Collections;
14
import java.util.HashMap;
15
import java.util.List;
16
import java.util.Map;
17
import java.util.UUID;
18
import java.util.stream.Collectors;
19

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

    
24
import org.eclipse.core.runtime.IProgressMonitor;
25
import org.eclipse.e4.core.contexts.ContextInjectionFactory;
26
import org.eclipse.e4.core.contexts.IEclipseContext;
27
import org.eclipse.e4.ui.di.Focus;
28
import org.eclipse.e4.ui.di.Persist;
29
import org.eclipse.e4.ui.model.application.ui.MDirtyable;
30
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
31
import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
32
import org.eclipse.jface.viewers.IStructuredSelection;
33
import org.eclipse.nebula.widgets.nattable.NatTable;
34
import org.eclipse.swt.SWT;
35
import org.eclipse.swt.custom.StackLayout;
36
import org.eclipse.swt.widgets.Composite;
37
import org.eclipse.swt.widgets.Label;
38

    
39
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
40
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
41
import eu.etaxonomy.cdm.api.service.IDescriptionService;
42
import eu.etaxonomy.cdm.api.service.IDescriptiveDataSetService;
43
import eu.etaxonomy.cdm.model.description.DescriptionBase;
44
import eu.etaxonomy.cdm.model.description.DescriptiveDataSet;
45
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
46
import eu.etaxonomy.cdm.model.term.TermType;
47
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
48
import eu.etaxonomy.taxeditor.editor.internal.TaxeditorEditorPlugin;
49
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
50
import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
51
import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
52
import eu.etaxonomy.taxeditor.model.IPartContentHasSupplementalData;
53
import eu.etaxonomy.taxeditor.model.MessagingUtils;
54
import eu.etaxonomy.taxeditor.session.ICdmEntitySession;
55
import eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled;
56
import eu.etaxonomy.taxeditor.store.CdmStore;
57
import eu.etaxonomy.taxeditor.workbench.part.IE4SavablePart;
58

    
59
/**
60
 * Character matrix editor for editing specimen/taxon descriptions in a table
61
 * @author pplitzner
62
 * @since Nov 26, 2017
63
 *
64
 */
65
public class CharacterMatrixPart implements IE4SavablePart, IConversationEnabled, IDirtyMarkable,
66
ICdmEntitySessionEnabled, IPartContentHasSupplementalData, IPartContentHasDetails{
67

    
68
    private static final List<String> WS_PROPERTY_PATH = Arrays.asList(new String[] {
69
            "descriptions", //$NON-NLS-1$
70
            "descriptions.describedSpecimenOrObservation", //$NON-NLS-1$
71
            "descriptions.describedSpecimenOrObservation.gatheringEvent", //$NON-NLS-1$
72
            "descriptions.describedSpecimenOrObservation.gatheringEvent.actor", //$NON-NLS-1$
73
            "descriptions.descriptionElements", //$NON-NLS-1$
74
            "descriptions.descriptionElements.stateData", //$NON-NLS-1$
75
            "descriptions.descriptionElements.stateData.state", //$NON-NLS-1$
76
            "descriptions.descriptionElements.inDescription", //$NON-NLS-1$
77
            "descriptions.descriptionElements.inDescription.descriptionElements", //$NON-NLS-1$
78
            "descriptions.descriptionElements.feature", //$NON-NLS-1$
79
            "representations", //$NON-NLS-1$
80
            "descriptiveSystem", //$NON-NLS-1$
81
            "taxonSubtreeFilter", //$NON-NLS-1$
82
            "taxonSubtreeFilter.parent", //$NON-NLS-1$
83
            "taxonSubtreeFilter.classification", //$NON-NLS-1$
84
            "taxonSubtreeFilter.taxon", //$NON-NLS-1$
85
            "taxonSubtreeFilter.taxon.name", //$NON-NLS-1$
86
            "taxonSubtreeFilter.taxon.name.rank", //$NON-NLS-1$
87
            "geoFilter", //$NON-NLS-1$
88
            "minRank", //$NON-NLS-1$
89
            "maxRank", //$NON-NLS-1$
90
    });
91

    
92
    private DescriptiveDataSet descriptiveDataSet;
93

    
94
    private ConversationHolder conversation;
95

    
96
    private ICdmEntitySession cdmEntitySession;
97

    
98
    @Inject
99
    private ESelectionService selService;
100

    
101
    @Inject
102
    private MDirtyable dirty;
103

    
104
    @Inject
105
    private MPart thisPart;
106

    
107
    private CharacterMatrix matrix;
108

    
109
    private StackLayout stackLayout;
110

    
111
    private Label label;
112

    
113
    @PostConstruct
114
    public void create(Composite parent, IEclipseContext context) {
115
        if(CdmStore.isActive() && conversation==null){
116
            conversation = CdmStore.createConversation();
117
        }
118
        if(cdmEntitySession == null){
119
            cdmEntitySession = CdmStore.getCurrentSessionManager().newSession(this, true);
120
        }
121
        else{
122
            return;
123
        }
124
        stackLayout = new StackLayout();
125
        parent.setLayout(stackLayout);
126
        matrix = new CharacterMatrix(parent, this);
127
        label = new Label(parent, SWT.NONE);
128
        label.setText(Messages.CharacterMatrixPart_LOADING_MATRIX);
129
        stackLayout.topControl = label;
130
        ContextInjectionFactory.inject(matrix, context);
131
    }
132

    
133
    public void init(UUID descriptiveDataSetUuid, boolean treeView) {
134
        this.descriptiveDataSet = CdmStore.getService(IDescriptiveDataSetService.class).load(descriptiveDataSetUuid, WS_PROPERTY_PATH);
135
        if(descriptiveDataSet!=null){
136
            if(descriptiveDataSet.getDescriptiveSystem()==null
137
                    || descriptiveDataSet.getDescriptiveSystem().getTermType()==null
138
                    || !descriptiveDataSet.getDescriptiveSystem().getTermType().equals(TermType.Character)){
139
                MessagingUtils.informationDialog(Messages.CharacterMatrixPart_COULD_NOT_OPEN, Messages.CharacterMatrixPart_COULD_NOT_OPEN_MESSAGE);
140
                loadingFailed();
141
                return;
142
            }
143
            try {
144
                matrix.initDescriptiveDataSet();
145
            } catch (IllegalArgumentException e) {
146
                MessagingUtils.errorDialog("Error during initilization", this, "Could not load matrix", TaxeditorEditorPlugin.PLUGIN_ID, e, false);
147
                loadingFailed();
148
                return;
149
            }
150
            matrix.createTable(treeView, true, true);
151
            thisPart.setLabel(descriptiveDataSet.getLabel());
152
            matrix.loadDescriptions(descriptiveDataSetUuid, true);
153
        }
154
    }
155

    
156
    public IStructuredSelection getSelection(){
157
        return matrix.getSelection();
158
    }
159

    
160
    public void setDirty() {
161
        this.dirty.setDirty(true);
162
    }
163

    
164
    public DescriptiveDataSet getDescriptiveDataSet() {
165
        return descriptiveDataSet;
166
    }
167

    
168
    public void setDescriptiveDataSet(DescriptiveDataSet dataSet) {
169
        this.descriptiveDataSet = dataSet;
170
    }
171

    
172
    public NatTable getNatTable() {
173
        return matrix.getNatTable();
174
    }
175

    
176
    public ESelectionService getSelectionService() {
177
        return selService;
178
    }
179

    
180
    public CharacterMatrix getMatrix() {
181
        return matrix;
182
    }
183

    
184
    @Persist
185
    @Override
186
    public void save(IProgressMonitor monitor) {
187
        // save edited descriptions
188
        List<DescriptionBase> descriptions = matrix.getRowsToSave().stream()
189
        .map(row->row.getDescription())
190
        .collect(Collectors.toList());
191
        CdmStore.getService(IDescriptionService.class).merge(descriptions);
192

    
193
        matrix.getRowsToSave().clear();
194

    
195
        conversation.commit();
196
        dirty.setDirty(false);
197
    }
198

    
199
    @Override
200
    public boolean isDirty() {
201
        return dirty.isDirty();
202
    }
203

    
204
    @Focus
205
    public void setFocus(){
206
        if(conversation!=null){
207
            conversation.bind();
208
        }
209
        if(cdmEntitySession != null) {
210
            cdmEntitySession.bind();
211
        }
212
    }
213

    
214
    @PreDestroy
215
    public void dispose(){
216
        if (conversation != null) {
217
            conversation.close();
218
            conversation = null;
219
        }
220
        if(cdmEntitySession != null) {
221
            cdmEntitySession.dispose();
222
            cdmEntitySession = null;
223
        }
224
        dirty.setDirty(false);
225
    }
226

    
227
    @Override
228
    public void update(CdmDataChangeMap arg0) {
229
    }
230

    
231
    @Override
232
    public ConversationHolder getConversationHolder() {
233
        return conversation;
234
    }
235

    
236
    @Override
237
    public void changed(Object element) {
238
        setDirty();
239
        matrix.getNatTable().refresh();
240
    }
241

    
242
    @Override
243
    public void forceDirty() {
244
        setDirty();
245
    }
246

    
247
    @Override
248
    public ICdmEntitySession getCdmEntitySession() {
249
        return cdmEntitySession;
250
    }
251

    
252
    @Override
253
    public Collection<DescriptiveDataSet> getRootEntities() {
254
        return Collections.singleton(this.descriptiveDataSet);
255
    }
256

    
257
    @Override
258
    public Map<Object, List<String>> getPropertyPathsMap() {
259
        Map<Object, List<String>> propertyMap = new HashMap<>();
260
        propertyMap.put(SpecimenOrObservationBase.class,WS_PROPERTY_PATH);
261
        return propertyMap;
262
    }
263

    
264
    private void loadingFailed() {
265
        label.setText(Messages.CharacterMatrixPart_LOADING_MATRIX_FAILED);
266
    }
267

    
268
    void loadingDone() {
269
        stackLayout.topControl = matrix;
270
        matrix.getParent().layout();
271
    }
272

    
273
}
(9-9/20)