2 * Copyright (C) 2017 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.taxeditor
.editor
.workingSet
.matrix
;
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
;
20 import java
.util
.UUID
;
22 import javax
.annotation
.PostConstruct
;
23 import javax
.annotation
.PreDestroy
;
24 import javax
.inject
.Inject
;
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
;
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
;
51 * Character matrix editor for editing specimen/taxon descriptions in a table
56 public class CharacterMatrixPart
implements IE4SavablePart
, IConversationEnabled
, IDirtyMarkable
,
57 ICdmEntitySessionEnabled
{
59 private static final List
<String
> WS_PROPERTY_PATH
= Arrays
.asList(new String
[] {
60 "descriptions", //$NON-NLS-1$
61 "descriptions.describedSpecimenOrObservation", //$NON-NLS-1$
62 "descriptions.describedSpecimenOrObservation.gatheringEvent", //$NON-NLS-1$
63 "descriptions.describedSpecimenOrObservation.gatheringEvent.actor", //$NON-NLS-1$
64 "descriptions.descriptionElements", //$NON-NLS-1$
65 "descriptions.descriptionElements.stateData", //$NON-NLS-1$
66 "descriptions.descriptionElements.stateData.state", //$NON-NLS-1$
67 "descriptions.descriptionElements.inDescription", //$NON-NLS-1$
68 "descriptions.descriptionElements.inDescription.descriptionElements", //$NON-NLS-1$
69 "descriptions.descriptionElements.feature", //$NON-NLS-1$
70 "representations", //$NON-NLS-1$
71 "descriptiveSystem", //$NON-NLS-1$
72 "taxonSubtreeFilter", //$NON-NLS-1$
73 "taxonSubtreeFilter.parent", //$NON-NLS-1$
74 "taxonSubtreeFilter.classification", //$NON-NLS-1$
75 "taxonSubtreeFilter.taxon", //$NON-NLS-1$
76 "taxonSubtreeFilter.taxon.name", //$NON-NLS-1$
77 "taxonSubtreeFilter.taxon.name.rank", //$NON-NLS-1$
78 "geoFilter", //$NON-NLS-1$
79 "minRank", //$NON-NLS-1$
80 "maxRank", //$NON-NLS-1$
83 private static final String CHARACTER_MATRIX_STATE_PROPERTIES
= "characterMatrixState.properties"; //$NON-NLS-1$
85 private WorkingSet workingSet
;
87 private ConversationHolder conversation
;
89 private ICdmEntitySession cdmEntitySession
;
92 private ESelectionService selService
;
95 private MDirtyable dirty
;
98 private MPart thisPart
;
100 private CharacterMatrix matrix
;
103 public void create(Composite parent
) {
104 if(CdmStore
.isActive() && conversation
==null){
105 conversation
= CdmStore
.createConversation();
107 if(cdmEntitySession
== null){
108 cdmEntitySession
= CdmStore
.getCurrentSessionManager().newSession(this, true);
113 matrix
= new CharacterMatrix(parent
, this);
116 public void init(UUID workingSetUuid
, boolean treeView
) {
117 this.workingSet
= CdmStore
.getService(IWorkingSetService
.class).load(workingSetUuid
, WS_PROPERTY_PATH
);
118 if(workingSet
!=null){
119 if(workingSet
.getDescriptiveSystem()==null){
120 MessagingUtils
.informationDialog(Messages
.CharacterMatrixPart_COULD_NOT_OPEN
, Messages
.CharacterMatrixPart_COULD_NOT_OPEN_MESSAGE
);
123 matrix
.initWorkingSet(workingSet
);
124 matrix
.createTable(treeView
);
125 thisPart
.setLabel(workingSet
.getLabel());
126 matrix
.loadDescriptions(workingSet
);
130 public void setDirty() {
131 this.dirty
.setDirty(true);
134 public WorkingSet
getWorkingSet() {
138 private File
getStatePropertiesFile() {
139 return new File(WorkbenchUtility
.getBaseLocation(), CHARACTER_MATRIX_STATE_PROPERTIES
);
142 public NatTable
getNatTable() {
143 return matrix
.getNatTable();
146 public ESelectionService
getSelectionService() {
152 public void save(IProgressMonitor monitor
) {
153 CdmStore
.getService(IWorkingSetService
.class).merge(workingSet
, true);
154 conversation
.commit();
155 dirty
.setDirty(false);
159 public void setFocus(){
160 if(conversation
!=null){
163 if(cdmEntitySession
!= null) {
164 cdmEntitySession
.bind();
169 public void dispose(){
170 if (conversation
!= null) {
171 conversation
.close();
174 if(cdmEntitySession
!= null) {
175 cdmEntitySession
.dispose();
176 cdmEntitySession
= null;
178 dirty
.setDirty(false);
179 if(matrix
.getNatTableState()!=null){
180 try (FileOutputStream tableStateStream
=
181 new FileOutputStream(getStatePropertiesFile())) {
182 matrix
.getNatTableState().store(tableStateStream
, null);
183 } catch (IOException ioe
) {
184 ioe
.printStackTrace();
190 public void update(CdmDataChangeMap arg0
) {
194 public ConversationHolder
getConversationHolder() {
199 public void changed(Object element
) {
201 matrix
.getNatTable().refresh();
205 public void forceDirty() {
210 public ICdmEntitySession
getCdmEntitySession() {
211 return cdmEntitySession
;
215 public Collection
<WorkingSet
> getRootEntities() {
216 return Collections
.singleton(this.workingSet
);
220 public Map
<Object
, List
<String
>> getPropertyPathsMap() {
221 Map
<Object
, List
<String
>> propertyMap
= new HashMap
<>();
222 propertyMap
.put(SpecimenOrObservationBase
.class,WS_PROPERTY_PATH
);