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