KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > ide > dialogs > AbstractEncodingFieldEditor


1 /*******************************************************************************
2  * Copyright (c) 2004, 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  * Daniel Megert daniel_megert@ch.ibm.com Bug 169696
11  *******************************************************************************/

12 package org.eclipse.ui.ide.dialogs;
13
14 import java.nio.charset.Charset JavaDoc;
15 import java.nio.charset.IllegalCharsetNameException JavaDoc;
16 import java.util.List JavaDoc;
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 /**
37  * The abstract superclass of field editors used to set an encoding. Any user
38  * entered encodings will be added to the list of encodings available via
39  * {@link org.eclipse.ui.ide.IDEEncoding}.
40  * <p>
41  * Subclasses may extend, but must call <code>createEncodingGroup</code>
42  * during <code>doFillIntoGrid</code>.
43  * </p>
44  *
45  * @see org.eclipse.ui.ide.IDEEncoding
46  * @since 3.1
47  */

48 public abstract class AbstractEncodingFieldEditor extends FieldEditor {
49
50     private Composite container;
51
52     private Button defaultEncodingButton;
53
54     private String JavaDoc defaultEnc;
55
56     private Button otherEncodingButton;
57
58     private Combo encodingCombo;
59
60     private boolean isValid = true;
61
62     private String JavaDoc oldSelectedEncoding;
63
64     private String JavaDoc groupTitle = IDEWorkbenchMessages.WorkbenchPreference_encoding;
65
66     /**
67      * Creates a new encoding field editor with no settings set.
68      */

69     protected AbstractEncodingFieldEditor() {
70         super();
71     }
72
73     /**
74      * Creates a new encoding field editor with the given preference name, label
75      * and parent.
76      *
77      * @param name
78      * the name of the preference this field editor works on
79      * @param labelText
80      * the label text of the field editor
81      * @param parent
82      * the parent of the field editor's control
83      */

84     protected AbstractEncodingFieldEditor(String JavaDoc name, String JavaDoc labelText,
85             Composite parent) {
86         super(name, labelText, parent);
87     }
88
89     /**
90      * Creates a new encoding field editor with the given preference name, label
91      * and parent.
92      *
93      * @param name
94      * the name of the preference this field editor works on
95      * @param labelText
96      * the label text of the field editor
97      * @param groupTitle
98      * the title for the field editor's control. If groupTitle is
99      * <code>null</code> the control will be unlabelled
100      * (by default a {@link Composite} instead of a {@link Group}.
101      * @param parent
102      * the parent of the field editor's control
103      * @see AbstractEncodingFieldEditor#setGroupTitle(String)
104      * @since 3.3
105      */

106     protected AbstractEncodingFieldEditor(String JavaDoc name, String JavaDoc labelText,
107             String JavaDoc groupTitle, Composite parent) {
108         init(name, labelText);
109         this.groupTitle = groupTitle;
110         createControl(parent);
111     }
112
113     /*
114      * (non-Javadoc)
115      *
116      * @see org.eclipse.jface.preference.FieldEditor#adjustForNumColumns(int)
117      */

118     protected void adjustForNumColumns(int numColumns) {
119         ((GridData) getContainer().getLayoutData()).horizontalSpan = numColumns;
120     }
121
122     /*
123      * (non-Javadoc)
124      *
125      * @see org.eclipse.jface.preference.FieldEditor#doFillIntoGrid(org.eclipse.swt.widgets.Composite,
126      * int)
127      */

128     protected void doFillIntoGrid(Composite parent, int numColumns) {
129         container = createEncodingGroup(parent, numColumns);
130     }
131
132     /*
133      * (non-Javadoc)
134      *
135      * @see org.eclipse.jface.preference.FieldEditor#doLoad()
136      */

137     protected void doLoad() {
138         if (encodingCombo != null) {
139             List JavaDoc encodings = IDEEncoding.getIDEEncodings();
140             String JavaDoc resourcePreference = getStoredValue();
141             populateEncodingsCombo(encodings, resourcePreference);
142             updateEncodingState(resourcePreference == null);
143         }
144     }
145
146     /**
147      * Returns the value that is currently stored for the encoding.
148      *
149      * @return the currently stored encoding
150      */

151     protected abstract String JavaDoc getStoredValue();
152
153     /*
154      * (non-Javadoc)
155      *
156      * @see org.eclipse.jface.preference.FieldEditor#doLoadDefault()
157      */

158     protected void doLoadDefault() {
159         updateEncodingState(true);
160     }
161
162     /*
163      * (non-Javadoc)
164      *
165      * @see org.eclipse.jface.preference.FieldEditor#getNumberOfControls()
166      */

167     public int getNumberOfControls() {
168         return 1;
169     }
170
171     /*
172      * (non-Javadoc)
173      *
174      * @see org.eclipse.jface.preference.FieldEditor#isValid()
175      */

176     public boolean isValid() {
177         return isValid;
178     }
179
180     /*
181      * (non-Javadoc)
182      *
183      * @see org.eclipse.jface.preference.FieldEditor#refreshValidState()
184      */

185     protected void refreshValidState() {
186         updateValidState();
187     }
188
189     /*
190      * (non-Javadoc)
191      *
192      * @see org.eclipse.jface.preference.FieldEditor#setPreferenceStore(org.eclipse.jface.preference.IPreferenceStore)
193      */

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     /**
209      * Creates a composite with all the encoding controls.
210      * <p>
211      * Subclasses may extend.
212      * </p>
213      *
214      * @param parent
215      * the parent widget
216      * @param numColumns
217      * the number of columns in the parent
218      * @return the group control
219      */

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             /*
266              * (non-Javadoc)
267              *
268              * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
269              */

270             public void widgetSelected(SelectionEvent e) {
271                 updateValidState();
272             }
273         });
274         encodingCombo.addKeyListener(new KeyAdapter() {
275             /*
276              * (non-Javadoc)
277              *
278              * @see org.eclipse.swt.events.KeyListener#keyReleased(org.eclipse.swt.events.KeyEvent)
279              */

280             public void keyReleased(KeyEvent e) {
281                 updateValidState();
282             }
283         });
284
285         return topControl;
286     }
287
288     /*
289      * (non-Javadoc)
290      *
291      * @see org.eclipse.jface.preference.FieldEditor#setEnabled(boolean,
292      * org.eclipse.swt.widgets.Composite)
293      * @since 3.3
294      */

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     /**
308      * Returns the default encoding for the object being shown.
309      *
310      * @return the default encoding for the object being shown
311      */

