1
|
/**
|
2
|
* Copyright (C) 2007 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.ui.section;
|
10
|
|
11
|
import java.util.ArrayList;
|
12
|
import java.util.Collection;
|
13
|
import java.util.EnumSet;
|
14
|
|
15
|
import org.eclipse.jface.action.ToolBarManager;
|
16
|
import org.eclipse.jface.viewers.ISelectionProvider;
|
17
|
import org.eclipse.swt.SWT;
|
18
|
import org.eclipse.swt.graphics.Color;
|
19
|
import org.eclipse.swt.widgets.Control;
|
20
|
import org.eclipse.ui.forms.events.ExpansionEvent;
|
21
|
import org.eclipse.ui.forms.events.IExpansionListener;
|
22
|
import org.eclipse.ui.forms.widgets.ExpandableComposite;
|
23
|
import org.eclipse.ui.forms.widgets.TableWrapData;
|
24
|
import org.eclipse.ui.forms.widgets.TableWrapLayout;
|
25
|
|
26
|
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
|
27
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
28
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
29
|
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
|
30
|
import eu.etaxonomy.cdm.model.common.Language;
|
31
|
import eu.etaxonomy.cdm.model.description.TextData;
|
32
|
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
|
33
|
import eu.etaxonomy.cdm.model.permission.CRUD;
|
34
|
import eu.etaxonomy.cdm.model.term.DefinedTermBase;
|
35
|
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
|
36
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
37
|
import eu.etaxonomy.taxeditor.store.StoreUtil;
|
38
|
import eu.etaxonomy.taxeditor.ui.element.AbstractFormSection;
|
39
|
import eu.etaxonomy.taxeditor.ui.element.CacheRelevance;
|
40
|
import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
|
41
|
import eu.etaxonomy.taxeditor.ui.element.ICacheRelevantFormElement;
|
42
|
import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
|
43
|
import eu.etaxonomy.taxeditor.ui.element.IEnableableFormElement;
|
44
|
import eu.etaxonomy.taxeditor.ui.element.ISelectableElement;
|
45
|
import eu.etaxonomy.taxeditor.ui.element.ToggleableTextElement;
|
46
|
|
47
|
/**
|
48
|
* This class visualizes an CDM entity of type ENTITY.
|
49
|
*
|
50
|
* @param <ENTITY> A CDM entity which should be visualized by this section.
|
51
|
*
|
52
|
* @author n.hoffmann
|
53
|
* @created Feb 26, 2010
|
54
|
*/
|
55
|
public abstract class AbstractCdmDetailSection<ENTITY>
|
56
|
extends AbstractFormSection<ENTITY>
|
57
|
implements IEnableableFormElement, IExpansionListener, ICacheRelevantFormElement {
|
58
|
|
59
|
protected AbstractCdmDetailElement<ENTITY> detailElement;
|
60
|
|
61
|
public AbstractCdmDetailSection(CdmFormFactory formFactory,
|
62
|
ConversationHolder conversation, ICdmFormElement parentElement,
|
63
|
ISelectionProvider selectionProvider, int style) {
|
64
|
this(formFactory, null, conversation, parentElement, selectionProvider, style);
|
65
|
}
|
66
|
|
67
|
//TODO remove conversation
|
68
|
public AbstractCdmDetailSection(CdmFormFactory formFactory, Class<ENTITY> clazz,
|
69
|
ConversationHolder conversation, ICdmFormElement parentElement,
|
70
|
ISelectionProvider selectionProvider, int style) {
|
71
|
super(formFactory, parentElement, selectionProvider,
|
72
|
ExpandableComposite.CLIENT_INDENT | style);
|
73
|
|
74
|
setText(getHeading());
|
75
|
|
76
|
addExpansionListener(this);
|
77
|
|
78
|
if(clazz==null){
|
79
|
createControls(this, style);
|
80
|
}
|
81
|
else{
|
82
|
createControlsByType(this, clazz, SWT.NULL);
|
83
|
}
|
84
|
}
|
85
|
|
86
|
@Override
|
87
|
public void setText(String title) {
|
88
|
super.setText(StoreUtil.cleanTitleString(title));
|
89
|
}
|
90
|
|
91
|
public AbstractCdmDetailElement<ENTITY> getDetailElement() {
|
92
|
return detailElement;
|
93
|
}
|
94
|
|
95
|
public void setDetailElement(AbstractCdmDetailElement<ENTITY> detailElement) {
|
96
|
this.detailElement = detailElement;
|
97
|
}
|
98
|
|
99
|
protected void createControlsByType(AbstractCdmDetailSection<ENTITY> formElement, Class<ENTITY> entityClass, int style) {
|
100
|
TableWrapLayout layout = (TableWrapLayout) getLayoutComposite().getLayout();
|
101
|
layout.topMargin = 10;
|
102
|
layout.numColumns = DEFAULT_NUM_COLUMNS;
|
103
|
|
104
|
getLayoutComposite().setLayout(layout);
|
105
|
if(entityClass==null){
|
106
|
detailElement = createCdmDetailElement(formElement, style);
|
107
|
}
|
108
|
else{
|
109
|
detailElement = createCdmDetailElementByType(formElement, entityClass, style);
|
110
|
}
|
111
|
}
|
112
|
|
113
|
protected void createControls(AbstractCdmDetailSection<ENTITY> formElement, int style) {
|
114
|
createControlsByType(formElement, null, style);
|
115
|
}
|
116
|
|
117
|
protected abstract AbstractCdmDetailElement<ENTITY> createCdmDetailElement(AbstractCdmDetailSection<ENTITY> parentElement, int style);
|
118
|
|
119
|
protected AbstractCdmDetailElement<ENTITY> createCdmDetailElementByType(AbstractCdmDetailSection<ENTITY> parentElement, Class<ENTITY> entityClass, int style){
|
120
|
return createCdmDetailElement(parentElement, style);
|
121
|
}
|
122
|
|
123
|
public abstract String getHeading();
|
124
|
|
125
|
@Override
|
126
|
public void dispose() {
|
127
|
if (detailElement instanceof ISelectableElement) {
|
128
|
ISelectableElement selectableElement = (ISelectableElement) detailElement;
|
129
|
if (selectableElement.getSelectionArbitrator() != null) {
|
130
|
formFactory.destroySelectionArbitrator(selectableElement
|
131
|
.getSelectionArbitrator());
|
132
|
}
|
133
|
}
|
134
|
super.dispose();
|
135
|
}
|
136
|
|
137
|
@Override
|
138
|
public void setBackground(Color color) {
|
139
|
if (detailElement != null) {
|
140
|
detailElement.setBackground(color);
|
141
|
}
|
142
|
super.setBackground(color);
|
143
|
}
|
144
|
|
145
|
@Override
|
146
|
public void setEntity(ENTITY entity) {
|
147
|
EnumSet<CRUD> requiredCrud;
|
148
|
if(entity == null || (entity instanceof CdmBase &&StoreUtil.getCdmEntity(entity).getId() == 0 )) {
|
149
|
// new entity, not yet saved
|
150
|
requiredCrud = EnumSet.of(CRUD.CREATE);
|
151
|
} else {
|
152
|
requiredCrud = EnumSet.of(CRUD.UPDATE);
|
153
|
}
|
154
|
if (detailElement != null) {
|
155
|
detailElement.setEntity(entity);
|
156
|
formFactory.adapt(detailElement);
|
157
|
Collection<Object> except = new ArrayList<>();
|
158
|
for(ICdmFormElement formElement:detailElement.getElements()){
|
159
|
if(formElement instanceof IEnableableFormElement && !((IEnableableFormElement) formElement).isEnabled()){
|
160
|
except.add(formElement);
|
161
|
}
|
162
|
}
|
163
|
if (entity != null && entity instanceof CdmBase){
|
164
|
boolean hasPermission = CdmStore.currentAuthentiationHasPermission(StoreUtil.getCdmEntity(entity), requiredCrud);
|
165
|
detailElement.setEnabled(isEnabled() && hasPermission, except);
|
166
|
}else{
|
167
|
detailElement.setEnabled(isEnabled() && true, except);
|
168
|
}
|
169
|
|
170
|
}
|
171
|
super.setEntity(entity);
|
172
|
setSectionTitle();
|
173
|
layout();
|
174
|
}
|
175
|
|
176
|
protected void setSectionTitle() {
|
177
|
String title = "";
|
178
|
String label = "";
|
179
|
if (getEntity() != null && (getEntity() instanceof IdentifiableEntity) && !(getEntity() instanceof SpecimenOrObservationBase)) {
|
180
|
if (getEntity() instanceof DefinedTermBase<?> ){
|
181
|
label = ((DefinedTermBase<?>)getEntity()).getPreferredRepresentation(PreferencesUtil.getGlobalLanguage()).getLabel();
|
182
|
// if (label == null){
|
183
|
// label = ((DefinedTermBase<?>)getEntity()).getLabel();
|
184
|
// }
|
185
|
} else if (getEntity() instanceof TextData){
|
186
|
label = ((TextData)getEntity()).getLanguageText(PreferencesUtil.getGlobalLanguage()).getText();
|
187
|
if (label == null){
|
188
|
label = ((TextData)getEntity()).getLanguageText(Language.DEFAULT()).getText();
|
189
|
}
|
190
|
}
|
191
|
if(CdmUtils.isBlank(label)){
|
192
|
label = ((IdentifiableEntity<?>) getEntity()).getTitleCache();
|
193
|
}
|
194
|
if(CdmUtils.isBlank(label)){
|
195
|
label = ((IdentifiableEntity<?>) getEntity()).generateTitle();
|
196
|
}
|
197
|
title = ": " + label;
|
198
|
}
|
199
|
this.setText(String.format("%s%s", getHeading(), title));
|
200
|
setTextClient(createToolbar());
|
201
|
}
|
202
|
|
203
|
protected Control createToolbar() {
|
204
|
ToolBarManager toolBarManager = new ToolBarManager(SWT.FLAT);
|
205
|
return toolBarManager.createControl(this);
|
206
|
}
|
207
|
|
208
|
public void updateTitle() {
|
209
|
if (!isDisposed()) {
|
210
|
setSectionTitle();
|
211
|
layout();
|
212
|
}
|
213
|
}
|
214
|
|
215
|
@Override
|
216
|
public void updateCacheRelevance() {
|
217
|
if (detailElement != null) {
|
218
|
detailElement.updateCacheRelevance();
|
219
|
}
|
220
|
}
|
221
|
|
222
|
@Override
|
223
|
public void addDependsOnCache(ToggleableTextElement toggleElement) {
|
224
|
if (detailElement != null) {
|
225
|
detailElement.addDependsOnCache(toggleElement);
|
226
|
}
|
227
|
}
|
228
|
|
229
|
@Override
|
230
|
public CacheRelevance cacheRelevance() {
|
231
|
return detailElement.cacheRelevance();
|
232
|
}
|
233
|
|
234
|
@Override
|
235
|
public void expansionStateChanging(ExpansionEvent e) {
|
236
|
// logger.warn("Expansion State Changing");
|
237
|
}
|
238
|
|
239
|
@Override
|
240
|
public void expansionStateChanged(ExpansionEvent e) {
|
241
|
// logger.warn("Expansion State Changed");
|
242
|
}
|
243
|
|
244
|
public void setIndent(int indent){
|
245
|
TableWrapData tableWrapData = (TableWrapData)this.getLayoutData();
|
246
|
if (tableWrapData == null){
|
247
|
tableWrapData = new TableWrapData();
|
248
|
}
|
249
|
tableWrapData.indent = indent;
|
250
|
setLayoutData(tableWrapData);
|
251
|
}
|
252
|
}
|