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