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