1 20 21 package org.apache.directory.ldapstudio.browser.common.widgets; 22 23 24 import java.util.Arrays ; 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 56 public class OptionsInput extends BrowserWidget 57 { 58 59 60 private String title; 61 62 63 private Group titleGroup; 64 65 66 private String defaultRawValue; 67 68 69 private String defaultDisplayValue; 70 71 72 private Button defaultButton; 73 74 75 private String [] otherRawValues; 76 77 78 private String [] otherDisplayValues; 79 80 81 private Button otherButton; 82 83 84 private Combo otherCombo; 85 86 87 private String initialRawValue; 88 89 90 private boolean asGroup; 91 92 93 private boolean allowCustomInput; 94 95 96 110 public OptionsInput( String title, String defaultDisplayValue, String defaultRawValue, String [] otherDisplayValues, 111 String [] otherRawValues, String 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 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 196 public String getRawValue() 197 { 198 if ( defaultButton.getSelection() ) 199 { 200 return defaultRawValue; 201 } 202 else 203 { 204 String 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 222 public void setRawValue( String 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 |