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