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