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
|
if (!checkbox.isDisposed()){
|
95
|
checkbox.setEnabled(enabled);
|
96
|
if (label != null){
|
97
|
label.setEnabled(enabled);
|
98
|
}
|
99
|
}
|
100
|
}
|
101
|
|
102
|
@Override
|
103
|
public boolean isEnabled() {
|
104
|
return checkbox.isEnabled();
|
105
|
}
|
106
|
|
107
|
@Override
|
108
|
public void updateCacheRelevance() {
|
109
|
Color color = cacheRelevance().getColor();
|
110
|
setCheckboxBackgroundOnly(color);
|
111
|
}
|
112
|
|
113
|
public void setCheckboxBackgroundOnly(Color color) {
|
114
|
if (checkbox.isDisposed()){
|
115
|
return;
|
116
|
}
|
117
|
checkbox.setBackground(color);
|
118
|
}
|
119
|
|
120
|
@Override
|
121
|
public void setBackground(Color color) {
|
122
|
setCheckboxBackgroundOnly(color);
|
123
|
if (label != null && !label.isDisposed()) {
|
124
|
label.setBackground(color);
|
125
|
}
|
126
|
}
|
127
|
|
128
|
public void setIndent(int indent){
|
129
|
TableWrapData tableWrapData = (TableWrapData)label.getLayoutData();
|
130
|
if (tableWrapData == null){
|
131
|
tableWrapData = new TableWrapData();
|
132
|
|
133
|
}
|
134
|
tableWrapData.indent = indent;
|
135
|
label.setLayoutData(tableWrapData);
|
136
|
getLayoutComposite().layout();
|
137
|
}
|
138
|
}
|