Project

General

Profile

Download (8.58 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

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

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

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

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

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

    
90
    private DescriptiveDataSet descriptiveDataSet;
91

    
92
    private ConversationHolder conversation;
93

    
94
    private ICdmEntitySession cdmEntitySession;
95

    
96
    @Inject
97
    private ESelectionService selService;
98

    
99
    @Inject
100
    private MDirtyable dirty;
101

    
102
    @Inject
103
    private MPart thisPart;
104

    
105
    private CharacterMatrix matrix;
106

    
107
    private StackLayout stackLayout;
108

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

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

    
145
    public IStructuredSelection getSelection(){
146
        return matrix.getSelection();
147
    }
148

    
149
    public void setDirty() {
150
        this.dirty.setDirty(true);
151
    }
152

    
153
    public DescriptiveDataSet getDescriptiveDataSet() {
154
        return descriptiveDataSet;
155
    }
156

    
157
    public NatTable getNatTable() {
158
        return matrix.getNatTable();
159
    }
160

    
161
    public ESelectionService getSelectionService() {
162
        return selService;
163
    }
164

    
165
    public CharacterMatrix getMatrix() {
166
        return matrix;
167
    }
168

    
169
    @Persist
170
    @Override
171
    public void save(IProgressMonitor monitor) {
172
        //save descriptions
173
        matrix.getDescriptions().stream()
174
        .filter(o->o instanceof RowWrapperDTO)
175
        .forEach(wrapper->save((RowWrapperDTO)wrapper));
176

    
177
        //save data set
178
        CdmStore.getService(IDescriptiveDataSetService.class).merge(descriptiveDataSet, true);
179

    
180
        conversation.commit();
181
        dirty.setDirty(false);
182
    }
183

    
184
    private void save(RowWrapperDTO wrapper){
185
        CdmStore.getService(IDescriptionService.class).merge(wrapper.getDescription());
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)