Project

General

Profile

Download (3.57 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2007 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9

    
10
package eu.etaxonomy.taxeditor.ui.element;
11

    
12
import org.eclipse.swt.SWT;
13
import org.eclipse.swt.events.SelectionEvent;
14
import org.eclipse.swt.events.SelectionListener;
15
import org.eclipse.swt.graphics.Color;
16
import org.eclipse.swt.widgets.Button;
17
import org.eclipse.swt.widgets.Label;
18

    
19
import eu.etaxonomy.taxeditor.preference.Resources;
20

    
21
/**
22
 * <p>CheckboxElement class.</p>
23
 *
24
 * @author n.hoffmann
25
 * @created Nov 5, 2009
26
 * @version 1.0
27
 */
28
public class CheckboxElement extends AbstractCdmFormElement implements SelectionListener, IEnableableFormElement, ISelectable {
29

    
30
	private final Button checkbox;
31

    
32
	private Label label;
33

    
34
	/**
35
	 * <p>Constructor for CheckboxElement.</p>
36
	 *
37
	 * @param initialState a boolean.
38
	 * @param style a int.
39
	 * @param formFactory a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory} object.
40
	 * @param parentElement a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement} object.
41
	 * @param labelString a {@link java.lang.String} object.
42
	 */
43
	protected CheckboxElement(CdmFormFactory formFactory, ICdmFormElement parentElement, String labelString, boolean initialState,
44
			int style) {
45
		super(formFactory, parentElement);
46

    
47
		if(labelString != null){
48
			label = formFactory.createLabel(getLayoutComposite(), labelString);
49
			addControl(label);
50
		}
51

    
52
		checkbox = formFactory.createButton(getLayoutComposite(), null, SWT.CHECK | style);
53
		checkbox.setLayoutData(LayoutConstants.FILL_HORIZONTALLY());
54

    
55
		checkbox.setSelection(initialState);
56

    
57
		checkbox.addSelectionListener(this);
58
		addControl(checkbox);
59
	}
60

    
61
	/**
62
	 * <p>setSelection</p>
63
	 *
64
	 * @param selected a boolean.
65
	 */
66
	public void setSelection(boolean selected){
67
		checkbox.removeSelectionListener(this);
68
		checkbox.setSelection(selected);
69
		checkbox.addSelectionListener(this);
70
	}
71

    
72
	/**
73
	 * <p>getSelection</p>
74
	 *
75
	 * @return a boolean.
76
	 */
77
	public boolean getSelection(){
78
		return checkbox.getSelection();
79
	}
80

    
81
	/* (non-Javadoc)
82
	 * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
83
	 */
84
	/** {@inheritDoc} */
85
	@Override
86
    public void widgetSelected(SelectionEvent e) {
87
		firePropertyChangeEvent(new CdmPropertyChangeEvent(this, e));
88
	}
89

    
90
	public Button getMainControl(){
91
	    return checkbox;
92
	}
93

    
94
	/** {@inheritDoc} */
95
	@Override
96
    public void widgetDefaultSelected(SelectionEvent e) {}
97

    
98
	/** {@inheritDoc} */
99
	@Override
100
    public void setSelected(boolean selected) {
101
		setBackground(selected ? SELECTED : getPersistentBackground());
102
	}
103

    
104
	/** {@inheritDoc} */
105
	@Override
106
    public void setEnabled(boolean enabled) {
107
		checkbox.setEnabled(enabled);
108
		label.setEnabled(enabled);
109
	}
110

    
111
	/* (non-Javadoc)
112
	 * @see eu.etaxonomy.taxeditor.ui.element.IEnableableFormElement#isEnabled()
113
	 */
114
	@Override
115
	public boolean isEnabled() {
116
	    return checkbox.isEnabled();
117
	}
118

    
119
	/** {@inheritDoc} */
120
	@Override
121
    public void setIrrelevant(boolean irrelevant) {
122
		String colorId = irrelevant ? Resources.COLOR_COMPOSITE_IRRELEVANT : Resources.COLOR_COMPOSITE_BACKGROUND;
123

    
124
		Color color = getColor(colorId);
125
		setBackground(color);
126
	}
127

    
128
	/* (non-Javadoc)
129
	 * @see eu.etaxonomy.taxeditor.forms.AbstractCdmFormElement#setBackground(org.eclipse.swt.graphics.Color)
130
	 */
131
	/** {@inheritDoc} */
132
	@Override
133
	public void setBackground(Color color) {
134
		checkbox.setBackground(color);
135
		if (label != null) {
136
            label.setBackground(color);
137
        }
138
	}
139
}
(9-9/48)