1 11 package org.eclipse.team.internal.ccvs.ui.repo; 12 13 import java.io.UnsupportedEncodingException ; 14 import java.util.ArrayList ; 15 import java.util.Collections ; 16 import org.eclipse.core.resources.ResourcesPlugin; 17 import org.eclipse.jface.dialogs.Dialog; 18 import org.eclipse.jface.preference.FieldEditor; 19 import org.eclipse.jface.preference.IPreferenceStore; 20 import org.eclipse.swt.SWT; 21 import org.eclipse.swt.events.*; 22 import org.eclipse.swt.graphics.*; 23 import org.eclipse.swt.layout.GridData; 24 import org.eclipse.swt.layout.GridLayout; 25 import org.eclipse.swt.widgets.*; 26 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages; 27 28 31 public class EncodingFieldEditor extends FieldEditor { 32 private Composite container; 33 private Button defaultEncodingButton; 34 private String defaultEnc; 35 private Button otherEncodingButton; 36 private Combo encodingCombo; 37 private boolean isValid = true; 38 private String oldSelectedEncoding; 39 40 46 public EncodingFieldEditor(String name, String labelText, Composite parent) { 47 super(name, labelText, parent); 48 } 49 50 53 protected void adjustForNumColumns(int numColumns) { 54 ((GridData)getContainer().getLayoutData()).horizontalSpan = numColumns; 55 } 56 57 60 protected void doFillIntoGrid(Composite parent, int numColumns) { 61 container = createEncodingGroup(parent, numColumns); 62 } 63 64 67 protected void doLoad() { 68 if (encodingCombo != null) { 69 String value = getPreferenceStore().getString(getPreferenceName()); 70 if (value.equals(defaultEnc)) { 71 doLoadDefault(); 72 } else { 73 encodingCombo.setText(value); 74 oldSelectedEncoding = value; 75 updateEncodingState(false); 76 } 77 } 78 } 79 80 83 protected void doLoadDefault() { 84 updateEncodingState(true); 85 } 86 87 90 protected void doStore() { 91 String encoding = getSelectedEncoding(); 92 if (encoding.equals(defaultEnc)) { 93 getPreferenceStore().setToDefault(getPreferenceName()); 94 } else { 95 getPreferenceStore().setValue(getPreferenceName(), encoding); 96 } 97 } 98 99 102 public int getNumberOfControls() { 103 return 1; 104 } 105 106 109 public boolean isValid() { 110 return isValid; 111 } 112 113 116 protected void refreshValidState() { 117 updateValidState(); 118 } 119 120 123 public void setPreferenceStore(IPreferenceStore store) { 124 super.setPreferenceStore(store); 125 defaultEnc = store.getDefaultString(getPreferenceName()); 126 updateDefaultEncoding(); 127 } 128 129 private void updateDefaultEncoding() { 130 defaultEncodingButton.setText(IDEWorkbenchMessages.format("WorkbenchPreference.defaultEncoding", new String [] { defaultEnc })); } 132 133 private Composite getContainer() { 134 return container; 135 } 136 137 private Group createEncodingGroup(Composite parent, int numColumns) { 138 139 Font font = parent.getFont(); 140 Group group = new Group(parent, SWT.NONE); 141 GridData data = new GridData(GridData.FILL_HORIZONTAL); 142 data.horizontalSpan = numColumns; 143 group.setLayoutData(data); 144 GridLayout layout = new GridLayout(); 145 layout.numColumns = 2; 146 group.setLayout(layout); 147 group.setText(getLabelText()); group.setFont(font); 149 150 SelectionAdapter buttonListener = new SelectionAdapter() { 151 public void widgetSelected(SelectionEvent e) { 152 updateEncodingState(defaultEncodingButton.getSelection()); 153 updateValidState(); 154 } 155 }; 156 157 if (defaultEnc == null) { 158 defaultEnc = System.getProperty("file.encoding", "UTF-8"); } 160 defaultEncodingButton = new Button(group, SWT.RADIO); 161 updateDefaultEncoding(); 162 data = new GridData(); 163 data.horizontalSpan = 2; 164 defaultEncodingButton.setLayoutData(data); 165 defaultEncodingButton.addSelectionListener(buttonListener); 166 defaultEncodingButton.setFont(font); 167 168 otherEncodingButton = new Button(group, SWT.RADIO); 169 otherEncodingButton.setText(IDEWorkbenchMessages.getString("WorkbenchPreference.otherEncoding")); otherEncodingButton.addSelectionListener(buttonListener); 171 otherEncodingButton.setFont(font); 172 173 encodingCombo = new Combo(group, SWT.NONE); 174 encodingCombo.setFont(font); 175 data = new GridData(); 176 data.widthHint = convertWidthInCharsToPixels(encodingCombo, 15); 177 encodingCombo.setLayoutData(data); 178 encodingCombo.addModifyListener(new ModifyListener() { 179 public void modifyText(ModifyEvent e) { 180 updateValidState(); 181 } 182 }); 183 encodingCombo.addSelectionListener(new SelectionAdapter() { 184 public void widgetSelected(SelectionEvent e) { 185 updateValidState(); 186 } 187 }); 188 189 ArrayList encodings = new ArrayList (); 190 int n = 0; 191 try { 192 n = Integer.parseInt(IDEWorkbenchMessages.getString("WorkbenchPreference.numDefaultEncodings")); } 194 catch (NumberFormatException e) { 195 } 197 for (int i = 0; i < n; ++i) { 198 String enc = IDEWorkbenchMessages.getString("WorkbenchPreference.defaultEncoding" + (i+1), null); if (enc != null) { 200 encodings.add(enc); 201 } 202 } 203 204 if (!encodings.contains(defaultEnc)) { 205 encodings.add(defaultEnc); 206 } 207 208 String enc = ResourcesPlugin.getPlugin().getPluginPreferences().getString(ResourcesPlugin.PREF_ENCODING); 209 boolean isDefault = enc == null || enc.length() == 0; 210 211 if (!isDefault && !encodings.contains(enc)) { 212 encodings.add(enc); 213 } 214 Collections.sort(encodings); 215 for (int i = 0; i < encodings.size(); ++i) { 216 encodingCombo.add((String ) encodings.get(i)); 217 } 218 219 encodingCombo.setText(isDefault ? defaultEnc : enc); 220 221 updateEncodingState(isDefault); 222 return group; 223 } 224 225 private int convertWidthInCharsToPixels(Control control, int chars) { 226 GC gc= new GC(control); 227 gc.setFont(control.getFont()); 228 FontMetrics fontMetrics = gc.getFontMetrics(); 229 int result = Dialog.convertWidthInCharsToPixels(fontMetrics, chars); 230 gc.dispose(); 231 return result; 232 } 233 234 private void updateEncodingState(boolean useDefault) { 235 defaultEncodingButton.setSelection(useDefault); 236 otherEncodingButton.setSelection(!useDefault); 237 encodingCombo.setEnabled(!useDefault); 238 updateValidState(); 239 } 240 241 private void updateValidState() { 242 boolean isValidNow = isEncodingValid(); 243 if (isValidNow != isValid) { 244 isValid = isValidNow; 245 if (isValid) { 246 clearErrorMessage(); 247 } else { 248 showErrorMessage(IDEWorkbenchMessages.getString("WorkbenchPreference.unsupportedEncoding")); } 250 fireStateChanged(IS_VALID, !isValid, isValid); 251 252 String newValue = getSelectedEncoding(); 253 if (isValid && !newValue.equals(oldSelectedEncoding)) { 254 fireValueChanged(VALUE, oldSelectedEncoding, newValue); 255 oldSelectedEncoding = newValue; 256 } 257 } 258 } 259 260 private String getSelectedEncoding() { 261 if (defaultEncodingButton.getSelection()) { 262 return defaultEnc; 263 } 264 else { 265 return encodingCombo.getText(); 266 } 267 } 268 269 private boolean isEncodingValid() { 270 return defaultEncodingButton.getSelection() || 271 isValidEncoding(encodingCombo.getText()); 272 } 273 274 private boolean isValidEncoding(String enc) { 275 try { 276 new String (new byte[0], enc); 277 return true; 278 } catch (UnsupportedEncodingException e) { 279 return false; 280 } 281 } 282 } 283 | Popular Tags |