001: /*******************************************************************************
002: * Copyright (c) 2000, 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.texteditor;
011:
012: import java.util.ResourceBundle;
013:
014: import org.eclipse.swt.SWT;
015: import org.eclipse.swt.layout.GridData;
016: import org.eclipse.swt.layout.GridLayout;
017: import org.eclipse.swt.widgets.Button;
018: import org.eclipse.swt.widgets.Composite;
019: import org.eclipse.swt.widgets.Control;
020: import org.eclipse.swt.widgets.Shell;
021:
022: import org.eclipse.core.resources.IResource;
023:
024: import org.eclipse.jface.dialogs.Dialog;
025: import org.eclipse.jface.dialogs.DialogPage;
026: import org.eclipse.jface.dialogs.IDialogConstants;
027: import org.eclipse.jface.dialogs.MessageDialog;
028: import org.eclipse.jface.preference.IPreferenceStore;
029: import org.eclipse.jface.preference.PreferenceStore;
030:
031: import org.eclipse.ui.editors.text.IEncodingSupport;
032:
033: import org.eclipse.ui.ide.dialogs.AbstractEncodingFieldEditor;
034: import org.eclipse.ui.ide.dialogs.EncodingFieldEditor;
035: import org.eclipse.ui.ide.dialogs.ResourceEncodingFieldEditor;
036:
037: /**
038: * Action for changing the encoding of the editor's
039: * input element.
040: * <p>
041: * The following keys, prepended by the given option prefix,
042: * are used for retrieving resources from the given bundle:
043: * <ul>
044: * <li><code>"dialog.title"</code> - the input dialog's title</li>
045: * </ul>
046: * This class may be instantiated but is not intended to be subclassed.
047: * </p>
048: *
049: * @since 3.1
050: */
051: public class ChangeEncodingAction extends TextEditorAction {
052:
053: private static final int APPLY_ID = IDialogConstants.OK_ID
054: + IDialogConstants.CANCEL_ID + 1;
055:
056: private String fDialogTitle;
057: private static final String ENCODING_PREF_KEY = "encoding"; //$NON-NLS-1$
058:
059: /**
060: * Creates a new action for the given text editor. The action configures its
061: * visual representation from the given resource bundle.
062: *
063: * @param bundle the resource bundle
064: * @param prefix a prefix to be prepended to the various resource keys
065: * (described in <code>ResourceAction</code> constructor), or
066: * <code>null</code> if none
067: * @param editor the text editor
068: * @see TextEditorAction#TextEditorAction(ResourceBundle, String, ITextEditor)
069: */
070: public ChangeEncodingAction(ResourceBundle bundle, String prefix,
071: ITextEditor editor) {
072: super (bundle, prefix, editor);
073:
074: String key = "dialog.title"; //$NON-NLS-1$;
075: if (prefix != null && prefix.length() > 0)
076: key = prefix + key;
077:
078: fDialogTitle = getString(bundle, key, null);
079: }
080:
081: /*
082: * @see org.eclipse.jface.action.Action#run()
083: */
084: public void run() {
085: final IResource resource = getResource();
086: final Shell parentShell = getTextEditor().getSite().getShell();
087: final IEncodingSupport encodingSupport = getEncodingSupport();
088: if (resource == null && encodingSupport == null) {
089: MessageDialog
090: .openInformation(
091: parentShell,
092: fDialogTitle,
093: TextEditorMessages.ChangeEncodingAction_message_noEncodingSupport);
094: return;
095: }
096:
097: Dialog dialog = new Dialog(parentShell) {
098: private AbstractEncodingFieldEditor fEncodingEditor;
099: private IPreferenceStore store = null;
100:
101: /*
102: * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
103: */
104: protected void configureShell(Shell newShell) {
105: super .configureShell(newShell);
106: newShell.setText(fDialogTitle);
107: }
108:
109: /*
110: * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
111: */
112: protected Control createDialogArea(Composite parent) {
113: Control composite = super .createDialogArea(parent);
114: if (!(composite instanceof Composite)) {
115: composite.dispose();
116: composite = new Composite(parent, SWT.NONE);
117: }
118:
119: GridLayout layout = new GridLayout();
120: layout.marginHeight = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
121: layout.marginWidth = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
122: layout.verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);
123: layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
124: parent.setLayout(layout);
125:
126: GridData data = new GridData(GridData.FILL_BOTH);
127: composite.setLayoutData(data);
128: composite.setFont(parent.getFont());
129:
130: DialogPage page = new MessageDialogPage(
131: (Composite) composite) {
132: public void setErrorMessage(String newMessage) {
133: super .setErrorMessage(newMessage);
134: setButtonEnabledState(IDialogConstants.OK_ID,
135: newMessage == null);
136: setButtonEnabledState(APPLY_ID,
137: newMessage == null);
138: }
139:
140: private void setButtonEnabledState(int id,
141: boolean state) {
142: Button button = getButton(id);
143: if (button != null)
144: button.setEnabled(state);
145: }
146: };
147:
148: if (resource != null) {
149: fEncodingEditor = new ResourceEncodingFieldEditor(
150: "", (Composite) composite, resource); //$NON-NLS-1$
151: fEncodingEditor.setPage(page);
152: fEncodingEditor.load();
153: } else {
154: fEncodingEditor = new EncodingFieldEditor(
155: ENCODING_PREF_KEY,
156: "", (Composite) composite); //$NON-NLS-1$
157: store = new PreferenceStore();
158: String defaultEncoding = encodingSupport
159: .getDefaultEncoding();
160: store
161: .setDefault(ENCODING_PREF_KEY,
162: defaultEncoding);
163: String encoding = encodingSupport.getEncoding();
164: if (encoding != null)
165: store.setValue(ENCODING_PREF_KEY, encoding);
166: fEncodingEditor.setPreferenceStore(store);
167:
168: fEncodingEditor.setPage(page);
169: fEncodingEditor.load();
170:
171: if (encoding == null
172: || encoding.equals(defaultEncoding)
173: || encoding.length() == 0)
174: fEncodingEditor.loadDefault();
175: }
176:
177: return composite;
178: }
179:
180: /*
181: * @see org.eclipse.jface.dialogs.Dialog#createButtonsForButtonBar(org.eclipse.swt.widgets.Composite)
182: */
183: protected void createButtonsForButtonBar(Composite parent) {
184: createButton(
185: parent,
186: APPLY_ID,
187: TextEditorMessages.ChangeEncodingAction_button_apply_label,
188: false);
189: super .createButtonsForButtonBar(parent);
190: }
191:
192: /*
193: * @see org.eclipse.jface.dialogs.Dialog#buttonPressed(int)
194: */
195: protected void buttonPressed(int buttonId) {
196: if (buttonId == APPLY_ID)
197: apply();
198: else
199: super .buttonPressed(buttonId);
200: }
201:
202: /*
203: * @see org.eclipse.jface.dialogs.Dialog#okPressed()
204: */
205: protected void okPressed() {
206: apply();
207: super .okPressed();
208: }
209:
210: private void apply() {
211: fEncodingEditor.store();
212:
213: if (resource == null) {
214: String encoding = fEncodingEditor
215: .getPreferenceStore()
216: .getString(
217: fEncodingEditor.getPreferenceName());
218: encodingSupport.setEncoding(encoding);
219: }
220: }
221: };
222: dialog.open();
223: }
224:
225: /*
226: * @see org.eclipse.ui.texteditor.IUpdate#update()
227: */
228: public void update() {
229: setEnabled((getResource() != null || getEncodingSupport() != null)
230: && !getTextEditor().isDirty());
231: }
232:
233: /**
234: * Gets the resource which is being edited in the editor.
235: *
236: * @return the resource being edited or <code>null</code>s
237: */
238: private IResource getResource() {
239: if (getTextEditor() != null
240: && getTextEditor().getEditorInput() != null)
241: return (IResource) getTextEditor().getEditorInput()
242: .getAdapter(IResource.class);
243:
244: return null;
245: }
246:
247: /**
248: * Gets the editor's encoding support.
249: *
250: * @return the resource being edited or <code>null</code>s
251: */
252: private IEncodingSupport getEncodingSupport() {
253: if (getTextEditor() != null)
254: return (IEncodingSupport) getTextEditor().getAdapter(
255: IEncodingSupport.class);
256:
257: return null;
258: }
259: }
|