KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-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 java.io.UnsupportedEncodingException JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Collections JavaDoc;
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 /**
29  * A field editor that allows the user to choose an encoding.
30  */

31 public class EncodingFieldEditor extends FieldEditor {
32     private Composite container;
33     private Button defaultEncodingButton;
34     private String JavaDoc defaultEnc;
35     private Button otherEncodingButton;
36     private Combo encodingCombo;
37     private boolean isValid = true;
38     private String JavaDoc oldSelectedEncoding;
39     
40     /**
41      * Create an encoding filed editor.
42      * @param name the name of the preference this field editor works on
43      * @param labelText the label text of the field editor
44      * @param parent the parent of the field editor's control
45      */

46     public EncodingFieldEditor(String JavaDoc name, String JavaDoc labelText, Composite parent) {
47         super(name, labelText, parent);
48     }
49     
50     /* (non-Javadoc)
51      * @see org.eclipse.jface.preference.FieldEditor#adjustForNumColumns(int)
52      */

53     protected void adjustForNumColumns(int numColumns) {
54         ((GridData)getContainer().getLayoutData()).horizontalSpan = numColumns;
55     }
56
57     /* (non-Javadoc)
58      * @see org.eclipse.jface.preference.FieldEditor#doFillIntoGrid(org.eclipse.swt.widgets.Composite, int)
59      */

60     protected void doFillIntoGrid(Composite parent, int numColumns) {
61         container = createEncodingGroup(parent, numColumns);
62     }
63     
64     /* (non-Javadoc)
65      * @see org.eclipse.jface.preference.FieldEditor#doLoad()
66      */

67     protected void doLoad() {
68         if (encodingCombo != null) {
69             String JavaDoc 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     /* (non-Javadoc)
81      * @see org.eclipse.jface.preference.FieldEditor#doLoadDefault()
82      */

83     protected void doLoadDefault() {
84         updateEncodingState(true);
85     }
86     
87     /* (non-Javadoc)
88      * @see org.eclipse.jface.preference.FieldEditor#doStore()
89      */

90     protected void doStore() {
91         String JavaDoc encoding = getSelectedEncoding();
92         if (encoding.equals(defaultEnc)) {
93             getPreferenceStore().setToDefault(getPreferenceName());
94         } else {
95             getPreferenceStore().setValue(getPreferenceName(), encoding);
96         }
97     }
98     
99     /* (non-Javadoc)
100      * @see org.eclipse.jface.preference.FieldEditor#getNumberOfControls()
101      */

102     public int getNumberOfControls() {
103         return 1;
104     }
105     
106     /* (non-Javadoc)
107      * @see org.eclipse.jface.preference.FieldEditor#isValid()
108      */

109     public boolean isValid() {
110         return isValid;
111     }
112     
113     /* (non-Javadoc)
114      * @see org.eclipse.jface.preference.FieldEditor#refreshValidState()
115      */

116     protected void refreshValidState() {
117         updateValidState();
118     }
119     
120     /* (non-Javadoc)
121      * @see org.eclipse.jface.preference.FieldEditor#setPreferenceStore(org.eclipse.jface.preference.IPreferenceStore)
122      */

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 JavaDoc[] { defaultEnc })); //$NON-NLS-1$
131
}
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()); //$NON-NLS-1$
148
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"); //$NON-NLS-1$ //$NON-NLS-2$
159
}
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")); //$NON-NLS-1$
170
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 JavaDoc encodings = new ArrayList JavaDoc();
190         int n = 0;
191         try {
192             n = Integer.parseInt(IDEWorkbenchMessages.getString("WorkbenchPreference.numDefaultEncodings")); //$NON-NLS-1$
193
}
194         catch (NumberFormatException JavaDoc e) {
195             // Ignore;
196
}
197         for (int i = 0; i < n; ++i) {
198             String JavaDoc enc = IDEWorkbenchMessages.getString("WorkbenchPreference.defaultEncoding" + (i+1), null); //$NON-NLS-1$
199
if (enc != null) {
200                 encodings.add(enc);
201             }
202         }
203         
204         if (!encodings.contains(defaultEnc)) {
205             encodings.add(defaultEnc);
206         }
207
208         String JavaDoc 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 JavaDoc) 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")); //$NON-NLS-1$
249
}
250             fireStateChanged(IS_VALID, !isValid, isValid);
251             
252             String JavaDoc newValue = getSelectedEncoding();
253             if (isValid && !newValue.equals(oldSelectedEncoding)) {
254                 fireValueChanged(VALUE, oldSelectedEncoding, newValue);
255                 oldSelectedEncoding = newValue;
256             }
257         }
258     }
259     
260     private String JavaDoc 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 JavaDoc enc) {
275         try {
276             new String JavaDoc(new byte[0], enc);
277             return true;
278         } catch (UnsupportedEncodingException JavaDoc e) {
279             return false;
280         }
281     }
282 }
283
Popular Tags