86dbf40e53f3f952c8e3e5a87f057dbf088b92a4
[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.element.AbstractFormSection;
26 import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
27 import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory.DetailType;
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 AbstractCdmDetailElement<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 super(formFactory, parentElement, selectionProvider,
70 ExpandableComposite.CLIENT_INDENT | style);
71
72 setText(getHeading());
73
74 addExpansionListener(this);
75
76 createControls(this, SWT.NULL);
77 }
78
79 public AbstractCdmDetailSection(CdmFormFactory formFactory, Class<ENTITY> clazz,
80 ConversationHolder conversation, ICdmFormElement parentElement,
81 ISelectionProvider selectionProvider, int style) {
82 super(formFactory, parentElement, selectionProvider,
83 ExpandableComposite.CLIENT_INDENT | style);
84
85 setText(getHeading());
86
87 addExpansionListener(this);
88
89 createControlsByType(this, clazz, SWT.NULL);
90 }
91
92 /**
93 * @param abstractCdmDetailSection
94 * @param definedTermClass
95 * @param null1
96 */
97 protected void createControlsByType(AbstractCdmDetailSection<ENTITY> abstractCdmDetailSection, Class<ENTITY> entityClass, int style) {
98 }
99
100
101 /**
102 * <p>
103 * createControls
104 * </p>
105 *
106 * @param formElement
107 * a
108 * {@link eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailSection}
109 * object.
110 * @param style
111 * a int.
112 */
113 protected void createControls(AbstractCdmDetailSection<ENTITY> formElement, int style) {
114 TableWrapLayout layout = (TableWrapLayout) getLayoutComposite().getLayout();
115 layout.topMargin = 10;
116 layout.numColumns = 2;
117
118 getLayoutComposite().setLayout(layout);
119 detailElement = createCdmDetailElement(formElement, style);
120 }
121
122 //TODO: when all detail type enums are removed this can be abstract
123 protected AbstractCdmDetailElement<ENTITY> createCdmDetailElement(AbstractCdmDetailSection<ENTITY> parentElement, int style){
124 return formFactory.createCdmDetailElement(getDetailType(), parentElement, style);
125 }
126
127 /**
128 * <p>
129 * getDetailType
130 * </p>
131 *
132 * @return a
133 * {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory.DetailType}
134 * object.
135 */
136 protected abstract DetailType getDetailType();
137
138 /**
139 * <p>
140 * getHeading
141 * </p>
142 *
143 * @return the heading for this section
144 */
145 public abstract String getHeading();
146
147 /** {@inheritDoc} */
148 @Override
149 public void dispose() {
150 if (detailElement instanceof ISelectableElement) {
151 ISelectableElement selectableElement = (ISelectableElement) detailElement;
152 if (selectableElement.getSelectionArbitrator() != null) {
153 formFactory.destroySelectionArbitrator(selectableElement
154 .getSelectionArbitrator());
155 }
156 }
157 super.dispose();
158 }
159
160 /*
161 * (non-Javadoc)
162 *
163 * @see
164 * eu.etaxonomy.taxeditor.forms.section.AbstractEditorFormSection#setBackground
165 * (org.eclipse.swt.graphics.Color)
166 */
167 /** {@inheritDoc} */
168 @Override
169 public void setBackground(Color color) {
170 if (detailElement != null) {
171 // detailComposite.setBackground(color);
172 }
173 super.setBackground(color);
174 }
175
176 /**
177 * <p>
178 * setEntity
179 * </p>
180 *
181 * @param entity
182 * a ENTITY object.
183 */
184 @Override
185 public void setEntity(ENTITY entity) {
186 if (detailElement != null) {
187 detailElement.setEntity(entity);
188 }
189 super.setEntity(entity);
190 setSectionTitle();
191 layout();
192 }
193
194 /**
195 * <p>
196 * setSectionTitle
197 * </p>
198 */
199 protected void setSectionTitle() {
200 String title = "";
201 if (getEntity() != null && (getEntity() instanceof IdentifiableEntity)) {
202 title = ": " + ((IdentifiableEntity) getEntity()).getTitleCache();
203 }
204 this.setText(String.format("%s%s", getHeading(), title));
205 setTextClient(createToolbar());
206 }
207
208 /**
209 * @return
210 */
211 protected Control createToolbar() {
212 ToolBarManager toolBarManager = new ToolBarManager(SWT.FLAT);
213 return toolBarManager.createControl(this);
214 }
215
216 /**
217 * <p>
218 * updateTitle
219 * </p>
220 */
221 public void updateTitle() {
222 if (!isDisposed()) {
223 setSectionTitle();
224 layout();
225 }
226 }
227
228 /** {@inheritDoc} */
229 @Override
230 public void setIrrelevant(boolean irrelevant) {
231 if (detailElement != null) {
232 detailElement.setIrrelevant(irrelevant);
233 }
234 }
235
236 /** {@inheritDoc} */
237 @Override
238 public void expansionStateChanging(ExpansionEvent e) {
239 // logger.warn("Expansion State Changing");
240 }
241
242 /** {@inheritDoc} */
243 @Override
244 public void expansionStateChanged(ExpansionEvent e) {
245 // logger.warn("Expansion State Changed");
246 }
247 }