312     protected String JavaDoc findDefaultEncoding() {
313         return WorkbenchEncoding.getWorkbenchDefaultEncoding();
314     }
315
316     /**
317      * Returns the text for the default encoding button.
318      *
319      * @return the text for the default encoding button
320      */

321     protected String JavaDoc defaultButtonText() {
322         return NLS.bind(
323                 IDEWorkbenchMessages.WorkbenchPreference_defaultEncoding,
324                 defaultEnc);
325     }
326
327     /**
328      * Populates the encodings combo. Sets the text based on the selected
329      * encoding. If there is no selected encoding, the text is set to the
330      * default encoding.
331      *
332      * @param encodings
333      * the list of encodings (list of String)
334      * @param selectedEncoding
335      * the selected encoding, or <code>null</code>
336      */

337     private void populateEncodingsCombo(List JavaDoc encodings, String JavaDoc selectedEncoding) {
338         String JavaDoc[] encodingStrings = new String JavaDoc[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 JavaDoc newValue = getSelectedEncoding();
372         if (isValid && !newValue.equals(oldSelectedEncoding)) {
373             fireValueChanged(VALUE, oldSelectedEncoding, newValue);
374             oldSelectedEncoding = newValue;
375         }
376     }
377
378     /**
379      * Returns the currently selected encoding.
380      *
381      * @return the currently selected encoding
382      */

383     protected String JavaDoc 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     /**
396      * Returns whether or not the given encoding is valid.
397      *
398      * @param enc
399      * the encoding to validate
400      * @return <code>true</code> if the encoding is valid, <code>false</code>
401      * otherwise
402      */

403     private boolean isValidEncoding(String JavaDoc enc) {
404         try {
405             return Charset.isSupported(enc);
406         } catch (IllegalCharsetNameException JavaDoc e) {
407             // This is a valid exception
408
return false;
409         }
410
411     }
412
413     /**
414      * Returns the default encoding.
415      *
416      * @return the default encoding
417      */

418     protected String JavaDoc getDefaultEnc() {
419         return defaultEnc;
420     }
421
422     /**
423      * Returns whether or not the encoding setting changed.
424      *
425      * @param encodingSetting
426      * the setting from the page.
427      * @return boolean <code>true</code> if the resource encoding is the same
428      * as before.
429      */

430     protected boolean hasSameEncoding(String JavaDoc encodingSetting) {
431
432         String JavaDoc current = getStoredValue();
433
434         if (encodingSetting == null) {
435             // Changed if default is selected and there is no setting
436
return current == null || current.length() == 0;
437         }
438         return encodingSetting.equals(current);
439     }
440
441     /**
442      * Return whether or not the default has been selected.
443      *
444      * @return <code>true</code> if the default button has been selected.
445      */

446     boolean isDefaultSelected() {
447         return defaultEncodingButton.getSelection();
448     }
449
450     /**
451      * Set the title of the group to groupTitle. If this is not called a default
452      * title is used. If groupTitle is <code>null</code> the control will be
453      * unlabelled (by default a {@link Composite} instead of a {@link Group}.
454      *
455      * <strong>NOTE</strong> this value must be set before
456      * {@link #createControl(Composite)} is called or it will be ignored.
457      *
458      * @param groupTitle
459      * The groupTitle to set.
460      * @since 3.3
461      */

462     public void setGroupTitle(String JavaDoc groupTitle) {
463         this.groupTitle = groupTitle;
464     }
465
466 }
467
Popular Tags