KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > editors > text > EncodingActionGroup


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.editors.text;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.ResourceBundle JavaDoc;
17
18 import org.eclipse.core.resources.IFile;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.content.IContentDescription;
21
22 import org.eclipse.jface.action.IAction;
23 import org.eclipse.jface.action.IMenuListener;
24 import org.eclipse.jface.action.IMenuManager;
25 import org.eclipse.jface.action.MenuManager;
26 import org.eclipse.jface.action.Separator;
27 import org.eclipse.jface.dialogs.IInputValidator;
28 import org.eclipse.jface.dialogs.InputDialog;
29 import org.eclipse.jface.window.Window;
30
31 import org.eclipse.ui.IActionBars;
32 import org.eclipse.ui.IEditorInput;
33 import org.eclipse.ui.IFileEditorInput;
34 import org.eclipse.ui.IWorkbenchActionConstants;
35 import org.eclipse.ui.actions.ActionGroup;
36 import org.eclipse.ui.internal.editors.text.NLSUtility;
37 import org.eclipse.ui.texteditor.ITextEditor;
38 import org.eclipse.ui.texteditor.IUpdate;
39 import org.eclipse.ui.texteditor.ResourceAction;
40 import org.eclipse.ui.texteditor.RetargetTextEditorAction;
41 import org.eclipse.ui.texteditor.TextEditorAction;
42
43
44 /**
45  * Action group for encoding actions.
46  * @since 2.0
47  * @deprecated As of 3.1, encoding needs to be changed via properties dialog
48  */

