Refactoring of selection elements. Additional minor refactoring. Fixed a bug with...
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / element / CheckboxElement.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10
11 package eu.etaxonomy.taxeditor.ui.element;
12
13 import org.eclipse.swt.SWT;
14 import org.eclipse.swt.events.SelectionEvent;
15 import org.eclipse.swt.events.SelectionListener;
16 import org.eclipse.swt.graphics.Color;
17 import org.eclipse.swt.widgets.Button;
18 import org.eclipse.swt.widgets.Label;
19
20 import eu.etaxonomy.taxeditor.preference.Resources;
21
22 /**
23 * <p>CheckboxElement class.</p>
24 *
25 * @author n.hoffmann
26 * @created Nov 5, 2009
27 * @version 1.0
28 */
29 public class CheckboxElement extends AbstractCdmFormElement implements SelectionListener, IEnableableFormElement, ISelectable {
30
31 private Button checkbox;
32
33 private Label label;
34
35 /**
36 * <p>Constructor for CheckboxElement.</p>
37 *
38 * @param initialState a boolean.
39 * @param style a int.
40 * @param formFactory a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory} object.
41 * @param parentElement a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement} object.
42 * @param labelString a {@link java.lang.String} object.
43 */
44 protected CheckboxElement(CdmFormFactory formFactory, ICdmFormElement parentElement, String labelString, boolean initialState,
45 int style) {
46 super(formFactory, parentElement);
47
48 if(labelString != null){
49 label = formFactory.createLabel(getLayoutComposite(), labelString);
50 addControl(label);
51 }
52
53 checkbox = formFactory.createButton(getLayoutComposite(), null, SWT.CHECK | style);
54 checkbox.setLayoutData(LayoutConstants.FILL_HORIZONTALLY());
55
56 checkbox.setSelection(initialState);
57
58 checkbox.addSelectionListener(this);
59 addControl(checkbox);
60 }
61
62 /**
63 * <p>setSelection</p>
64 *
65 * @param selected a boolean.
66 */
67 public void setSelection(boolean selected){
68 checkbox.removeSelectionListener(this);
69 checkbox.setSelection(selected);
70 checkbox.addSelectionListener(this);
71 }
72
73 /**
74 * <p>getSelection</p>
75 *
76 * @return a boolean.
77 */
78 public boolean getSelection(){
79 return checkbox.getSelection();
80 }
81
82 /* (non-Javadoc)
83 * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
84 */
85 /** {@inheritDoc} */
86 public void widgetSelected(SelectionEvent e) {
87 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, e));
88 }
89
90 /** {@inheritDoc} */
91 public void widgetDefaultSelected(SelectionEvent e) {}
92
93 /** {@inheritDoc} */
94 public void setSelected(boolean selected) {
95 setBackground(selected ? SELECTED : getPersistentBackground());
96 }
97
98 /** {@inheritDoc} */
99 public void setEnabled(boolean enabled) {
100 checkbox.setEnabled(enabled);
101 }
102
103 /** {@inheritDoc} */
104 public void setIrrelevant(boolean irrelevant) {
105 String colorId = irrelevant ? Resources.COLOR_COMPOSITE_IRRELEVANT : Resources.COLOR_COMPOSITE_BACKGROUND;
106
107 Color color = getColor(colorId);
108 setBackground(color);
109 }
110
111 /* (non-Javadoc)
112 * @see eu.etaxonomy.taxeditor.forms.AbstractCdmFormElement#setBackground(org.eclipse.swt.graphics.Color)
113 */
114 /** {@inheritDoc} */
115 @Override
116 public void setBackground(Color color) {
117 checkbox.setBackground(color);
118 if (label != null) label.setBackground(color);
119 }
120 }