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