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