Project

General

Profile

Download (9.11 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 * 
3
 */
4
package eu.etaxonomy.taxeditor.forms.selection;
5

    
6
import org.eclipse.swt.SWT;
7
import org.eclipse.swt.events.SelectionAdapter;
8
import org.eclipse.swt.events.SelectionEvent;
9
import org.eclipse.swt.events.SelectionListener;
10
import org.eclipse.swt.graphics.Color;
11
import org.eclipse.swt.widgets.Button;
12
import org.eclipse.swt.widgets.Composite;
13
import org.eclipse.swt.widgets.Label;
14
import org.eclipse.swt.widgets.Shell;
15

    
16
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
17
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
18
import eu.etaxonomy.cdm.common.CdmUtils;
19
import eu.etaxonomy.cdm.model.common.IAnnotatableEntity;
20
import eu.etaxonomy.cdm.model.common.IIdentifiableEntity;
21
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
22
import eu.etaxonomy.taxeditor.editor.EditorUtil;
23
import eu.etaxonomy.taxeditor.forms.AbstractCdmFormElement;
24
import eu.etaxonomy.taxeditor.forms.CdmFormFactory;
25
import eu.etaxonomy.taxeditor.forms.CdmPropertyChangeEvent;
26
import eu.etaxonomy.taxeditor.forms.ICdmFormElement;
27
import eu.etaxonomy.taxeditor.forms.IEnableableFormElement;
28
import eu.etaxonomy.taxeditor.forms.IEntityElement;
29
import eu.etaxonomy.taxeditor.forms.ILabeledElement;
30
import eu.etaxonomy.taxeditor.forms.ISelectableElement;
31
import eu.etaxonomy.taxeditor.forms.SelectionArbitrator;
32
import eu.etaxonomy.taxeditor.model.ImageResources;
33
import eu.etaxonomy.taxeditor.preference.Resources;
34

    
35
/**
36
 * <p>Abstract AbstractSelectionElement class.</p>
37
 *
38
 * @author n.hoffmann
39
 * @created Nov 17, 2009
40
 * @version 1.0
41
 * @param <T>
42
 */
43
public abstract class AbstractSelectionElement<T extends IAnnotatableEntity> extends AbstractCdmFormElement implements SelectionListener, IEnableableFormElement, ISelectableElement, IEntityElement<T>, ILabeledElement, IConversationEnabled{
44

    
45
	protected T entity;
46
	
47
	protected Label label;
48
	protected Label text;
49
	protected Button button_selection;
50
	
51
	private SelectionArbitrator selectionArbitrator;
52

    
53
	protected Button button_edit;
54

    
55
	private String labelString;
56

    
57
	private Composite selectableComposite;
58

    
59
	private Button button_remove;
60

    
61
	private boolean isEditable;
62

    
63
	private boolean isDeletable;
64

    
65
	private EditFromSelectionWizard wizard;
66
	
67
	private ConversationHolder conversation;
68

    
69
	/**
70
	 * <p>Constructor for AbstractSelectionElement.</p>
71
	 *
72
	 * @param formFactory a {@link eu.etaxonomy.taxeditor.forms.CdmFormFactory} object.
73
	 * @param conversation TODO
74
	 * @param parentElement a {@link eu.etaxonomy.taxeditor.forms.ICdmFormElement} object.
75
	 * @param labelString a {@link java.lang.String} object.
76
	 * @param entity a T object.
77
	 * @param isEditable a boolean.
78
	 * @param isSelectable a boolean.
79
	 * @param isDeletable a boolean.
80
	 * @param style a int.
81
	 * @param <T> a T object.
82
	 */
83
	public AbstractSelectionElement(
84
			CdmFormFactory formFactory, 
85
			ConversationHolder conversation, 
86
			ICdmFormElement parentElement, 
87
			String labelString, 
88
			T entity, 
89
			boolean isEditable, 
90
			boolean isSelectable, 
91
			boolean isDeletable, int style) {
92
		super(formFactory, parentElement);
93
		
94
		this.isEditable = isEditable;
95
		this.isDeletable = isDeletable;
96
		
97
		this.labelString = labelString;
98
		
99
		this.conversation = conversation;
100
		
101
		if(isSelectable && formFactory.getSelectionProvider() != null){
102
			selectionArbitrator = formFactory.createSelectionArbitrator(this);
103
		}
104
		
105
		createControls(getLayoutComposite(), SWT.NULL);		
106
		
107
		setEntity(entity);
108
	}
109
	
110
	private void createControls(Composite parent, int style){
111
		
112
		label = formFactory.createLabel(getLayoutComposite(), labelString, SWT.NULL);
113
		
114
		addControl(label);
115
		
116
		selectableComposite = formFactory.createComposite(getLayoutComposite());
117
		
118
		int columns = 2;
119
		if(isEditable) columns += 1;
120
		if(isDeletable) columns += 1;		
121
		
122
		selectableComposite.setLayout(CdmFormFactory.LAYOUT(columns, false));
123
		selectableComposite.setLayoutData(CdmFormFactory.FILL_HORIZONTALLY());
124
		
125
		addControl(selectableComposite);
126
		
127
		text = formFactory.createLabel(selectableComposite, null, SWT.WRAP);
128
		addControl(text);
129
		
130
		text.setLayoutData(CdmFormFactory.FILL_HORIZONTALLY());
131
		text.setBackground(EditorUtil.getColor(Resources.COLOR_TEXT_DISABLED_BACKGROUND));
132
		
133
		button_selection = formFactory.createButton(selectableComposite, null, SWT.PUSH);
134
		button_selection.setImage(ImageResources.getImage(ImageResources.BROWSE_ICON));
135
		button_selection.setToolTipText("Browse existing");
136
		addControl(button_selection);
137
		button_selection.addSelectionListener(this);
138
		
139
		if(isEditable){
140
	 		button_edit = formFactory.createButton(selectableComposite, null, SWT.PUSH);
141
	 		button_edit.setImage(ImageResources.getImage(ImageResources.EDIT_ICON));
142
	 		button_edit.setToolTipText("Edit");
143
			addControl(button_edit);
144
		}
145
		
146
		if(isDeletable){
147
			button_remove = formFactory.createButton(selectableComposite, null, SWT.PUSH);
148
			button_remove.setImage(ImageResources.getImage(ImageResources.TRASH_ICON));
149
			button_remove.setToolTipText("Remove");
150
			addControl(button_remove);
151
			button_remove.addSelectionListener(new DeleteListener(this));
152
		}
153
	}
154
	
155
	/**
156
	 * Return the selected object
157
	 *
158
	 * @return a T object.
159
	 */
160
	public T getSelection(){
161
		return entity;
162
	}
163
	
164
	/*
165
	 * (non-Javadoc)
166
	 * @see eu.etaxonomy.taxeditor.forms.IEnableableFormElement#setEnabled(boolean)
167
	 */
168
	/** {@inheritDoc} */
169
	public void setEnabled(boolean enabled) {
170
		button_selection.setEnabled(enabled);
171
		if(isEditable){
172
			button_edit.setEnabled(enabled && entity != null);
173
		}
174
	}
175
	
176
	/**
177
	 * <p>setSelectionInternal</p>
178
	 *
179
	 * @param selection a T object.
180
	 */
181
	protected void setSelectionInternal(T selection){
182
		if(selection != null && !selection.equals(this.entity)){
183
			setEntity(selection);
184
			firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
185
		}
186
	}
187
	
188
	/**
189
	 * <p>Setter for the field <code>entity</code>.</p>
190
	 *
191
	 * @param selection a T object.
192
	 */
193
	public void setEntity(T selection){
194
		this.entity = selection;	
195
		if(isEditable){
196
			createEditWizard();
197
		}
198
		updateElement();
199
	}
200
	
201
	private void createEditWizard(){
202
		if(entity != null){
203
			wizard = new EditFromSelectionWizard(this);
204
			button_edit.addSelectionListener(wizard);
205
		}
206
	}
207

    
208
	/**
209
	 * Updates this elements view
210
	 */
211
	protected void updateElement(){
212
		String title = CdmUtils.Nz(getTitle());
213
		text.setText(title); // title can be null
214
		if(isEditable){
215
			button_edit.setEnabled(entity != null);
216
		}
217
	}
218
	
219
	/**
220
	 * <p>updateFromWizard</p>
221
	 */
222
	protected void updateFromWizard(){
223
		updateElement();
224
		firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
225
	}
226
	
227
	/**
228
	 * <p>getTitle</p>
229
	 *
230
	 * @return a {@link java.lang.String} object.
231
	 */
232
	protected String getTitle(){
233
		if (entity != null && entity instanceof IIdentifiableEntity) {
234
			return ((IIdentifiableEntity) entity).getTitleCache();
235
		}
236
		return "";
237
	}
238
	
239
	/** {@inheritDoc} */
240
	public void setSelected(boolean selected) {
241
		Color color = getColor(selected);
242
		getLayoutComposite().setBackground(color);
243
		selectableComposite.setBackground(color);
244
		label.setBackground(color);
245
	}
246
	
247
	/* (non-Javadoc)
248
	 * @see eu.etaxonomy.taxeditor.forms.IEntityElement#getEntity()
249
	 */
250
	/**
251
	 * <p>Getter for the field <code>entity</code>.</p>
252
	 *
253
	 * @return a T object.
254
	 */
255
	public T getEntity() {
256
		return entity;
257
	}
258
	
259
	/* (non-Javadoc)
260
	 * @see eu.etaxonomy.taxeditor.forms.section.cdmdetail.ISelectableElement#getSelectionArbitrator()
261
	 */
262
	/**
263
	 * <p>Getter for the field <code>selectionArbitrator</code>.</p>
264
	 *
265
	 * @return a {@link eu.etaxonomy.taxeditor.forms.SelectionArbitrator} object.
266
	 */
267
	public SelectionArbitrator getSelectionArbitrator() {
268
		return selectionArbitrator;
269
	}
270
	
271
	/**
272
	 * Convenient access to current shell
273
	 *
274
	 * @return a {@link org.eclipse.swt.widgets.Shell} object.
275
	 */
276
	protected Shell getShell(){
277
		return getLayoutComposite().getShell();
278
	}
279

    
280
	/** {@inheritDoc} */
281
	public void setIrrelevant(boolean irrelevant) {
282
		String colorId = irrelevant ? Resources.COLOR_COMPOSITE_IRRELEVANT : Resources.COLOR_TEXT_DISABLED_BACKGROUND;
283
		
284
		Color color = EditorUtil.getColor(colorId);
285
		text.setBackground(color);
286
	}
287
	
288
	private class DeleteListener extends SelectionAdapter{
289
		
290
		private AbstractSelectionElement selectionElement;
291

    
292
		public DeleteListener(AbstractSelectionElement selectionElement){
293
			this.selectionElement = selectionElement;
294
		}
295
		
296
		@Override
297
		public void widgetSelected(SelectionEvent e) {
298
			setEntity(null);
299
			firePropertyChangeEvent(new CdmPropertyChangeEvent(selectionElement, null));
300
		}
301
	}
302
	
303
	// not used
304
	/** {@inheritDoc} */
305
	public void widgetDefaultSelected(SelectionEvent e) {}
306
	
307
	/**
308
	 * <p>getConversationHolder</p>
309
	 *
310
	 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
311
	 */
312
	public ConversationHolder getConversationHolder() {
313
		return conversation;
314
	}
315
	
316
	/** {@inheritDoc} */
317
	@Override
318
	public void setBackground(Color color) {
319
		label.setBackground(color);
320
	}
321
	
322
	/** {@inheritDoc} */
323
	public void setLabel(String labelString){
324
		if(label != null){
325
			label.setText(labelString);
326
		}
327
	}
328
	
329
	/**
330
	 * <p>Getter for the field <code>label</code>.</p>
331
	 *
332
	 * @return a {@link java.lang.String} object.
333
	 */
334
	public String getLabel(){
335
		if(label != null){
336
			return label.getText();
337
		}
338
		return null;
339
	}
340
	
341
	/** {@inheritDoc} */
342
	public void update(CdmDataChangeMap changeEvents) {}
343
}
(1-1/17)