merge-update from trunk
[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 import eu.etaxonomy.taxeditor.ui.campanula.compatibility.ICdmFormElement;
22
23 /**
24 * <p>CheckboxElement class.</p>
25 *
26 * @author n.hoffmann
27 * @created Nov 5, 2009
28 * @version 1.0
29 */
30 public class CheckboxElement extends AbstractCdmFormElement implements SelectionListener, IEnableableFormElement, ISelectable {
31
32 private Button checkbox;
33
34 private Label label;
35
36 /**
37 * <p>Constructor for CheckboxElement.</p>
38 *
39 * @param initialState a boolean.
40 * @param style a int.
41 * @param formFactory a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory} object.
42 * @param parentElement a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement} object.
43 * @param labelString a {@link java.lang.String} object.
44 */
45 protected CheckboxElement(CdmFormFactory formFactory, ICdmFormElement parentElement, String labelString, boolean initialState,
46 int style) {
47 super(formFactory, parentElement);
48
49 if(labelString != null){
50 label = formFactory.createLabel(getLayoutComposite(), labelString);
51 addControl(label);
52 }
53
54 checkbox = formFactory.createButton(getLayoutComposite(), null, SWT.CHECK | style);
55 checkbox.setLayoutData(LayoutConstants.FILL_HORIZONTALLY());
56
57 checkbox.setSelection(initialState);
58
59 checkbox.addSelectionListener(this);
60 addControl(checkbox);
61 }
62
63 /**
64 * <p>setSelection</p>
65 *
66 * @param selected a boolean.
67 */
68 public void setSelection(boolean selected){
69 checkbox.removeSelectionListener(this);
70 checkbox.setSelection(selected);
71 checkbox.addSelectionListener(this);
72 }
73
74 /**
75 * <p>getSelection</p>
76 *
77 * @return a boolean.
78 */
79 public boolean getSelection(){
80 return checkbox.getSelection();
81 }
82
83 /* (non-Javadoc)
84 * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
85 */
86 /** {@inheritDoc} */
87 public void widgetSelected(SelectionEvent e) {
88 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, e));
89 }
90
91 /** {@inheritDoc} */
92 public void widgetDefaultSelected(SelectionEvent e) {}
93
94 /** {@inheritDoc} */
95 public void setSelected(boolean selected) {
96 setBackground(selected ? SELECTED : getPersistentBackground());
97 }
98
99 /** {@inheritDoc} */
100 public void setEnabled(boolean enabled) {
101 checkbox.setEnabled(enabled);
102 }
103
104 /** {@inheritDoc} */
105 public void setIrrelevant(boolean irrelevant) {
106 String colorId = irrelevant ? Resources.COLOR_COMPOSITE_IRRELEVANT : Resources.COLOR_COMPOSITE_BACKGROUND;
107
108 Color color = getColor(colorId);
109 setBackground(color);
110 }
111
112 /* (non-Javadoc)
113 * @see eu.etaxonomy.taxeditor.forms.AbstractCdmFormElement#setBackground(org.eclipse.swt.graphics.Color)
114 */
115 /** {@inheritDoc} */
116 @Override
117 public void setBackground(Color color) {
118 checkbox.setBackground(color);
119 if (label != null) label.setBackground(color);
120 }
121 }