1
|
/**
|
2
|
*
|
3
|
*/
|
4
|
package eu.etaxonomy.taxeditor.ui.section;
|
5
|
|
6
|
import java.util.Collection;
|
7
|
|
8
|
import org.apache.commons.collections.CollectionUtils;
|
9
|
import org.eclipse.jface.action.Action;
|
10
|
import org.eclipse.jface.action.ToolBarManager;
|
11
|
import org.eclipse.jface.resource.ImageDescriptor;
|
12
|
import org.eclipse.swt.SWT;
|
13
|
import org.eclipse.swt.events.SelectionAdapter;
|
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.graphics.ImageData;
|
18
|
import org.eclipse.swt.widgets.Composite;
|
19
|
import org.eclipse.swt.widgets.Control;
|
20
|
import org.eclipse.swt.widgets.Label;
|
21
|
import org.eclipse.ui.forms.events.ExpansionEvent;
|
22
|
import org.eclipse.ui.forms.events.IExpansionListener;
|
23
|
import org.eclipse.ui.forms.widgets.Section;
|
24
|
|
25
|
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
|
26
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
27
|
import eu.etaxonomy.taxeditor.model.ImageResources;
|
28
|
import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
|
29
|
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
|
30
|
import eu.etaxonomy.taxeditor.preference.Resources;
|
31
|
import eu.etaxonomy.taxeditor.singlesource.ui.forms.CdmFormFactoryFacade;
|
32
|
import eu.etaxonomy.taxeditor.store.StoreUtil;
|
33
|
import eu.etaxonomy.taxeditor.ui.forms.AbstractFormSection;
|
34
|
import eu.etaxonomy.taxeditor.ui.forms.CdmPropertyChangeEvent;
|
35
|
import eu.etaxonomy.taxeditor.ui.forms.ICdmFormElement;
|
36
|
|
37
|
/**
|
38
|
* <p>Abstract AbstractEntityCollectionSection class.</p>
|
39
|
*
|
40
|
* @author n.hoffmann
|
41
|
* @version $Id: $
|
42
|
*/
|
43
|
public abstract class AbstractEntityCollectionSection<ENTITY, ELEMENT> extends AbstractFormSection<ENTITY> implements IExpansionListener{
|
44
|
|
45
|
protected Composite container;
|
46
|
|
47
|
private Label label_empty;
|
48
|
|
49
|
private String title;
|
50
|
|
51
|
/**
|
52
|
* <p>Constructor for AbstractEntityCollectionSection.</p>
|
53
|
*
|
54
|
* @param conversation
|
55
|
* @param parentElement a {@link eu.etaxonomy.taxeditor.ui.forms.ICdmFormElement} object.
|
56
|
* @param style a int.
|
57
|
* @param formFactory a {@link eu.etaxonomy.taxeditor.singlesource.ui.forms.CdmFormFactoryFacade} object.
|
58
|
* @param title a {@link java.lang.String} object.
|
59
|
* @param <ENTITY> a ENTITY object.
|
60
|
* @param <ELEMENT> a ELEMENT object.
|
61
|
*/
|
62
|
public AbstractEntityCollectionSection(CdmFormFactoryFacade formFactory, ConversationHolder conversation, ICdmFormElement parentElement, String title, int style) {
|
63
|
super(formFactory, conversation, parentElement, Section.CLIENT_INDENT | style);
|
64
|
this.title = title;
|
65
|
this.setText(getTitleString());
|
66
|
showToolbar();
|
67
|
|
68
|
addExpansionListener(this);
|
69
|
}
|
70
|
|
71
|
protected Control createToolbar() {
|
72
|
ToolBarManager toolBarManager = new ToolBarManager(SWT.FLAT);
|
73
|
|
74
|
Action addAction = new Action("add", Action.AS_PUSH_BUTTON){
|
75
|
/* (non-Javadoc)
|
76
|
* @see org.eclipse.jface.action.Action#run()
|
77
|
*/
|
78
|
@Override
|
79
|
public void run() {
|
80
|
ELEMENT element = createNewElement();
|
81
|
if(element != null){
|
82
|
addElement(element);
|
83
|
if(! getSection().isExpanded())
|
84
|
getSection().setExpanded(true);
|
85
|
internalUpdateSection(true);
|
86
|
}
|
87
|
}
|
88
|
};
|
89
|
addAction.setImageDescriptor(new ImageDescriptor() {
|
90
|
|
91
|
@Override
|
92
|
public ImageData getImageData() {
|
93
|
return ImageResources.getImage(ImageResources.ADD_ICON).getImageData();
|
94
|
}
|
95
|
});
|
96
|
addAction.setToolTipText(getTooltipString());
|
97
|
|
98
|
toolBarManager.add(addAction);
|
99
|
|
100
|
return toolBarManager.createControl(this);
|
101
|
}
|
102
|
|
103
|
public void showToolbar(){
|
104
|
setTextClient(createToolbar());
|
105
|
}
|
106
|
|
107
|
public void removeToolbar(){
|
108
|
setTextClient(null);
|
109
|
}
|
110
|
|
111
|
/**
|
112
|
* <p>setEntity</p>
|
113
|
*
|
114
|
* @param entity a ENTITY object.
|
115
|
*/
|
116
|
@Override
|
117
|
public void setEntity(ENTITY entity) {
|
118
|
if(entity != null && hasCollectionChanged(entity)){
|
119
|
super.setEntity(entity);
|
120
|
internalUpdateSection(false);
|
121
|
}
|
122
|
setSectionTitle();
|
123
|
layout();
|
124
|
};
|
125
|
|
126
|
/**
|
127
|
* Sets the title for the section. Adds a "+" sign if the collection is not empty for this section.
|
128
|
* Override in subclasses if you want to have a different behaviour.
|
129
|
*/
|
130
|
protected void setSectionTitle() {
|
131
|
if(getCollection(getEntity()) != null && getCollection(getEntity()).size() > 0){
|
132
|
this.setText(getTitleString() + " +");
|
133
|
}else{
|
134
|
this.setText(getTitleString());
|
135
|
}
|
136
|
}
|
137
|
|
138
|
/**
|
139
|
* Removes all content from the container
|
140
|
*/
|
141
|
private void destroyDynamicContent(){
|
142
|
if(label_empty != null){
|
143
|
label_empty.dispose();
|
144
|
label_empty = null;
|
145
|
}
|
146
|
removeElements();
|
147
|
}
|
148
|
|
149
|
/**
|
150
|
* Call this method after dynamically changing the client area.
|
151
|
* If the options changed is set to true, will also fire a state changed
|
152
|
* event to inform the user of unsaved changes.
|
153
|
*
|
154
|
* @param changed a boolean.
|
155
|
*/
|
156
|
protected void internalUpdateSection(boolean changed){
|
157
|
destroyDynamicContent();
|
158
|
if(isExpanded() || expandSectionWhenContentAvailable())
|
159
|
renderContent(isExpanded());
|
160
|
if(changed)
|
161
|
firePropertyChangeEvent(this);
|
162
|
}
|
163
|
|
164
|
/**
|
165
|
* Whether the entities specific collection changed
|
166
|
*
|
167
|
* @param newEntity
|
168
|
* @return
|
169
|
*/
|
170
|
private boolean hasCollectionChanged(ENTITY newEntity){
|
171
|
|
172
|
// return true on null
|
173
|
if(getEntity() == null || newEntity == null) return true;
|
174
|
|
175
|
// if the entities differ the collection has changed
|
176
|
if(! getEntity().equals(newEntity)) return true;
|
177
|
|
178
|
Collection<ELEMENT> oldCollection = getCollection(getEntity());
|
179
|
Collection<ELEMENT> newCollection = getCollection(newEntity);
|
180
|
|
181
|
// return true on null
|
182
|
if(oldCollection == null || newCollection == null) return true;
|
183
|
|
184
|
// if the collections are object equal, check if the content is equal, too
|
185
|
if(oldCollection.equals(newCollection)){
|
186
|
|
187
|
boolean equal = CollectionUtils.isEqualCollection(oldCollection, newCollection);
|
188
|
// return true when collections are not equal
|
189
|
return equal ? false : true;
|
190
|
}
|
191
|
return true;
|
192
|
}
|
193
|
|
194
|
/**
|
195
|
* Create the elements to be shown in this seciton client area
|
196
|
*/
|
197
|
private void renderContent(boolean forceExpansion)
|
198
|
{
|
199
|
Collection<ELEMENT> elements = getCollection(getEntity());
|
200
|
|
201
|
if(elements == null || elements.isEmpty()){
|
202
|
createEmptyContent();
|
203
|
}else{
|
204
|
createDynamicContents(elements);
|
205
|
forceExpansion = true;
|
206
|
}
|
207
|
|
208
|
this.setExpanded(forceExpansion);
|
209
|
|
210
|
reflow();
|
211
|
}
|
212
|
|
213
|
/**
|
214
|
* <p>createEmptyContent</p>
|
215
|
*/
|
216
|
protected void createEmptyContent(){
|
217
|
label_empty = formFactory.createLabel(getLayoutComposite(), getEmptyString());
|
218
|
}
|
219
|
|
220
|
/**
|
221
|
* Creates the widgets for the collection
|
222
|
*
|
223
|
* @param elements a {@link java.util.Collection} object.
|
224
|
*/
|
225
|
protected void createDynamicContents(Collection<ELEMENT> elements)
|
226
|
{
|
227
|
int i = 0;
|
228
|
for(final ELEMENT element : elements){
|
229
|
SelectionAdapter removeListener = new SelectionAdapter(){
|
230
|
@Override
|
231
|
public void widgetSelected(SelectionEvent e) {
|
232
|
removeElement(element);
|
233
|
internalUpdateSection(true);
|
234
|
}
|
235
|
};
|
236
|
boolean modulo = i++%2 == 0;
|
237
|
String colorResource = modulo ? Resources.COLOR_LIST_EVEN : Resources.COLOR_LIST_ODD;
|
238
|
createElementComposite(element, removeListener, StoreUtil.getColor(colorResource));
|
239
|
}
|
240
|
}
|
241
|
|
242
|
/**
|
243
|
* Create the specific widget for the element
|
244
|
*
|
245
|
* @param element a ELEMENT object.
|
246
|
* @param removeListener a {@link org.eclipse.swt.events.SelectionListener} object.
|
247
|
* @param backgroundColor a {@link org.eclipse.swt.graphics.Color} object.
|
248
|
*/
|
249
|
protected void createElementComposite(ELEMENT element, SelectionListener removeListener, Color backgroundColor){
|
250
|
AbstractEntityCollectionElement formElement = formFactory.createEntityCollectionElement(this, element, removeListener, backgroundColor, SWT.NULL);
|
251
|
}
|
252
|
|
253
|
/* (non-Javadoc)
|
254
|
* @see eu.etaxonomy.taxeditor.forms.section.AbstractEditorFormSection#setBackground(org.eclipse.swt.graphics.Color)
|
255
|
*/
|
256
|
/** {@inheritDoc} */
|
257
|
@Override
|
258
|
public void setBackground(Color color) {
|
259
|
if(label_empty != null && !label_empty.isDisposed()){
|
260
|
label_empty.setBackground(color);
|
261
|
}
|
262
|
super.setBackground(color);
|
263
|
}
|
264
|
|
265
|
/**
|
266
|
* <p>getTitleString</p>
|
267
|
*
|
268
|
* @return a {@link java.lang.String} object.
|
269
|
*/
|
270
|
public String getTitleString() {
|
271
|
return CdmUtils.Nz(title);
|
272
|
}
|
273
|
|
274
|
/**
|
275
|
* <p>setTitleString</p>
|
276
|
*
|
277
|
* @param title a {@link java.lang.String} object.
|
278
|
*/
|
279
|
public void setTitleString(String title){
|
280
|
this.title = title;
|
281
|
setSectionTitle();
|
282
|
layout();
|
283
|
}
|
284
|
|
285
|
/** {@inheritDoc} */
|
286
|
public void expansionStateChanging(ExpansionEvent e) {
|
287
|
// logger.warn("Expansion State Changing");
|
288
|
}
|
289
|
|
290
|
/** {@inheritDoc} */
|
291
|
public void expansionStateChanged(ExpansionEvent e) {
|
292
|
if(isExpanded()){
|
293
|
renderContent(isExpanded());
|
294
|
}else{
|
295
|
destroyDynamicContent();
|
296
|
}
|
297
|
}
|
298
|
|
299
|
private boolean expandSectionWhenContentAvailable(){
|
300
|
return PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.SHOULD_EXPAND_SECTION_WHEN_DATA_AVAILABLE);
|
301
|
}
|
302
|
|
303
|
/**
|
304
|
* Get the specific collection of this entity
|
305
|
*
|
306
|
* @param entity a ENTITY object.
|
307
|
* @return a {@link java.util.Collection} object.
|
308
|
*/
|
309
|
public abstract Collection<ELEMENT> getCollection(ENTITY entity);
|
310
|
|
311
|
/**
|
312
|
* Create a new Element for this collection
|
313
|
*
|
314
|
* @return a ELEMENT object.
|
315
|
*/
|
316
|
public abstract ELEMENT createNewElement();
|
317
|
|
318
|
/**
|
319
|
* Add an element to the entities collection
|
320
|
*
|
321
|
* @param element a ELEMENT object.
|
322
|
*/
|
323
|
public abstract void addElement(ELEMENT element);
|
324
|
|
325
|
/**
|
326
|
* Remove an element from the entities collection
|
327
|
*
|
328
|
* @param element a ELEMENT object.
|
329
|
*/
|
330
|
public abstract void removeElement(ELEMENT element);
|
331
|
|
332
|
/**
|
333
|
* String to display when the collection is empty
|
334
|
*
|
335
|
* @return a {@link java.lang.String} object.
|
336
|
*/
|
337
|
public abstract String getEmptyString();
|
338
|
|
339
|
/**
|
340
|
* <p>getTooltipString</p>
|
341
|
*
|
342
|
* @return String to display when hovering the add button
|
343
|
*/
|
344
|
protected abstract String getTooltipString();
|
345
|
}
|