fix #3737: improve handling of protected/not protected cache fields
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / section / AbstractCdmDetailSection.java
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 org.eclipse.jface.action.ToolBarManager;
13 import org.eclipse.jface.viewers.ISelectionProvider;
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.graphics.Color;
16 import org.eclipse.swt.widgets.Control;
17 import org.eclipse.ui.forms.events.ExpansionEvent;
18 import org.eclipse.ui.forms.events.IExpansionListener;
19 import org.eclipse.ui.forms.widgets.ExpandableComposite;
20 import org.eclipse.ui.forms.widgets.TableWrapLayout;
21
22 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
23 import eu.etaxonomy.cdm.model.common.DefinedTermBase;
24 import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
25 import eu.etaxonomy.cdm.model.common.Language;
26 import eu.etaxonomy.cdm.model.description.TextData;
27 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
28 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
29 import eu.etaxonomy.taxeditor.ui.element.AbstractFormSection;
30 import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
31 import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
32 import eu.etaxonomy.taxeditor.ui.element.IEnableableFormElement;
33 import eu.etaxonomy.taxeditor.ui.element.ISelectableElement;
34
35 /**
36 * This class visualizes an CDM entity of type ENTITY.
37 *
38 * @param <ENTITY> A CDM entity which should be visualized by this section.
39 *
40 * @author n.hoffmann
41 * @created Feb 26, 2010
42 * @version 1.0
43 */
44 public abstract class AbstractCdmDetailSection<ENTITY> extends AbstractFormSection<ENTITY> implements IEnableableFormElement, IExpansionListener {
45
46 protected ICdmDetailElement<ENTITY> detailElement;
47
48 public AbstractCdmDetailSection(CdmFormFactory formFactory,
49 ConversationHolder conversation, ICdmFormElement parentElement,
50 ISelectionProvider selectionProvider, int style) {
51 this(formFactory, null, conversation, parentElement, selectionProvider, style);
52 }
53
54
55 public AbstractCdmDetailSection(CdmFormFactory formFactory, Class<ENTITY> clazz,
56 ConversationHolder conversation, ICdmFormElement parentElement,
57 ISelectionProvider selectionProvider, int style) {
58 super(formFactory, parentElement, selectionProvider,
59 ExpandableComposite.CLIENT_INDENT | style);
60
61 setText(getHeading());
62
63 addExpansionListener(this);
64
65 if(clazz==null){
66 createControls(this, style);
67 }
68 else{
69 createControlsByType(this, clazz, SWT.NULL);
70 }
71 }
72
73 protected void createControlsByType(AbstractCdmDetailSection<ENTITY> formElement, Class<ENTITY> entityClass, int style) {
74 TableWrapLayout layout = (TableWrapLayout) getLayoutComposite().getLayout();
75 layout.topMargin = 10;
76 layout.numColumns = DEFAULT_NUM_COLUMNS;
77
78 getLayoutComposite().setLayout(layout);
79 if(entityClass==null){
80 detailElement = createCdmDetailElement(formElement, style);
81 }
82 else{
83 detailElement = createCdmDetailElementByType(formElement, entityClass, style);
84 }
85 }
86
87 protected void createControls(AbstractCdmDetailSection<ENTITY> formElement, int style) {
88 createControlsByType(formElement, null, style);
89 }
90
91 protected abstract ICdmDetailElement<ENTITY> createCdmDetailElement(AbstractCdmDetailSection<ENTITY> parentElement, int style);
92
93 protected ICdmDetailElement<ENTITY> createCdmDetailElementByType(AbstractCdmDetailSection<ENTITY> parentElement, Class<ENTITY> entityClass, int style){
94 return createCdmDetailElement(parentElement, style);
95 }
96
97 public abstract String getHeading();
98
99 /** {@inheritDoc} */
100 @Override
101 public void dispose() {
102 if (detailElement instanceof ISelectableElement) {
103 ISelectableElement selectableElement = (ISelectableElement) detailElement;
104 if (selectableElement.getSelectionArbitrator() != null) {
105 formFactory.destroySelectionArbitrator(selectableElement
106 .getSelectionArbitrator());
107 }
108 }
109 super.dispose();
110 }
111
112 /** {@inheritDoc} */
113 @Override
114 public void setBackground(Color color) {
115 if (detailElement != null) {
116 // detailComposite.setBackground(color);
117 }
118 super.setBackground(color);
119 }
120
121 @Override
122 public void setEntity(ENTITY entity) {
123 if (detailElement != null) {
124 detailElement.setEntity(entity);
125 }
126 super.setEntity(entity);
127 setSectionTitle();
128 layout();
129
130 }
131
132 protected void setSectionTitle() {
133 String title = "";
134 String label = "";
135 if (getEntity() != null && (getEntity() instanceof IdentifiableEntity) && !(getEntity() instanceof SpecimenOrObservationBase)) {
136 if (getEntity() instanceof DefinedTermBase<?> ){
137 label = ((DefinedTermBase<?>)getEntity()).getLabel(PreferencesUtil.getGlobalLanguage());
138 if (label == null){
139 label = ((DefinedTermBase<?>)getEntity()).getLabel();
140 }
141
142 } else if (getEntity() instanceof TextData){
143 label = ((TextData)getEntity()).getLanguageText(PreferencesUtil.getGlobalLanguage()).getText();
144 if (label == null){
145 label = ((TextData)getEntity()).getLanguageText(Language.DEFAULT()).getText();
146 }
147 } else{
148 label =((IdentifiableEntity) getEntity()).getTitleCache();
149 }
150 title = ": " + label;
151
152 // we have to duplicate ampersands otherwise they are treated as
153 // mnenomic (see Label.setText() documentation)
154 // see also #4302
155 title = title.replace("&", "&&");
156 }
157 this.setText(String.format("%s%s", getHeading(), title));
158 setTextClient(createToolbar());
159 }
160
161 protected Control createToolbar() {
162 ToolBarManager toolBarManager = new ToolBarManager(SWT.FLAT);
163 return toolBarManager.createControl(this);
164 }
165
166 public void updateTitle() {
167 if (!isDisposed()) {
168 setSectionTitle();
169 layout();
170 }
171 }
172
173 /** {@inheritDoc} */
174 @Override
175 public void setIrrelevant(boolean irrelevant) {
176 if (detailElement != null) {
177
178 detailElement.setIrrelevant(irrelevant);
179 }
180 }
181
182 /** {@inheritDoc} */
183 @Override
184 public void expansionStateChanging(ExpansionEvent e) {
185 // logger.warn("Expansion State Changing");
186 }
187
188 /** {@inheritDoc} */
189 @Override
190 public void expansionStateChanged(ExpansionEvent e) {
191 // logger.warn("Expansion State Changed");
192 }
193 }