KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > ui > templates > StringOption


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.ui.templates;
12 import org.eclipse.swt.SWT;
13 import org.eclipse.swt.events.ModifyEvent;
14 import org.eclipse.swt.events.ModifyListener;
15 import org.eclipse.swt.layout.GridData;
16 import org.eclipse.swt.widgets.Composite;
17 import org.eclipse.swt.widgets.Label;
18 import org.eclipse.swt.widgets.Text;
19 /**
20  * This template option can be used to collect string option from the user in
21  * the template section wizard page.
22  *
23  * @since 2.0
24  */

25 public class StringOption extends TemplateOption {
26     private Text text;
27     private Label labelControl;
28     private boolean ignoreListener;
29     private int fStyle;
30     
31     private final static int F_DEFAULT_STYLE = SWT.SINGLE | SWT.BORDER;
32     /**
33      * The constructor.
34      *
35      * @param section
36      * the parent section
37      * @param name
38      * the unique option name
39      * @param label
40      * the translatable label of the option
41      */

42     public StringOption(BaseOptionTemplateSection section, String JavaDoc name,
43             String JavaDoc label) {
44         super(section, name, label);
45         fStyle = F_DEFAULT_STYLE;
46         setRequired(true);
47     }
48     
49     /**
50      * Update the text widget style to be read only
51      * Added to default style (does not override)
52      * @param readOnly
53      */

54     public void setReadOnly(boolean readOnly) {
55         if (readOnly) {
56             fStyle = F_DEFAULT_STYLE | SWT.READ_ONLY;
57         } else {
58             fStyle = F_DEFAULT_STYLE;
59         }
60     }
61     
62     /**
63      * A utility version of the <samp>getValue() </samp> method that converts
64      * the current value into the String object.
65      *
66      * @return the string version of the current value.
67      */

68     public String JavaDoc getText() {
69         if (getValue() != null)
70             return getValue().toString();
71         return null;
72     }
73     /**
74      * A utility version of the <samp>setValue </samp> method that accepts
75      * String objects.
76      *
77      * @param newText
78      * the new text value of the option
79      * @see #setValue(Object)
80      */

81     public void setText(String JavaDoc newText) {
82         setValue(newText);
83     }
84     /**
85      * Implements the superclass method by passing the string value of the new
86      * value to the widget
87      *
88      * @param value
89      * the new option value
90      */

91     public void setValue(Object JavaDoc value) {
92         super.setValue(value);
93         if (text != null) {
94             ignoreListener = true;
95             String JavaDoc textValue = getText();
96             text.setText(textValue != null ? textValue : ""); //$NON-NLS-1$
97
ignoreListener = false;
98         }
99     }
100     /**
101      * Creates the string option control.
102      *
103      * @param parent
104      * parent composite of the string option widget
105      * @param span
106      * the number of columns that the widget should span
107      */

108     public void createControl(Composite parent, int span) {
109         labelControl = createLabel(parent, 1);
110         labelControl.setEnabled(isEnabled());
111         text = new Text(parent, fStyle);
112         if (getValue() != null)
113             text.setText(getValue().toString());
114         GridData gd = new GridData(GridData.FILL_HORIZONTAL);
115         gd.horizontalSpan = span - 1;
116         text.setLayoutData(gd);
117         text.setEnabled(isEnabled());
118         text.addModifyListener(new ModifyListener() {
119             public void modifyText(ModifyEvent e) {
120                 if (ignoreListener)
121                     return;
122                 StringOption.super.setValue(text.getText());
123                 getSection().validateOptions(StringOption.this);
124             }
125         });
126     }
127     /**
128      * A string option is empty if its text field contains no text.
129      *
130      * @return true if there is no text in the text field.
131      */

132     public boolean isEmpty() {
133         return getValue() == null || getValue().toString().length() == 0;
134     }
135     /**
136      * Implements the superclass method by passing the enabled state to the
137      * option's widget.
138      *
139      * @param enabled
140      */

141     public void setEnabled(boolean enabled) {
142         super.setEnabled(enabled);
143         if (labelControl != null) {
144             labelControl.setEnabled(enabled);
145             text.setEnabled(enabled);
146         }
147     }
148 }
149
Popular Tags