Project

General

Profile

Download (8.75 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.io.FileOutputStream;
12
import java.io.IOException;
13
import java.util.Arrays;
14
import java.util.Collection;
15
import java.util.Collections;
16
import java.util.HashMap;
17
import java.util.List;
18
import java.util.Map;
19
import java.util.UUID;
20

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

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

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

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

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

    
91
    private DescriptiveDataSet descriptiveDataSet;
92

    
93
    private ConversationHolder conversation;
94

    
95
    private ICdmEntitySession cdmEntitySession;
96

    
97
    @Inject
98
    private ESelectionService selService;
99

    
100
    @Inject
101
    private MDirtyable dirty;
102

    
103
    @Inject
104
    private MPart thisPart;
105

    
106
    private CharacterMatrix matrix;
107

    
108
    private StackLayout stackLayout;
109

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

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

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

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

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

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

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

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

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

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

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

    
183
    private void save(RowWrapperDTO wrapper){
184
        CdmStore.getService(IDescriptionService.class).merge(wrapper.getDescription());
185
    }
186

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

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

    
202
    @PreDestroy
203
    public void dispose(){
204
        if (conversation != null) {
205
            conversation.close();
206
            conversation = null;
207
        }
208
        if(cdmEntitySession != null) {
209
            cdmEntitySession.dispose();
210
            cdmEntitySession = null;
211
        }
212
        dirty.setDirty(false);
213
        if(matrix.getNatTableState()!=null){
214
            try (FileOutputStream tableStateStream =
215
                    new FileOutputStream(matrix.getStatePropertiesFile())) {
216
                matrix.getNatTableState().store(tableStateStream, null);
217
            } catch (IOException ioe) {
218
                ioe.printStackTrace();
219
            }
220
        }
221
    }
222

    
223
    @Override
224
    public void update(CdmDataChangeMap arg0) {
225
    }
226

    
227
    @Override
228
    public ConversationHolder getConversationHolder() {
229
        return conversation;
230
    }
231

    
232
    @Override
233
    public void changed(Object element) {
234
        setDirty();
235
        matrix.getNatTable().refresh();
236
    }
237

    
238
    @Override
239
    public void forceDirty() {
240
        setDirty();
241
    }
242

    
243
    @Override
244
    public ICdmEntitySession getCdmEntitySession() {
245
        return cdmEntitySession;
246
    }
247

    
248
    @Override
249
    public Collection<DescriptiveDataSet> getRootEntities() {
250
        return Collections.singleton(this.descriptiveDataSet);
251
    }
252

    
253
    @Override
254
    public Map<Object, List<String>> getPropertyPathsMap() {
255
        Map<Object, List<String>> propertyMap = new HashMap<>();
256
        propertyMap.put(SpecimenOrObservationBase.class,WS_PROPERTY_PATH);
257
        return propertyMap;
258
    }
259

    
260
    public void loadingDone() {
261
        stackLayout.topControl = matrix;
262
        matrix.getParent().layout();
263
    }
264

    
265
}
(11-11/20)