Fixes #2309
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / selection / AbstractSelectionElement.java
1 /**
2 *
3 */
4 package eu.etaxonomy.taxeditor.ui.selection;
5
6 import org.eclipse.core.runtime.IStatus;
7 import org.eclipse.jface.wizard.WizardDialog;
8 import org.eclipse.swt.SWT;
9 import org.eclipse.swt.events.SelectionAdapter;
10 import org.eclipse.swt.events.SelectionEvent;
11 import org.eclipse.swt.events.SelectionListener;
12 import org.eclipse.swt.graphics.Color;
13 import org.eclipse.swt.widgets.Button;
14 import org.eclipse.swt.widgets.Composite;
15 import org.eclipse.swt.widgets.Label;
16 import org.eclipse.swt.widgets.Shell;
17
18 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
19 import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
20 import eu.etaxonomy.cdm.common.CdmUtils;
21 import eu.etaxonomy.cdm.model.common.IIdentifiableEntity;
22 import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
23 import eu.etaxonomy.taxeditor.model.ImageResources;
24 import eu.etaxonomy.taxeditor.preference.Resources;
25 import eu.etaxonomy.taxeditor.store.StoreUtil;
26 import eu.etaxonomy.taxeditor.ui.forms.AbstractCdmFormElement;
27 import eu.etaxonomy.taxeditor.ui.forms.CdmFormFactory;
28 import eu.etaxonomy.taxeditor.ui.forms.CdmPropertyChangeEvent;
29 import eu.etaxonomy.taxeditor.ui.forms.ICdmFormElement;
30 import eu.etaxonomy.taxeditor.ui.forms.IEnableableFormElement;
31 import eu.etaxonomy.taxeditor.ui.forms.IEntityElement;
32 import eu.etaxonomy.taxeditor.ui.forms.ILabeledElement;
33 import eu.etaxonomy.taxeditor.ui.forms.ISelectableElement;
34 import eu.etaxonomy.taxeditor.ui.forms.SelectionArbitrator;
35
36 /**
37 * <p>
38 * Abstract AbstractSelectionElement class.
39 * </p>
40 *
41 * @author n.hoffmann
42 * @created Nov 17, 2009
43 * @version 1.0
44 * @param <T>
45 */
46 public abstract class AbstractSelectionElement<T> extends
47 AbstractCdmFormElement implements SelectionListener,
48 IEnableableFormElement, ISelectableElement, IEntityElement<T>,
49 ILabeledElement, IConversationEnabled {
50
51 protected T entity;
52
53 protected Label label;
54 protected Label text;
55 protected Button button_selection;
56
57 private SelectionArbitrator selectionArbitrator;
58
59 protected Button button_edit;
60
61 private final String labelString;
62
63 private Composite selectableComposite;
64
65 private Button button_remove;
66
67 private final boolean isEditable;
68
69 private final boolean isDeletable;
70
71 private final ConversationHolder conversation;
72
73 /**
74 * <p>
75 * Constructor for AbstractSelectionElement.
76 * </p>
77 *
78 * @param formFactory
79 * a {@link eu.etaxonomy.taxeditor.ui.forms.CdmFormFactory}
80 * object.
81 * @param conversation
82 * TODO
83 * @param parentElement
84 * a {@link eu.etaxonomy.taxeditor.ui.forms.ICdmFormElement}
85 * object.
86 * @param labelString
87 * a {@link java.lang.String} object.
88 * @param entity
89 * a T object.
90 * @param isEditable
91 * a boolean.
92 * @param isSelectable
93 * a boolean.
94 * @param isDeletable
95 * a boolean.
96 * @param style
97 * a int.
98 * @param <T>
99 * a T object.
100 */
101 public AbstractSelectionElement(CdmFormFactory formFactory,
102 ConversationHolder conversation, ICdmFormElement parentElement,
103 String labelString, T entity, boolean isEditable,
104 boolean isSelectable, boolean isDeletable, int style) {
105 super(formFactory, parentElement);
106
107 this.isEditable = isEditable;
108 this.isDeletable = isDeletable;
109
110 this.labelString = labelString;
111
112 this.conversation = conversation;
113
114 if (isSelectable && formFactory.getSelectionProvider() != null) {
115 selectionArbitrator = formFactory.createSelectionArbitrator(this);
116 }
117
118 createControls(getLayoutComposite(), SWT.NULL);
119
120 setEntity(entity);
121 }
122
123 private void createControls(Composite parent, int style) {
124
125 label = formFactory.createLabel(getLayoutComposite(), labelString,
126 SWT.NULL);
127
128 addControl(label);
129
130 selectableComposite = formFactory.createComposite(getLayoutComposite());
131
132 int columns = 2;
133 if (isEditable) {
134 columns += 1;
135 }
136 if (isDeletable) {
137 columns += 1;
138 }
139
140 selectableComposite.setLayout(CdmFormFactory.LAYOUT(columns, false));
141 selectableComposite.setLayoutData(CdmFormFactory.FILL_HORIZONTALLY());
142
143 addControl(selectableComposite);
144
145 text = formFactory.createLabel(selectableComposite, null, SWT.WRAP);
146 addControl(text);
147
148 text.setLayoutData(CdmFormFactory.FILL_HORIZONTALLY());
149 text.setBackground(StoreUtil
150 .getColor(Resources.COLOR_TEXT_DISABLED_BACKGROUND));
151
152 button_selection = formFactory.createButton(selectableComposite, null,
153 SWT.PUSH);
154 button_selection.setImage(ImageResources
155 .getImage(ImageResources.BROWSE_ICON));
156 button_selection.setToolTipText("Browse existing");
157 addControl(button_selection);
158 button_selection.addSelectionListener(this);
159
160 if (isEditable) {
161 button_edit = formFactory.createButton(selectableComposite, null,
162 SWT.PUSH);
163 button_edit.setImage(ImageResources
164 .getImage(ImageResources.EDIT_ICON));
165 button_edit.setToolTipText("Edit");
166 addControl(button_edit);
167 button_edit.addSelectionListener(new EditListener(this));
168 }
169
170 if (isDeletable) {
171 button_remove = formFactory.createButton(selectableComposite, null,
172 SWT.PUSH);
173 button_remove.setImage(ImageResources
174 .getImage(ImageResources.TRASH_ICON));
175 button_remove.setToolTipText("Remove");
176 addControl(button_remove);
177 button_remove.addSelectionListener(new DeleteListener(this));
178 }
179 }
180
181 /**
182 * Return the selected object
183 *
184 * @return a T object.
185 */
186 public T getSelection() {
187 return entity;
188 }
189
190 /*
191 * (non-Javadoc)
192 *
193 * @see
194 * eu.etaxonomy.taxeditor.forms.IEnableableFormElement#setEnabled(boolean)
195 */
196 /** {@inheritDoc} */
197 @Override
198 public void setEnabled(boolean enabled) {
199 button_selection.setEnabled(enabled);
200 if (isEditable) {
201 button_edit.setEnabled(enabled && entity != null);
202 }
203 }
204
205 /**
206 * <p>
207 * setSelectionInternal
208 * </p>
209 *
210 * @param selection
211 * a T object.
212 */
213 protected void setSelectionInternal(T selection) {
214 if (selection != null && !selection.equals(this.entity)) {
215 setEntity(selection);
216 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
217 }
218 }
219
220 /**
221 * <p>
222 * Setter for the field <code>entity</code>.
223 * </p>
224 *
225 * @param selection
226 * a T object.
227 */
228 public void setEntity(T selection) {
229 this.entity = selection;
230 updateElement();
231 }
232
233 /**
234 * Updates this elements view
235 */
236 protected void updateElement() {
237 String title = CdmUtils.Nz(getTitle());
238 // we have to duplicate ampersands otherwise they are treated as
239 // mnenomic (see Label.setText() documentation)
240 title = title.replace("&", "&&");
241 text.setText(title); // title can be null
242 if (isEditable) {
243 button_edit.setEnabled(entity != null);
244 }
245 }
246
247 /**
248 * <p>
249 * updateFromWizard
250 * </p>
251 */
252 protected void updateFromWizard() {
253 updateElement();
254 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
255 }
256
257 /**
258 * <p>
259 * getTitle
260 * </p>
261 *
262 * @return a {@link java.lang.String} object.
263 */
264 protected String getTitle() {
265 if (entity != null && entity instanceof IIdentifiableEntity) {
266 return ((IIdentifiableEntity) entity).getTitleCache();
267 }
268 return "";
269 }
270
271 /** {@inheritDoc} */
272 @Override
273 public void setSelected(boolean selected) {
274 Color color = getColor(selected);
275 getLayoutComposite().setBackground(color);
276 selectableComposite.setBackground(color);
277 label.setBackground(color);
278 }
279
280 /*
281 * (non-Javadoc)
282 *
283 * @see eu.etaxonomy.taxeditor.forms.IEntityElement#getEntity()
284 */
285 /**
286 * <p>
287 * Getter for the field <code>entity</code>.
288 * </p>
289 *
290 * @return a T object.
291 */
292 @Override
293 public T getEntity() {
294 return entity;
295 }
296
297 /*
298 * (non-Javadoc)
299 *
300 * @see eu.etaxonomy.taxeditor.forms.section.cdmdetail.ISelectableElement#
301 * getSelectionArbitrator()
302 */
303 /**
304 * <p>
305 * Getter for the field <code>selectionArbitrator</code>.
306 * </p>
307 *
308 * @return a {@link eu.etaxonomy.taxeditor.ui.forms.SelectionArbitrator}
309 * object.
310 */
311 @Override
312 public SelectionArbitrator getSelectionArbitrator() {
313 return selectionArbitrator;
314 }
315
316 /**
317 * Convenient access to current shell
318 *
319 * @return a {@link org.eclipse.swt.widgets.Shell} object.
320 */
321 protected Shell getShell() {
322 return getLayoutComposite().getShell();
323 }
324
325 /** {@inheritDoc} */
326 @Override
327 public void setIrrelevant(boolean irrelevant) {
328 String colorId = irrelevant ? Resources.COLOR_COMPOSITE_IRRELEVANT
329 : Resources.COLOR_TEXT_DISABLED_BACKGROUND;
330
331 Color color = StoreUtil.getColor(colorId);
332 text.setBackground(color);
333 }
334
335 private class DeleteListener extends SelectionAdapter {
336
337 private final AbstractSelectionElement<T> selectionElement;
338
339 public DeleteListener(AbstractSelectionElement<T> selectionElement) {
340 this.selectionElement = selectionElement;
341 }
342
343 @Override
344 public void widgetSelected(SelectionEvent e) {
345 setEntity(null);
346 firePropertyChangeEvent(new CdmPropertyChangeEvent(
347 selectionElement, null));
348 }
349 }
350
351 private class EditListener extends SelectionAdapter {
352
353 private final AbstractSelectionElement<T> selectionElement;
354
355 public EditListener(AbstractSelectionElement<T> selectionElement) {
356 this.selectionElement = selectionElement;
357 }
358
359 /** {@inheritDoc} */
360 @Override
361 public void widgetSelected(SelectionEvent e) {
362 WizardDialog dialog = new WizardDialog(selectionElement.getShell(),
363 new EditFromSelectionWizard(selectionElement));
364 if (dialog.open() == IStatus.OK) {
365 selectionElement.updateFromWizard();
366 }
367 }
368 }
369
370 // not used
371 /** {@inheritDoc} */
372 @Override
373 public void widgetDefaultSelected(SelectionEvent e) {
374 }
375
376 /**
377 * <p>
378 * getConversationHolder
379 * </p>
380 *
381 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
382 * object.
383 */
384 @Override
385 public ConversationHolder getConversationHolder() {
386 return conversation;
387 }
388
389 /** {@inheritDoc} */
390 @Override
391 public void setBackground(Color color) {
392 label.setBackground(color);
393 }
394
395 /** {@inheritDoc} */
396 @Override
397 public void setLabel(String labelString) {
398 if (label != null) {
399 label.setText(labelString);
400 }
401 }
402
403 /**
404 * <p>
405 * Getter for the field <code>label</code>.
406 * </p>
407 *
408 * @return a {@link java.lang.String} object.
409 */
410 @Override
411 public String getLabel() {
412 if (label != null) {
413 return label.getText();
414 }
415 return null;
416 }
417
418 /** {@inheritDoc} */
419 @Override
420 public void update(CdmDataChangeMap changeEvents) {
421 }
422 }