KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ui > repo > RepositoryEncodingPropertyPage


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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 /**
40  * Repository preference page for setting the encoding of the server
41  */

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         /**
56          * Create a wrapper for the given OSGI preferences node
57          * @param preferences an OSGI preferences node
58          */

59         public OSGIPreferenceStore(Preferences preferences, Preferences defaults) {
60             this.preferences = preferences;
61             this.defaults = defaults;
62         }
63
64         /* (non-Javadoc)
65          * @see org.eclipse.jface.preference.IPreferenceStore#addPropertyChangeListener(org.eclipse.jface.util.IPropertyChangeListener)
66          */

67         public void addPropertyChangeListener(IPropertyChangeListener listener) {
68             // TODO Auto-generated method stub
69

70         }
71         
72         /* (non-Javadoc)
73          * @see org.eclipse.jface.preference.IPreferenceStore#removePropertyChangeListener(org.eclipse.jface.util.IPropertyChangeListener)
74          */

75         public void removePropertyChangeListener(IPropertyChangeListener listener) {
76             // TODO Auto-generated method stub
77

78         }
79         
80         /* (non-Javadoc)
81          * @see org.eclipse.jface.preference.IPreferenceStore#firePropertyChangeEvent(java.lang.String, java.lang.Object, java.lang.Object)
82          */

83         public void firePropertyChangeEvent(String JavaDoc name, Object JavaDoc oldValue, Object JavaDoc newValue) {
84             // TODO Auto-generated method stub
85

86         }
87
88         /* (non-Javadoc)
89          * @see org.eclipse.jface.preference.IPreferenceStore#contains(java.lang.String)
90          */

