Project

General

Profile

Download (8.68 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.api.service.dto.RowWrapperDTO;
44
import eu.etaxonomy.cdm.model.description.DescriptionBase;
45
import eu.etaxonomy.cdm.model.description.DescriptiveDataSet;
46
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
47
import eu.etaxonomy.cdm.model.term.TermType;
48
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
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
    @PostConstruct
112
    public void create(Composite parent, IEclipseContext context) {
113
        if(CdmStore.isActive() && conversation==null){
114
            conversation = CdmStore.createConversation();
115
        }
116
        if(cdmEntitySession == null){
117
            cdmEntitySession = CdmStore.getCurrentSessionManager().newSession(this, true);
118
        }
119
        else{
120
            return;
121
        }
122
        stackLayout = new StackLayout();
123
        parent.setLayout(stackLayout);
124
        matrix = new CharacterMatrix(parent, this);
125
        Label label = new Label(parent, SWT.NONE);
126
        label.setText(Messages.CharacterMatrixPart_LOADING_MATRIX);
127
        stackLayout.topControl = label;
128
        ContextInjectionFactory.inject(matrix, context);
129
    }
130

    
131
    public void init(UUID descriptiveDataSetUuid, boolean treeView) {
132
        this.descriptiveDataSet = CdmStore.getService(IDescriptiveDataSetService.class).load(descriptiveDataSetUuid, WS_PROPERTY_PATH);
133
        if(descriptiveDataSet!=null){
134
            if(descriptiveDataSet.getDescriptiveSystem()==null
135
                    || descriptiveDataSet.getDescriptiveSystem().getTermType()==null
136
                    || !descriptiveDataSet.getDescriptiveSystem().getTermType().equals(TermType.Character)){
137
                MessagingUtils.informationDialog(Messages.CharacterMatrixPart_COULD_NOT_OPEN, Messages.CharacterMatrixPart_COULD_NOT_OPEN_MESSAGE);
138
                return;
139
            }
140
            matrix.initDescriptiveDataSet(descriptiveDataSet);
141
            matrix.createTable(treeView, true);
142
            thisPart.setLabel(descriptiveDataSet.getLabel());
143
            matrix.loadDescriptions(descriptiveDataSet);
144
        }
145
    }
146

    
147
    public IStructuredSelection getSelection(){
148
        return matrix.getSelection();
149
    }
150

    
151
    public void setDirty() {
152
        this.dirty.setDirty(true);
153
    }
154

    
155
    public DescriptiveDataSet getDescriptiveDataSet() {
156
        return descriptiveDataSet;
157
    }
158

    
159
    public NatTable getNatTable() {
160
        return matrix.getNatTable();
161
    }
162

    
163
    public ESelectionService getSelectionService() {
164
        return selService;
165
    }
166

    
167
    public CharacterMatrix getMatrix() {
168
        return matrix;
169
    }
170

    
171
    @Persist
172
    @Override
173
    public void save(IProgressMonitor monitor) {
174
        //save descriptions
175
        List<DescriptionBase> descriptions = matrix.getDescriptions().stream()
176
        .filter(o->o instanceof RowWrapperDTO)
177
        .map(o->((RowWrapperDTO)o).getDescription())
178
        .collect(Collectors.toList());
179
        CdmStore.getService(IDescriptionService.class).merge(descriptions);
180

    
181
        //save data set
182
        CdmStore.getService(IDescriptiveDataSetService.class).merge(descriptiveDataSet, true);
183

    
184
        conversation.commit();
185
        dirty.setDirty(false);
186
    }
187

    
188
    @Override
189
    public boolean isDirty() {
190
        return dirty.isDirty();
191
    }
192

    
193
    @Focus
194
    public void setFocus(){
195
        if(conversation!=null){
196
            conversation.bind();
197
        }
198
        if(cdmEntitySession != null) {
199
            cdmEntitySession.bind();
200
        }
201
    }
202

    
203
    @PreDestroy
204
    public void dispose(){
205
        if (conversation != null) {
206
            conversation.close();
207
            conversation = null;
208
        }
209
        if(cdmEntitySession != null) {
210
            cdmEntitySession.dispose();
211
            cdmEntitySession = null;
212
        }
213
        dirty.setDirty(false);
214
    }
215

    
216
    @Override
217
    public void update(CdmDataChangeMap arg0) {
218
    }
219

    
220
    @Override
221
    public ConversationHolder getConversationHolder() {
222
        return conversation;
223
    }
224

    
225
    @Override
226
    public void changed(Object element) {
227
        setDirty();
228
        matrix.getNatTable().refresh();
229
    }
230

    
231
    @Override
232
    public void forceDirty() {
233
        setDirty();
234
    }
235

    
236
    @Override
237
    public ICdmEntitySession getCdmEntitySession() {
238
        return cdmEntitySession;
239
    }
240

    
241
    @Override
242
    public Collection<DescriptiveDataSet> getRootEntities() {
243
        return Collections.singleton(this.descriptiveDataSet);
244
    }
245

    
246
    @Override
247
    public Map<Object, List<String>> getPropertyPathsMap() {
248
        Map<Object, List<String>> propertyMap = new HashMap<>();
249
        propertyMap.put(SpecimenOrObservationBase.class,WS_PROPERTY_PATH);
250
        return propertyMap;
251
    }
252

    
253
    public void loadingDone() {
254
        stackLayout.topControl = matrix;
255
        matrix.getParent().layout();
256
    }
257

    
258
}
(11-11/21)