1
|
/**
|
2
|
* Copyright (C) 2007 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
package eu.etaxonomy.taxeditor.ui.section;
|
10
|
|
11
|
import org.eclipse.jface.util.PropertyChangeEvent;
|
12
|
import org.eclipse.swt.SWT;
|
13
|
import org.eclipse.swt.events.SelectionEvent;
|
14
|
import org.eclipse.swt.events.SelectionListener;
|
15
|
import org.eclipse.swt.graphics.Color;
|
16
|
import org.eclipse.swt.widgets.Button;
|
17
|
import org.eclipse.swt.widgets.Composite;
|
18
|
import org.eclipse.swt.widgets.Display;
|
19
|
import org.eclipse.ui.forms.widgets.TableWrapLayout;
|
20
|
|
21
|
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
|
22
|
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
|
23
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
24
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
25
|
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
|
26
|
import eu.etaxonomy.taxeditor.model.ImageResources;
|
27
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
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.IEnableableFormElement;
|
33
|
import eu.etaxonomy.taxeditor.ui.element.IEntityElement;
|
34
|
import eu.etaxonomy.taxeditor.ui.element.ISelectable;
|
35
|
import eu.etaxonomy.taxeditor.ui.element.LabelElement;
|
36
|
import eu.etaxonomy.taxeditor.ui.element.LayoutConstants;
|
37
|
|
38
|
/**
|
39
|
* Visualizes an element of type ENTITY in an {@link AbstractEntityCollectionSection}
|
40
|
* and links listener functionalities to it.
|
41
|
*
|
42
|
* @param ENTITY the type of the element which is visualized by this class
|
43
|
*
|
44
|
* @author n.hoffmann
|
45
|
* @created Nov 16, 2009
|
46
|
*/
|
47
|
public abstract class AbstractEntityCollectionElement<ENTITY>
|
48
|
extends AbstractCdmFormElement
|
49
|
implements IEntityElement<ENTITY>, SelectionListener,
|
50
|
IConversationEnabled, IEnableableFormElement {
|
51
|
|
52
|
protected ENTITY entity;
|
53
|
|
54
|
private final Composite container;
|
55
|
|
56
|
/**
|
57
|
* Composite "around" the actual content. Is used for control action like e.g. remove button
|
58
|
*/
|
59
|
private final Composite box;
|
60
|
|
61
|
private boolean isEnabled;
|
62
|
|
63
|
private Button btnRemove;
|
64
|
|
65
|
private Button btnChooseEntity;
|
66
|
|
67
|
private Color backgroundColor;
|
68
|
|
69
|
private LabelElement warnForReferencedObjects;
|
70
|
|
71
|
public AbstractEntityCollectionElement(CdmFormFactory formFactory,
|
72
|
AbstractFormSection section, ENTITY entity, SelectionListener removeListener,
|
73
|
Color backgroundColor, int style) {
|
74
|
super(formFactory, (ICdmFormElement) section);
|
75
|
this.entity = entity;
|
76
|
|
77
|
init();
|
78
|
|
79
|
formFactory.addPropertyChangeListener(this);
|
80
|
|
81
|
box = formFactory.createComposite(section.getLayoutComposite());
|
82
|
box.setBackgroundMode(SWT.INHERIT_DEFAULT);
|
83
|
addControl(box);
|
84
|
|
85
|
TableWrapLayout boxLayout = LayoutConstants.LAYOUT(4, false);
|
86
|
boxLayout.topMargin = 4;
|
87
|
boxLayout.bottomMargin = 4;
|
88
|
box.setLayout(boxLayout);
|
89
|
|
90
|
box.setLayoutData(LayoutConstants.FILL_HORIZONTALLY());
|
91
|
|
92
|
container = formFactory.createComposite(box);
|
93
|
container.setBackgroundMode(SWT.INHERIT_DEFAULT);
|
94
|
|
95
|
setLayoutComposite(container);
|
96
|
|
97
|
addControl(container);
|
98
|
TableWrapLayout containerLayout = LayoutConstants.LAYOUT(2, false);
|
99
|
containerLayout.horizontalSpacing = 5;
|
100
|
|
101
|
container.setLayout(containerLayout);
|
102
|
container.setLayoutData(LayoutConstants.FILL_HORIZONTALLY());
|
103
|
|
104
|
if (removeListener != null) {
|
105
|
btnRemove = formFactory.createButton(box, null, SWT.PUSH);
|
106
|
addControl(btnRemove);
|
107
|
btnRemove.setLayoutData(LayoutConstants.RIGHT());
|
108
|
btnRemove.setImage(ImageResources
|
109
|
.getImage(ImageResources.TRASH_ICON));
|
110
|
btnRemove.setToolTipText("Remove");
|
111
|
|
112
|
btnRemove.addSelectionListener(removeListener);
|
113
|
btnRemove.setEnabled(isEnabled);
|
114
|
}
|
115
|
|
116
|
|
117
|
|
118
|
setEntity(entity);
|
119
|
createControls(this, style);
|
120
|
}
|
121
|
|
122
|
/**
|
123
|
* Init gets executed before any other setup of the section takes place
|
124
|
*
|
125
|
* Implement this if you want to configure the section
|
126
|
*/
|
127
|
public void init() {
|
128
|
// default implementation is empty
|
129
|
}
|
130
|
|
131
|
public abstract void setEntity(ENTITY entity);
|
132
|
|
133
|
@Override
|
134
|
public ENTITY getEntity() {
|
135
|
return entity;
|
136
|
}
|
137
|
|
138
|
/**
|
139
|
* Sub classes should override to provide the functionality to choose the
|
140
|
* entity from existing ones from the data source.<br>
|
141
|
* <b>Note:</b> to enable this functionality sub classes have to set
|
142
|
* the corresponding flag in the super constructor
|
143
|
* @return an existing entity from the data source
|
144
|
*/
|
145
|
protected ENTITY selectFromDialog(){
|
146
|
return null;
|
147
|
}
|
148
|
|
149
|
public abstract void createControls(ICdmFormElement element, int style);
|
150
|
|
151
|
/**
|
152
|
* Mark <code>this</code> element as selected.
|
153
|
*/
|
154
|
@Override
|
155
|
public void setSelected(boolean selected) {
|
156
|
|
157
|
for (ICdmFormElement element : getElements()) {
|
158
|
if (element instanceof ISelectable) {
|
159
|
((ISelectable) element).setSelected(selected);
|
160
|
}
|
161
|
}
|
162
|
setBackground(selected ? SELECTED : getPersistentBackground());
|
163
|
}
|
164
|
|
165
|
@Override
|
166
|
public void propertyChange(PropertyChangeEvent event) {
|
167
|
if (event == null) {
|
168
|
return;
|
169
|
}
|
170
|
Object eventSource = event.getSource();
|
171
|
if (getElements().contains(eventSource) || getControls().contains(eventSource)) {
|
172
|
handleEvent(eventSource);
|
173
|
}
|
174
|
}
|
175
|
|
176
|
public abstract void handleEvent(Object eventSource);
|
177
|
|
178
|
@Override
|
179
|
public void setBackground(Color color) {
|
180
|
if(box.isDisposed() || container.isDisposed()){
|
181
|
return;
|
182
|
}
|
183
|
backgroundColor = color;
|
184
|
super.setBackground(color);
|
185
|
box.setBackground(color);
|
186
|
container.setBackground(color);
|
187
|
}
|
188
|
|
189
|
/**
|
190
|
* {@inheritDoc}
|
191
|
*
|
192
|
* React when selection occurs
|
193
|
*/
|
194
|
@Override
|
195
|
public void widgetSelected(SelectionEvent e) {
|
196
|
|
197
|
}
|
198
|
|
199
|
@Override
|
200
|
public void widgetDefaultSelected(SelectionEvent e) {
|
201
|
}
|
202
|
|
203
|
@Override
|
204
|
public Composite getLayoutComposite() {
|
205
|
return container;
|
206
|
}
|
207
|
|
208
|
public Color getBackgroundColor() {
|
209
|
return backgroundColor;
|
210
|
}
|
211
|
|
212
|
public Composite getBox() {
|
213
|
return box;
|
214
|
}
|
215
|
|
216
|
@Override
|
217
|
public ConversationHolder getConversationHolder() {
|
218
|
if (getParentElement() instanceof IConversationEnabled) {
|
219
|
return ((IConversationEnabled) getParentElement())
|
220
|
.getConversationHolder();
|
221
|
}
|
222
|
throw new IllegalArgumentException(
|
223
|
"Parent element should be IConversationEnabled");
|
224
|
}
|
225
|
|
226
|
@Override
|
227
|
public void update(CdmDataChangeMap changeEvents) {
|
228
|
}
|
229
|
|
230
|
public void setWarnForReferencedObjects(LabelElement warnForReferencedObjects) {
|
231
|
this.warnForReferencedObjects = warnForReferencedObjects;
|
232
|
}
|
233
|
|
234
|
public void setWarnForReferencingObjects(ICdmFormElement formElement, int defaultReferencingObjects){
|
235
|
if (entity instanceof CdmBase){
|
236
|
CdmBase cdmBase = (CdmBase)entity;
|
237
|
if (cdmBase.getId() != 0){
|
238
|
long referencingObjectsCount = CdmStore.getCommonService().getReferencingObjectsCount(cdmBase);
|
239
|
|
240
|
if (referencingObjectsCount > defaultReferencingObjects){
|
241
|
setWarnForReferencedObjects(formFactory.createLabel(formElement, CdmUtils.Nz("The "+ cdmBase.getUserFriendlyTypeName()+" is referenced by more than one object, if you change it, it is changed for all these objects")));
|
242
|
warnForReferencedObjects.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_RED));
|
243
|
warnForReferencedObjects.setLayout(LayoutConstants.FILL(2, 3));
|
244
|
warnForReferencedObjects.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_RED));
|
245
|
}
|
246
|
}
|
247
|
}
|
248
|
}
|
249
|
|
250
|
public void setWarnForReferencingObjects(ICdmFormElement formElement){
|
251
|
setWarnForReferencingObjects(formElement, 1);
|
252
|
}
|
253
|
|
254
|
public void setWarnForReferencingObjectsVisible(boolean isVisible){
|
255
|
if (warnForReferencedObjects != null){
|
256
|
warnForReferencedObjects.setVisible(isVisible);
|
257
|
}
|
258
|
}
|
259
|
|
260
|
@Override
|
261
|
public void setEnabled(boolean enabled) {
|
262
|
this.isEnabled = enabled;
|
263
|
if (btnRemove != null){
|
264
|
btnRemove.setEnabled(enabled);
|
265
|
}
|
266
|
if (btnChooseEntity != null){
|
267
|
btnChooseEntity.setEnabled(enabled);
|
268
|
}
|
269
|
|
270
|
for (ICdmFormElement element:this.getElements()){
|
271
|
if (element instanceof IEnableableFormElement){
|
272
|
((IEnableableFormElement)element).setEnabled(enabled);
|
273
|
}
|
274
|
}
|
275
|
}
|
276
|
|
277
|
@Override
|
278
|
public boolean isEnabled() {
|
279
|
return isEnabled;
|
280
|
}
|
281
|
}
|