91         public boolean contains(String JavaDoc name) {
92             try {
93                 String JavaDoc[] keys = preferences.keys();
94                 for (int i = 0; i < keys.length; i++) {
95                     String JavaDoc 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         /* (non-Javadoc)
109          * @see org.eclipse.jface.preference.IPreferenceStore#getBoolean(java.lang.String)
110          */

111         public boolean getBoolean(String JavaDoc name) {
112             return preferences.getBoolean(name, getDefaultBoolean(name));
113         }
114
115         /* (non-Javadoc)
116          * @see org.eclipse.jface.preference.IPreferenceStore#getDefaultBoolean(java.lang.String)
117          */

118         public boolean getDefaultBoolean(String JavaDoc name) {
119             if (defaults != null) {
120                 return defaults.getBoolean(name, false);
121             }
122             return false;
123         }
124
125         /* (non-Javadoc)
126          * @see org.eclipse.jface.preference.IPreferenceStore#getDefaultDouble(java.lang.String)
127          */

128         public double getDefaultDouble(String JavaDoc name) {
129             if (defaults != null) {
130                 return defaults.getDouble(name, 0);
131             }
132             return 0;
133         }
134
135         /* (non-Javadoc)
136          * @see org.eclipse.jface.preference.IPreferenceStore#getDefaultFloat(java.lang.String)
137          */

138         public float getDefaultFloat(String JavaDoc name) {
139             if (defaults != null) {
140                 return defaults.getFloat(name, 0);
141             }
142             return 0;
143         }
144
145         /* (non-Javadoc)
146          * @see org.eclipse.jface.preference.IPreferenceStore#getDefaultInt(java.lang.String)
147          */

148         public int getDefaultInt(String JavaDoc name) {
149             if (defaults != null) {
150                 return defaults.getInt(name, 0);
151             }
152             return 0;
153         }
154
155         /* (non-Javadoc)
156          * @see org.eclipse.jface.preference.IPreferenceStore#getDefaultLong(java.lang.String)
157          */

158         public long getDefaultLong(String JavaDoc name) {
159             if (defaults != null) {
160                 return defaults.getLong(name, 0);
161             }
162             return 0;
163         }
164
165         /* (non-Javadoc)
166          * @see org.eclipse.jface.preference.IPreferenceStore#getDefaultString(java.lang.String)
167          */

168         public String JavaDoc getDefaultString(String JavaDoc name) {
169             if (defaults != null) {
170                 return defaults.get(name, null);
171             }
172             return null;
173         }
174
175         /* (non-Javadoc)
176          * @see org.eclipse.jface.preference.IPreferenceStore#getDouble(java.lang.String)
177          */

178         public double getDouble(String JavaDoc name) {
179             return preferences.getDouble(name, getDefaultDouble(name));
180         }
181
182         /* (non-Javadoc)
183          * @see org.eclipse.jface.preference.IPreferenceStore#getFloat(java.lang.String)
184          */

185         public float getFloat(String JavaDoc name) {
186             return preferences.getFloat(name, getDefaultFloat(name));
187         }
188
189         /* (non-Javadoc)
190          * @see org.eclipse.jface.preference.IPreferenceStore#getInt(java.lang.String)
191          */

192         public int getInt(String JavaDoc name) {
193             return preferences.getInt(name, getDefaultInt(name));
194         }
195
196         /* (non-Javadoc)
197          * @see org.eclipse.jface.preference.IPreferenceStore#getLong(java.lang.String)
198          */

199         public long getLong(String JavaDoc name) {
200             return preferences.getLong(name, getDefaultLong(name));
201         }
202
203         /* (non-Javadoc)
204          * @see org.eclipse.jface.preference.IPreferenceStore#getString(java.lang.String)
205          */

206         public String JavaDoc getString(String JavaDoc name) {
207             return preferences.get(name, getDefaultString(name));
208         }
209
210         /* (non-Javadoc)
211          * @see org.eclipse.jface.preference.IPreferenceStore#isDefault(java.lang.String)
212          */

213         public boolean isDefault(String JavaDoc name) {
214             return !contains(name);
215         }
216
217         /* (non-Javadoc)
218          * @see org.eclipse.jface.preference.IPreferenceStore#needsSaving()
219          */

220         public boolean needsSaving() {
221             return dirty;
222         }
223
224         /* (non-Javadoc)
225          * @see org.eclipse.jface.preference.IPreferenceStore#putValue(java.lang.String, java.lang.String)
226          */

227         public void putValue(String JavaDoc name, String JavaDoc value) {
228             preferences.put(name, value);
229             dirty = true;
230         }
231
232         /* (non-Javadoc)
233          * @see org.eclipse.jface.preference.IPreferenceStore#setDefault(java.lang.String, double)
234          */

235         public void setDefault(String JavaDoc name, double value) {
236             // Defaults cannot be set this way
237
}
238
239         /* (non-Javadoc)
240          * @see org.eclipse.jface.preference.IPreferenceStore#setDefault(java.lang.String, float)
241          */

242         public void setDefault(String JavaDoc name, float value) {
243             // Defaults cannot be set this way
244
}
245
246         /* (non-Javadoc)
247          * @see org.eclipse.jface.preference.IPreferenceStore#setDefault(java.lang.String, int)
248          */

249         public void setDefault(String JavaDoc name, int value) {
250             // Defaults cannot be set this way
251
}
252
253         /* (non-Javadoc)
254          * @see org.eclipse.jface.preference.IPreferenceStore#setDefault(java.lang.String, long)
255          */

256         public void setDefault(String JavaDoc name, long value) {
257             // Defaults cannot be set this way
258
}
259
260         /* (non-Javadoc)
261          * @see org.eclipse.jface.preference.IPreferenceStore#setDefault(java.lang.String, java.lang.String)
262          */

263         public void setDefault(String JavaDoc name, String JavaDoc defaultObject) {
264             // Defaults cannot be set this way
265
}
266
267         /* (non-Javadoc)
268          * @see org.eclipse.jface.preference.IPreferenceStore#setDefault(java.lang.String, boolean)
269          */

270         public void setDefault(String JavaDoc name, boolean value) {
271             // Defaults cannot be set this way
272
}
273
274         /* (non-Javadoc)
275          * @see org.eclipse.jface.preference.IPreferenceStore#setToDefault(java.lang.String)
276          */

277         public void setToDefault(String JavaDoc name) {
278             preferences.remove(name);
279             dirty = true;
280         }
281
282         /* (non-Javadoc)
283          * @see org.eclipse.jface.preference.IPreferenceStore#setValue(java.lang.String, double)
284          */

285         public void setValue(String JavaDoc name, double value) {
286             preferences.putDouble(name, value);
287             dirty = true;
288         }
289
290         /* (non-Javadoc)
291          * @see org.eclipse.jface.preference.IPreferenceStore#setValue(java.lang.String, float)
292          */

293         public void setValue(String JavaDoc name, float value) {
294             preferences.putFloat(name, value);
295             dirty = true;
296         }
297
298         /* (non-Javadoc)
299          * @see org.eclipse.jface.preference.IPreferenceStore#setValue(java.lang.String, int)
300          */

301         public void setValue(String JavaDoc name, int value) {
302             preferences.putInt(name, value);
303             dirty = true;
304         }
305
306         /* (non-Javadoc)
307          * @see org.eclipse.jface.preference.IPreferenceStore#setValue(java.lang.String, long)
308          */

309         public void setValue(String JavaDoc name, long value) {
310             preferences.putLong(name, value);
311             dirty = true;
312         }
313
314         /* (non-Javadoc)
315          * @see org.eclipse.jface.preference.IPreferenceStore#setValue(java.lang.String, java.lang.String)
316          */

317         public void setValue(String JavaDoc name, String JavaDoc value) {
318             putValue(name, value);
319         }
320
321         /* (non-Javadoc)
322          * @see org.eclipse.jface.preference.IPreferenceStore#setValue(java.lang.String, boolean)
323          */

324         public void setValue(String JavaDoc name, boolean value) {
325             preferences.putBoolean(name, value);
326             dirty = true;
327         }
328     }
329     
330     /* (non-Javadoc)
331      * @see org.eclipse.jface.preference.PreferencePage#createContents(org.eclipse.swt.widgets.Composite)
332      */

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             /*
352              * (non-Javadoc)
353              *
354              * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
355              */

356             public void widgetSelected(SelectionEvent e) {
357             
358                 PreferenceDialog dialog = PreferencesUtil
359                         .createPreferenceDialogOn(
360                                 getShell(),
361                                 "org.eclipse.ui.preferencePages.Workspace", null, null); //$NON-NLS-1$
362
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 JavaDoc adapter = element.getAdapter(ICVSRepositoryLocation.class);
388             if (adapter instanceof ICVSRepositoryLocation) {
389                 location = (ICVSRepositoryLocation)adapter;
390             }
391         }
392     }
393
394     /* (non-Javadoc)
395      * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
396      */

397     public void propertyChange(PropertyChangeEvent event) {
398         if (event.getProperty() == FieldEditor.IS_VALID) {
399             setValid(((Boolean JavaDoc)event.getNewValue()).booleanValue());
400             return;
401         } else if (event.getProperty() == FieldEditor.VALUE) {
402             valueChanged = true;
403             return;
404         }
405     }
406     
407     /* (non-Javadoc)
408      * @see org.eclipse.jface.preference.IPreferencePage#performOk()
409      */

410     public boolean performOk() {
411         if (!valueChanged) {
412             // See bug 137073
413
// return true;
414
}
415         if (!KnownRepositories.getInstance().isKnownRepository(location.getLocation(false))) {
416             // The location may have been replaced by the main properties page
417
MessageDialog.openInformation(getShell(), CVSUIMessages.RepositoryEncodingPropertyPage_0, NLS.bind(CVSUIMessages.RepositoryEncodingPropertyPage_1, new String JavaDoc[] { location.getLocation(true) })); //
418
return true;
419         }
420         encoding.store();
421         try {
422             ((CVSRepositoryLocation)location).getPreferences().flush();
423         } catch (BackingStoreException e) {
424             // Log and ignore
425
CVSUIPlugin.log(new CVSStatus(IStatus.ERROR, CVSUIMessages.internal, e));
426         }
427         return true;
428     }
429     
430     /* (non-Javadoc)
431      * @see org.eclipse.jface.preference.PreferencePage#performDefaults()
432      */

433     protected void performDefaults() {
434         super.performDefaults();
435         encoding.loadDefault();
436     }
437     
438     private Label createWrappingLabel(Composite parent, String JavaDoc 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