87151f9bfdf9f3edb34eef3fa881f4e8ac3621a7
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / section / campanula / ToggleableTextFieldController.java
1 // $Id$
2 /**
3 * Copyright (C) 2013 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 package eu.etaxonomy.taxeditor.ui.section.campanula;
11
12 import java.util.HashSet;
13 import java.util.Set;
14
15 import org.eclipse.swt.SWT;
16 import org.eclipse.swt.events.KeyAdapter;
17 import org.eclipse.swt.events.KeyEvent;
18 import org.eclipse.swt.events.ModifyEvent;
19 import org.eclipse.swt.events.ModifyListener;
20 import org.eclipse.swt.events.SelectionEvent;
21 import org.eclipse.swt.events.SelectionListener;
22 import org.eclipse.swt.graphics.Color;
23 import org.eclipse.swt.widgets.Button;
24 import org.eclipse.swt.widgets.Text;
25
26 import eu.etaxonomy.taxeditor.preference.Resources;
27 import eu.etaxonomy.taxeditor.store.StoreUtil;
28 import eu.etaxonomy.taxeditor.ui.element.AbstractCdmFormElement;
29 import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
30 import eu.etaxonomy.taxeditor.ui.element.CdmPropertyChangeEvent;
31 import eu.etaxonomy.taxeditor.ui.element.IEnableableFormElement;
32 import eu.etaxonomy.taxeditor.ui.element.ISelectable;
33 import eu.etaxonomy.taxeditor.ui.element.LayoutConstants;
34 import eu.etaxonomy.taxeditor.ui.section.campanula.compatibility.ICdmFormElement;
35
36 /**
37 * @author pplitzner
38 * @date 14.08.2013
39 *
40 */
41 public class ToggleableTextFieldController extends AbstractCdmFormElement implements SelectionListener, ModifyListener, IEnableableFormElement, ISelectable {
42
43 private ToggleableTextField toggleableTextField;
44
45 private boolean state;
46 private Set<SelectionListener> selectionListener = new HashSet<SelectionListener>();
47 //TODO also connect label
48 // private Label label;
49
50 /**
51 * <p>Constructor for ToggleableTextElement.</p>
52 *
53 * @param style a int.
54 * @param formFactory a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory} object.
55 * @param parentElement a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement} object.
56 * @param labelString a {@link java.lang.String} object.
57 * @param initialText a {@link java.lang.String} object.
58 * @param initialState a boolean.
59 */
60 protected ToggleableTextFieldController(ToggleableTextField toggleableTextField, CdmFormFactory formFactory, ICdmFormElement parentElement,
61 String initialText, boolean initialState, int style) {
62 super(formFactory, parentElement);
63 setPropertyChangeListeners(formFactory.getPropertyChangeListeners());
64 formFactory.addPropertyChangeListener(this);
65
66 this.toggleableTextField = toggleableTextField;
67
68 // label = formFactory.createLabel(getLayoutComposite(), labelString, style);
69 // addControl(label);
70
71 Text text = toggleableTextField.getText();
72 addControl(text);
73 text.addModifyListener(this);
74 text.setLayoutData(LayoutConstants.FILL());
75
76 // Don't accept carriage returns as input
77 text.addKeyListener( new KeyAdapter(){
78 @Override
79 public void keyPressed(KeyEvent e) {
80 if(e.character == SWT.CR) {
81 e.doit = false;
82 }
83 }
84 });
85
86 Button editButton = toggleableTextField.getEditButton();
87 addControl(editButton);
88 editButton.addSelectionListener(this);
89
90 setState(initialState);
91 }
92
93 /**
94 * <p>setText</p>
95 *
96 * @param text a {@link java.lang.String} object.
97 */
98 public void setText(String text){
99 if(text != null){
100 // store current caret position
101 Text textField = toggleableTextField.getText();
102 int caretPosition = textField.getCaretPosition();
103
104 textField.removeModifyListener(this);
105 textField.setText(text);
106 textField.addModifyListener(this);
107
108 // restore caret position
109 textField.setSelection(caretPosition);
110 }
111 }
112
113 /**
114 * <p>getText</p>
115 *
116 * @return a {@link java.lang.String} object.
117 */
118 public String getText(){
119 return toggleableTextField.getText().getText();
120 }
121
122 /**
123 * <p>Setter for the field <code>state</code>.</p>
124 *
125 * @param state a boolean.
126 */
127 public void setState(boolean state) {
128 this.state = state;
129 setEnabled(state);
130 }
131
132 /** {@inheritDoc} */
133 @Override
134 public void setEnabled(boolean enabled) {
135 toggleableTextField.getText().setEnabled(enabled);
136 String symbolicName = enabled ? Resources.COLOR_FONT_DEFAULT : Resources.COLOR_TEXT_DISABLED;
137 toggleableTextField.getText().setForeground(StoreUtil.getColor(symbolicName));
138 toggleableTextField.getEditButton().setSelection(enabled);
139 }
140
141 /**
142 * <p>Getter for the field <code>state</code>.</p>
143 *
144 * @return a boolean.
145 */
146 public boolean getState(){
147 return state;
148 }
149
150 /* (non-Javadoc)
151 * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
152 */
153 /** {@inheritDoc} */
154 @Override
155 public void widgetSelected(SelectionEvent e) {
156 setState(toggleableTextField.getEditButton().getSelection());
157 for(SelectionListener listener : selectionListener){
158 listener.widgetSelected(e);
159 }
160 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, e));
161 }
162
163 /**
164 * <p>addSelectionListener</p>
165 *
166 * @param listener a {@link org.eclipse.swt.events.SelectionListener} object.
167 */
168 public void addSelectionListener(SelectionListener listener){
169 selectionListener.add(listener);
170 }
171
172 /**
173 * <p>removeSelectionListener</p>
174 *
175 * @param listener a {@link org.eclipse.swt.events.SelectionListener} object.
176 */
177 public void removeSelectionListener(SelectionListener listener){
178 selectionListener.remove(listener);
179 }
180
181 /** {@inheritDoc} */
182 @Override
183 public void widgetDefaultSelected(SelectionEvent e) {}
184
185 /** {@inheritDoc} */
186 @Override
187 public void modifyText(ModifyEvent e) {
188 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, e));
189 }
190
191 /** {@inheritDoc} */
192 @Override
193 public void setIrrelevant(boolean irrelevant) {
194 String colorId = irrelevant ? Resources.COLOR_COMPOSITE_IRRELEVANT : Resources.COLOR_COMPOSITE_BACKGROUND;
195
196 Color color = getColor(colorId);
197 toggleableTextField.getText().setBackground(color);
198 }
199
200 /** {@inheritDoc} */
201 @Override
202 public void setBackground(Color color) {
203 // label.setBackground(color);
204 toggleableTextField.setBackground(color);
205 }
206
207 @Override
208 public void setSelected(boolean selected) {
209 setBackground(selected ? SELECTED : getPersistentBackground());
210 }
211 }