Project

General

Profile

Download (6.32 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.workingSet.matrix;
10

    
11
import java.io.File;
12
import java.io.FileOutputStream;
13
import java.io.IOException;
14
import java.util.Arrays;
15
import java.util.Collection;
16
import java.util.Collections;
17
import java.util.HashMap;
18
import java.util.List;
19
import java.util.Map;
20
import java.util.UUID;
21

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

    
26
import org.eclipse.core.runtime.IProgressMonitor;
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.nebula.widgets.nattable.NatTable;
33
import org.eclipse.swt.widgets.Composite;
34

    
35
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
36
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
37
import eu.etaxonomy.cdm.api.service.IWorkingSetService;
38
import eu.etaxonomy.cdm.model.description.WorkingSet;
39
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
40
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
41
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
42
import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
43
import eu.etaxonomy.taxeditor.model.MessagingUtils;
44
import eu.etaxonomy.taxeditor.session.ICdmEntitySession;
45
import eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled;
46
import eu.etaxonomy.taxeditor.store.CdmStore;
47
import eu.etaxonomy.taxeditor.workbench.WorkbenchUtility;
48
import eu.etaxonomy.taxeditor.workbench.part.IE4SavablePart;
49

    
50
/**
51
 * Character matrix editor for editing specimen/taxon descriptions in a table
52
 * @author pplitzner
53
 * @since Nov 26, 2017
54
 *
55
 */
56
public class CharacterMatrixPart implements IE4SavablePart, IConversationEnabled, IDirtyMarkable,
57
ICdmEntitySessionEnabled{
58

    
59
    private static final List<String> WS_PROPERTY_PATH = Arrays.asList(new String[] {
60
            "descriptions", //$NON-NLS-1$
61
            "descriptions.descriptionElements", //$NON-NLS-1$
62
            "descriptions.descriptionElements.inDescription", //$NON-NLS-1$
63
            "descriptions.descriptionElements.inDescription.descriptionElements", //$NON-NLS-1$
64
            "descriptions.descriptionElements.feature", //$NON-NLS-1$
65
    });
66

    
67
    private static final String CHARACTER_MATRIX_STATE_PROPERTIES = "characterMatrixState.properties"; //$NON-NLS-1$
68

    
69
    private WorkingSet workingSet;
70

    
71
    private ConversationHolder conversation;
72

    
73
    private ICdmEntitySession cdmEntitySession;
74

    
75
    @Inject
76
    private ESelectionService selService;
77

    
78
    @Inject
79
    private MDirtyable dirty;
80

    
81
    @Inject
82
    private MPart thisPart;
83

    
84
    private CharacterMatrix matrix;
85

    
86
    @PostConstruct
87
    public void create(Composite parent) {
88
        if(CdmStore.isActive() && conversation==null){
89
            conversation = CdmStore.createConversation();
90
        }
91
        if(cdmEntitySession == null){
92
            cdmEntitySession = CdmStore.getCurrentSessionManager().newSession(this, true);
93
        }
94
        else{
95
            return;
96
        }
97
        matrix = new CharacterMatrix(parent, this);
98
    }
99

    
100
    public void init(UUID workingSetUuid, boolean treeView) {
101
        this.workingSet = CdmStore.getService(IWorkingSetService.class).load(workingSetUuid, WS_PROPERTY_PATH);
102
        if(workingSet!=null){
103
            if(workingSet.getDescriptiveSystem()==null){
104
                MessagingUtils.informationDialog(Messages.CharacterMatrixPart_COULD_NOT_OPEN, Messages.CharacterMatrixPart_COULD_NOT_OPEN_MESSAGE);
105
                return;
106
            }
107
            matrix.initWorkingSet(workingSet);
108
            matrix.createTable(treeView);
109
            thisPart.setLabel(workingSet.getLabel());
110
        }
111
    }
112

    
113
    public void setDirty() {
114
        this.dirty.setDirty(true);
115
    }
116

    
117
    public WorkingSet getWorkingSet() {
118
        return workingSet;
119
    }
120

    
121
    private File getStatePropertiesFile() {
122
        return new File(WorkbenchUtility.getBaseLocation(), CHARACTER_MATRIX_STATE_PROPERTIES);
123
    }
124

    
125
    public NatTable getNatTable() {
126
        return matrix.getNatTable();
127
    }
128

    
129
    public ESelectionService getSelectionService() {
130
        return selService;
131
    }
132

    
133
    @Persist
134
    @Override
135
    public void save(IProgressMonitor monitor) {
136
        CdmStore.getService(IWorkingSetService.class).merge(workingSet, true);
137
        conversation.commit();
138
        dirty.setDirty(false);
139
    }
140

    
141
    @Focus
142
    public void setFocus(){
143
        if(conversation!=null){
144
            conversation.bind();
145
        }
146
        if(cdmEntitySession != null) {
147
            cdmEntitySession.bind();
148
        }
149
    }
150

    
151
    @PreDestroy
152
    public void dispose(){
153
        if (conversation != null) {
154
            conversation.close();
155
            conversation = null;
156
        }
157
        if(cdmEntitySession != null) {
158
            cdmEntitySession.dispose();
159
            cdmEntitySession = null;
160
        }
161
        dirty.setDirty(false);
162
        if(matrix.getNatTableState()!=null){
163
            try (FileOutputStream tableStateStream =
164
                    new FileOutputStream(getStatePropertiesFile())) {
165
                matrix.getNatTableState().store(tableStateStream, null);
166
            } catch (IOException ioe) {
167
                ioe.printStackTrace();
168
            }
169
        }
170
    }
171

    
172
    @Override
173
    public void update(CdmDataChangeMap arg0) {
174
    }
175

    
176
    @Override
177
    public ConversationHolder getConversationHolder() {
178
        return conversation;
179
    }
180

    
181
    @Override
182
    public void changed(Object element) {
183
        setDirty();
184
        matrix.getNatTable().refresh();
185
    }
186

    
187
    @Override
188
    public void forceDirty() {
189
        setDirty();
190
    }
191

    
192
    @Override
193
    public ICdmEntitySession getCdmEntitySession() {
194
        return cdmEntitySession;
195
    }
196

    
197
    @Override
198
    public Collection<WorkingSet> getRootEntities() {
199
        return Collections.singleton(this.workingSet);
200
    }
201

    
202
    @Override
203
    public Map<Object, List<String>> getPropertyPathsMap() {
204
        Map<Object, List<String>> propertyMap = new HashMap<>();
205
        propertyMap.put(SpecimenOrObservationBase.class,WS_PROPERTY_PATH);
206
        return propertyMap;
207
    }
208

    
209
}
(3-3/12)