Merge branch 'release/4.2.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / section / AbstractCdmDetailSection.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10
11 package eu.etaxonomy.taxeditor.ui.section;
12
13 import org.eclipse.jface.action.ToolBarManager;
14 import org.eclipse.jface.viewers.ISelectionProvider;
15 import org.eclipse.swt.SWT;
16 import org.eclipse.swt.graphics.Color;
17 import org.eclipse.swt.widgets.Control;
18 import org.eclipse.ui.forms.events.ExpansionEvent;
19 import org.eclipse.ui.forms.events.IExpansionListener;
20 import org.eclipse.ui.forms.widgets.ExpandableComposite;
21 import org.eclipse.ui.forms.widgets.TableWrapLayout;
22
23 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
24 import eu.etaxonomy.cdm.model.common.DefinedTermBase;
25 import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
26 import eu.etaxonomy.cdm.model.common.Language;
27 import eu.etaxonomy.cdm.model.description.TextData;
28 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
29 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
30 import eu.etaxonomy.taxeditor.ui.element.AbstractFormSection;
31 import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
32 import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
33 import eu.etaxonomy.taxeditor.ui.element.IEnableableFormElement;
34 import eu.etaxonomy.taxeditor.ui.element.ISelectableElement;
35
36 /**
37 * This class visualizes an CDM entity of type ENTITY.
38 *
39 * @param <ENTITY> A CDM entity which should be visualized by this section.
40 *
41 * @author n.hoffmann
42 * @created Feb 26, 2010
43 * @version 1.0
44 */
45 public abstract class AbstractCdmDetailSection<ENTITY> extends AbstractFormSection<ENTITY> implements IEnableableFormElement, IExpansionListener {
46
47 protected ICdmDetailElement<ENTITY> detailElement;
48
49 public AbstractCdmDetailSection(CdmFormFactory formFactory,
50 ConversationHolder conversation, ICdmFormElement parentElement,
51 ISelectionProvider selectionProvider, int style) {
52 this(formFactory, null, conversation, parentElement, selectionProvider, style);
53 }
54
55
56 public AbstractCdmDetailSection(CdmFormFactory formFactory, Class<ENTITY> clazz,
57 ConversationHolder conversation, ICdmFormElement parentElement,
58 ISelectionProvider selectionProvider, int style) {
59 super(formFactory, parentElement, selectionProvider,
60 ExpandableComposite.CLIENT_INDENT | style);
61
62 setText(getHeading());
63
64 addExpansionListener(this);
65
66 if(clazz==null){
67 createControls(this, style);
68 }
69 else{
70 createControlsByType(this, clazz, SWT.NULL);
71 }
72 }
73
74 protected void createControlsByType(AbstractCdmDetailSection<ENTITY> formElement, Class<ENTITY> entityClass, int style) {
75 TableWrapLayout layout = (TableWrapLayout) getLayoutComposite().getLayout();
76 layout.topMargin = 10;
77 layout.numColumns = DEFAULT_NUM_COLUMNS;
78
79 getLayoutComposite().setLayout(layout);
80 if(entityClass==null){
81 detailElement = createCdmDetailElement(formElement, style);
82 }
83 else{
84 detailElement = createCdmDetailElementByType(formElement, entityClass, style);
85 }
86 }
87
88 protected void createControls(AbstractCdmDetailSection<ENTITY> formElement, int style) {
89 createControlsByType(formElement, null, style);
90 }
91
92 protected abstract ICdmDetailElement<ENTITY> createCdmDetailElement(AbstractCdmDetailSection<ENTITY> parentElement, int style);
93
94 protected ICdmDetailElement<ENTITY> createCdmDetailElementByType(AbstractCdmDetailSection<ENTITY> parentElement, Class<ENTITY> entityClass, int style){
95 return createCdmDetailElement(parentElement, style);
96 }
97
98 public abstract String getHeading();
99
100 /** {@inheritDoc} */
101 @Override
102 public void dispose() {
103 if (detailElement instanceof ISelectableElement) {
104 ISelectableElement selectableElement = (ISelectableElement) detailElement;
105 if (selectableElement.getSelectionArbitrator() != null) {
106 formFactory.destroySelectionArbitrator(selectableElement
107 .getSelectionArbitrator());
108 }
109 }
110 super.dispose();
111 }
112
113 /** {@inheritDoc} */
114 @Override
115 public void setBackground(Color color) {
116 if (detailElement != null) {
117 // detailComposite.setBackground(color);
118 }
119 super.setBackground(color);
120 }
121
122 @Override
123 public void setEntity(ENTITY entity) {
124 if (detailElement != null) {
125 detailElement.setEntity(entity);
126 }
127 super.setEntity(entity);
128 setSectionTitle();
129 layout();
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 detailElement.setIrrelevant(irrelevant);
178 }
179 }
180
181 /** {@inheritDoc} */
182 @Override
183 public void expansionStateChanging(ExpansionEvent e) {
184 // logger.warn("Expansion State Changing");
185 }
186
187 /** {@inheritDoc} */
188 @Override
189 public void expansionStateChanged(ExpansionEvent e) {
190 // logger.warn("Expansion State Changed");
191 }
192 }