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