- moved ICdmFormElement to campanula.compatibility
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / section / AbstractEntityCollectionElement.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.util.PropertyChangeEvent;
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.events.SelectionEvent;
16 import org.eclipse.swt.events.SelectionListener;
17 import org.eclipse.swt.graphics.Color;
18 import org.eclipse.swt.widgets.Button;
19 import org.eclipse.swt.widgets.Composite;
20 import org.eclipse.swt.widgets.Layout;
21 import org.eclipse.ui.forms.widgets.TableWrapLayout;
22
23 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
24 import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
25 import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
26 import eu.etaxonomy.taxeditor.model.ImageResources;
27 import eu.etaxonomy.taxeditor.ui.element.AbstractCdmFormElement;
28 import eu.etaxonomy.taxeditor.ui.element.AbstractFormSection;
29 import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
30 import eu.etaxonomy.taxeditor.ui.element.IEntityElement;
31 import eu.etaxonomy.taxeditor.ui.element.ISelectable;
32 import eu.etaxonomy.taxeditor.ui.element.LayoutConstants;
33 import eu.etaxonomy.taxeditor.ui.section.campanula.compatibility.ICdmFormElement;
34
35 /**
36 * <p>
37 * Abstract AbstractEntityCollectionElement class.
38 * </p>
39 *
40 * @author n.hoffmann
41 * @created Nov 16, 2009
42 * @version 1.0
43 */
44 public abstract class AbstractEntityCollectionElement<ENTITY> extends
45 AbstractCdmFormElement implements IEntityElement<ENTITY>,
46 SelectionListener, IConversationEnabled, ISelectable {
47
48 protected ENTITY entity;
49
50 private final Composite container;
51
52 private final Composite box;
53
54 private Button button_remove;
55
56 private Color backgroundColor;
57
58 /**
59 * <p>
60 * Constructor for AbstractEntityCollectionElement.
61 * </p>
62 *
63 * @param style
64 * a int.
65 * @param formFactory
66 * a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory}
67 * object.
68 * @param section
69 * a {@link eu.etaxonomy.taxeditor.ui.element.AbstractFormSection}
70 * object.
71 * @param entity
72 * a ENTITY object.
73 * @param removeListener
74 * a {@link org.eclipse.swt.events.SelectionListener} object.
75 * @param <ENTITY>
76 * a ENTITY object.
77 */
78 public AbstractEntityCollectionElement(CdmFormFactory formFactory,
79 AbstractFormSection section, ENTITY entity,
80 SelectionListener removeListener, Color backgroundColor, int style) {
81 super(formFactory, (ICdmFormElement) section);
82
83 init();
84
85 formFactory.addPropertyChangeListener(this);
86
87 // section.getLayoutComposite().setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_CYAN));
88
89 box = formFactory.createComposite(section.getLayoutComposite());
90 box.setBackgroundMode(SWT.INHERIT_DEFAULT);
91 addControl(box);
92
93 TableWrapLayout boxLayout = LayoutConstants.LAYOUT(2, false);
94 boxLayout.topMargin = 4;
95 boxLayout.bottomMargin = 4;
96 box.setLayout(boxLayout);
97
98 box.setLayoutData(LayoutConstants.FILL_HORIZONTALLY());
99
100 // box.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_BLUE));
101
102 container = formFactory.createComposite(box);
103 container.setBackgroundMode(SWT.INHERIT_DEFAULT);
104
105 // container.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_RED));
106 setLayoutComposite(container);
107
108 addControl(container);
109 Layout containerLayout = LayoutConstants.LAYOUT(2, false);
110
111 container.setLayout(containerLayout);
112 container.setLayoutData(LayoutConstants.FILL_HORIZONTALLY());
113
114 if (removeListener != null) {
115 button_remove = formFactory.createButton(box, null, SWT.PUSH);
116 addControl(button_remove);
117 button_remove.setLayoutData(LayoutConstants.RIGHT());
118 button_remove.setImage(ImageResources
119 .getImage(ImageResources.TRASH_ICON));
120 button_remove.setToolTipText("Remove");
121
122 button_remove.addSelectionListener(removeListener);
123 }
124
125 createControls(this, style);
126
127 setEntity(entity);
128 }
129
130 /**
131 * Init gets executed before any other setup of the section takes place
132 *
133 * Implement this if you want to configure the section
134 */
135 public void init() {
136 // default implementation is empty
137 }
138
139 /**
140 * <p>
141 * Setter for the field <code>entity</code>.
142 * </p>
143 *
144 * @param entity
145 * a ENTITY object.
146 */
147 public abstract void setEntity(ENTITY entity);
148
149 /**
150 * <p>
151 * Getter for the field <code>entity</code>.
152 * </p>
153 *
154 * @return a ENTITY object.
155 */
156 @Override
157 public ENTITY getEntity() {
158 return entity;
159 }
160
161 /**
162 * <p>
163 * createControls
164 * </p>
165 *
166 * @param element
167 * a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement}
168 * object.
169 * @param style
170 * a int.
171 */
172 public abstract void createControls(ICdmFormElement element, int style);
173
174 /**
175 * Mark <code>this</code> element as selected.
176 */
177 @Override
178 public void setSelected(boolean selected) {
179
180 for (ICdmFormElement element : getElements()) {
181 if (element instanceof ISelectable) {
182 ((ISelectable) element).setSelected(selected);
183 }
184 }
185 setBackground(selected ? SELECTED : getPersistentBackground());
186 }
187
188 /*
189 * (non-Javadoc)
190 *
191 * @see
192 * eu.etaxonomy.taxeditor.forms.AbstractCdmFormElement#propertyChange(org
193 * .eclipse.jface.util.PropertyChangeEvent)
194 */
195 /** {@inheritDoc} */
196 @Override
197 public void propertyChange(PropertyChangeEvent event) {
198 if (event == null) {
199 return;
200 }
201 Object eventSource = event.getSource();
202 if (getElements().contains(eventSource)) {
203 handleEvent(eventSource);
204 }
205 }
206
207 /**
208 * <p>
209 * handleEvent
210 * </p>
211 *
212 * @param eventSource
213 * a {@link java.lang.Object} object.
214 */
215 public abstract void handleEvent(Object eventSource);
216
217 /** {@inheritDoc} */
218 @Override
219 public void setBackground(Color color) {
220 backgroundColor = color;
221 super.setBackground(color);
222 box.setBackground(color);
223 container.setBackground(color);
224 }
225
226 /**
227 * {@inheritDoc}
228 *
229 * React when selection occurs
230 */
231 @Override
232 public void widgetSelected(SelectionEvent e) {
233
234 }
235
236 /** {@inheritDoc} */
237 @Override
238 public void widgetDefaultSelected(SelectionEvent e) {
239 }
240
241 /** {@inheritDoc} */
242 @Override
243 public Composite getLayoutComposite() {
244 return container;
245 }
246
247 /**
248 * <p>
249 * Getter for the field <code>backgroundColor</code>.
250 * </p>
251 *
252 * @return the backgroundColor
253 */
254 public Color getBackgroundColor() {
255 return backgroundColor;
256 }
257
258 /**
259 * <p>
260 * getConversationHolder
261 * </p>
262 *
263 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
264 * object.
265 */
266 @Override
267 public ConversationHolder getConversationHolder() {
268 if (getParentElement() instanceof IConversationEnabled) {
269 return ((IConversationEnabled) getParentElement())
270 .getConversationHolder();
271 }
272 throw new IllegalArgumentException(
273 "Parent element should be IConversationEnabled");
274 }
275
276 /** {@inheritDoc} */
277 @Override
278 public void update(CdmDataChangeMap changeEvents) {
279 }
280 }