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