Project

General

Profile

Download (9.36 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
            cdmEntitySession.bind();
121
        }
122
        else{
123
            return;
124
        }
125
        stackLayout = new StackLayout();
126
        parent.setLayout(stackLayout);
127
        matrix = new CharacterMatrix(parent, this);
128
        label = new Label(parent, SWT.NONE);
129
        label.setText(Messages.CharacterMatrixPart_LOADING_MATRIX);
130
        stackLayout.topControl = label;
131
        ContextInjectionFactory.inject(matrix, context);
132
    }
133

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

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

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

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

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

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

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

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

    
185
    @Persist
186
    @Override
187
    public void save(IProgressMonitor monitor) {
188
        // save edited descriptions
189
        List<DescriptionBase> descriptions = matrix.getRowsToSave().stream()
190
        .map(row->row.getDescription())
191
        .collect(Collectors.toList());
192
        matrix.addSpecimensToDescriptiveDataSet();
193
        CdmStore.getService(IDescriptiveDataSetService.class).removeDescriptions(matrix.getDescriptionsToDelete(), matrix.getDescriptiveDataSet().getUuid());
194

    
195
        CdmStore.getService(IDescriptionService.class).merge(descriptions);
196
        matrix.getRowsToSave().clear();
197
        conversation.commit();
198
        dirty.setDirty(false);
199
    }
200

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

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

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

    
229
    @Override
230
    public void update(CdmDataChangeMap arg0) {
231
    }
232

    
233
    @Override
234
    public ConversationHolder getConversationHolder() {
235
        return conversation;
236
    }
237

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

    
244
    @Override
245
    public void forceDirty() {
246
        setDirty();
247
    }
248

    
249
    @Override
250
    public ICdmEntitySession getCdmEntitySession() {
251
        return cdmEntitySession;
252
    }
253

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

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

    
266
    private void loadingFailed() {
267
        label.setText(Messages.CharacterMatrixPart_LOADING_MATRIX_FAILED);
268
    }
269

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

    
275
}
(9-9/20)