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 //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
);
74 setText(getHeading());
76 addExpansionListener(this);
79 createControls(this, style
);
82 createControlsByType(this, clazz
, SWT
.NULL
);
87 public void setText(String title
) {
88 super.setText(StoreUtil
.cleanTitleString(title
));
91 public AbstractCdmDetailElement
<ENTITY
> getDetailElement() {
95 public void setDetailElement(AbstractCdmDetailElement
<ENTITY
> detailElement
) {
96 this.detailElement
= detailElement
;
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
;
104 getLayoutComposite().setLayout(layout
);
105 if(entityClass
==null){
106 detailElement
= createCdmDetailElement(formElement
, style
);
109 detailElement
= createCdmDetailElementByType(formElement
, entityClass
, style
);
113 protected void createControls(AbstractCdmDetailSection
<ENTITY
> formElement
, int style
) {
114 createControlsByType(formElement
, null, style
);
117 protected abstract AbstractCdmDetailElement
<ENTITY
> createCdmDetailElement(AbstractCdmDetailSection
<ENTITY
> parentElement
, int style
);
119 protected AbstractCdmDetailElement
<ENTITY
> createCdmDetailElementByType(AbstractCdmDetailSection
<ENTITY
> parentElement
, Class
<ENTITY
> entityClass
, int style
){
120 return createCdmDetailElement(parentElement
, style
);
123 public abstract String
getHeading();
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());
138 public void setBackground(Color color
) {
139 if (detailElement
!= null) {
140 detailElement
.setBackground(color
);
142 super.setBackground(color
);
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
);
152 requiredCrud
= EnumSet
.of(CRUD
.UPDATE
);
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
);
163 if (entity
!= null && entity
instanceof CdmBase
){
164 boolean hasPermission
= CdmStore
.currentAuthentiationHasPermission(StoreUtil
.getCdmEntity(entity
), requiredCrud
);
165 detailElement
.setEnabled(isEnabled() && hasPermission
, except
);
167 detailElement
.setEnabled(isEnabled() && true, except
);
171 super.setEntity(entity
);
176 protected void setSectionTitle() {
179 if (getEntity() != null && (getEntity() instanceof IdentifiableEntity
) && !(getEntity() instanceof SpecimenOrObservationBase
)) {
180 if (getEntity() instanceof DefinedTermBase
<?
> ){
181 label
= ((DefinedTermBase
<?
>)getEntity()).getLabel(PreferencesUtil
.getGlobalLanguage());
183 label
= ((DefinedTermBase
<?
>)getEntity()).getLabel();
185 } else if (getEntity() instanceof TextData
){
186 label
= ((TextData
)getEntity()).getLanguageText(PreferencesUtil
.getGlobalLanguage()).getText();
188 label
= ((TextData
)getEntity()).getLanguageText(Language
.DEFAULT()).getText();
191 if(CdmUtils
.isBlank(label
)){
192 label
= ((IdentifiableEntity
<?
>) getEntity()).getTitleCache();
194 if(CdmUtils
.isBlank(label
)){
195 label
= ((IdentifiableEntity
<?
>) getEntity()).generateTitle();
197 title
= ": " + label
;
199 this.setText(String
.format("%s%s", getHeading(), title
));
200 setTextClient(createToolbar());
203 protected Control
createToolbar() {
204 ToolBarManager toolBarManager
= new ToolBarManager(SWT
.FLAT
);
205 return toolBarManager
.createControl(this);
208 public void updateTitle() {
216 public void updateCacheRelevance() {
217 if (detailElement
!= null) {
218 detailElement
.updateCacheRelevance();
223 public void addDependsOnCache(ToggleableTextElement toggleElement
) {
224 if (detailElement
!= null) {
225 detailElement
.addDependsOnCache(toggleElement
);
230 public CacheRelevance
cacheRelevance() {
231 return detailElement
.cacheRelevance();
235 public void expansionStateChanging(ExpansionEvent e
) {
236 // logger.warn("Expansion State Changing");
240 public void expansionStateChanged(ExpansionEvent e
) {
241 // logger.warn("Expansion State Changed");
244 public void setIndent(int indent
){
245 TableWrapData tableWrapData
= (TableWrapData
)this.getLayoutData();
246 if (tableWrapData
== null){
247 tableWrapData
= new TableWrapData();
249 tableWrapData
.indent
= indent
;
250 setLayoutData(tableWrapData
);