001: /*******************************************************************************
002: * Copyright (c) 2004, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.ide.dialogs;
011:
012: import org.eclipse.core.resources.IContainer;
013: import org.eclipse.core.resources.IFile;
014: import org.eclipse.core.resources.IResource;
015: import org.eclipse.core.resources.IWorkspaceRoot;
016: import org.eclipse.core.runtime.Assert;
017: import org.eclipse.core.runtime.CoreException;
018: import org.eclipse.core.runtime.IProgressMonitor;
019: import org.eclipse.core.runtime.IStatus;
020: import org.eclipse.core.runtime.Status;
021: import org.eclipse.core.runtime.content.IContentDescription;
022: import org.eclipse.core.runtime.jobs.Job;
023: import org.eclipse.jface.dialogs.DialogPage;
024: import org.eclipse.jface.dialogs.IDialogConstants;
025: import org.eclipse.jface.dialogs.MessageDialog;
026: import org.eclipse.osgi.util.NLS;
027: import org.eclipse.swt.SWT;
028: import org.eclipse.swt.layout.GridData;
029: import org.eclipse.swt.widgets.Composite;
030: import org.eclipse.swt.widgets.Control;
031: import org.eclipse.swt.widgets.Group;
032: import org.eclipse.swt.widgets.Label;
033: import org.eclipse.swt.widgets.Shell;
034: import org.eclipse.ui.WorkbenchEncoding;
035: import org.eclipse.ui.ide.IDEEncoding;
036: import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
037: import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
038:
039: /**
040: * The ResourceEncodingFieldEditor is a field editor for editing the encoding of
041: * a resource and does not use a preference store.
042: * <p>
043: * This class may be instantiated; it is not intended to be subclassed.
044: * </p>
045: *
046: * @since 3.1
047: */
048: public final class ResourceEncodingFieldEditor extends
049: AbstractEncodingFieldEditor {
050:
051: /**
052: * The resource being edited.
053: */
054: private IResource resource;
055:
056: private Composite group;
057:
058: /**
059: * Creates a new encoding field editor for setting the encoding on the given
060: * resource.
061: *
062: * @param labelText
063: * the label text of the field editor
064: * @param parent
065: * the parent of the field editor's control
066: * @param charsetResource
067: * must be an <code>IContainer</code> or an <code>IFile</code>.
068: *
069: * @see org.eclipse.core.resources.IContainer#getDefaultCharset()
070: * @see org.eclipse.core.resources.IFile#getCharset()
071: */
072: public ResourceEncodingFieldEditor(String labelText,
073: Composite parent, IResource charsetResource) {
074: super ();
075: setLabelAndResource(labelText, charsetResource);
076: createControl(parent);
077: }
078:
079: /**
080: * Creates a new encoding field editor for setting the encoding on the given
081: * resource.
082: *
083: * @param labelText
084: * the label text of the field editor
085: * @param parent
086: * the parent of the field editor's control
087: * @param charsetResource
088: * must be an <code>IContainer</code> or an <code>IFile</code>.
089: * @param groupTitle
090: * the title for the field editor's control. If groupTitle is
091: * <code>null</code> the control will be unlabelled
092: * (by default a {@link Composite} instead of a {@link Group}.
093: *
094: * @see org.eclipse.core.resources.IContainer#getDefaultCharset()
095: * @see org.eclipse.core.resources.IFile#getCharset()
096: * @see AbstractEncodingFieldEditor#setGroupTitle(String)
097: * @since 3.3
098: */
099: public ResourceEncodingFieldEditor(String labelText,
100: Composite parent, IResource charsetResource,
101: String 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 labelText,
115: IResource charsetResource) {
116: Assert.isTrue(charsetResource instanceof IContainer
117: || charsetResource instanceof IFile);
118: setLabelText(labelText);
119: this .resource = charsetResource;
120: }
121:
122: /*
123: * (non-Javadoc)
124: *
125: * @see org.eclipse.ui.internal.ide.dialogs.AbstractEncodingFieldEditor#getStoredValue()
126: */
127: protected String getStoredValue() {
128: try {
129: if (resource instanceof IContainer) {
130: return ((IContainer) resource).getDefaultCharset(false);
131: }
132: return ((IFile) resource).getCharset(false);
133:
134: } catch (CoreException e) {// If there is an error return the default
135: IDEWorkbenchPlugin
136: .log(
137: IDEWorkbenchMessages.ResourceEncodingFieldEditor_ErrorLoadingMessage,
138: e.getStatus());
139: return WorkbenchEncoding.getWorkbenchDefaultEncoding();
140: }
141:
142: }
143:
144: /*
145: * (non-Javadoc)
146: *
147: * @see org.eclipse.jface.preference.FieldEditor#doStore()
148: */
149: protected void doStore() {
150:
151: String encoding = getSelectedEncoding();
152:
153: // Clear the value if nothing is selected
154: if (isDefaultSelected()) {
155: encoding = null;
156: }
157: // Don't update if the same thing is selected
158: if (hasSameEncoding(encoding)) {
159: return;
160: }
161:
162: String descriptionCharset = getCharsetFromDescription();
163: if (descriptionCharset != null
164: && !(descriptionCharset.equals(encoding))
165: && encoding != null) {
166: Shell shell = null;
167: DialogPage page = getPage();
168: if (page != null) {
169: shell = page.getShell();
170: }
171:
172: MessageDialog dialog = new MessageDialog(
173: shell,
174: IDEWorkbenchMessages.ResourceEncodingFieldEditor_EncodingConflictTitle,
175: null,
176: NLS
177: .bind(
178: IDEWorkbenchMessages.ResourceEncodingFieldEditor_EncodingConflictMessage,
179: encoding, descriptionCharset),
180: MessageDialog.WARNING, new String[] {
181: IDialogConstants.YES_LABEL,
182: IDialogConstants.NO_LABEL }, 0); // yes is the
183: // default
184: if (dialog.open() > 0) {
185: return;
186: }
187: }
188:
189: IDEEncoding.addIDEEncoding(encoding);
190:
191: final String finalEncoding = encoding;
192:
193: Job charsetJob = new Job(
194: IDEWorkbenchMessages.IDEEncoding_EncodingJob) {
195: /*
196: * (non-Javadoc)
197: *
198: * @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
199: */
200: protected IStatus run(IProgressMonitor monitor) {
201: try {
202: if (resource instanceof IContainer) {
203: ((IContainer) resource).setDefaultCharset(
204: finalEncoding, monitor);
205: } else {
206: ((IFile) resource).setCharset(finalEncoding,
207: monitor);
208: }
209: return Status.OK_STATUS;
210: } catch (CoreException e) {// If there is an error return the
211: // default
212: IDEWorkbenchPlugin
213: .log(
214: IDEWorkbenchMessages.ResourceEncodingFieldEditor_ErrorStoringMessage,
215: e.getStatus());
216: return e.getStatus();
217: }
218: }
219: };
220:
221: charsetJob.schedule();
222:
223: }
224:
225: /*
226: * (non-Javadoc)
227: *
228: * @see org.eclipse.jface.preference.FieldEditor#store()
229: */
230: public void store() {// Override the store method as we are not using a
231: // preference store
232: doStore();
233: }
234:
235: /*
236: * (non-Javadoc)
237: *
238: * @see org.eclipse.jface.preference.FieldEditor#load()
239: */
240: public void load() {// Override the load method as we are not using a
241: // preference store
242: setPresentsDefaultValue(false);
243: doLoad();
244: }
245:
246: /*
247: * (non-Javadoc)
248: *
249: * @see org.eclipse.jface.preference.FieldEditor#loadDefault()
250: */
251: public void loadDefault() {
252: setPresentsDefaultValue(true);
253: doLoadDefault();
254: refreshValidState();
255: }
256:
257: /*
258: * (non-Javadoc)
259: *
260: * @see org.eclipse.ui.ide.dialogs.AbstractEncodingFieldEditor#findDefaultEncoding()
261: */
262: protected String findDefaultEncoding() {
263:
264: if (resource instanceof IWorkspaceRoot) {
265: return super .findDefaultEncoding();
266: }
267:
268: String defaultCharset = getCharsetFromDescription();
269: defaultCharset = getCharsetFromDescription();
270:
271: if (defaultCharset != null && defaultCharset.length() > 0) {
272: return defaultCharset;
273: }
274: try {
275: // Query up the whole hierarchy
276: defaultCharset = resource.getParent().getDefaultCharset(
277: true);
278: } catch (CoreException exception) {
279: // If there is an exception try again
280: }
281:
282: if (defaultCharset != null && defaultCharset.length() > 0) {
283: return defaultCharset;
284: }
285:
286: return super .findDefaultEncoding();
287: }
288:
289: /**
290: * Returns the charset from the content description if there is one.
291: *
292: * @return the charset from the content description, or <code>null</code>
293: */
294: private String getCharsetFromDescription() {
295: IContentDescription description = getContentDescription();
296: if (description != null) {
297: return description.getCharset();
298: }
299: return null;
300: }
301:
302: /*
303: * (non-Javadoc)
304: *
305: * @see org.eclipse.ui.ide.dialogs.AbstractEncodingFieldEditor#defaultButtonText()
306: */
307: protected String defaultButtonText() {
308:
309: if (resource instanceof IWorkspaceRoot) {
310: return super .defaultButtonText();
311: }
312:
313: if (resource instanceof IFile) {
314: try {
315: IContentDescription description = ((IFile) resource)
316: .getContentDescription();
317: // If we can find a charset from the description then derive
318: // from that
319: if (description == null
320: || description.getCharset() == null) {
321: return NLS
322: .bind(
323: IDEWorkbenchMessages.ResourceInfo_fileContainerEncodingFormat,
324: getDefaultEnc());
325: }
326:
327: return NLS
328: .bind(
329: IDEWorkbenchMessages.ResourceInfo_fileContentEncodingFormat,
330: getDefaultEnc());
331:
332: } catch (CoreException exception) {
333: // Do nothing here as we will just try to derive from the
334: // container
335: }
336: }
337:
338: return NLS
339: .bind(
340: IDEWorkbenchMessages.ResourceInfo_containerEncodingFormat,
341: getDefaultEnc());
342:
343: }
344:
345: /*
346: * (non-Javadoc)
347: *
348: * @see org.eclipse.ui.ide.dialogs.AbstractEncodingFieldEditor#createEncodingGroup(org.eclipse.swt.widgets.Composite,
349: * int)
350: */
351: protected Composite createEncodingGroup(Composite parent,
352: int numColumns) {
353: group = super .createEncodingGroup(parent, numColumns);
354: String byteOrderLabel = IDEEncoding
355: .getByteOrderMarkLabel(getContentDescription());
356: if (byteOrderLabel != null) {
357: Label label = new Label(group, SWT.NONE);
358: label
359: .setText(NLS
360: .bind(
361: IDEWorkbenchMessages.WorkbenchPreference_encoding_encodingMessage,
362: byteOrderLabel));
363: GridData layoutData = new GridData();
364: layoutData.horizontalSpan = numColumns + 1;
365: label.setLayoutData(layoutData);
366:
367: }
368: return group;
369: }
370:
371: /**
372: * Returns the content description of the resource if it is a file and it
373: * has a content description.
374: *
375: * @return the content description or <code>null</code> if resource is not
376: * an <code>IFile</code> or it does not have a description
377: */
378: private IContentDescription getContentDescription() {
379: try {
380: if (resource instanceof IFile) {
381: return (((IFile) resource).getContentDescription());
382: }
383: } catch (CoreException exception) {
384: // If we cannot find it return null
385: }
386: return null;
387: }
388:
389: /* (non-Javadoc)
390: * @see org.eclipse.jface.preference.FieldEditor#setEnabled(boolean, org.eclipse.swt.widgets.Composite)
391: */
392: public void setEnabled(boolean enabled, Composite parent) {
393: super .setEnabled(enabled, parent);
394: group.setEnabled(enabled);
395: Control[] children = group.getChildren();
396: for (int i = 0; i < children.length; i++) {
397: children[i].setEnabled(enabled);
398:
399: }
400: }
401:
402: }
|