Project

General

Profile

Download (6.12 KB) Statistics
| Branch: | Tag: | Revision:
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
	    if(state){
131
	        button_toggle.setImage(ImageResources.getImage(ImageResources.LOCK_OPEN_ICON));
132
	    }
133
	    else{
134
	        button_toggle.setImage(ImageResources.getImage(ImageResources.LOCK_ICON));
135
	    }
136
		this.state = state;
137
		setEnabled(state);
138
	}
139

    
140
	/** {@inheritDoc} */
141
	@Override
142
    public void setEnabled(boolean enabled) {
143
		text_cache.setEnabled(enabled);
144
		String symbolicName = enabled ? Resources.COLOR_FONT_DEFAULT : Resources.COLOR_TEXT_DISABLED;
145
		text_cache.setForeground(StoreUtil.getColor(symbolicName));
146
		button_toggle.setSelection(enabled);
147
	}
148

    
149
	/**
150
	 * <p>Getter for the field <code>state</code>.</p>
151
	 *
152
	 * @return a boolean.
153
	 */
154
	public boolean getState(){
155
		return state;
156
	}
157

    
158
	/* (non-Javadoc)
159
	 * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
160
	 */
161
	/** {@inheritDoc} */
162
	@Override
163
    public void widgetSelected(SelectionEvent e) {
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
}
(37-37/38)