merge-update from trunk
[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.IdentifiableEntity;
25 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
26 import eu.etaxonomy.taxeditor.ui.element.AbstractFormSection;
27 import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
28 import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
29 import eu.etaxonomy.taxeditor.ui.element.IEnableableFormElement;
30 import eu.etaxonomy.taxeditor.ui.element.ISelectableElement;
31
32 /**
33 * This class visualizes an CDM entity of type ENTITY.
34 *
35 * @param <ENTITY> A CDM entity which should be visualized by this section.
36 *
37 * @author n.hoffmann
38 * @created Feb 26, 2010
39 * @version 1.0
40 */
41 public abstract class AbstractCdmDetailSection<ENTITY> extends AbstractFormSection<ENTITY> implements IEnableableFormElement, IExpansionListener {
42
43 protected ICdmDetailElement<ENTITY> detailElement;
44
45 /**
46 * <p>
47 * Constructor for AbstractCdmDetailSection.
48 * </p>
49 *
50 * @param formFactory
51 * a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory}
52 * object.
53 * @param conversation
54 * a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
55 * object.
56 * @param parentElement
57 * a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement}
58 * object.
59 * @param selectionProvider
60 * a {@link org.eclipse.jface.viewers.ISelectionProvider} object.
61 * @param style
62 * a int.
63 * @param <ENTITY>
64 * a ENTITY object.
65 */
66 public AbstractCdmDetailSection(CdmFormFactory formFactory,
67 ConversationHolder conversation, ICdmFormElement parentElement,
68 ISelectionProvider selectionProvider, int style) {
69 this(formFactory, null, conversation, parentElement, selectionProvider, style);
70 }
71
72
73 public AbstractCdmDetailSection(CdmFormFactory formFactory, Class<ENTITY> clazz,
74 ConversationHolder conversation, ICdmFormElement parentElement,
75 ISelectionProvider selectionProvider, int style) {
76 super(formFactory, parentElement, selectionProvider,
77 ExpandableComposite.CLIENT_INDENT | style);
78
79 setText(getHeading());
80
81 addExpansionListener(this);
82
83 if(clazz==null){
84 createControls(this, style);
85 }
86 else{
87 createControlsByType(this, clazz, SWT.NULL);
88 }
89 }
90
91 /**
92 * @param abstractCdmDetailSection
93 * @param definedTermClass
94 * @param null1
95 */
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 = 2;
100
101 getLayoutComposite().setLayout(layout);
102 if(entityClass==null){
103 detailElement = createCdmDetailElement(formElement, style);
104 }
105 else{
106 detailElement = createCdmDetailElementByType(formElement, entityClass, style);
107 }
108 }
109
110
111 /**
112 * <p>
113 * createControls
114 * </p>
115 *
116 * @param formElement
117 * a
118 * {@link eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailSection}
119 * object.
120 * @param style
121 * a int.
122 */
123 protected void createControls(AbstractCdmDetailSection<ENTITY> formElement, int style) {
124 createControlsByType(formElement, null, style);
125 }
126
127 protected abstract ICdmDetailElement<ENTITY> createCdmDetailElement(AbstractCdmDetailSection<ENTITY> parentElement, int style);
128
129 protected ICdmDetailElement<ENTITY> createCdmDetailElementByType(AbstractCdmDetailSection<ENTITY> parentElement, Class<ENTITY> entityClass, int style){
130 return createCdmDetailElement(parentElement, style);
131 }
132
133 /**
134 * <p>
135 * getHeading
136 * </p>
137 *
138 * @return the heading for this section
139 */
140 public abstract String getHeading();
141
142 /** {@inheritDoc} */
143 @Override
144 public void dispose() {
145 if (detailElement instanceof ISelectableElement) {
146 ISelectableElement selectableElement = (ISelectableElement) detailElement;
147 if (selectableElement.getSelectionArbitrator() != null) {
148 formFactory.destroySelectionArbitrator(selectableElement
149 .getSelectionArbitrator());
150 }
151 }
152 super.dispose();
153 }
154
155 /*
156 * (non-Javadoc)
157 *
158 * @see
159 * eu.etaxonomy.taxeditor.forms.section.AbstractEditorFormSection#setBackground
160 * (org.eclipse.swt.graphics.Color)
161 */
162 /** {@inheritDoc} */
163 @Override
164 public void setBackground(Color color) {
165 if (detailElement != null) {
166 // detailComposite.setBackground(color);
167 }
168 super.setBackground(color);
169 }
170
171 /**
172 * <p>
173 * setEntity
174 * </p>
175 *
176 * @param entity
177 * a ENTITY object.
178 */
179 @Override
180 public void setEntity(ENTITY entity) {
181 if (detailElement != null) {
182 detailElement.setEntity(entity);
183 }
184 super.setEntity(entity);
185 setSectionTitle();
186 layout();
187 }
188
189 /**
190 * <p>
191 * setSectionTitle
192 * </p>
193 */
194 protected void setSectionTitle() {
195 String title = "";
196 if (getEntity() != null && (getEntity() instanceof IdentifiableEntity) && !(getEntity() instanceof SpecimenOrObservationBase)) {
197 title = ": " + ((IdentifiableEntity) getEntity()).getTitleCache();
198 }
199 this.setText(String.format("%s%s", getHeading(), title));
200 setTextClient(createToolbar());
201 }
202
203 /**
204 * @return
205 */
206 protected Control createToolbar() {
207 ToolBarManager toolBarManager = new ToolBarManager(SWT.FLAT);
208 return toolBarManager.createControl(this);
209 }
210
211 /**
212 * <p>
213 * updateTitle
214 * </p>
215 */
216 public void updateTitle() {
217 if (!isDisposed()) {
218 setSectionTitle();
219 layout();
220 }
221 }
222
223 /** {@inheritDoc} */
224 @Override
225 public void setIrrelevant(boolean irrelevant) {
226 if (detailElement != null) {
227 detailElement.setIrrelevant(irrelevant);
228 }
229 }
230
231 /** {@inheritDoc} */
232 @Override
233 public void expansionStateChanging(ExpansionEvent e) {
234 // logger.warn("Expansion State Changing");
235 }
236
237 /** {@inheritDoc} */
238 @Override
239 public void expansionStateChanged(ExpansionEvent e) {
240 // logger.warn("Expansion State Changed");
241 }
242 }