Project

General

Profile

Download (6.37 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
            matrix.loadDescriptions(workingSet);
111
        }
112
    }
113

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
210
}
(3-3/12)