ca1353b2c53d0de4301743261bc778221977301e
[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 * <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 Composite container;
51
52 private 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 public ENTITY getEntity() {
157 return entity;
158 }
159
160 /**
161 * <p>
162 * createControls
163 * </p>
164 *
165 * @param element
166 * a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement}
167 * object.
168 * @param style
169 * a int.
170 */
171 public abstract void createControls(ICdmFormElement element, int style);
172
173 /**
174 * Mark <code>this</code> element as selected.
175 */
176 public void setSelected(boolean selected) {
177
178 for (ICdmFormElement element : getElements()) {
179 if (element instanceof ISelectable) {
180 ((ISelectable) element).setSelected(selected);
181 }
182 }
183 setBackground(selected ? SELECTED : getPersistentBackground());
184 }
185
186 /*
187 * (non-Javadoc)
188 *
189 * @see
190 * eu.etaxonomy.taxeditor.forms.AbstractCdmFormElement#propertyChange(org
191 * .eclipse.jface.util.PropertyChangeEvent)
192 */
193 /** {@inheritDoc} */
194 @Override
195 public void propertyChange(PropertyChangeEvent event) {
196 if (event == null) {
197 return;
198 }
199 Object eventSource = event.getSource();
200 if (getElements().contains(eventSource)) {
201 handleEvent(eventSource);
202 }
203 }
204
205 /**
206 * <p>
207 * handleEvent
208 * </p>
209 *
210 * @param eventSource
211 * a {@link java.lang.Object} object.
212 */
213 public abstract void handleEvent(Object eventSource);
214
215 /** {@inheritDoc} */
216 public void setBackground(Color color) {
217 backgroundColor = color;
218 super.setBackground(color);
219 box.setBackground(color);
220 container.setBackground(color);
221 }
222
223 /**
224 * {@inheritDoc}
225 *
226 * React when selection occurs
227 */
228 public void widgetSelected(SelectionEvent e) {
229
230 }
231
232 /** {@inheritDoc} */
233 public void widgetDefaultSelected(SelectionEvent e) {
234 }
235
236 /** {@inheritDoc} */
237 @Override
238 public Composite getLayoutComposite() {
239 return container;
240 }
241
242 /**
243 * <p>
244 * Getter for the field <code>backgroundColor</code>.
245 * </p>
246 *
247 * @return the backgroundColor
248 */
249 public Color getBackgroundColor() {
250 return backgroundColor;
251 }
252
253 /**
254 * <p>
255 * getConversationHolder
256 * </p>
257 *
258 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
259 * object.
260 */
261 public ConversationHolder getConversationHolder() {
262 if (getParentElement() instanceof IConversationEnabled) {
263 return ((IConversationEnabled) getParentElement())
264 .getConversationHolder();
265 }
266 throw new IllegalArgumentException(
267 "Parent element should be IConversationEnabled");
268 }
269
270 /** {@inheritDoc} */
271 public void update(CdmDataChangeMap changeEvents) {
272 }
273 }