2 * Copyright (C) 2007 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
.ui
.section
;
11 import java
.util
.ArrayList
;
12 import java
.util
.Collection
;
13 import java
.util
.EnumSet
;
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
;
26 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
27 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
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
.CacheRelevance
;
39 import eu
.etaxonomy
.taxeditor
.ui
.element
.CdmFormFactory
;
40 import eu
.etaxonomy
.taxeditor
.ui
.element
.ICacheRelevantFormElement
;
41 import eu
.etaxonomy
.taxeditor
.ui
.element
.ICdmFormElement
;
42 import eu
.etaxonomy
.taxeditor
.ui
.element
.IEnableableFormElement
;
43 import eu
.etaxonomy
.taxeditor
.ui
.element
.ISelectableElement
;
44 import eu
.etaxonomy
.taxeditor
.ui
.element
.ToggleableTextElement
;
47 * This class visualizes an CDM entity of type ENTITY.
49 * @param <ENTITY> A CDM entity which should be visualized by this section.
52 * @created Feb 26, 2010
54 public abstract class AbstractCdmDetailSection
<ENTITY
>
55 extends AbstractFormSection
<ENTITY
>
56 implements IEnableableFormElement
, IExpansionListener
, ICacheRelevantFormElement
{
58 protected AbstractCdmDetailElement
<ENTITY
> detailElement
;
60 public AbstractCdmDetailSection(CdmFormFactory formFactory
,
61 ICdmFormElement parentElement
,
62 ISelectionProvider selectionProvider
, int style
) {
63 this(formFactory
, null, parentElement
, selectionProvider
, style
);
66 public AbstractCdmDetailSection(CdmFormFactory formFactory
, Class
<ENTITY
> clazz
,
67 ICdmFormElement parentElement
,
68 ISelectionProvider selectionProvider
, int style
) {
69 super(formFactory
, parentElement
, selectionProvider
,
70 ExpandableComposite
.CLIENT_INDENT
| style
);
72 setText(getHeading());
74 addExpansionListener(this);
77 createControls(this, style
);
80 createControlsByType(this, clazz
, SWT
.NULL
);
85 public void setText(String title
) {
86 super.setText(StoreUtil
.cleanTitleString(title
));
89 public AbstractCdmDetailElement
<ENTITY
> getDetailElement() {
93 public void setDetailElement(AbstractCdmDetailElement
<ENTITY
> detailElement
) {
94 this.detailElement
= detailElement
;
97 protected void createControlsByType(AbstractCdmDetailSection
<ENTITY
> formElement
, Class
<ENTITY
> entityClass
, int style
) {
98 TableWrapLayout layout
= (TableWrapLayout
) getLayoutComposite().getLayout();
99 layout
.topMargin
= 10;
100 layout
.numColumns
= DEFAULT_NUM_COLUMNS
;
102 getLayoutComposite().setLayout(layout
);
103 if(entityClass
==null){
104 detailElement
= createCdmDetailElement(formElement
, style
);
107 detailElement
= createCdmDetailElementByType(formElement
, entityClass
, style
);
111 protected void createControls(AbstractCdmDetailSection
<ENTITY
> formElement
, int style
) {
112 createControlsByType(formElement
, null, style
);
115 protected abstract AbstractCdmDetailElement
<ENTITY
> createCdmDetailElement(AbstractCdmDetailSection
<ENTITY
> parentElement
, int style
);
117 protected AbstractCdmDetailElement
<ENTITY
> createCdmDetailElementByType(AbstractCdmDetailSection
<ENTITY
> parentElement
, Class
<ENTITY
> entityClass
, int style
){
118 return createCdmDetailElement(parentElement
, style
);
121 public abstract String
getHeading();
124 public void dispose() {
125 if (detailElement
instanceof ISelectableElement
) {
126 ISelectableElement selectableElement
= (ISelectableElement
) detailElement
;
127 if (selectableElement
.getSelectionArbitrator() != null) {
128 formFactory
.destroySelectionArbitrator(selectableElement
129 .getSelectionArbitrator());
136 public void setBackground(Color color
) {
137 if (detailElement
!= null) {
138 detailElement
.setBackground(color
);
140 super.setBackground(color
);
144 public void setEntity(ENTITY entity
) {
145 EnumSet
<CRUD
> requiredCrud
;
146 if(entity
== null || (entity
instanceof CdmBase
&&StoreUtil
.getCdmEntity(entity
).getId() == 0 )) {
147 // new entity, not yet saved
148 requiredCrud
= EnumSet
.of(CRUD
.CREATE
);
150 requiredCrud
= EnumSet
.of(CRUD
.UPDATE
);
152 if (detailElement
!= null) {
153 detailElement
.setEntity(entity
);
154 formFactory
.adapt(detailElement
);
155 Collection
<Object
> except
= new ArrayList
<>();
156 for(ICdmFormElement formElement
:detailElement
.getElements()){
157 if(formElement
instanceof IEnableableFormElement
&& !((IEnableableFormElement
) formElement
).isEnabled()){
158 except
.add(formElement
);
161 if (entity
!= null && entity
instanceof CdmBase
){
162 boolean hasPermission
= CdmStore
.currentAuthentiationHasPermission(StoreUtil
.getCdmEntity(entity
), requiredCrud
);
163 detailElement
.setEnabled(isEnabled() && hasPermission
, except
);
165 detailElement
.setEnabled(isEnabled() && true, except
);
169 super.setEntity(entity
);
174 public void setEntityWithoutUpdate(ENTITY entity
) {
176 if (detailElement
!= null) {
177 detailElement
.fillFields();
180 super.setEntity(entity
);
185 protected void setSectionTitle() {
188 if (getEntity() != null && (getEntity() instanceof IdentifiableEntity
) && !(getEntity() instanceof SpecimenOrObservationBase
)) {
189 if (getEntity() instanceof DefinedTermBase
<?
> ){
190 label
= ((DefinedTermBase
<?
>)getEntity()).getPreferredRepresentation(PreferencesUtil
.getGlobalLanguage()).getLabel();
191 // if (label == null){
192 // label = ((DefinedTermBase<?>)getEntity()).getLabel();
194 } else if (getEntity() instanceof TextData
){
195 label
= ((TextData
)getEntity()).getLanguageText(PreferencesUtil
.getGlobalLanguage()).getText();
197 label
= ((TextData
)getEntity()).getLanguageText(Language
.DEFAULT()).getText();
200 if(CdmUtils
.isBlank(label
)){
201 label
= ((IdentifiableEntity
<?
>) getEntity()).getTitleCache();
203 if(CdmUtils
.isBlank(label
)){
204 label
= ((IdentifiableEntity
<?
>) getEntity()).generateTitle();
206 title
= ": " + label
;
208 this.setText(String
.format("%s%s", getHeading(), title
));
209 setTextClient(createToolbar());
212 protected Control
createToolbar() {
213 ToolBarManager toolBarManager
= new ToolBarManager(SWT
.FLAT
);
214 return toolBarManager
.createControl(this);
217 public void updateTitle() {
225 public void updateCacheRelevance() {
226 if (detailElement
!= null) {
227 detailElement
.updateCacheRelevance();
232 public void addDependsOnCache(ToggleableTextElement toggleElement
) {
233 if (detailElement
!= null) {
234 detailElement
.addDependsOnCache(toggleElement
);
239 public CacheRelevance
cacheRelevance() {
240 return detailElement
.cacheRelevance();
244 public void expansionStateChanging(ExpansionEvent e
) {
245 // logger.warn("Expansion State Changing");
249 public void expansionStateChanged(ExpansionEvent e
) {
250 // logger.warn("Expansion State Changed");
253 public void setIndent(int indent
){
254 TableWrapData tableWrapData
= (TableWrapData
)this.getLayoutData();
255 if (tableWrapData
== null){
256 tableWrapData
= new TableWrapData();
258 tableWrapData
.indent
= indent
;
259 setLayoutData(tableWrapData
);