49 public class EncodingActionGroup extends ActionGroup {
50
51     private static final String JavaDoc FILE_CONTENT_ENCODING_FORMAT= TextEditorMessages.ResourceInfo_fileContentEncodingFormat;
52     private static final String JavaDoc FILE_CONTAINER_ENCODING_FORMAT= TextEditorMessages.ResourceInfo_fileContainerEncodingFormat;
53
54
55     /**
56      * Action for setting the encoding of the editor to the value this action has
57      * been initialized with.
58      */

59     static class PredefinedEncodingAction extends TextEditorAction {
60
61         /** The target encoding of this action. */
62         private String JavaDoc fEncoding;
63         /** The action label. */
64         private String JavaDoc fLabel;
65         /** Indicates whether the target encoding is the default encoding. */
66         private boolean fIsDefault;
67
68         /**
69          * Creates a new action for the given specification.
70          *
71          * @param bundle the resource bundle
72          * @param prefix the prefix for lookups from the resource bundle
73          * @param encoding the target encoding
74          * @param editor the target editor
75          */

76         public PredefinedEncodingAction(ResourceBundle JavaDoc bundle, String JavaDoc prefix, String JavaDoc encoding, ITextEditor editor) {
77             super(bundle, prefix, editor);
78             fEncoding= encoding;
79             if (prefix == null)
80                 setText(encoding);
81             fLabel= getText();
82         }
83
84         /**
85          * Creates a new action for the given specification.
86          *
87          * @param bundle the resource bundle
88          * @param encoding the target encoding
89          * @param editor the target editor
90          */

91         public PredefinedEncodingAction(ResourceBundle JavaDoc bundle, String JavaDoc encoding, ITextEditor editor) {
92             super(bundle, null, editor);
93             fEncoding= encoding;
94             setText(encoding);
95             fLabel= getText();
96         }
97
98         /**
99          * Returns the encoding support of the action's editor.
100          *
101          * @return the encoding support of the action's editor or <code>null</code> if none
102          */

103         private IEncodingSupport getEncodingSupport() {
104             ITextEditor editor= getTextEditor();
105             if (editor != null)
106                 return (IEncodingSupport) editor.getAdapter(IEncodingSupport.class);
107             return null;
108         }
109
110         /*
111          * @see IAction#run()
112          */

113         public void run() {
114             IEncodingSupport s= getEncodingSupport();
115             if (s != null)
116                 s.setEncoding(fIsDefault ? null : fEncoding);
117         }
118
119         /**
120          * Returns the encoding currently used in the given editor.
121          *
122          * @param editor the editor
123          * @return the encoding currently used in the given editor or <code>null</code> if no encoding support is installed
124          */

125         private String JavaDoc getEncoding(ITextEditor editor) {
126
127             IEditorInput input= (editor.getEditorInput());
128             if (input instanceof IFileEditorInput) {
129                 IFile file= ((IFileEditorInput)input).getFile();
130                 try {
131                     String JavaDoc explicitEncoding;
132                     explicitEncoding= file.getCharset(false);
133                     if (explicitEncoding == null)
134                         return null;
135                 } catch (CoreException e) {
136                     // continue - assume file is not using default encoding
137
}
138             }
139
140             IEncodingSupport s= getEncodingSupport();
141             if (s != null)
142                 return s.getEncoding();
143
144             return null;
145         }
146
147         /*
148          * @see IUpdate#update()
149          */

150         public void update() {
151
152             if (fEncoding == null) {
153                 setEnabled(false);
154                 return;
155             }
156
157             ITextEditor editor= getTextEditor();
158             if (editor == null) {
159                 setEnabled(false);
160                 return;
161             }
162
163             // update label
164
fIsDefault= IEncodingActionsConstants.DEFAULT.equals(fEncoding);
165             if (fIsDefault)
166                 setText(getDefaultEncodingText(editor, fLabel));
167             else
168                 setText(fLabel);
169
170             // update enable state
171
if (editor.isDirty())
172                 setEnabled(false);
173             else
174                 setEnabled(true);
175
176             // update checked state
177
String JavaDoc current= getEncoding(editor);
178             if (fIsDefault)
179                 setChecked(current == null);
180             else
181                 setChecked(fEncoding.equals(current));
182         }
183
184     }
185
186     /*
187      * @since 3.0
188      */

189     private static String JavaDoc getDefaultEncodingText(ITextEditor editor, String JavaDoc defaultText) {
190         IEditorInput input= (editor.getEditorInput());
191         if (!(input instanceof IFileEditorInput))
192             return defaultText;
193
194         IFile file= ((IFileEditorInput)input).getFile();
195
196         String JavaDoc format= FILE_CONTENT_ENCODING_FORMAT;
197         String JavaDoc encoding;
198         try {
199             encoding= getEncodingFromContent(file);
200             if (encoding == null) {
201                 format= FILE_CONTAINER_ENCODING_FORMAT;
202                 encoding= file.getParent().getDefaultCharset();
203             }
204         } catch (CoreException ex) {
205             return defaultText;
206         }
207
208         return NLSUtility.format(format, encoding);
209     }
210
211     /*
212      * @since 3.0
213      */

214     private static String JavaDoc getEncodingFromContent(IFile file) throws CoreException {
215         IContentDescription description = file.getContentDescription();
216         if (description != null) {
217             byte[] bom= (byte[])description.getProperty(IContentDescription.BYTE_ORDER_MARK);
218             if (bom == null)
219                 return (String JavaDoc)description.getProperty(IContentDescription.CHARSET);
220             if (bom == IContentDescription.BOM_UTF_8)
221                 return TextEditorMessages.WorkbenchPreference_encoding_BOM_UTF_8;
222             if (bom == IContentDescription.BOM_UTF_16BE)
223                 return TextEditorMessages.WorkbenchPreference_encoding_BOM_UTF_16BE;
224             if (bom == IContentDescription.BOM_UTF_16LE)
225                 return TextEditorMessages.WorkbenchPreference_encoding_BOM_UTF_16LE;
226         }
227
228         return null;
229     }
230
231     /**
232      * Sets the encoding of an editor to the value that has interactively been defined.
233      */

234     static class CustomEncodingAction extends TextEditorAction {
235
236
237         /*
238          * @see org.eclipse.ui.texteditor.TextEditorAction#TextEditorAction(ResourceBundle, String, ITextEditor)
239          */

240         protected CustomEncodingAction(ResourceBundle JavaDoc bundle, String JavaDoc prefix, ITextEditor editor) {
241             super(bundle, prefix, editor);
242         }
243
244         /*
245          * @see IUpdate#update()
246          */

247         public void update() {
248             ITextEditor editor= getTextEditor();
249             setEnabled(editor != null && !editor.isDirty());
250         }
251
252         /*
253          * @see IAction#run()
254          */

255         public void run() {
256
257             ITextEditor editor= getTextEditor();
258             if (editor == null)
259                 return;
260
261             IEncodingSupport encodingSupport= (IEncodingSupport) editor.getAdapter(IEncodingSupport.class);
262             if (encodingSupport == null)
263                 return;
264
265             String JavaDoc title= TextEditorMessages.Editor_ConvertEncoding_Custom_dialog_title;
266             String JavaDoc message= TextEditorMessages.Editor_ConvertEncoding_Custom_dialog_message;
267             IInputValidator inputValidator = new IInputValidator() {
268                 public String JavaDoc isValid(String JavaDoc newText) {
269                     return (newText == null || newText.length() == 0) ? " " : null; //$NON-NLS-1$
270
}
271             };
272
273             String JavaDoc initialValue= encodingSupport.getEncoding();
274             if (initialValue == null)
275                 initialValue= encodingSupport.getDefaultEncoding();
276             if (initialValue == null)
277                 initialValue= ""; //$NON-NLS-1$
278

279             InputDialog d= new InputDialog(editor.getSite().getShell(), title, message, initialValue, inputValidator);
280             if (d.open() == Window.OK)
281                 encodingSupport.setEncoding(d.getValue());
282         }
283     }
284
285
286     /** List of predefined encodings. */
287     private static final String JavaDoc[][] ENCODINGS;
288
289     /** The default encoding. */
290     private static final String JavaDoc SYSTEM_ENCODING;
291
292     /**
293      * Initializer: computes the set of predefined encoding actions.
294      */

295     static {
296
297         String JavaDoc[][] encodings= {
298             { IEncodingActionsConstants.DEFAULT, IEncodingActionsHelpContextIds.DEFAULT, IEncodingActionsDefinitionIds.DEFAULT },
299             { IEncodingActionsConstants.US_ASCII, IEncodingActionsHelpContextIds.US_ASCII, IEncodingActionsDefinitionIds.US_ASCII },
300             { IEncodingActionsConstants.ISO_8859_1, IEncodingActionsHelpContextIds.ISO_8859_1, IEncodingActionsDefinitionIds.ISO_8859_1 },
301             { IEncodingActionsConstants.UTF_8, IEncodingActionsHelpContextIds.UTF_8, IEncodingActionsDefinitionIds.UTF_8 },
302             { IEncodingActionsConstants.UTF_16BE, IEncodingActionsHelpContextIds.UTF_16BE, IEncodingActionsDefinitionIds.UTF_16BE },
303             { IEncodingActionsConstants.UTF_16LE, IEncodingActionsHelpContextIds.UTF_16LE, IEncodingActionsDefinitionIds.UTF_16LE },
304             { IEncodingActionsConstants.UTF_16, IEncodingActionsHelpContextIds.UTF_16, IEncodingActionsDefinitionIds.UTF_16 }
305         };
306
307         String JavaDoc system= System.getProperty("file.encoding"); //$NON-NLS-1$
308
if (system != null) {
309
310             int i;
311             for (i= 0; i < encodings.length; i++) {
312                 if (encodings[i][0].equals(system))
313                     break;
314             }
315
316             if (i != encodings.length) {
317                 // bring system encoding in second position - first is default encoding
318
String JavaDoc[] s= encodings[i];
319                 encodings[i]= encodings[1];
320                 encodings[1]= s;
321                 // forget system encoding as it's already in the list
322
system= null;
323             }
324         }
325
326         SYSTEM_ENCODING= system;
327         ENCODINGS= encodings;
328     }
329
330
331
332     /** List of encoding actions of this group. */
333     private List JavaDoc fRetargetActions= new ArrayList JavaDoc();
334
335     /**
336      * Creates a new encoding action group for an action bar contributor.
337      */

338     public EncodingActionGroup() {
339
340         fRetargetActions.add(new RetargetTextEditorAction(TextEditorMessages.getBundleForConstructedKeys(), "Editor.ConvertEncoding." + ENCODINGS[0][0] + ".", ENCODINGS[0][0], IAction.AS_RADIO_BUTTON)); //$NON-NLS-1$ //$NON-NLS-2$
341

342         if (SYSTEM_ENCODING != null)
343             fRetargetActions.add(new RetargetTextEditorAction(TextEditorMessages.getBundleForConstructedKeys(), "Editor.ConvertEncoding.System.", IEncodingActionsConstants.SYSTEM, IAction.AS_RADIO_BUTTON)); //$NON-NLS-1$
344

345         for (int i= 1; i < ENCODINGS.length; i++)
346             fRetargetActions.add(new RetargetTextEditorAction(TextEditorMessages.getBundleForConstructedKeys(), "Editor.ConvertEncoding." + ENCODINGS[i][0] + ".", ENCODINGS[i][0], IAction.AS_RADIO_BUTTON)); //$NON-NLS-1$ //$NON-NLS-2$
347

348         fRetargetActions.add(new RetargetTextEditorAction(TextEditorMessages.getBundleForConstructedKeys(), "Editor.ConvertEncoding.Custom.", IEncodingActionsConstants.CUSTOM, IAction.AS_PUSH_BUTTON)); //$NON-NLS-1$
349
}
350
351     /*
352      * @see ActionGroup#fillActionBars(org.eclipse.ui.IActionBars)
353      */

354     public void fillActionBars(IActionBars actionBars) {
355         IMenuManager menuManager= actionBars.getMenuManager();
356         IMenuManager editMenu= menuManager.findMenuUsingPath(IWorkbenchActionConstants.M_EDIT);
357         if (editMenu != null && fRetargetActions.size() > 0) {
358             MenuManager subMenu= new MenuManager(TextEditorMessages.Editor_ConvertEncoding_submenu_label);
359             subMenu.addMenuListener(new IMenuListener() {
360                 public void menuAboutToShow(IMenuManager manager) {
361                     update();
362                 }
363             });
364
365             Iterator JavaDoc e= fRetargetActions.iterator();
366             subMenu.add((IAction) e.next());
367             subMenu.add(new Separator());
368             while (e.hasNext())
369                 subMenu.add((IAction) e.next());
370
371             editMenu.add(subMenu);
372         }
373     }
374
375     /**
376      * Retargets this action group to the given editor.
377      *
378      * @param editor the text editor to which the group should be retargeted
379      */

380     public void retarget(ITextEditor editor) {
381         fTextEditor= editor;
382         Iterator JavaDoc e= fRetargetActions.iterator();
383         while (e.hasNext()) {
384             RetargetTextEditorAction a= (RetargetTextEditorAction) e.next();
385             a.setAction(editor == null ? null : editor.getAction(a.getId()));
386         }
387     }
388
389
390     //------------------------------------------------------------------------------------------
391

392
393     /** Text editor this group is associated with. */
394     private ITextEditor fTextEditor;
395
396     /**
397      * Creates a new encoding action group for the given editor.
398      *
399      * @param editor the text editor
400      */

401     public EncodingActionGroup(ITextEditor editor) {
402
403         fTextEditor= editor;
404
405         ResourceAction a;
406         if (SYSTEM_ENCODING != null) {
407             a= new PredefinedEncodingAction(TextEditorMessages.getBundleForConstructedKeys(), SYSTEM_ENCODING, editor);
408             a.setHelpContextId(IEncodingActionsHelpContextIds.SYSTEM);
409             a.setActionDefinitionId(IEncodingActionsDefinitionIds.SYSTEM);
410             editor.setAction(IEncodingActionsConstants.SYSTEM, a);
411         }
412
413         for (int i= 0; i < ENCODINGS.length; i++) {
414             a= new PredefinedEncodingAction(TextEditorMessages.getBundleForConstructedKeys(), "Editor.ConvertEncoding." + ENCODINGS[i][0] + ".", ENCODINGS[i][0], editor); //$NON-NLS-1$ //$NON-NLS-2$
415
a.setHelpContextId( ENCODINGS[i][1]);
416             a.setActionDefinitionId( ENCODINGS[i][2]);
417             editor.setAction(ENCODINGS[i][0], a);
418         }
419
420         a= new CustomEncodingAction(TextEditorMessages.getBundleForConstructedKeys(), "Editor.ConvertEncoding." + IEncodingActionsConstants.CUSTOM + ".", editor); //$NON-NLS-1$ //$NON-NLS-2$
421
a.setHelpContextId(IEncodingActionsHelpContextIds.CUSTOM);
422         a.setActionDefinitionId(IEncodingActionsDefinitionIds.CUSTOM);
423         editor.setAction(IEncodingActionsConstants.CUSTOM, a);
424     }
425
426     /**
427      * Updates all actions of this action group.
428      */

429     public void update() {
430         if (fTextEditor == null)
431             return;
432
433         IAction a= fTextEditor.getAction(IEncodingActionsConstants.SYSTEM);
434         if (a instanceof IUpdate)
435             ((IUpdate) a).update();
436
437         for (int i= 0; i < ENCODINGS.length; i++) {
438             a= fTextEditor.getAction(ENCODINGS[i][0]);
439             if (a instanceof IUpdate)
440                 ((IUpdate) a).update();
441         }
442
443         a= fTextEditor.getAction(IEncodingActionsConstants.CUSTOM);
444         if (a instanceof IUpdate)
445             ((IUpdate) a).update();
446     }
447
448     /*
449      * @see ActionGroup#dispose()
450      */

451     public void dispose() {
452         if (fTextEditor != null) {
453             fTextEditor.setAction(IEncodingActionsConstants.SYSTEM, null);
454             for (int i= 0; i < ENCODINGS.length; i++)
455                 fTextEditor.setAction(ENCODINGS[i][0], null);
456             fTextEditor.setAction(IEncodingActionsConstants.CUSTOM, null);
457
458             fTextEditor= null;
459         }
460     }
461 }
462
Popular Tags