1 12 package org.eclipse.ui.ide.dialogs; 13 14 import java.nio.charset.Charset ; 15 import java.nio.charset.IllegalCharsetNameException ; 16 import java.util.List ; 17 18 import org.eclipse.jface.preference.FieldEditor; 19 import org.eclipse.jface.preference.IPreferenceStore; 20 import org.eclipse.osgi.util.NLS; 21 import org.eclipse.swt.SWT; 22 import org.eclipse.swt.events.KeyAdapter; 23 import org.eclipse.swt.events.KeyEvent; 24 import org.eclipse.swt.events.SelectionAdapter; 25 import org.eclipse.swt.events.SelectionEvent; 26 import org.eclipse.swt.layout.GridData; 27 import org.eclipse.swt.layout.GridLayout; 28 import org.eclipse.swt.widgets.Button; 29 import org.eclipse.swt.widgets.Combo; 30 import org.eclipse.swt.widgets.Composite; 31 import org.eclipse.swt.widgets.Group; 32 import org.eclipse.ui.WorkbenchEncoding; 33 import org.eclipse.ui.ide.IDEEncoding; 34 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages; 35 36 48 public abstract class AbstractEncodingFieldEditor extends FieldEditor { 49 50 private Composite container; 51 52 private Button defaultEncodingButton; 53 54 private String defaultEnc; 55 56 private Button otherEncodingButton; 57 58 private Combo encodingCombo; 59 60 private boolean isValid = true; 61 62 private String oldSelectedEncoding; 63 64 private String groupTitle = IDEWorkbenchMessages.WorkbenchPreference_encoding; 65 66 69 protected AbstractEncodingFieldEditor() { 70 super(); 71 } 72 73 84 protected AbstractEncodingFieldEditor(String name, String labelText, 85 Composite parent) { 86 super(name, labelText, parent); 87 } 88 89 106 protected AbstractEncodingFieldEditor(String name, String labelText, 107 String groupTitle, Composite parent) { 108 init(name, labelText); 109 this.groupTitle = groupTitle; 110 createControl(parent); 111 } 112 113 118 protected void adjustForNumColumns(int numColumns) { 119 ((GridData) getContainer().getLayoutData()).horizontalSpan = numColumns; 120 } 121 122 128 protected void doFillIntoGrid(Composite parent, int numColumns) { 129 container = createEncodingGroup(parent, numColumns); 130 } 131 132 137 protected void doLoad() { 138 if (encodingCombo != null) { 139 List encodings = IDEEncoding.getIDEEncodings(); 140 String resourcePreference = getStoredValue(); 141 populateEncodingsCombo(encodings, resourcePreference); 142 updateEncodingState(resourcePreference == null); 143 } 144 } 145 146 151 protected abstract String getStoredValue(); 152 153 158 protected void doLoadDefault() { 159 updateEncodingState(true); 160 } 161 162 167 public int getNumberOfControls() { 168 return 1; 169 } 170 171 176 public boolean isValid() { 177 return isValid; 178 } 179 180 185 protected void refreshValidState() { 186 updateValidState(); 187 } 188 189 194 public void setPreferenceStore(IPreferenceStore store) { 195 super.setPreferenceStore(store); 196 defaultEnc = store.getDefaultString(getPreferenceName()); 197 updateDefaultEncoding(); 198 } 199 200 private void updateDefaultEncoding() { 201 defaultEncodingButton.setText(defaultButtonText()); 202 } 203 204 private Composite getContainer() { 205 return container; 206 } 207 208 220 protected Composite createEncodingGroup(Composite parent, int numColumns) { 221 222 Composite topControl; 223 GridLayout layout = new GridLayout(); 224 layout.numColumns = 2; 225 226 if (groupTitle == null){ 227 topControl = new Composite(parent, SWT.NONE); 228 layout.marginWidth = 0; 229 layout.marginHeight = 0; 230 } 231 else { 232 Group top = new Group(parent, SWT.NONE); 233 top.setText(groupTitle); 234 topControl = top; 235 } 236 237 GridData data = new GridData(GridData.FILL_HORIZONTAL); 238 topControl.setLayoutData(data); 239 topControl.setLayout(layout); 240 241 SelectionAdapter buttonListener = new SelectionAdapter() { 242 public void widgetSelected(SelectionEvent e) { 243 updateEncodingState(defaultEncodingButton.getSelection()); 244 updateValidState(); 245 } 246 }; 247 248 defaultEncodingButton = new Button(topControl, SWT.RADIO); 249 defaultEnc = findDefaultEncoding(); 250 defaultEncodingButton.setText(defaultButtonText()); 251 data = new GridData(); 252 data.horizontalSpan = 2; 253 defaultEncodingButton.setLayoutData(data); 254 defaultEncodingButton.addSelectionListener(buttonListener); 255 256 otherEncodingButton = new Button(topControl, SWT.RADIO); 257 otherEncodingButton 258 .setText(IDEWorkbenchMessages.WorkbenchPreference_otherEncoding); 259 otherEncodingButton.addSelectionListener(buttonListener); 260 261 encodingCombo = new Combo(topControl, SWT.NONE); 262 data = new GridData(); 263 encodingCombo.setLayoutData(data); 264 encodingCombo.addSelectionListener(new SelectionAdapter() { 265 270 public void widgetSelected(SelectionEvent e) { 271 updateValidState(); 272 } 273 }); 274 encodingCombo.addKeyListener(new KeyAdapter() { 275 280 public void keyReleased(KeyEvent e) { 281 updateValidState(); 282 } 283 }); 284 285 return topControl; 286 } 287 288 295 public void setEnabled(boolean enabled, Composite parent) { 296 if (container != null) 297 container.setEnabled(enabled); 298 if (defaultEncodingButton != null) 299 defaultEncodingButton.setEnabled(enabled); 300 if (otherEncodingButton != null) 301 otherEncodingButton.setEnabled(enabled); 302 if (encodingCombo != null) 303 encodingCombo.setEnabled(enabled); 304 305 } 306 307 312 protected String findDefaultEncoding() { 313 return WorkbenchEncoding.getWorkbenchDefaultEncoding(); 314 } 315 316 321 protected String defaultButtonText() { 322 return NLS.bind( 323 IDEWorkbenchMessages.WorkbenchPreference_defaultEncoding, 324 defaultEnc); 325 } 326 327 337 private void populateEncodingsCombo(List encodings, String selectedEncoding) { 338 String [] encodingStrings = new String [encodings.size()]; 339 encodings.toArray(encodingStrings); 340 encodingCombo.setItems(encodingStrings); 341 342 if (selectedEncoding == null) { 343 encodingCombo.setText(getDefaultEnc()); 344 } else { 345 encodingCombo.setText(selectedEncoding); 346 } 347 } 348 349 private void updateEncodingState(boolean useDefault) { 350 defaultEncodingButton.setSelection(useDefault); 351 otherEncodingButton.setSelection(!useDefault); 352 if (useDefault) { 353 encodingCombo.setText(getDefaultEnc()); 354 } 355 encodingCombo.setEnabled(!useDefault); 356 setPresentsDefaultValue(useDefault); 357 updateValidState(); 358 } 359 360 private void updateValidState() { 361 boolean isValidNow = isEncodingValid(); 362 if (isValidNow != isValid) { 363 isValid = isValidNow; 364 if (isValid) { 365 clearErrorMessage(); 366 } else { 367 showErrorMessage(IDEWorkbenchMessages.WorkbenchPreference_unsupportedEncoding); 368 } 369 fireStateChanged(IS_VALID, !isValid, isValid); 370 } 371 String newValue = getSelectedEncoding(); 372 if (isValid && !newValue.equals(oldSelectedEncoding)) { 373 fireValueChanged(VALUE, oldSelectedEncoding, newValue); 374 oldSelectedEncoding = newValue; 375 } 376 } 377 378 383 protected String getSelectedEncoding() { 384 if (defaultEncodingButton.getSelection()) { 385 return defaultEnc; 386 } 387 return encodingCombo.getText(); 388 } 389 390 private boolean isEncodingValid() { 391 return defaultEncodingButton.getSelection() 392 || isValidEncoding(encodingCombo.getText()); 393 } 394 395 403 private boolean isValidEncoding(String enc) { 404 try { 405 return Charset.isSupported(enc); 406 } catch (IllegalCharsetNameException e) { 407 return false; 409 } 410 411 } 412 413 418 protected String getDefaultEnc() { 419 return defaultEnc; 420 } 421 422 430 protected boolean hasSameEncoding(String encodingSetting) { 431 432 String current = getStoredValue(); 433 434 if (encodingSetting == null) { 435 return current == null || current.length() == 0; 437 } 438 return encodingSetting.equals(current); 439 } 440 441 446 boolean isDefaultSelected() { 447 return defaultEncodingButton.getSelection(); 448 } 449 450 462 public void setGroupTitle(String groupTitle) { 463 this.groupTitle = groupTitle; 464 } 465 466 } 467 | Popular Tags |