KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > texteditor > ChangeEncodingAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 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
12 package org.eclipse.ui.texteditor;
13
14 import java.util.ResourceBundle JavaDoc;
15
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.layout.GridData;
18 import org.eclipse.swt.layout.GridLayout;
19 import org.eclipse.swt.widgets.Button;
20 import org.eclipse.swt.widgets.Composite;
21 import org.eclipse.swt.widgets.Control;
22 import org.eclipse.swt.widgets.Shell;
23
24 import org.eclipse.core.resources.IResource;
25
26 import org.eclipse.jface.dialogs.Dialog;
27 import org.eclipse.jface.dialogs.DialogPage;
28 import org.eclipse.jface.dialogs.IDialogConstants;
29 import org.eclipse.jface.dialogs.MessageDialog;
30 import org.eclipse.jface.preference.IPreferenceStore;
31 import org.eclipse.jface.preference.PreferenceStore;
32
33 import org.eclipse.ui.editors.text.IEncodingSupport;
34
35 import org.eclipse.ui.ide.dialogs.AbstractEncodingFieldEditor;
36 import org.eclipse.ui.ide.dialogs.EncodingFieldEditor;
37 import org.eclipse.ui.ide.dialogs.ResourceEncodingFieldEditor;
38
39 /**
40  * Action for changing the encoding of the editor's
41  * input element.
42  * <p>
43  * The following keys, prepended by the given option prefix,
44  * are used for retrieving resources from the given bundle:
45  * <ul>
46  * <li><code>"dialog.title"</code> - the input dialog's title</li>
47  * </ul>
48  * This class may be instantiated but is not intended to be subclassed.
49  * </p>
50  *
51  * @since 3.1
52  */

