Project

General

Profile

Download (6.55 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.CdmFormFactory;
25
import eu.etaxonomy.taxeditor.ui.forms.CdmPropertyChangeEvent;
26
import eu.etaxonomy.taxeditor.ui.forms.ICdmFormElement;
27
import eu.etaxonomy.taxeditor.ui.forms.IEnableableFormElement;
28

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

    
46
	protected Text text;
47
	private Label label;
48

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

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

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

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

    
101
		addControl(text);
102

    
103
		// text.setWO
104

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

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

    
132
		text.setLayoutData(layoutData);
133

    
134
		text.addModifyListener(this);
135

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

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

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

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

    
165
	/**
166
	 * Set the text of this composites text composite
167
	 * 
168
	 * @param string
169
	 *            a {@link java.lang.String} object.
170
	 */
171
	public void setText(String string) {
172
		IMPL.setTextInternal(string);
173
	}
174

    
175
	public abstract void setTextInternal(String string);
176
	
177
	/*
178
	 * (non-Javadoc)
179
	 * 
180
	 * @see
181
	 * org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events
182
	 * .ModifyEvent)
183
	 */
184
	/** {@inheritDoc} */
185
	public void modifyText(ModifyEvent e) {
186
		firePropertyChangeEvent(new CdmPropertyChangeEvent(this, e));
187
	}
188

    
189
	/** {@inheritDoc} */
190
	public void setSelected(boolean selected) {
191
		setBackground(getColor(selected));
192
	}
193

    
194
	/** {@inheritDoc} */
195
	public void setEnabled(boolean enabled) {
196
		text.setEnabled(enabled);
197
		String symbolicName = enabled ? Resources.COLOR_FONT_DEFAULT
198
				: Resources.COLOR_TEXT_DISABLED;
199
		text.setForeground(getColor(symbolicName));
200
	}
201

    
202
	/** {@inheritDoc} */
203
	public void setIrrelevant(boolean irrelevant) {
204
		String colorId = irrelevant ? Resources.COLOR_COMPOSITE_IRRELEVANT
205
				: Resources.COLOR_COMPOSITE_BACKGROUND;
206

    
207
		Color color = getColor(colorId);
208
		text.setBackground(color);
209
	}
210

    
211
	/** {@inheritDoc} */
212
	@Override
213
	public void setBackground(Color color) {
214
		if (label != null)
215
			label.setBackground(color);
216
	}
217

    
218
	/*
219
	 * (non-Javadoc)
220
	 * 
221
	 * @see eu.etaxonomy.taxeditor.forms.AbstractCdmFormElement#setFocus()
222
	 */
223
	/** {@inheritDoc} */
224
	@Override
225
	public void setFocus() {
226
		text.setFocus();
227
	}
228

    
229
	/**
230
	 * <p>
231
	 * getMainControl
232
	 * </p>
233
	 * 
234
	 * @return a {@link org.eclipse.swt.widgets.Control} object.
235
	 */
236
	public Control getMainControl() {
237
		return text;
238
	}
239

    
240
	/**
241
	 * <p>
242
	 * setTextLimit
243
	 * </p>
244
	 * 
245
	 * @param limit
246
	 *            a int.
247
	 */
248
	public void setTextLimit(int limit) {
249
		text.setTextLimit(limit);
250
	}
251
}
(3-3/4)