- replaced "Edit" text with lock icons for title cache fields
[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
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 final Text text_cache;
43 private final Button button_toggle;
44
45 private boolean state;
46 private final Set<SelectionListener> selectionListener = new HashSet<SelectionListener>();
47 private final Label label;
48 private final 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
87 button_toggle = formFactory.createButton(container, "Edit", SWT.TOGGLE);
88 button_toggle.setText("");
89 button_toggle.setImage(ImageResources.getImage(ImageResources.LOCK_ICON));
90 addControl(button_toggle);
91 button_toggle.addSelectionListener(this);
92
93 setState(initialState);
94 }
95
96 /**
97 * <p>setText</p>
98 *
99 * @param text a {@link java.lang.String} object.
100 */
101 public void setText(String text){
102 if(text != null){
103 // store current caret position
104 int caretPosition = text_cache.getCaretPosition();
105
106 text_cache.removeModifyListener(this);
107 text_cache.setText(text);
108 text_cache.addModifyListener(this);
109
110 // restore caret position
111 text_cache.setSelection(caretPosition);
112 }
113 }
114
115 /**
116 * <p>getText</p>
117 *
118 * @return a {@link java.lang.String} object.
119 */
120 public String getText(){
121 return text_cache.getText();
122 }
123
124 /**
125 * <p>Setter for the field <code>state</code>.</p>
126 *
127 * @param state a boolean.
128 */
129 public void setState(boolean state) {
130 this.state = state;
131 setEnabled(state);
132 }
133
134 /** {@inheritDoc} */
135 @Override
136 public void setEnabled(boolean enabled) {
137 text_cache.setEnabled(enabled);
138 String symbolicName = enabled ? Resources.COLOR_FONT_DEFAULT : Resources.COLOR_TEXT_DISABLED;
139 text_cache.setForeground(StoreUtil.getColor(symbolicName));
140 button_toggle.setSelection(enabled);
141 }
142
143 /**
144 * <p>Getter for the field <code>state</code>.</p>
145 *
146 * @return a boolean.
147 */
148 public boolean getState(){
149 return state;
150 }
151
152 /* (non-Javadoc)
153 * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
154 */
155 /** {@inheritDoc} */
156 @Override
157 public void widgetSelected(SelectionEvent e) {
158 if(button_toggle.getSelection()){
159 button_toggle.setImage(ImageResources.getImage(ImageResources.LOCK_OPEN_ICON));
160 }
161 else{
162 button_toggle.setImage(ImageResources.getImage(ImageResources.LOCK_ICON));
163 }
164 setState(button_toggle.getSelection());
165 for(SelectionListener listener : selectionListener){
166 listener.widgetSelected(e);
167 }
168 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, e));
169 }
170
171 /**
172 * <p>addSelectionListener</p>
173 *
174 * @param listener a {@link org.eclipse.swt.events.SelectionListener} object.
175 */
176 public void addSelectionListener(SelectionListener listener){
177 selectionListener.add(listener);
178 }
179
180 /**
181 * <p>removeSelectionListener</p>
182 *
183 * @param listener a {@link org.eclipse.swt.events.SelectionListener} object.
184 */
185 public void removeSelectionListener(SelectionListener listener){
186 selectionListener.remove(listener);
187 }
188
189 /** {@inheritDoc} */
190 @Override
191 public void widgetDefaultSelected(SelectionEvent e) {}
192
193 /** {@inheritDoc} */
194 @Override
195 public void modifyText(ModifyEvent e) {
196 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, e));
197 }
198
199 /** {@inheritDoc} */
200 @Override
201 public void setIrrelevant(boolean irrelevant) {
202 String colorId = irrelevant ? Resources.COLOR_COMPOSITE_IRRELEVANT : Resources.COLOR_COMPOSITE_BACKGROUND;
203
204 Color color = getColor(colorId);
205 text_cache.setBackground(color);
206 }
207
208 /** {@inheritDoc} */
209 @Override
210 public void setBackground(Color color) {
211 label.setBackground(color);
212 container.setBackground(color);
213 }
214
215 @Override
216 public void setSelected(boolean selected) {
217 setBackground(selected ? SELECTED : getPersistentBackground());
218 }
219 }