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.
10 package eu
.etaxonomy
.taxeditor
.ui
.section
;
12 import java
.util
.ArrayList
;
13 import java
.util
.Collection
;
14 import java
.util
.EnumSet
;
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
.TableWrapData
;
25 import org
.eclipse
.ui
.forms
.widgets
.TableWrapLayout
;
27 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
28 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
29 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
30 import eu
.etaxonomy
.cdm
.model
.common
.IdentifiableEntity
;
31 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
32 import eu
.etaxonomy
.cdm
.model
.description
.TextData
;
33 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationBase
;
34 import eu
.etaxonomy
.cdm
.model
.permission
.CRUD
;
35 import eu
.etaxonomy
.cdm
.model
.term
.DefinedTermBase
;
36 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
37 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
38 import eu
.etaxonomy
.taxeditor
.store
.StoreUtil
;
39 import eu
.etaxonomy
.taxeditor
.ui
.element
.AbstractFormSection
;
40 import eu
.etaxonomy
.taxeditor
.ui
.element
.CdmFormFactory
;
41 import eu
.etaxonomy
.taxeditor
.ui
.element
.ICdmFormElement
;
42 import eu
.etaxonomy
.taxeditor
.ui
.element
.IEnableableFormElement
;
43 import eu
.etaxonomy
.taxeditor
.ui
.element
.ISelectableElement
;
46 * This class visualizes an CDM entity of type ENTITY.
48 * @param <ENTITY> A CDM entity which should be visualized by this section.
51 * @created Feb 26, 2010
53 public abstract class AbstractCdmDetailSection
<ENTITY
>
54 extends AbstractFormSection
<ENTITY
>
55 implements IEnableableFormElement
, IExpansionListener
{
57 protected AbstractCdmDetailElement
<ENTITY
> detailElement
;
59 public AbstractCdmDetailSection(CdmFormFactory formFactory
,
60 ConversationHolder conversation
, ICdmFormElement parentElement
,
61 ISelectionProvider selectionProvider
, int style
) {
62 this(formFactory
, null, conversation
, parentElement
, selectionProvider
, style
);
65 public AbstractCdmDetailSection(CdmFormFactory formFactory
, Class
<ENTITY
> clazz
,
66 ConversationHolder conversation
, ICdmFormElement parentElement
,
67 ISelectionProvider selectionProvider
, int style
) {
68 super(formFactory
, parentElement
, selectionProvider
,
69 ExpandableComposite
.CLIENT_INDENT
| style
);
71 setText(getHeading());
73 addExpansionListener(this);
76 createControls(this, style
);
79 createControlsByType(this, clazz
, SWT
.NULL
);
84 public void setText(String title
) {
85 super.setText(StoreUtil
.cleanTitleString(title
));
88 public AbstractCdmDetailElement
<ENTITY
> getDetailElement() {
92 public void setDetailElement(AbstractCdmDetailElement
<ENTITY
> detailElement
) {
93 this.detailElement
= detailElement
;
96 protected void createControlsByType(AbstractCdmDetailSection
<ENTITY
> formElement
, Class
<ENTITY
> entityClass
, int style
) {
97 TableWrapLayout layout
= (TableWrapLayout
) getLayoutComposite().getLayout();
98 layout
.topMargin
= 10;
99 layout
.numColumns
= DEFAULT_NUM_COLUMNS
;
101 getLayoutComposite().setLayout(layout
);
102 if(entityClass
==null){
103 detailElement
= createCdmDetailElement(formElement
, style
);
106 detailElement
= createCdmDetailElementByType(formElement
, entityClass
, style
);
110 protected void createControls(AbstractCdmDetailSection
<ENTITY
> formElement
, int style
) {
111 createControlsByType(formElement
, null, style
);
114 protected abstract AbstractCdmDetailElement
<ENTITY
> createCdmDetailElement(AbstractCdmDetailSection
<ENTITY
> parentElement
, int style
);
116 protected AbstractCdmDetailElement
<ENTITY
> createCdmDetailElementByType(AbstractCdmDetailSection
<ENTITY
> parentElement
, Class
<ENTITY
> entityClass
, int style
){
117 return createCdmDetailElement(parentElement
, style
);
120 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());
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
);
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() {
217 public void setIrrelevant(boolean irrelevant
) {
218 if (detailElement
!= null) {
220 detailElement
.setIrrelevant(irrelevant
);
226 public void expansionStateChanging(ExpansionEvent e
) {
227 // logger.warn("Expansion State Changing");
232 public void expansionStateChanged(ExpansionEvent e
) {
233 // logger.warn("Expansion State Changed");
236 public void setIndent(int indent
){
237 TableWrapData tableWrapData
= (TableWrapData
)this.getLayoutData();
238 if (tableWrapData
== null){
239 tableWrapData
= new TableWrapData();
241 tableWrapData
.indent
= indent
;
242 setLayoutData(tableWrapData
);