KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > parts > FormEntry


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.ui.parts;
12 import org.eclipse.pde.internal.ui.editor.FormLayoutFactory;
13 import org.eclipse.swt.SWT;
14 import org.eclipse.swt.events.FocusAdapter;
15 import org.eclipse.swt.events.FocusEvent;
16 import org.eclipse.swt.events.KeyAdapter;
17 import org.eclipse.swt.events.KeyEvent;
18 import org.eclipse.swt.events.ModifyEvent;
19 import org.eclipse.swt.events.ModifyListener;
20 import org.eclipse.swt.events.SelectionAdapter;
21 import org.eclipse.swt.events.SelectionEvent;
22 import org.eclipse.swt.layout.GridData;
23 import org.eclipse.swt.layout.GridLayout;
24 import org.eclipse.swt.widgets.Button;
25 import org.eclipse.swt.widgets.Composite;
26 import org.eclipse.swt.widgets.Control;
27 import org.eclipse.swt.widgets.Layout;
28 import org.eclipse.swt.widgets.Text;
29 import org.eclipse.ui.forms.IFormColors;
30 import org.eclipse.ui.forms.widgets.FormToolkit;
31 import org.eclipse.ui.forms.widgets.Hyperlink;
32 import org.eclipse.ui.forms.widgets.TableWrapData;
33 import org.eclipse.ui.forms.widgets.TableWrapLayout;
34 /**
35  * The helper class for creating entry fields with label and text. Optionally,
36  * a button can be added after the text. The attached listener reacts to all
37  * the events. Entring new text makes the entry 'dirty', but only when 'commit'
38  * is called is 'valueChanged' method called (and only if 'dirty' flag is set).
39  * This allows delayed commit.
40  */

