Project

General

Profile

Download (3.47 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
package eu.etaxonomy.taxeditor.ui.element;
10

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

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

    
29
	private final Button checkbox;
30

    
31
	private Label label;
32

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

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

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

    
54
		checkbox.setSelection(initialState);
55

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

    
60
	/**
61
	 * <p>setSelection</p>
62
	 */
63
	public void setSelection(boolean selected){
64
		checkbox.removeSelectionListener(this);
65
		checkbox.setSelection(selected);
66
		checkbox.addSelectionListener(this);
67
	}
68
	/**
69
	 * <p>getSelection</p>
70
	 */
71
	public boolean getSelection(){
72
		return checkbox.getSelection();
73
	}
74

    
75
	@Override
76
    public void widgetSelected(SelectionEvent e) {
77
		firePropertyChangeEvent(new CdmPropertyChangeEvent(this, e));
78
	}
79

    
80
	public Button getMainControl(){
81
	    return checkbox;
82
	}
83

    
84
	@Override
85
    public void widgetDefaultSelected(SelectionEvent e) {}
86

    
87
	@Override
88
    public void setSelected(boolean selected) {
89
	    setBackground(selected ? SELECTED : getPersistentBackground());
90
	}
91

    
92
	@Override
93
    public void setEnabled(boolean enabled) {
94
		checkbox.setEnabled(enabled);
95
		label.setEnabled(enabled);
96
	}
97

    
98
	@Override
99
	public boolean isEnabled() {
100
	    return checkbox.isEnabled();
101
	}
102

    
103
	@Override
104
    public void updateCacheRelevance() {
105
	    Color color = cacheRelevance().getColor();
106
		setCheckboxBackgroundOnly(color);
107
	}
108

    
109
    public void setCheckboxBackgroundOnly(Color color) {
110
        if (checkbox.isDisposed()){
111
            return;
112
        }
113
        checkbox.setBackground(color);
114
    }
115

    
116
	@Override
117
	public void setBackground(Color color) {
118
	    setCheckboxBackgroundOnly(color);
119
        if (label != null && !label.isDisposed()) {
120
            label.setBackground(color);
121
        }
122
	}
123

    
124
    public void setIndent(int indent){
125
	    TableWrapData tableWrapData = (TableWrapData)label.getLayoutData();
126
        if (tableWrapData == null){
127
            tableWrapData = new TableWrapData();
128

    
129
        }
130
        tableWrapData.indent = indent;
131
        label.setLayoutData(tableWrapData);
132
        getLayoutComposite().layout();
133
    }
134
}
(13-13/57)