1 11 package org.eclipse.team.internal.ccvs.ui.repo; 12 13 import org.eclipse.core.runtime.IAdaptable; 14 import org.eclipse.core.runtime.IStatus; 15 import org.eclipse.jface.dialogs.Dialog; 16 import org.eclipse.jface.dialogs.MessageDialog; 17 import org.eclipse.jface.preference.*; 18 import org.eclipse.jface.util.IPropertyChangeListener; 19 import org.eclipse.jface.util.PropertyChangeEvent; 20 import org.eclipse.osgi.util.NLS; 21 import org.eclipse.swt.SWT; 22 import org.eclipse.swt.events.SelectionAdapter; 23 import org.eclipse.swt.events.SelectionEvent; 24 import org.eclipse.swt.layout.GridData; 25 import org.eclipse.swt.layout.GridLayout; 26 import org.eclipse.swt.widgets.*; 27 import org.eclipse.team.internal.ccvs.core.CVSStatus; 28 import org.eclipse.team.internal.ccvs.core.ICVSRepositoryLocation; 29 import org.eclipse.team.internal.ccvs.core.connection.CVSRepositoryLocation; 30 import org.eclipse.team.internal.ccvs.core.util.KnownRepositories; 31 import org.eclipse.team.internal.ccvs.ui.*; 32 import org.eclipse.ui.PlatformUI; 33 import org.eclipse.ui.dialogs.PreferencesUtil; 34 import org.eclipse.ui.dialogs.PropertyPage; 35 import org.eclipse.ui.ide.dialogs.EncodingFieldEditor; 36 import org.osgi.service.prefs.BackingStoreException; 37 import org.osgi.service.prefs.Preferences; 38 39 42 public class RepositoryEncodingPropertyPage extends PropertyPage implements IPropertyChangeListener { 43 44 private static final int LABEL_WIDTH_HINT = 400; 45 46 private EncodingFieldEditor encoding; 47 private ICVSRepositoryLocation location; 48 49 private boolean valueChanged; 50 51 public class OSGIPreferenceStore implements IPreferenceStore { 52 private Preferences preferences, defaults; 53 private boolean dirty; 54 55 59 public OSGIPreferenceStore(Preferences preferences, Preferences defaults) { 60 this.preferences = preferences; 61 this.defaults = defaults; 62 } 63 64 67 public void addPropertyChangeListener(IPropertyChangeListener listener) { 68 70 } 71 72 75 public void removePropertyChangeListener(IPropertyChangeListener listener) { 76 78 } 79 80 83 public void firePropertyChangeEvent(String name, Object oldValue, Object newValue) { 84 86 } 87 88 91 public boolean contains(String name) { 92 try { 93 String [] keys = preferences.keys(); 94 for (int i = 0; i < keys.length; i++) { 95 String string = keys[i]; 96 if (string.equals(name)) { 97 return true; 98 } 99 } 100 return false; 101 } catch (BackingStoreException e) { 102 CVSUIPlugin.log(new CVSStatus(IStatus.ERROR, CVSUIMessages.internal, e)); 103 return false; 104 } 105 } 106 107 108 111 public boolean getBoolean(String name) { 112 return preferences.getBoolean(name, getDefaultBoolean(name)); 113 } 114 115 118 public boolean getDefaultBoolean(String name) { 119 if (defaults != null) { 120 return defaults.getBoolean(name, false); 121 } 122 return false; 123 } 124 125 128 public double getDefaultDouble(String name) { 129 if (defaults != null) { 130 return defaults.getDouble(name, 0); 131 } 132 return 0; 133 } 134 135 138 public float getDefaultFloat(String name) { 139 if (defaults != null) { 140 return defaults.getFloat(name, 0); 141 } 142 return 0; 143 } 144 145 148 public int getDefaultInt(String name) { 149 if (defaults != null) { 150 return defaults.getInt(name, 0); 151 } 152 return 0; 153 } 154 155 158 public long getDefaultLong(String name) { 159 if (defaults != null) { 160 return defaults.getLong(name, 0); 161 } 162 return 0; 163 } 164 165 168 public String getDefaultString(String name) { 169 if (defaults != null) { 170 return defaults.get(name, null); 171 } 172 return null; 173 } 174 175 178 public double getDouble(String name) { 179 return preferences.getDouble(name, getDefaultDouble(name)); 180 } 181 182 185 public float getFloat(String name) { 186 return preferences.getFloat(name, getDefaultFloat(name)); 187 } 188 189 192 public int getInt(String name) { 193 return preferences.getInt(name, getDefaultInt(name)); 194 } 195 196 199 public long getLong(String name) { 200 return preferences.getLong(name, getDefaultLong(name)); 201 } 202 203 206 public String getString(String name) { 207 return preferences.get(name, getDefaultString(name)); 208 } 209 210 213 public boolean isDefault(String name) { 214 return !contains(name); 215 } 216 217 220 public boolean needsSaving() { 221 return dirty; 222 } 223 224 227 public void putValue(String name, String value) { 228 preferences.put(name, value); 229 dirty = true; 230 } 231 232 235 public void setDefault(String name, double value) { 236 } 238 239 242 public void setDefault(String name, float value) { 243 } 245 246 249 public void setDefault(String name, int value) { 250 } 252 253 256 public void setDefault(String name, long value) { 257 } 259 260 263 public void setDefault(String name, String defaultObject) { 264 } 266 267 270 public void setDefault(String name, boolean value) { 271 } 273 274 277 public void setToDefault(String name) { 278 preferences.remove(name); 279 dirty = true; 280 } 281 282 285 public void setValue(String name, double value) { 286 preferences.putDouble(name, value); 287 dirty = true; 288 } 289 290 293 public void setValue(String name, float value) { 294 preferences.putFloat(name, value); 295 dirty = true; 296 } 297 298 301 public void setValue(String name, int value) { 302 preferences.putInt(name, value); 303 dirty = true; 304 } 305 306 309 public void setValue(String name, long value) { 310 preferences.putLong(name, value); 311 dirty = true; 312 } 313 314 317 public void setValue(String name, String value) { 318 putValue(name, value); 319 } 320 321 324 public void setValue(String name, boolean value) { 325 preferences.putBoolean(name, value); 326 dirty = true; 327 } 328 } 329 330 333 protected Control createContents(Composite parent) { 334 initialize(); 335 336 Composite composite = new Composite(parent, SWT.NULL); 337 composite.setLayoutData(new GridData(GridData.FILL_BOTH)); 338 GridLayout layout = new GridLayout(); 339 composite.setLayout(layout); 340 341 Label label = createWrappingLabel(composite, CVSUIMessages.RepositoryEncodingPropertyPage_2, 1); 342 343 encoding = new EncodingFieldEditor(CVSRepositoryLocation.PREF_SERVER_ENCODING, CVSUIMessages.RepositoryEncodingPropertyPage_3, composite); 344 encoding.setPage(this); 345 encoding.setPreferenceStore(getLocationPreferenceStore()); 346 encoding.load(); 347 encoding.setPropertyChangeListener(this); 348 349 Link pageLink = new Link(composite, SWT.LEFT | SWT.WRAP); 350 pageLink.addSelectionListener(new SelectionAdapter() { 351 356 public void widgetSelected(SelectionEvent e) { 357 358 PreferenceDialog dialog = PreferencesUtil 359 .createPreferenceDialogOn( 360 getShell(), 361 "org.eclipse.ui.preferencePages.Workspace", null, null); dialog.open(); 363 364 } 365 }); 366 367 pageLink.setLayoutData(label.getLayoutData()); 368 pageLink.setText(CVSUIMessages.RepositoryEncodingPropertyPage_4); 369 370 PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IHelpContextIds.REPOSITORY_ENCODING_PROPERTY_PAGE); 371 Dialog.applyDialogFont(parent); 372 return composite; 373 } 374 375 private IPreferenceStore getLocationPreferenceStore() { 376 return new OSGIPreferenceStore( 377 ((CVSRepositoryLocation)location).getPreferences(), 378 CVSRepositoryLocation.getDefaultPreferences()); 379 } 380 381 private void initialize() { 382 location = null; 383 IAdaptable element = getElement(); 384 if (element instanceof ICVSRepositoryLocation) { 385 location = (ICVSRepositoryLocation)element; 386 } else { 387 Object adapter = element.getAdapter(ICVSRepositoryLocation.class); 388 if (adapter instanceof ICVSRepositoryLocation) { 389 location = (ICVSRepositoryLocation)adapter; 390 } 391 } 392 } 393 394 397 public void propertyChange(PropertyChangeEvent event) { 398 if (event.getProperty() == FieldEditor.IS_VALID) { 399 setValid(((Boolean )event.getNewValue()).booleanValue()); 400 return; 401 } else if (event.getProperty() == FieldEditor.VALUE) { 402 valueChanged = true; 403 return; 404 } 405 } 406 407 410 public boolean performOk() { 411 if (!valueChanged) { 412 } 415 if (!KnownRepositories.getInstance().isKnownRepository(location.getLocation(false))) { 416 MessageDialog.openInformation(getShell(), CVSUIMessages.RepositoryEncodingPropertyPage_0, NLS.bind(CVSUIMessages.RepositoryEncodingPropertyPage_1, new String [] { location.getLocation(true) })); return true; 419 } 420 encoding.store(); 421 try { 422 ((CVSRepositoryLocation)location).getPreferences().flush(); 423 } catch (BackingStoreException e) { 424 CVSUIPlugin.log(new CVSStatus(IStatus.ERROR, CVSUIMessages.internal, e)); 426 } 427 return true; 428 } 429 430 433 protected void performDefaults() { 434 super.performDefaults(); 435 encoding.loadDefault(); 436 } 437 438 private Label createWrappingLabel(Composite parent, String text, int horizontalSpan) { 439 Label label = new Label(parent, SWT.LEFT | SWT.WRAP); 440 label.setText(text); 441 label.setFont(parent.getFont()); 442 GridData data = new GridData(); 443 data.horizontalSpan = horizontalSpan; 444 data.horizontalAlignment = GridData.FILL; 445 data.grabExcessHorizontalSpace = true; 446 data.widthHint = LABEL_WIDTH_HINT; 447 label.setLayoutData(data); 448 return label; 449 } 450 } 451 | Popular Tags |