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