Project

General

Profile

Download (3.52 KB) Statistics
| Branch: | Tag: | Revision:
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 final 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
	@Override
87
    public void widgetSelected(SelectionEvent e) {
88
		firePropertyChangeEvent(new CdmPropertyChangeEvent(this, e));
89
	}
90

    
91
	/** {@inheritDoc} */
92
	@Override
93
    public void widgetDefaultSelected(SelectionEvent e) {}
94

    
95
	/** {@inheritDoc} */
96
	@Override
97
    public void setSelected(boolean selected) {
98
		setBackground(selected ? SELECTED : getPersistentBackground());
99
	}
100

    
101
	/** {@inheritDoc} */
102
	@Override
103
    public void setEnabled(boolean enabled) {
104
		checkbox.setEnabled(enabled);
105
		label.setEnabled(enabled);
106
	}
107

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

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

    
121
		Color color = getColor(colorId);
122
		setBackground(color);
123
	}
124

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