KickJava   Java API By Example, From Geeks To Geeks.

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


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  *******************************************************************************/

11 package org.eclipse.ui.ide.dialogs;
12
13 import org.eclipse.core.resources.IContainer;
14 import org.eclipse.core.resources.IFile;
15 import org.eclipse.core.resources.IResource;
16 import org.eclipse.core.resources.IWorkspaceRoot;
17 import org.eclipse.core.runtime.Assert;
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IProgressMonitor;
20 import org.eclipse.core.runtime.IStatus;
21 import org.eclipse.core.runtime.Status;
22 import org.eclipse.core.runtime.content.IContentDescription;
23 import org.eclipse.core.runtime.jobs.Job;
24 import org.eclipse.jface.dialogs.DialogPage;
25 import org.eclipse.jface.dialogs.IDialogConstants;
26 import org.eclipse.jface.dialogs.MessageDialog;
27 import org.eclipse.osgi.util.NLS;
28 import org.eclipse.swt.SWT;
29 import org.eclipse.swt.layout.GridData;
30 import org.eclipse.swt.widgets.Composite;
31 import org.eclipse.swt.widgets.Control;
32 import org.eclipse.swt.widgets.Group;
33 import org.eclipse.swt.widgets.Label;
34 import org.eclipse.swt.widgets.Shell;
35 import org.eclipse.ui.WorkbenchEncoding;
36 import org.eclipse.ui.ide.IDEEncoding;
37 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
38 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
39
40 /**
41  * The ResourceEncodingFieldEditor is a field editor for editing the encoding of
42  * a resource and does not use a preference store.
43  * <p>
44  * This class may be instantiated; it is not intended to be subclassed.
45  * </p>
46  *
47  * @since 3.1
48  */

