KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > common > widgets > OptionsInput


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  *
19  */

20
21 package org.apache.directory.ldapstudio.browser.common.widgets;
22
23
24 import java.util.Arrays JavaDoc;
25
26 import org.eclipse.swt.events.ModifyEvent;
27 import org.eclipse.swt.events.ModifyListener;
28 import org.eclipse.swt.events.SelectionAdapter;
29 import org.eclipse.swt.events.SelectionEvent;
30 import org.eclipse.swt.widgets.Button;
31 import org.eclipse.swt.widgets.Combo;
32 import org.eclipse.swt.widgets.Composite;
33 import org.eclipse.swt.widgets.Group;
34
35
36 /**
37  * The OptionsInput could be used to select one option out of several options.
38  * It consists of two radio buttons. With the first radio button you could
39  * select the most likely default option. With the second radio button a combo
40  * is activated where you could select another option from a drop-down list.
41  * <p>
42  * Both, the default option and the options in the drop-down list have a raw
43  * value that is returned by {@link #getRawValue()} and a display value
44  * that is shown to the user.
45  * <p>
46  * If the initial raw value is equal to the default raw value then the
47  * default radio is checked and the drop-down list is disabled. Otherwise
48  * the second radio is checked, the drop-down list is enabled and the
49  * initial value is selected.
50  * <p>
51  * The OptionsInput is used by {@link TextFormatsPreferencePage}.
52  *
53  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
54  * @version $Rev$, $Date$
55  */

56 public class OptionsInput extends BrowserWidget
57 {
58
59     /** The option's title */
60     private String JavaDoc title;
61
62     /** The group, only used when asGroup is true */
63     private Group titleGroup;
64
65     /** The default raw value */
66     private String JavaDoc defaultRawValue;
67
68     /** The default display value */
69     private String JavaDoc defaultDisplayValue;
70
71     /** The radio button to select the default value */
72     private Button defaultButton;
73
74     /** The other raw values */
75     private String JavaDoc[] otherRawValues;
76
77     /** The other display values */
78     private String JavaDoc[] otherDisplayValues;
79
80     /** The radio button to select a value from drop-down list */
81     private Button otherButton;
82
83     /** The combo with the other values */
84     private Combo otherCombo;
85
86     /** The initial raw value */
87     private String JavaDoc initialRawValue;
88
89     /** If true the options are aggregated in a group widget */
90     private boolean asGroup;
91
92     /** If true it is possible to enter a custom value into the combo field */
93     private boolean allowCustomInput;
94
95
96     /**
97      * Creates a new instance of OptionsInput.
98      *
99      * @param title the option's title
100      * @param defaultDisplayValue the default display value
101      * @param defaultRawValue the default raw value
102      * @param otherDisplayValues the other display values
103      * @param otherRawValues the other raw vaues
104      * @param initialRawValue the initial raw value
105      * @param asGroup a flag indicating if the options should be
106      * aggregated in a group widget
107      * @param allowCustomInput true to make it possible to enter a
108      * custom value into the combo field
109      */

110     public OptionsInput( String JavaDoc title, String JavaDoc defaultDisplayValue, String JavaDoc defaultRawValue, String JavaDoc[] otherDisplayValues,
111         String JavaDoc[] otherRawValues, String JavaDoc initialRawValue, boolean asGroup, boolean allowCustomInput )
112     {
113         super();
114         this.title = title;
115         this.defaultDisplayValue = defaultDisplayValue;
116         this.defaultRawValue = defaultRawValue;
117         this.otherDisplayValues = otherDisplayValues;
118         this.otherRawValues = otherRawValues;
119         this.initialRawValue = initialRawValue;
120         this.asGroup = asGroup;
121         this.allowCustomInput = allowCustomInput;
122     }
123
124
125     /**
126      * Creates the widget.
127      *
128      * @param parent the parent
129      */

130     public void createWidget( Composite parent )
131     {
132
133         Composite composite;
134         if ( asGroup )
135         {
136             titleGroup = BaseWidgetUtils.createGroup( parent, title, 1 );
137             composite = BaseWidgetUtils.createColumnContainer( titleGroup, 1, 1 );
138         }
139         else
140         {
141             composite = parent;
142             Composite labelComposite = BaseWidgetUtils.createColumnContainer( composite, 1, 1 );
143             BaseWidgetUtils.createLabel( labelComposite, title + ":", 1 );
144         }
145
146         Composite defaultComposite = BaseWidgetUtils.createColumnContainer( composite, 1, 1 );
147         defaultButton = BaseWidgetUtils.createRadiobutton( defaultComposite, defaultDisplayValue, 1 );
148         defaultButton.addSelectionListener( new SelectionAdapter()
149         {
150             public void widgetSelected( SelectionEvent e )
151             {
152                 otherButton.setSelection( false );
153                 otherCombo.setEnabled( false );
154                 notifyListeners();
155             }
156         } );
157
158         Composite otherComposite = BaseWidgetUtils.createColumnContainer( composite, 2, 1 );
159         otherButton = BaseWidgetUtils.createRadiobutton( otherComposite, "Other: ", 1 );
160         otherButton.addSelectionListener( new SelectionAdapter()
161         {
162             public void widgetSelected( SelectionEvent e )
163             {
164                 defaultButton.setSelection( false );
165                 otherCombo.setEnabled( true );
166                 notifyListeners();
167             }
168         } );
169
170         if ( allowCustomInput )
171         {
172             otherCombo = BaseWidgetUtils.createCombo( otherComposite, otherDisplayValues, 0, 1 );
173         }
174         else
175         {
176             otherCombo = BaseWidgetUtils.createReadonlyCombo( otherComposite, otherDisplayValues, 0, 1 );
177         }
178         otherCombo.addModifyListener( new ModifyListener()
179         {
180             public void modifyText( ModifyEvent e )
181             {
182                 notifyListeners();
183             }
184         } );
185
186         setRawValue( initialRawValue );
187     }
188
189
190     /**
191      * Gets the raw value. Either the default value or
192      * the selected value from the combo.
193      *
194      * @return the raw value
195      */

196     public String JavaDoc getRawValue()
197     {
198         if ( defaultButton.getSelection() )
199         {
200             return defaultRawValue;
201         }
202         else
203         {
204             String JavaDoc t = otherCombo.getText();
205             for ( int i = 0; i < otherDisplayValues.length; i++ )
206             {
207                 if ( t.equals( otherDisplayValues[i] ) )
208                 {
209                     return otherRawValues[i];
210                 }
211             }
212             return t;
213         }
214     }
215
216
217     /**
218      * Sets the raw value.
219      *
220      * @param rawValue the raw value
221      */

222     public void setRawValue( String JavaDoc rawValue )
223     {
224         int index = Arrays.asList( otherRawValues ).indexOf( rawValue );
225         if ( index == -1 )
226         {
227             index = Arrays.asList( otherDisplayValues ).indexOf( rawValue );
228         }
229
230         if ( defaultRawValue.equals( rawValue ) )
231         {
232             defaultButton.setSelection( true );
233             otherButton.setSelection( false );
234             otherCombo.setEnabled( false );
235             otherCombo.select( index );
236         }
237         else if ( index > -1 )
238         {
239             defaultButton.setSelection( false );
240             otherButton.setSelection( true );
241             otherCombo.setEnabled( true );
242             otherCombo.select( index );
243         }
244         else
245         {
246             defaultButton.setSelection( false );
247             otherButton.setSelection( true );
248             otherCombo.setEnabled( true );
249             otherCombo.setText( rawValue );
250         }
251     }
252
253 }
254
Popular Tags