cdmlib version change
[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 addControl(button_selection);
168 button_selection.addSelectionListener(this);
169
170 if (isEditable) {
171 button_edit = formFactory.createButton(selectableComposite, null,
172 SWT.PUSH);
173 button_edit.setImage(ImageResources
174 .getImage(ImageResources.EDIT_ICON));
175 button_edit.setToolTipText("Edit");
176 addControl(button_edit);
177 button_edit.addSelectionListener(new EditListener(this));
178 }
179
180 if (isDeletable) {
181 button_remove = formFactory.createButton(selectableComposite, null,
182 SWT.PUSH);
183 button_remove.setImage(ImageResources
184 .getImage(ImageResources.TRASH_ICON));
185 button_remove.setToolTipText("Remove");
186 addControl(button_remove);
187 button_remove.addSelectionListener(new DeleteListener(this));
188 }
189 }
190
191 /**
192 * Return the selected object
193 *
194 * @return a T object.
195 */
196 public T getSelection() {
197 return entity;
198 }
199
200 /*
201 * (non-Javadoc)
202 *
203 * @see
204 * eu.etaxonomy.taxeditor.forms.IEnableableFormElement#setEnabled(boolean)
205 */
206 /** {@inheritDoc} */
207 @Override
208 public void setEnabled(boolean enabled) {
209 button_selection.setEnabled(enabled);
210 if (isEditable) {
211 button_edit.setEnabled(enabled && entity != null);
212 }
213 }
214
215 /**
216 * <p>
217 * setSelectionInternal
218 * </p>
219 *
220 * @param selection
221 * a T object.
222 */
223 protected void setSelectionInternal(T selection) {
224 if (selection != null && !selection.equals(this.entity)) {
225 setEntity(selection);
226 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
227 }
228 }
229
230 /**
231 * <p>
232 * Setter for the field <code>entity</code>.
233 * </p>
234 *
235 * @param selection
236 * a T object.
237 */
238 public void setEntity(T selection) {
239 this.entity = selection;
240 updateElement();
241 }
242
243 /**
244 * Updates this elements view
245 */
246 protected void updateElement() {
247 String title = CdmUtils.Nz(getTitle());
248 // we have to duplicate ampersands otherwise they are treated as
249 // mnenomic (see Label.setText() documentation)
250 title = title.replace("&", "&&");
251 text.setText(title); // title can be null
252 if (isEditable) {
253 button_edit.setEnabled(entity != null);
254 }
255 }
256
257 /**
258 * <p>
259 * updateFromWizard
260 * </p>
261 */
262 protected void updateFromWizard() {
263 updateElement();
264 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
265 }
266
267 /**
268 * <p>
269 * getTitle
270 * </p>
271 *
272 * @return a {@link java.lang.String} object.
273 */
274 protected String getTitle() {
275 if (entity != null && entity instanceof IIdentifiableEntity) {
276 return ((IIdentifiableEntity) entity).getTitleCache();
277 }
278 return "";
279 }
280
281 /** {@inheritDoc} */
282 @Override
283 public void setSelected(boolean selected) {
284 setBackground(selected ? SELECTED : getPersistentBackground());
285 }
286
287 /*
288 * (non-Javadoc)
289 *
290 * @see eu.etaxonomy.taxeditor.forms.IEntityElement#getEntity()
291 */
292 /**
293 * <p>
294 * Getter for the field <code>entity</code>.
295 * </p>
296 *
297 * @return a T object.
298 */
299 @Override
300 public T getEntity() {
301 return entity;
302 }
303
304 /*
305 * (non-Javadoc)
306 *
307 * @see eu.etaxonomy.taxeditor.forms.section.cdmdetail.ISelectableElement#
308 * getSelectionArbitrator()
309 */
310 /**
311 * <p>
312 * Getter for the field <code>selectionArbitrator</code>.
313 * </p>
314 *
315 * @return a {@link eu.etaxonomy.taxeditor.ui.forms.SelectionArbitrator}
316 * object.
317 */
318 @Override
319 public SelectionArbitrator getSelectionArbitrator() {
320 return selectionArbitrator;
321 }
322
323 /**
324 * Convenient access to current shell
325 *
326 * @return a {@link org.eclipse.swt.widgets.Shell} object.
327 */
328 protected Shell getShell() {
329 return getLayoutComposite().getShell();
330 }
331
332 /** {@inheritDoc} */
333 @Override
334 public void setIrrelevant(boolean irrelevant) {
335 String colorId = irrelevant ? Resources.COLOR_COMPOSITE_IRRELEVANT
336 : Resources.COLOR_TEXT_DISABLED_BACKGROUND;
337
338 Color color = StoreUtil.getColor(colorId);
339 text.setBackground(color);
340 }
341
342 private class DeleteListener extends SelectionAdapter {
343
344 private final AbstractSelectionElement<T> selectionElement;
345
346 public DeleteListener(AbstractSelectionElement<T> selectionElement) {
347 this.selectionElement = selectionElement;
348 }
349
350 @Override
351 public void widgetSelected(SelectionEvent e) {
352 setEntity(null);
353 firePropertyChangeEvent(new CdmPropertyChangeEvent(
354 selectionElement, null));
355 }
356 }
357
358 private class EditListener extends SelectionAdapter {
359
360 private final AbstractSelectionElement<T> selectionElement;
361
362 public EditListener(AbstractSelectionElement<T> selectionElement) {
363 this.selectionElement = selectionElement;
364 }
365
366 /** {@inheritDoc} */
367 @Override
368 public void widgetSelected(SelectionEvent e) {
369 WizardDialog dialog = new WizardDialog(selectionElement.getShell(),
370 new EditFromSelectionWizard(selectionElement));
371 if (dialog.open() == IStatus.OK) {
372 selectionElement.updateFromWizard();
373 }
374 }
375 }
376
377 // not used
378 /** {@inheritDoc} */
379 @Override
380 public void widgetDefaultSelected(SelectionEvent e) {
381 }
382
383 /**
384 * <p>
385 * getConversationHolder
386 * </p>
387 *
388 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
389 * object.
390 */
391 @Override
392 public ConversationHolder getConversationHolder() {
393 return conversation;
394 }
395
396 /** {@inheritDoc} */
397 @Override
398 public void setBackground(Color color) {
399 label.setBackground(color);
400 }
401
402 /** {@inheritDoc} */
403 @Override
404 public void setLabel(String labelString) {
405 if (label != null) {
406 label.setText(labelString);
407 }
408 }
409
410 /**
411 * <p>
412 * Getter for the field <code>label</code>.
413 * </p>
414 *
415 * @return a {@link java.lang.String} object.
416 */
417 @Override
418 public String getLabel() {
419 if (label != null) {
420 return label.getText();
421 }
422 return null;
423 }
424
425 /** {@inheritDoc} */
426 @Override
427 public void update(CdmDataChangeMap changeEvents) {
428 }
429 }