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