Project

General

Profile

Download (8.94 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 *
3
 */
4
package eu.etaxonomy.taxeditor.ui.section;
5

    
6
import java.util.Collection;
7

    
8
import org.eclipse.jface.action.Action;
9
import org.eclipse.jface.action.IAction;
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.ExpandableComposite;
24

    
25
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
26
import eu.etaxonomy.cdm.common.CdmUtils;
27
import eu.etaxonomy.taxeditor.model.AbstractUtility;
28
import eu.etaxonomy.taxeditor.model.ImageResources;
29
import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
30
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
31
import eu.etaxonomy.taxeditor.preference.Resources;
32
import eu.etaxonomy.taxeditor.ui.campanula.compatibility.ICdmFormElement;
33
import eu.etaxonomy.taxeditor.ui.element.AbstractFormSection;
34
import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
35

    
36
/**
37
 * This class visualizes an CDM entity of type ENTITY and additionally provides the functionality to add
38
 * other elements of type ELEMENT to them.
39
 *
40
 * @param <ENTITY> A CDM entity which should be visualized by this section.
41
 * @param <ELEMENT> An element that can be added (multiple times) to this entity.
42
 *
43
 * @author n.hoffmann
44
 * @version $Id: $
45
 */
46

    
47
public abstract class AbstractEntityCollectionSection<ENTITY, ELEMENT> extends AbstractFormSection<ENTITY> implements IExpansionListener{
48

    
49
	protected Composite container;
50

    
51
	private Label label_empty;
52

    
53
	private String title;
54

    
55
	/**
56
	 * <p>Constructor for AbstractEntityCollectionSection.</p>
57
	 *
58
	 * @param conversation
59
	 * @param parentElement a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement} object.
60
	 * @param style a int.
61
	 * @param formFactory a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory} object.
62
	 * @param title a {@link java.lang.String} object.
63
	 * @param <ENTITY> a ENTITY object.
64
	 * @param <ELEMENT> a ELEMENT object.
65
	 */
66
	public AbstractEntityCollectionSection(CdmFormFactory formFactory, ConversationHolder conversation, ICdmFormElement parentElement, String title, int style) {
67
		super(formFactory, parentElement, ExpandableComposite.CLIENT_INDENT | style);
68
		this.title = title;
69
		this.setText(getTitleString());
70
		showToolbar();
71

    
72
		addExpansionListener(this);
73
	}
74

    
75
	protected Control createToolbar() {
76
		ToolBarManager toolBarManager = new ToolBarManager(SWT.FLAT);
77

    
78
		Action addAction = new Action("add", IAction.AS_PUSH_BUTTON){
79
			/* (non-Javadoc)
80
			 * @see org.eclipse.jface.action.Action#run()
81
			 */
82
			@Override
83
			public void run() {
84
				ELEMENT element = createNewElement();
85
				if(element != null){
86
					addElement(element);
87
					if(! getSection().isExpanded()) {
88
                        getSection().setExpanded(true);
89
                    }
90
					internalUpdateSection(true);
91
				}
92
			}
93
		};
94
		addAction.setImageDescriptor(new ImageDescriptor() {
95

    
96
			@Override
97
			public ImageData getImageData() {
98
				return ImageResources.getImage(ImageResources.ADD_ICON).getImageData();
99
			}
100
		});
101
		addAction.setToolTipText(getTooltipString());
102

    
103
		toolBarManager.add(addAction);
104

    
105
		return toolBarManager.createControl(this);
106
	}
107

    
108
	public void showToolbar(){
109
		setTextClient(createToolbar());
110
	}
111

    
112
	public void removeToolbar(){
113
		setTextClient(null);
114
	}
115

    
116
	/**
117
	 * <p>setEntity</p>
118
	 *
119
	 * @param entity a ENTITY object.
120
	 */
121
	@Override
122
	public void setEntity(ENTITY entity) {
123
		if(entity != null){
124
			super.setEntity(entity);
125
			internalUpdateSection(false);
126
		}
127
		setSectionTitle();
128
		layout();
129
	}
130

    
131
	/**
132
	 * Sets the title for the section. Adds a "+" sign if the collection is not empty for this section.
133
	 * Override in subclasses if you want to have a different behaviour.
134
	 */
135
	protected void setSectionTitle() {
136
		if(getCollection(getEntity()) != null && getCollection(getEntity()).size() > 0){
137
			this.setText(getTitleString() + " +");
138
		}else{
139
			this.setText(getTitleString());
140
		}
141
	}
142

    
143
	/**
144
	 * Removes all content from the container
145
	 */
146
	private void destroyDynamicContent(){
147
		if(label_empty != null){
148
			label_empty.dispose();
149
			label_empty = null;
150
		}
151
		removeElements();
152
	}
153

    
154
	/**
155
	 * Call this method after dynamically changing the client area.
156
	 * If the options changed is set to true, will also fire a state changed
157
	 * event to inform the user of unsaved changes.
158
	 *
159
	 * @param changed a boolean.
160
	 */
161
	protected void internalUpdateSection(boolean changed){
162
		destroyDynamicContent();
163
		if(isExpanded() || expandSectionWhenContentAvailable()) {
164
            renderContent(isExpanded());
165
        }
166
		if(changed) {
167
            firePropertyChangeEvent(this);
168
        }
169
	}
170

    
171
	/**
172
	 * Create the elements to be shown in this section client area
173
	 */
174
	private void renderContent(boolean forceExpansion)
175
	{
176
		Collection<ELEMENT> elements = getCollection(getEntity());
177

    
178
		if(elements == null || elements.isEmpty()){
179
			createEmptyContent();
180
		}else{
181
			createDynamicContents(elements);
182
			forceExpansion = true;
183
		}
184

    
185
		this.setExpanded(forceExpansion);
186

    
187
		reflow();
188
	}
189

    
190
	/**
191
	 * <p>createEmptyContent</p>
192
	 */
193
	protected void createEmptyContent(){
194
		label_empty = formFactory.createLabel(getLayoutComposite(), getEmptyString());
195
	}
196

    
197
	/**
198
	 * Creates the widgets for the collection
199
	 *
200
	 * @param elements a {@link java.util.Collection} object.
201
	 */
202
	protected void createDynamicContents(Collection<ELEMENT> elements)
203
	{
204
		int i = 0;
205
		for(final ELEMENT element : elements){
206
			SelectionAdapter removeListener = new SelectionAdapter(){
207
				@Override
208
				public void widgetSelected(SelectionEvent e) {
209
					removeElement(element);
210
					internalUpdateSection(true);
211
				}
212
			};
213
			boolean modulo = i++%2 == 0;
214
			String colorResource = modulo ? Resources.COLOR_LIST_EVEN : Resources.COLOR_LIST_ODD;
215
			createElementComposite(element, removeListener, AbstractUtility.getColor(colorResource));
216
		}
217
	}
218

    
219
	/**
220
	 * Create the specific widget for the element
221
	 *
222
	 * @param element a ELEMENT object.
223
	 * @param removeListener a {@link org.eclipse.swt.events.SelectionListener} object.
224
	 * @param backgroundColor a {@link org.eclipse.swt.graphics.Color} object.
225
	 */
226
	protected void createElementComposite(ELEMENT element, SelectionListener removeListener, Color backgroundColor){
227
		formFactory.createEntityCollectionElement(this, element, removeListener, backgroundColor, SWT.NULL);
228
	}
229

    
230
	/* (non-Javadoc)
231
	 * @see eu.etaxonomy.taxeditor.forms.section.AbstractEditorFormSection#setBackground(org.eclipse.swt.graphics.Color)
232
	 */
233
	/** {@inheritDoc} */
234
	@Override
235
	public void setBackground(Color color) {
236
		if(label_empty != null && !label_empty.isDisposed()){
237
			label_empty.setBackground(color);
238
		}
239
		super.setBackground(color);
240
	}
241

    
242
	/**
243
	 * <p>getTitleString</p>
244
	 *
245
	 * @return a {@link java.lang.String} object.
246
	 */
247
	public String getTitleString() {
248
		return CdmUtils.Nz(title);
249
	}
250

    
251
	/**
252
	 * <p>setTitleString</p>
253
	 *
254
	 * @param title a {@link java.lang.String} object.
255
	 */
256
	public void setTitleString(String title){
257
		this.title = title;
258
		setSectionTitle();
259
		layout();
260
	}
261

    
262
	/** {@inheritDoc} */
263
	@Override
264
    public void expansionStateChanging(ExpansionEvent e) {
265
//		logger.warn("Expansion State Changing");
266
	}
267

    
268
	/** {@inheritDoc} */
269
	@Override
270
    public void expansionStateChanged(ExpansionEvent e) {
271
		if(isExpanded()){
272
			renderContent(isExpanded());
273
		}else{
274
			destroyDynamicContent();
275
		}
276
	}
277

    
278
	private boolean expandSectionWhenContentAvailable(){
279
		return PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.SHOULD_EXPAND_SECTION_WHEN_DATA_AVAILABLE);
280
	}
281

    
282
	/**
283
	 * Remove an element from the entities collection and update the section
284
	 *
285
	 * @param element a ELEMENT object.
286
	 */
287
	public void removeElementAndUpdate(ELEMENT element) {
288
		removeElement(element);
289
		internalUpdateSection(true);
290
	}
291

    
292
	/**
293
	 * Get the specific collection of this entity
294
	 *
295
	 * @param entity a ENTITY object.
296
	 * @return a {@link java.util.Collection} object.
297
	 */
298
	public abstract Collection<ELEMENT> getCollection(ENTITY entity);
299

    
300
	/**
301
	 * Create a new Element for this collection
302
	 *
303
	 * @return a ELEMENT object.
304
	 */
305
	public abstract ELEMENT createNewElement();
306

    
307
	/**
308
	 * Add an element to the entities collection
309
	 *
310
	 * @param element a ELEMENT object.
311
	 */
312
	public abstract void addElement(ELEMENT element);
313

    
314
	/**
315
	 * Remove an element from the entities collection
316
	 *
317
	 * @param element a ELEMENT object.
318
	 */
319
	public abstract void removeElement(ELEMENT element);
320

    
321
	/**
322
	 * String to display when the collection is empty
323
	 *
324
	 * @return a {@link java.lang.String} object.
325
	 */
326
	public abstract String getEmptyString();
327

    
328
	/**
329
	 * <p>getTooltipString</p>
330
	 *
331
	 * @return String to display when hovering the add button
332
	 */
333
	protected abstract String getTooltipString();
334
}
(4-4/8)