Merge branch 'release/5.18.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / element / CheckboxElement.java
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 import org.eclipse.ui.forms.widgets.TableWrapData;
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 public Button getMainControl(){
92 return checkbox;
93 }
94
95 /** {@inheritDoc} */
96 @Override
97 public void widgetDefaultSelected(SelectionEvent e) {}
98
99 /** {@inheritDoc} */
100 @Override
101 public void setSelected(boolean selected) {
102 setBackground(selected ? SELECTED : getPersistentBackground());
103 }
104
105 /** {@inheritDoc} */
106 @Override
107 public void setEnabled(boolean enabled) {
108 checkbox.setEnabled(enabled);
109 label.setEnabled(enabled);
110 }
111
112 /* (non-Javadoc)
113 * @see eu.etaxonomy.taxeditor.ui.element.IEnableableFormElement#isEnabled()
114 */
115 @Override
116 public boolean isEnabled() {
117 return checkbox.isEnabled();
118 }
119
120 /** {@inheritDoc} */
121 @Override
122 public void setIrrelevant(boolean irrelevant) {
123 String colorId = irrelevant ? Resources.COLOR_COMPOSITE_IRRELEVANT : Resources.COLOR_COMPOSITE_BACKGROUND;
124
125 Color color = getColor(colorId);
126 setBackground(color);
127 }
128
129 /* (non-Javadoc)
130 * @see eu.etaxonomy.taxeditor.forms.AbstractCdmFormElement#setBackground(org.eclipse.swt.graphics.Color)
131 */
132 /** {@inheritDoc} */
133 @Override
134 public void setBackground(Color color) {
135 if (checkbox.isDisposed()){
136 return;
137 }
138 checkbox.setBackground(color);
139 if (label != null) {
140 label.setBackground(color);
141 }
142 }
143 public void setIndent(int indent){
144 TableWrapData tableWrapData = (TableWrapData)label.getLayoutData();
145 if (tableWrapData == null){
146 tableWrapData = new TableWrapData();
147
148 }
149 tableWrapData.indent = indent;
150 label.setLayoutData(tableWrapData);
151 getLayoutComposite().layout();
152
153 }
154 }