Project

General

Profile

Download (6.75 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 * 
3
 */
4
package eu.etaxonomy.taxeditor.singlesource.ui.forms;
5

    
6
import javax.naming.OperationNotSupportedException;
7

    
8
import org.apache.log4j.Logger;
9
import org.eclipse.swt.SWT;
10
import org.eclipse.swt.events.KeyAdapter;
11
import org.eclipse.swt.events.KeyEvent;
12
import org.eclipse.swt.events.ModifyEvent;
13
import org.eclipse.swt.events.ModifyListener;
14
import org.eclipse.swt.graphics.Color;
15
import org.eclipse.swt.widgets.Control;
16
import org.eclipse.swt.widgets.Label;
17
import org.eclipse.swt.widgets.Text;
18
import org.eclipse.ui.forms.widgets.TableWrapData;
19

    
20
import eu.etaxonomy.cdm.common.CdmUtils;
21
import eu.etaxonomy.taxeditor.preference.Resources;
22
import eu.etaxonomy.taxeditor.singlesource.ImplementationLoader;
23
import eu.etaxonomy.taxeditor.ui.forms.AbstractCdmFormElement;
24
import eu.etaxonomy.taxeditor.ui.forms.CdmPropertyChangeEvent;
25
import eu.etaxonomy.taxeditor.ui.forms.ICdmFormElement;
26
import eu.etaxonomy.taxeditor.ui.forms.IEnableableFormElement;
27

    
28
/**
29
 * <p>
30
 * TextWithLabelElement class.
31
 * </p>
32
 * 
33
 * @author n.hoffmann
34
 * @version $Id: $
35
 */
36
public abstract class TextWithLabelElementFacade extends AbstractCdmFormElement implements
37
		ModifyListener, IEnableableFormElement {
38
	private static final Logger logger = Logger.getLogger(TextWithLabelElementFacade.class);
39
	
40
	private static TextWithLabelElementFacade IMPL;
41
	static{
42
		IMPL = (TextWithLabelElementFacade) ImplementationLoader.newInstance(TextWithLabelElementFacade.class);
43
	}
44

    
45
	protected Text text;
46
	private Label label;
47

    
48
	/** Constant <code>MAX_HEIGHT=0</code> */
49
	public static final int MAX_HEIGHT = 0;
50
	/** Constant <code>SINGLE=-1</code> */
51
	public static final int SINGLE = -1;
52

    
53
	protected TextWithLabelElementFacade(){
54
	}
55
	
56
	/**
57
	 * <p>
58
	 * Constructor for TextWithLabelElement.
59
	 * </p>
60
	 * 
61
	 * @param formFactory
62
	 *            a {@link eu.etaxonomy.taxeditor.singlesource.ui.forms.CdmFormFactoryFacade}
63
	 *            object.
64
	 * @param parentElement
65
	 *            a {@link eu.etaxonomy.taxeditor.ui.forms.ICdmFormElement}
66
	 *            object.
67
	 * @param labelString
68
	 *            a {@link java.lang.String} object.
69
	 * @param initialText
70
	 *            a {@link java.lang.String} object.
71
	 * @param textHeight
72
	 *            a {@link java.lang.Integer} object.
73
	 * @param style
74
	 *            a int.
75
	 */
76
	protected TextWithLabelElementFacade(CdmFormFactoryFacade formFactory,
77
			ICdmFormElement parentElement, String labelString,
78
			String initialText, Integer textHeight, int style) {
79
		super(formFactory, parentElement);
80

    
81
		if (labelString != null) {
82
			label = formFactory.createLabel(getLayoutComposite(),
83
					CdmUtils.Nz(labelString), SWT.NULL);
84
			addControl(label);
85
			label.setLayoutData(CdmFormFactoryFacade.LEFT());
86
		}
87

    
88
		int scrollStyle = textHeight == null ? SWT.NULL
89
				: (SWT.V_SCROLL | SWT.MULTI);
90
		
91
		int combinedStyle = style | SWT.BORDER | scrollStyle;
92
		
93
		//SWT.PASSWORD does not work when SWT.WRAP is set. 
94
		if(style != SWT.PASSWORD){
95
			combinedStyle = combinedStyle | SWT.WRAP;
96
		}		
97
		
98
		text = formFactory.createText(getLayoutComposite(), "", combinedStyle);
99

    
100
		addControl(text);
101

    
102
		// text.setWO
103

    
104
		if (textHeight == null) {
105
			text.addKeyListener(new KeyAdapter() {
106
				@Override
107
				public void keyPressed(KeyEvent e) {
108
					if (e.character == SWT.CR) {
109
						// Don't accept carriage returns as input when in single
110
						// line mode
111
						e.doit = false;
112
					} else if (e.character == SWT.TAB) {
113
						// traverse is not working for wrapped text widgets so
114
						// we reintroduce it here
115
						e.doit = false;
116
						try {
117
							traverse(SWT.TRAVERSE_TAB_NEXT);
118
						} catch (OperationNotSupportedException e1) {
119
							logger.warn("Text.traverse(int) not supported ! : " + e1.getMessage());
120
						}
121
					}
122
				}
123
			});
124
		}
125

    
126
		TableWrapData layoutData = CdmFormFactoryFacade.FILL();
127
		if (textHeight != null && textHeight > 0) {
128
			(layoutData).heightHint = textHeight;
129
		}
130

    
131
		text.setLayoutData(layoutData);
132

    
133
		text.addModifyListener(this);
134

    
135
		setText(initialText);
136
	}
137
	
138
	public static TextWithLabelElementFacade getInstance( CdmFormFactoryFacade formFactory,ICdmFormElement parentElement, String labelString,
139
	String initialText, Integer textHeight, int style)
140
	{
141
		return (TextWithLabelElementFacade)IMPL.getInstanceInternal(formFactory,parentElement, labelString, initialText, textHeight, style);
142
	}
143

    
144
	protected abstract Object getInstanceInternal(CdmFormFactoryFacade formFactory,ICdmFormElement parentElement, String labelString,
145
			String initialText, Integer textHeight, int style);
146
	
147
	
148
	public boolean traverse(int traversal) throws OperationNotSupportedException
149
	{
150
		return IMPL.traverseInternal(traversal);
151
	}
152

    
153
	public abstract boolean traverseInternal(int traversal) throws OperationNotSupportedException;
154

    
155
	/**
156
	 * Get the text of this composites text composite
157
	 * 
158
	 * @return a {@link java.lang.String} object.
159
	 */
160
	public String getText() {
161
		return text.getText();
162
	}
163

    
164
	/**
165
	 * Set the text of this composites text composite
166
	 * 
167
	 * @param string
168
	 *            a {@link java.lang.String} object.
169
	 */
170
	public void setText(String string) {
171
		Object[] listeners = removeModifyListenersInternal(text);
172

    
173
		text.setText(CdmUtils.Nz(string));
174

    
175
		addModifyListenersInternal(text, listeners);
176
	}
177

    
178
	public abstract Object[] removeModifyListenersInternal(Text text);
179
	
180
	public abstract void addModifyListenersInternal(Text text, Object[] listeners);
181
	
182
	/*
183
	 * (non-Javadoc)
184
	 * 
185
	 * @see
186
	 * org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events
187
	 * .ModifyEvent)
188
	 */
189
	/** {@inheritDoc} */
190
	public void modifyText(ModifyEvent e) {
191
		firePropertyChangeEvent(new CdmPropertyChangeEvent(this, e));
192
	}
193

    
194
	/** {@inheritDoc} */
195
	public void setSelected(boolean selected) {
196
		setBackground(getColor(selected));
197
	}
198

    
199
	/** {@inheritDoc} */
200
	public void setEnabled(boolean enabled) {
201
		text.setEnabled(enabled);
202
		String symbolicName = enabled ? Resources.COLOR_FONT_DEFAULT
203
				: Resources.COLOR_TEXT_DISABLED;
204
		text.setForeground(getColor(symbolicName));
205
	}
206

    
207
	/** {@inheritDoc} */
208
	public void setIrrelevant(boolean irrelevant) {
209
		String colorId = irrelevant ? Resources.COLOR_COMPOSITE_IRRELEVANT
210
				: Resources.COLOR_COMPOSITE_BACKGROUND;
211

    
212
		Color color = getColor(colorId);
213
		text.setBackground(color);
214
	}
215

    
216
	/** {@inheritDoc} */
217
	@Override
218
	public void setBackground(Color color) {
219
		if (label != null)
220
			label.setBackground(color);
221
	}
222

    
223
	/*
224
	 * (non-Javadoc)
225
	 * 
226
	 * @see eu.etaxonomy.taxeditor.forms.AbstractCdmFormElement#setFocus()
227
	 */
228
	/** {@inheritDoc} */
229
	@Override
230
	public void setFocus() {
231
		text.setFocus();
232
	}
233

    
234
	/**
235
	 * <p>
236
	 * getMainControl
237
	 * </p>
238
	 * 
239
	 * @return a {@link org.eclipse.swt.widgets.Control} object.
240
	 */
241
	public Control getMainControl() {
242
		return text;
243
	}
244

    
245
	/**
246
	 * <p>
247
	 * setTextLimit
248
	 * </p>
249
	 * 
250
	 * @param limit
251
	 *            a int.
252
	 */
253
	public void setTextLimit(int limit) {
254
		text.setTextLimit(limit);
255
	}
256
}
(3-3/4)