49 public final class ResourceEncodingFieldEditor extends
50         AbstractEncodingFieldEditor {
51
52     /**
53      * The resource being edited.
54      */

55     private IResource resource;
56
57     private Composite group;
58
59     /**
60      * Creates a new encoding field editor for setting the encoding on the given
61      * resource.
62      *
63      * @param labelText
64      * the label text of the field editor
65      * @param parent
66      * the parent of the field editor's control
67      * @param charsetResource
68      * must be an <code>IContainer</code> or an <code>IFile</code>.
69      *
70      * @see org.eclipse.core.resources.IContainer#getDefaultCharset()
71      * @see org.eclipse.core.resources.IFile#getCharset()
72      */

73     public ResourceEncodingFieldEditor(String JavaDoc labelText, Composite parent,
74             IResource charsetResource) {
75         super();
76         setLabelAndResource(labelText, charsetResource);
77         createControl(parent);
78     }
79     
80     /**
81      * Creates a new encoding field editor for setting the encoding on the given
82      * resource.
83      *
84      * @param labelText
85      * the label text of the field editor
86      * @param parent
87      * the parent of the field editor's control
88      * @param charsetResource
89      * must be an <code>IContainer</code> or an <code>IFile</code>.
90      * @param groupTitle
91      * the title for the field editor's control. If groupTitle is
92      * <code>null</code> the control will be unlabelled
93      * (by default a {@link Composite} instead of a {@link Group}.
94      *
95      * @see org.eclipse.core.resources.IContainer#getDefaultCharset()
96      * @see org.eclipse.core.resources.IFile#getCharset()
97      * @see AbstractEncodingFieldEditor#setGroupTitle(String)
98      * @since 3.3
99      */

100     public ResourceEncodingFieldEditor(String JavaDoc labelText, Composite parent,
101             IResource charsetResource,String JavaDoc groupTitle) {
102         super();
103         setLabelAndResource(labelText, charsetResource);
104         setGroupTitle(groupTitle);
105         createControl(parent);
106     }
107
108     /**
109      * Set the label text and the resource we are editing.
110      * @param labelText
111      * @param charsetResource
112      * @since 3.3
113      */

114     private void setLabelAndResource(String JavaDoc labelText, IResource charsetResource) {
115         Assert.isTrue(charsetResource instanceof IContainer
116                 || charsetResource instanceof IFile);
117         setLabelText(labelText);
118         this.resource = charsetResource;
119     }
120
121     /*
122      * (non-Javadoc)
123      *
124      * @see org.eclipse.ui.internal.ide.dialogs.AbstractEncodingFieldEditor#getStoredValue()
125      */

126     protected String JavaDoc getStoredValue() {
127         try {
128             if (resource instanceof IContainer) {
129                 return ((IContainer) resource).getDefaultCharset(false);
130             }
131             return ((IFile) resource).getCharset(false);
132
133         } catch (CoreException e) {// If there is an error return the default
134
IDEWorkbenchPlugin
135                     .log(
136                             IDEWorkbenchMessages.ResourceEncodingFieldEditor_ErrorLoadingMessage,
137                             e.getStatus());
138             return WorkbenchEncoding.getWorkbenchDefaultEncoding();
139         }
140
141     }
142
143     /*
144      * (non-Javadoc)
145      *
146      * @see org.eclipse.jface.preference.FieldEditor#doStore()
147      */

148     protected void doStore() {
149
150         String JavaDoc encoding = getSelectedEncoding();
151
152         // Clear the value if nothing is selected
153
if (isDefaultSelected()) {
154             encoding = null;
155         }
156         // Don't update if the same thing is selected
157
if (hasSameEncoding(encoding)) {
158             return;
159         }
160
161         String JavaDoc descriptionCharset = getCharsetFromDescription();
162         if (descriptionCharset != null
163                 && !(descriptionCharset.equals(encoding)) && encoding != null) {
164             Shell shell = null;
165             DialogPage page = getPage();
166             if (page != null) {
167                 shell = page.getShell();
168             }
169
170             MessageDialog dialog = new MessageDialog(
171                     shell,
172                     IDEWorkbenchMessages.ResourceEncodingFieldEditor_EncodingConflictTitle,
173                     null,
174                     NLS
175                             .bind(
176                                     IDEWorkbenchMessages.ResourceEncodingFieldEditor_EncodingConflictMessage,
177                                     encoding, descriptionCharset),
178                     MessageDialog.WARNING, new String JavaDoc[] {
179                             IDialogConstants.YES_LABEL,
180                             IDialogConstants.NO_LABEL }, 0); // yes is the
181
// default
182
if (dialog.open() > 0) {
183                 return;
184             }
185         }
186
187         IDEEncoding.addIDEEncoding(encoding);
188
189         final String JavaDoc finalEncoding = encoding;
190
191         Job charsetJob = new Job(IDEWorkbenchMessages.IDEEncoding_EncodingJob) {
192             /*
193              * (non-Javadoc)
194              *
195              * @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
196              */

197             protected IStatus run(IProgressMonitor monitor) {
198                 try {
199                     if (resource instanceof IContainer) {
200                         ((IContainer) resource).setDefaultCharset(
201                                 finalEncoding, monitor);
202                     } else {
203                         ((IFile) resource).setCharset(finalEncoding, monitor);
204                     }
205                     return Status.OK_STATUS;
206                 } catch (CoreException e) {// If there is an error return the
207
// default
208
IDEWorkbenchPlugin
209                             .log(
210                                     IDEWorkbenchMessages.ResourceEncodingFieldEditor_ErrorStoringMessage,
211                                     e.getStatus());
212                     return e.getStatus();
213                 }
214             }
215         };
216
217         charsetJob.schedule();
218
219     }
220
221     /*
222      * (non-Javadoc)
223      *
224      * @see org.eclipse.jface.preference.FieldEditor#store()
225      */

226     public void store() {// Override the store method as we are not using a
227
// preference store
228
doStore();
229     }
230
231     /*
232      * (non-Javadoc)
233      *
234      * @see org.eclipse.jface.preference.FieldEditor#load()
235      */

236     public void load() {// Override the load method as we are not using a
237
// preference store
238
setPresentsDefaultValue(false);
239         doLoad();
240     }
241
242     /*
243      * (non-Javadoc)
244      *
245      * @see org.eclipse.jface.preference.FieldEditor#loadDefault()
246      */

247     public void loadDefault() {
248         setPresentsDefaultValue(true);
249         doLoadDefault();
250         refreshValidState();
251     }
252
253     /*
254      * (non-Javadoc)
255      *
256      * @see org.eclipse.ui.ide.dialogs.AbstractEncodingFieldEditor#findDefaultEncoding()
257      */

258     protected String JavaDoc findDefaultEncoding() {
259
260         if (resource instanceof IWorkspaceRoot) {
261             return super.findDefaultEncoding();
262         }
263
264         String JavaDoc defaultCharset = getCharsetFromDescription();
265         defaultCharset = getCharsetFromDescription();
266
267         if (defaultCharset != null && defaultCharset.length() > 0) {
268             return defaultCharset;
269         }
270         try {
271             // Query up the whole hierarchy
272
defaultCharset = resource.getParent().getDefaultCharset(true);
273         } catch (CoreException exception) {
274             // If there is an exception try again
275
}
276
277         if (defaultCharset != null && defaultCharset.length() > 0) {
278             return defaultCharset;
279         }
280
281         return super.findDefaultEncoding();
282     }
283
284     /**
285      * Returns the charset from the content description if there is one.
286      *
287      * @return the charset from the content description, or <code>null</code>
288      */

289     private String JavaDoc getCharsetFromDescription() {
290         IContentDescription description = getContentDescription();
291         if (description != null) {
292             return description.getCharset();
293         }
294         return null;
295     }
296
297     /*
298      * (non-Javadoc)
299      *
300      * @see org.eclipse.ui.ide.dialogs.AbstractEncodingFieldEditor#defaultButtonText()
301      */

302     protected String JavaDoc defaultButtonText() {
303
304         if (resource instanceof IWorkspaceRoot) {
305             return super.defaultButtonText();
306         }
307
308         if (resource instanceof IFile) {
309             try {
310                 IContentDescription description = ((IFile) resource)
311                         .getContentDescription();
312                 // If we can find a charset from the description then derive
313
// from that
314
if (description == null || description.getCharset() == null) {
315                     return NLS
316                             .bind(
317                                     IDEWorkbenchMessages.ResourceInfo_fileContainerEncodingFormat,
318                                     getDefaultEnc());
319                 }
320
321                 return NLS
322                         .bind(
323                                 IDEWorkbenchMessages.ResourceInfo_fileContentEncodingFormat,
324                                 getDefaultEnc());
325
326             } catch (CoreException exception) {
327                 // Do nothing here as we will just try to derive from the
328
// container
329
}
330         }
331
332         return NLS.bind(
333                 IDEWorkbenchMessages.ResourceInfo_containerEncodingFormat,
334                 getDefaultEnc());
335
336     }
337
338     /*
339      * (non-Javadoc)
340      *
341      * @see org.eclipse.ui.ide.dialogs.AbstractEncodingFieldEditor#createEncodingGroup(org.eclipse.swt.widgets.Composite,
342      * int)
343      */

344     protected Composite createEncodingGroup(Composite parent, int numColumns) {
345         group = super.createEncodingGroup(parent, numColumns);
346         String JavaDoc byteOrderLabel = IDEEncoding
347                 .getByteOrderMarkLabel(getContentDescription());
348         if (byteOrderLabel != null) {
349             Label label = new Label(group, SWT.NONE);
350             label
351                     .setText(NLS
352                             .bind(
353                                     IDEWorkbenchMessages.WorkbenchPreference_encoding_encodingMessage,
354                                     byteOrderLabel));
355             GridData layoutData = new GridData();
356             layoutData.horizontalSpan = numColumns + 1;
357             label.setLayoutData(layoutData);
358
359         }
360         return group;
361     }
362
363     /**
364      * Returns the content description of the resource if it is a file and it
365      * has a content description.
366      *
367      * @return the content description or <code>null</code> if resource is not
368      * an <code>IFile</code> or it does not have a description
369      */

370     private IContentDescription getContentDescription() {
371         try {
372             if (resource instanceof IFile) {
373                 return (((IFile) resource).getContentDescription());
374             }
375         } catch (CoreException exception) {
376             // If we cannot find it return null
377
}
378         return null;
379     }
380
381     /* (non-Javadoc)
382      * @see org.eclipse.jface.preference.FieldEditor#setEnabled(boolean, org.eclipse.swt.widgets.Composite)
383      */

384     public void setEnabled(boolean enabled, Composite parent) {
385         super.setEnabled(enabled, parent);
386         group.setEnabled(enabled);
387         Control[] children = group.getChildren();
388         for (int i = 0; i < children.length; i++) {
389             children[i].setEnabled(enabled);
390
391         }
392     }
393
394 }
395
Popular Tags