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