41 public class FormEntry {
42     private Control fLabel;
43     private Text fText;
44     private Button fBrowse;
45     private String JavaDoc fValue=""; //$NON-NLS-1$
46
private boolean fDirty;
47     boolean fIgnoreModify = false;
48     private IFormEntryListener fListener;
49     
50     public static final int F_DEFAULT_TEXT_WIDTH_HINT = 100;
51     
52     /**
53      * The default constructor. Call 'createControl' to make it.
54      *
55      */

56     public FormEntry(Composite parent, FormToolkit toolkit, String JavaDoc labelText, int style) {
57         createControl(parent, toolkit, labelText, style, null, false, 0, 0);
58     }
59     /**
60      * This constructor create all the controls right away.
61      *
62      * @param parent
63      * @param toolkit
64      * @param labelText
65      * @param browseText
66      * @param linkLabel
67      */

68     public FormEntry(Composite parent, FormToolkit toolkit, String JavaDoc labelText,
69             String JavaDoc browseText, boolean linkLabel) {
70         this(parent, toolkit, labelText, browseText, linkLabel, 0);
71     }
72     
73     public FormEntry(Composite parent, FormToolkit toolkit, String JavaDoc labelText,
74             String JavaDoc browseText, boolean linkLabel, int indent) {
75         createControl(parent, toolkit, labelText, SWT.SINGLE, browseText, linkLabel, indent, 0);
76     }
77     
78     public FormEntry(Composite parent, FormToolkit toolkit, String JavaDoc labelText,
79             int indent, int tcolspan) {
80         createControl(parent, toolkit, labelText, SWT.SINGLE, null, false, indent, tcolspan);
81     }
82     
83     /**
84      * Create all the controls in the provided parent.
85      *
86      * @param parent
87      * @param toolkit
88      * @param labelText
89      * @param span
90      * @param browseText
91      * @param linkLabel
92      */

93     private void createControl(Composite parent, FormToolkit toolkit,
94             String JavaDoc labelText, int style, String JavaDoc browseText, boolean linkLabel, int indent, int tcolspan) {
95         if (linkLabel) {
96             Hyperlink link = toolkit.createHyperlink(parent, labelText,
97                     SWT.NULL);
98             fLabel = link;
99         } else {
100             if (labelText != null) {
101                 fLabel = toolkit.createLabel(parent, labelText);
102                 fLabel.setForeground(toolkit.getColors().getColor(IFormColors.TITLE));
103             }
104         }
105         fText = toolkit.createText(parent, "", style); //$NON-NLS-1$
106
addListeners();
107         if (browseText != null) {
108             fBrowse = toolkit.createButton(parent, browseText, SWT.PUSH);
109             fBrowse.addSelectionListener(new SelectionAdapter() {
110                 public void widgetSelected(SelectionEvent e) {
111                     if (fListener != null)
112                         fListener.browseButtonSelected(FormEntry.this);
113                 }
114             });
115         }
116         fillIntoGrid(parent, indent, tcolspan);
117         // Set the default text width hint and let clients modify accordingly
118
// after the fact
119
setTextWidthHint(F_DEFAULT_TEXT_WIDTH_HINT);
120     }
121     public void setEditable(boolean editable) {
122         fText.setEditable(editable);
123         if (fLabel instanceof Hyperlink)
124             ((Hyperlink)fLabel).setUnderlined(editable);
125         
126         if (fBrowse!=null)
127             fBrowse.setEnabled(editable);
128     }
129     private void fillIntoGrid(Composite parent, int indent, int tcolspan) {
130         Layout layout = parent.getLayout();
131         int tspan;
132         if (layout instanceof GridLayout) {
133             int span = ((GridLayout) layout).numColumns;
134             if (tcolspan > 0)
135                 tspan = tcolspan;
136             else
137                 tspan = fBrowse != null ? span - 2 : span - 1;
138             GridData gd;
139             if (fLabel != null) {
140                 gd = new GridData(GridData.VERTICAL_ALIGN_CENTER);
141                 gd.horizontalIndent = indent;
142                 fLabel.setLayoutData(gd);
143             }
144             gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
145             gd.horizontalSpan = tspan;
146             if (fLabel != null) {
147                 gd.horizontalIndent = FormLayoutFactory.CONTROL_HORIZONTAL_INDENT;
148             }
149             gd.grabExcessHorizontalSpace = (tspan == 1);
150             gd.widthHint = 10;
151             fText.setLayoutData(gd);
152             if (fBrowse != null) {
153                 gd = new GridData(GridData.VERTICAL_ALIGN_CENTER);
154                 fBrowse.setLayoutData(gd);
155             }
156         } else if (layout instanceof TableWrapLayout) {
157             int span = ((TableWrapLayout) layout).numColumns;
158             if (tcolspan > 0)
159                 tspan = tcolspan;
160             else
161                 tspan = fBrowse != null ? span - 2 : span - 1;
162             TableWrapData td;
163             if (fLabel != null) {
164                 td = new TableWrapData();
165                 td.valign = TableWrapData.MIDDLE;
166                 td.indent = indent;
167                 fLabel.setLayoutData(td);
168             }
169             td = new TableWrapData(TableWrapData.FILL);
170             td.colspan = tspan;
171             if (fLabel != null) {
172                 td.indent = FormLayoutFactory.CONTROL_HORIZONTAL_INDENT;
173             }
174             td.grabHorizontal = (tspan == 1);
175             td.valign = TableWrapData.MIDDLE;
176             fText.setLayoutData(td);
177             if (fBrowse != null) {
178                 td = new TableWrapData(TableWrapData.FILL);
179                 td.valign = TableWrapData.MIDDLE;
180                 fBrowse.setLayoutData(td);
181             }
182         }
183     }
184     /**
185      * Attaches the listener for the entry.
186      *
187      * @param listener
188      */

189     public void setFormEntryListener(IFormEntryListener listener) {
190         if (fLabel != null && fLabel instanceof Hyperlink) {
191             if (this.fListener!=null)
192                 ((Hyperlink)fLabel).removeHyperlinkListener(this.fListener);
193             if (listener!=null)
194                 ((Hyperlink)fLabel).addHyperlinkListener(listener);
195         }
196         this.fListener = listener;
197     }
198     private void addListeners() {
199         fText.addKeyListener(new KeyAdapter() {
200             public void keyReleased(KeyEvent e) {
201                 keyReleaseOccured(e);
202             }
203         });
204         fText.addModifyListener(new ModifyListener() {
205             public void modifyText(ModifyEvent e) {
206                 editOccured(e);
207             }
208         });
209         fText.addFocusListener(new FocusAdapter() {
210             public void focusGained(FocusEvent e) {
211                 if (fListener != null)
212                     fListener.focusGained(FormEntry.this);
213             }
214             public void focusLost(FocusEvent e) {
215                 if (fDirty)
216                     commit();
217             }
218         });
219     }
220     /**
221      * If dirty, commits the text in the widget to the value and notifies the
222      * listener. This call clears the 'dirty' flag.
223      *
224      */

225     public void commit() {
226         if (fDirty) {
227             fValue = fText.getText();
228             //if (value.length()==0)
229
//value = null;
230
//notify
231
if (fListener != null)
232                 fListener.textValueChanged(this);
233         }
234         fDirty = false;
235     }
236     public void cancelEdit() {
237         fDirty = false;
238     }
239     private void editOccured(ModifyEvent e) {
240         if (fIgnoreModify)
241             return;
242         fDirty = true;
243         if (fListener != null)
244             fListener.textDirty(this);
245     }
246     /**
247      * Returns the text control.
248      *
249      * @return
250      */

251     public Text getText() {
252         return fText;
253     }
254     
255     public Control getLabel() {
256         return fLabel;
257     }
258     
259     /**
260      * Returns the browse button control.
261      * @return
262      */

263     public Button getButton() {
264         return fBrowse;
265     }
266     /**
267      * Returns the current entry value. If the entry is dirty and was not
268      * commited, the value may be different from the text in the widget.
269      *
270      * @return
271      */

272     public String JavaDoc getValue() {
273         return fValue.trim();
274     }
275     /**
276      * Returns true if the text has been modified.
277      *
278      * @return
279      */

280     public boolean isDirty() {
281         return fDirty;
282     }
283     private void keyReleaseOccured(KeyEvent e) {
284         if (e.character == '\r') {
285             // commit value
286
if (fDirty)
287                 commit();
288         } else if (e.character == '\u001b') { // Escape character
289
if (!fValue.equals(fText.getText()))
290                 fText.setText(fValue != null ? fValue : ""); // restore old //$NON-NLS-1$
291
fDirty = false;
292         }
293         if (fListener != null)
294             fListener.selectionChanged(FormEntry.this);
295     }
296     /**
297      * Sets the value of this entry.
298      *
299      * @param value
300      */

301     public void setValue(String JavaDoc value) {
302         if (fText != null)
303             fText.setText(value != null ? value : ""); //$NON-NLS-1$
304
this.fValue = (value != null) ? value : ""; //$NON-NLS-1$
305
}
306     /**
307      * Sets the value of this entry with the possibility to turn the
308      * notification off.
309      *
310      * @param value
311      * @param blockNotification
312      */

313     public void setValue(String JavaDoc value, boolean blockNotification) {
314         fIgnoreModify = blockNotification;
315         setValue(value);
316         fIgnoreModify = false;
317     }
318     
319     public void setVisible(boolean visible) {
320         if (fLabel != null)
321             fLabel.setVisible(visible);
322         if (fText != null)
323             fText.setVisible(visible);
324         if (fBrowse != null)
325             fBrowse.setVisible(visible);
326     }
327
328     /**
329      * If GridData was used, set the width hint. If TableWrapData was used
330      * set the max width. If no layout data was specified, this method does
331      * nothing.
332      * @param width
333      */

334     public void setTextWidthHint(int width) {
335         Object JavaDoc data = getText().getLayoutData();
336         if (data == null) {
337             return;
338         } else if (data instanceof GridData) {
339             ((GridData)data).widthHint = width;
340         } else if (data instanceof TableWrapData) {
341             ((TableWrapData)data).maxWidth = width;
342         }
343     }
344 }
345
Popular Tags