53 public class ChangeEncodingAction extends TextEditorAction {
54
55     private static final int APPLY_ID= IDialogConstants.OK_ID + IDialogConstants.CANCEL_ID + 1;
56
57     private String JavaDoc fDialogTitle;
58     private static final String JavaDoc ENCODING_PREF_KEY= "encoding"; //$NON-NLS-1$
59

60     /**
61      * Creates a new action for the given text editor. The action configures its
62      * visual representation from the given resource bundle.
63      *
64      * @param bundle the resource bundle
65      * @param prefix a prefix to be prepended to the various resource keys
66      * (described in <code>ResourceAction</code> constructor), or
67      * <code>null</code> if none
68      * @param editor the text editor
69      * @see TextEditorAction#TextEditorAction(ResourceBundle, String, ITextEditor)
70      */

71     public ChangeEncodingAction(ResourceBundle JavaDoc bundle, String JavaDoc prefix, ITextEditor editor) {
72         super(bundle, prefix, editor);
73
74         String JavaDoc key= "dialog.title"; //$NON-NLS-1$;
75
if (prefix != null && prefix.length() > 0)
76             key= prefix + key;
77
78         fDialogTitle= getString(bundle, key, null);
79     }
80
81     /*
82      * @see org.eclipse.jface.action.Action#run()
83      */

84     public void run() {
85         final IResource resource= getResource();
86         final Shell parentShell= getTextEditor().getSite().getShell();
87         final IEncodingSupport encodingSupport= getEncodingSupport();
88         if (resource == null && encodingSupport == null) {
89             MessageDialog.openInformation(parentShell, fDialogTitle, TextEditorMessages.ChangeEncodingAction_message_noEncodingSupport);
90             return;
91         }
92
93         Dialog dialog= new Dialog(parentShell) {
94             private AbstractEncodingFieldEditor fEncodingEditor;
95             private IPreferenceStore store= null;
96
97             /*
98              * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
99              */

100             protected void configureShell(Shell newShell) {
101                 super.configureShell(newShell);
102                 newShell.setText(fDialogTitle);
103             }
104
105             /*
106              * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
107              */

108             protected Control createDialogArea(Composite parent) {
109                 Control composite= super.createDialogArea(parent);
110                 if (!(composite instanceof Composite)) {
111                     composite.dispose();
112                     composite= new Composite(parent, SWT.NONE);
113                 }
114
115                 GridLayout layout= new GridLayout();
116                 layout.marginHeight= convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
117                 layout.marginWidth= convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
118                 layout.verticalSpacing= convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);
119                 layout.horizontalSpacing= convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
120                 parent.setLayout(layout);
121
122                 GridData data = new GridData(GridData.FILL_BOTH);
123                 composite.setLayoutData(data);
124                 composite.setFont(parent.getFont());
125
126                 DialogPage page= new MessageDialogPage((Composite)composite) {
127                     public void setErrorMessage(String JavaDoc newMessage) {
128                         super.setErrorMessage(newMessage);
129                         setButtonEnabledState(IDialogConstants.OK_ID, newMessage == null);
130                         setButtonEnabledState(APPLY_ID, newMessage == null);
131                     }
132
133                     private void setButtonEnabledState(int id, boolean state) {
134                         Button button= getButton(id);
135                         if (button != null)
136                             button.setEnabled(state);
137                     }
138                 };
139
140                 if (resource != null) {
141                     fEncodingEditor= new ResourceEncodingFieldEditor("", (Composite)composite, resource); //$NON-NLS-1$
142
fEncodingEditor.setPage(page);
143                     fEncodingEditor.load();
144                 } else {
145                     fEncodingEditor= new EncodingFieldEditor(ENCODING_PREF_KEY, "", (Composite)composite); //$NON-NLS-1$
146
store= new PreferenceStore();
147                     String JavaDoc defaultEncoding= encodingSupport.getDefaultEncoding();
148                     store.setDefault(ENCODING_PREF_KEY, defaultEncoding);
149                     String JavaDoc encoding= encodingSupport.getEncoding();
150                     if (encoding != null)
151                         store.setValue(ENCODING_PREF_KEY, encoding);
152                     fEncodingEditor.setPreferenceStore(store);
153
154                     fEncodingEditor.setPage(page);
155                     fEncodingEditor.load();
156
157                     if (encoding == null || encoding.equals(defaultEncoding) || encoding.length() == 0)
158                         fEncodingEditor.loadDefault();
159                 }
160
161                 return composite;
162             }
163
164             /*
165              * @see org.eclipse.jface.dialogs.Dialog#createButtonsForButtonBar(org.eclipse.swt.widgets.Composite)
166              */

167             protected void createButtonsForButtonBar(Composite parent) {
168                 createButton(parent, APPLY_ID, TextEditorMessages.ChangeEncodingAction_button_apply_label, false);
169                 super.createButtonsForButtonBar(parent);
170             }
171
172             /*
173              * @see org.eclipse.jface.dialogs.Dialog#buttonPressed(int)
174              */

175             protected void buttonPressed(int buttonId) {
176                 if (buttonId == APPLY_ID)
177                     apply();
178                 else
179                     super.buttonPressed(buttonId);
180             }
181
182             /*
183              * @see org.eclipse.jface.dialogs.Dialog#okPressed()
184              */

185             protected void okPressed() {
186                 apply();
187                 super.okPressed();
188             }
189
190             private void apply() {
191                 fEncodingEditor.store();
192
193                 if (resource == null) {
194                     String JavaDoc encoding= fEncodingEditor.getPreferenceStore().getString(fEncodingEditor.getPreferenceName());
195                     encodingSupport.setEncoding(encoding);
196                 }
197             }
198         };
199         dialog.open();
200     }
201
202     /*
203      * @see org.eclipse.ui.texteditor.IUpdate#update()
204      */

205     public void update() {
206         setEnabled((getResource() != null || getEncodingSupport() != null) && !getTextEditor().isDirty());
207     }
208
209     /**
210      * Gets the resource which is being edited in the editor.
211      *
212      * @return the resource being edited or <code>null</code>s
213      */

214     private IResource getResource() {
215         if (getTextEditor() != null && getTextEditor().getEditorInput() != null)
216             return (IResource)getTextEditor().getEditorInput().getAdapter(IResource.class);
217
218         return null;
219     }
220
221     /**
222      * Gets the editor's encoding support.
223      *
224      * @return the resource being edited or <code>null</code>s
225      */

226     private IEncodingSupport getEncodingSupport() {
227         if (getTextEditor() != null)
228             return (IEncodingSupport)getTextEditor().getAdapter(IEncodingSupport.class);
229
230         return null;
231     }
232 }
233
Popular Tags