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