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
.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
;
48 * This class visualizes an CDM entity of type ENTITY.
50 * @param <ENTITY> A CDM entity which should be visualized by this section.
53 * @created Feb 26, 2010
55 public abstract class AbstractCdmDetailSection
<ENTITY
>
56 extends AbstractFormSection
<ENTITY
>
57 implements IEnableableFormElement
, IExpansionListener
, ICacheRelevantFormElement
{
59 protected AbstractCdmDetailElement
<ENTITY
> detailElement
;
61 public AbstractCdmDetailSection(CdmFormFactory formFactory
,
62 ConversationHolder conversation
, ICdmFormElement parentElement
,
63 ISelectionProvider selectionProvider
, int style
) {
64 this(formFactory
, null, conversation
, parentElement
, selectionProvider
, style
);
67 public AbstractCdmDetailSection(CdmFormFactory formFactory
, Class
<ENTITY
> clazz
,
68 ConversationHolder conversation
, ICdmFormElement parentElement
,
69 ISelectionProvider selectionProvider
, int style
) {
70 super(formFactory
, parentElement
, selectionProvider
,
71 ExpandableComposite
.CLIENT_INDENT
| style
);
73 setText(getHeading());
75 addExpansionListener(this);
78 createControls(this, style
);
81 createControlsByType(this, clazz
, SWT
.NULL
);
86 public void setText(String title
) {
87 super.setText(StoreUtil
.cleanTitleString(title
));
90 public AbstractCdmDetailElement
<ENTITY
> getDetailElement() {
94 public void setDetailElement(AbstractCdmDetailElement
<ENTITY
> detailElement
) {
95 this.detailElement
= detailElement
;
98 protected void createControlsByType(AbstractCdmDetailSection
<ENTITY
> formElement
, Class
<ENTITY
> entityClass
, int style
) {
99 TableWrapLayout layout
= (TableWrapLayout
) getLayoutComposite().getLayout();
100 layout
.topMargin
= 10;
101 layout
.numColumns
= DEFAULT_NUM_COLUMNS
;
103 getLayoutComposite().setLayout(layout
);
104 if(entityClass
==null){
105 detailElement
= createCdmDetailElement(formElement
, style
);
108 detailElement
= createCdmDetailElementByType(formElement
, entityClass
, style
);
112 protected void createControls(AbstractCdmDetailSection
<ENTITY
> formElement
, int style
) {
113 createControlsByType(formElement
, null, style
);
116 protected abstract AbstractCdmDetailElement
<ENTITY
> createCdmDetailElement(AbstractCdmDetailSection
<ENTITY
> parentElement
, int style
);
118 protected AbstractCdmDetailElement
<ENTITY
> createCdmDetailElementByType(AbstractCdmDetailSection
<ENTITY
> parentElement
, Class
<ENTITY
> entityClass
, int style
){
119 return createCdmDetailElement(parentElement
, style
);
122 public abstract String
getHeading();
125 public void dispose() {
126 if (detailElement
instanceof ISelectableElement
) {
127 ISelectableElement selectableElement
= (ISelectableElement
) detailElement
;
128 if (selectableElement
.getSelectionArbitrator() != null) {
129 formFactory
.destroySelectionArbitrator(selectableElement
130 .getSelectionArbitrator());
137 public void setBackground(Color color
) {
138 if (detailElement
!= null) {
139 detailElement
.setBackground(color
);
141 super.setBackground(color
);
145 public void setEntity(ENTITY entity
) {
146 EnumSet
<CRUD
> requiredCrud
;
147 if(entity
== null || (entity
instanceof CdmBase
&&StoreUtil
.getCdmEntity(entity
).getId() == 0 )) {
148 // new entity, not yet saved
149 requiredCrud
= EnumSet
.of(CRUD
.CREATE
);
151 requiredCrud
= EnumSet
.of(CRUD
.UPDATE
);
153 if (detailElement
!= null) {
154 detailElement
.setEntity(entity
);
155 formFactory
.adapt(detailElement
);
156 Collection
<Object
> except
= new ArrayList
<>();
157 for(ICdmFormElement formElement
:detailElement
.getElements()){
158 if(formElement
instanceof IEnableableFormElement
&& !((IEnableableFormElement
) formElement
).isEnabled()){
159 except
.add(formElement
);
162 if (entity
!= null && entity
instanceof CdmBase
){
163 boolean hasPermission
= CdmStore
.currentAuthentiationHasPermission(StoreUtil
.getCdmEntity(entity
), requiredCrud
);
164 detailElement
.setEnabled(isEnabled() && hasPermission
, except
);
166 detailElement
.setEnabled(isEnabled() && true, except
);
170 super.setEntity(entity
);
175 protected void setSectionTitle() {
178 if (getEntity() != null && (getEntity() instanceof IdentifiableEntity
) && !(getEntity() instanceof SpecimenOrObservationBase
)) {
179 if (getEntity() instanceof DefinedTermBase
<?
> ){
180 label
= ((DefinedTermBase
<?
>)getEntity()).getLabel(PreferencesUtil
.getGlobalLanguage());
182 label
= ((DefinedTermBase
<?
>)getEntity()).getLabel();
184 } else if (getEntity() instanceof TextData
){
185 label
= ((TextData
)getEntity()).getLanguageText(PreferencesUtil
.getGlobalLanguage()).getText();
187 label
= ((TextData
)getEntity()).getLanguageText(Language
.DEFAULT()).getText();
190 if(CdmUtils
.isBlank(label
)){
191 label
= ((IdentifiableEntity
) getEntity()).getTitleCache();
193 if(CdmUtils
.isBlank(label
)){
194 label
= ((IdentifiableEntity
) getEntity()).generateTitle();
196 title
= ": " + label
;
198 this.setText(String
.format("%s%s", getHeading(), title
));
199 setTextClient(createToolbar());
202 protected Control
createToolbar() {
203 ToolBarManager toolBarManager
= new ToolBarManager(SWT
.FLAT
);
204 return toolBarManager
.createControl(this);
207 public void updateTitle() {
215 public void updateCacheRelevance() {
216 if (detailElement
!= null) {
217 detailElement
.updateCacheRelevance();
222 public void addDependsOnCache(ToggleableTextElement toggleElement
) {
223 if (detailElement
!= null) {
224 detailElement
.addDependsOnCache(toggleElement
);
229 public CacheRelevance
cacheRelevance() {
230 return detailElement
.cacheRelevance();
234 public void expansionStateChanging(ExpansionEvent e
) {
235 // logger.warn("Expansion State Changing");
239 public void expansionStateChanged(ExpansionEvent e
) {
240 // logger.warn("Expansion State Changed");
243 public void setIndent(int indent
){
244 TableWrapData tableWrapData
= (TableWrapData
)this.getLayoutData();
245 if (tableWrapData
== null){
246 tableWrapData
= new TableWrapData();
248 tableWrapData
.indent
= indent
;
249 setLayoutData(tableWrapData
);