KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.CharConversionException JavaDoc;
14 import java.io.UnsupportedEncodingException JavaDoc;
15
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.custom.BusyIndicator;
18 import org.eclipse.swt.events.SelectionAdapter;
19 import org.eclipse.swt.events.SelectionEvent;
20 import org.eclipse.swt.graphics.Color;
21 import org.eclipse.swt.layout.GridData;
22 import org.eclipse.swt.widgets.Button;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.swt.widgets.Display;
25 import org.eclipse.swt.widgets.Label;
26 import org.eclipse.swt.widgets.Shell;
27
28 import org.eclipse.core.runtime.IStatus;
29 import org.eclipse.core.runtime.Preferences;
30
31 import org.eclipse.core.resources.ResourcesPlugin;
32
33 import org.eclipse.jface.action.IAction;
34
35 import org.eclipse.ui.IEditorInput;
36 import org.eclipse.ui.IWorkbenchWindow;
37 import org.eclipse.ui.PlatformUI;
38 import org.eclipse.ui.internal.editors.text.NLSUtility;
39 import org.eclipse.ui.texteditor.IDocumentProvider;
40 import org.eclipse.ui.texteditor.ITextEditorActionConstants;
41 import org.eclipse.ui.texteditor.StatusTextEditor;
42 import org.eclipse.ui.texteditor.TextEditorAction;
43
44
45 /**
46  * The standard implementation of <code>IEncodingSupport</code>.
47  * @since 2.0
48  */

49 public class DefaultEncodingSupport implements IEncodingSupport {
50
51     /** Internal property change listener. */
52     private Preferences.IPropertyChangeListener fPropertyChangeListener;
53     /** The editor this support is associated with. */
54     private StatusTextEditor fTextEditor;
55
56     /**
57      * Creates a new encoding support.
58      */

59     public DefaultEncodingSupport() {
60         super();
61     }
62
63     /**
64      * Associates this encoding support to the given text editor and initializes this encoding.
65      *
66      * @param textEditor the editor
67      */

68     public void initialize(StatusTextEditor textEditor) {
69
70         fTextEditor= textEditor;
71
72         fPropertyChangeListener= new Preferences.IPropertyChangeListener() {
73             public void propertyChange(Preferences.PropertyChangeEvent e) {
74                 if (ResourcesPlugin.PREF_ENCODING.equals(e.getProperty())) {
75                     Runnable JavaDoc runnable= new Runnable JavaDoc() {
76                         public void run() {
77                             setEncoding(null, false); // null means: use default
78
}
79                     };
80                     if (Display.getCurrent() != null)
81                         runnable.run();
82                     else {
83                         // Post runnable into UI thread
84
Shell shell;
85                         if (fTextEditor != null)
86                             shell= fTextEditor.getSite().getShell();
87                         else
88                             shell= getActiveWorkbenchShell();
89                         Display display;
90                         if (shell != null)
91                             display= shell.getDisplay();
92                         else
93                             display= Display.getDefault();
94                         display.asyncExec(runnable);
95                     }
96                 }
97             }
98         };
99
100         Preferences p= ResourcesPlugin.getPlugin().getPluginPreferences();
101         p.addPropertyChangeListener(fPropertyChangeListener);
102     }
103
104     /**
105      * Disposes this encoding support.
106      */

107     public void dispose() {
108         Preferences p= ResourcesPlugin.getPlugin().getPluginPreferences();
109         p.removePropertyChangeListener(fPropertyChangeListener);
110
111         fTextEditor= null;
112     }
113
114     /**
115      * Resets this encoding support. Should be called if, e.g., the input element of the
116      * associated editor changed.
117      */

118     public void reset() {
119     }
120
121     /**
122      * Sets the encoding of the editor's input to the given value. If <code>overwrite</code> is
123      * <code>true</code> the value is set even if the encoding is already set.
124      *
125      * @param encoding the new encoding
126      * @param overwrite <code>true</code> if current encoding should be overwritten
127      */

128     protected void setEncoding(String JavaDoc encoding, boolean overwrite) {
129         IDocumentProvider p= fTextEditor.getDocumentProvider();
130         if (p instanceof IStorageDocumentProvider) {
131             final IEditorInput input= fTextEditor.getEditorInput();
132             IStorageDocumentProvider provider= (IStorageDocumentProvider)p;
133             String JavaDoc current= provider.getEncoding(input);
134             if (!fTextEditor.isDirty()) {
135                 String JavaDoc internal= encoding == null ? "" : encoding; //$NON-NLS-1$
136
boolean apply= (overwrite || current == null) && !internal.equals(current);
137                 if (apply) {
138                     provider.setEncoding(input, encoding);
139                     Runnable JavaDoc encodingSetter=
140                         new Runnable JavaDoc() {
141                                public void run() {
142                                    fTextEditor.doRevertToSaved();
143                                }
144                         };
145                     Display display= fTextEditor.getSite().getShell().getDisplay();
146                     if (display != null && !display.isDisposed())
147                         BusyIndicator.showWhile(display, encodingSetter);
148                     else
149                         encodingSetter.run();
150                 }
151             }
152         }
153     }
154
155     /*
156      * @see IEncodingSupport#setEncoding(String)
157      */

158     public void setEncoding(String JavaDoc encoding) {
159         setEncoding(encoding, true);
160     }
161
162     /*
163      * @see IEncodingSupport#getEncoding()
164      */

165     public String JavaDoc getEncoding() {
166         IDocumentProvider p= fTextEditor.getDocumentProvider();
167         if (p instanceof IStorageDocumentProvider) {
168             IStorageDocumentProvider provider= (IStorageDocumentProvider) p;
169             return provider.getEncoding(fTextEditor.getEditorInput());
170         }
171         return null;
172     }
173
174     /*
175      * @see IEncodingSupport#getDefaultEncoding()
176      */

177     public String JavaDoc getDefaultEncoding() {
178         IDocumentProvider p= fTextEditor.getDocumentProvider();
179         if (p instanceof IStorageDocumentProvider) {
180             IStorageDocumentProvider provider= (IStorageDocumentProvider) p;
181             return provider.getDefaultEncoding();
182         }
183         return null;
184     }
185
186     /**
187      * Returns a status header for the given status.
188      *
189      * @param status the status
190      * @return a status header for the given status.
191      */

192     public String JavaDoc getStatusHeader(IStatus status) {
193         Throwable JavaDoc t= status.getException();
194
195         if (t instanceof CharConversionException JavaDoc)
196             return TextEditorMessages.Editor_error_unreadable_encoding_header;
197
198         if (t instanceof UnsupportedEncodingException JavaDoc)
199             return TextEditorMessages.Editor_error_unsupported_encoding_header;
200
201         return null;
202     }
203
204     /**
205      * Returns a banner for the given status.
206      *
207      * @param status the status
208      * @return a banner for the given status.
209      */

210     public String JavaDoc getStatusBanner(IStatus status) {
211         Throwable JavaDoc t= status.getException();
212
213         if (t instanceof CharConversionException JavaDoc)
214             return TextEditorMessages.Editor_error_unreadable_encoding_banner;
215
216         if (t instanceof UnsupportedEncodingException JavaDoc)
217             return TextEditorMessages.Editor_error_unsupported_encoding_banner;
218
219         return null;
220
221     }
222
223     /**
224      * Returns a status message if any.
225      *
226      * @param status the status
227      * @return a status message indicating encoding problems or <code>null</code> otherwise
228      */

229     public String JavaDoc getStatusMessage(IStatus status) {
230         Throwable JavaDoc t= status.getException();
231         if (t instanceof CharConversionException JavaDoc || t instanceof UnsupportedEncodingException JavaDoc) {
232
233             String JavaDoc encoding= getEncoding();
234             if (encoding == null)
235                 encoding= getDefaultEncoding();
236
237             if (t instanceof CharConversionException JavaDoc) {
238                 if (encoding != null)
239                     return NLSUtility.format(TextEditorMessages.Editor_error_unreadable_encoding_message_arg, encoding);
240                 return TextEditorMessages.Editor_error_unreadable_encoding_message;
241             }
242
243             if (t instanceof UnsupportedEncodingException JavaDoc) {
244                 if (encoding != null)
245                     return NLSUtility.format(TextEditorMessages.Editor_error_unsupported_encoding_message_arg, encoding);
246                 return TextEditorMessages.Editor_error_unsupported_encoding_message;
247             }
248         }
249
250         return null;
251     }
252
253     /**
254      * Returns <code>true</code> if the given status is an
255      * encoding error.
256      *
257      * @param status the status to check
258      * @return <code>true</code> if the given status is an encoding error
259      * @since 3.1
260      */

261     public boolean isEncodingError(IStatus status) {
262         if (status == null || status.getSeverity() != IStatus.ERROR)
263             return false;
264
265         Throwable JavaDoc t= status.getException();
266         return t instanceof CharConversionException JavaDoc || t instanceof UnsupportedEncodingException JavaDoc;
267     }
268
269     /**
270      * Creates the control which allows to change the encoding.
271      * In case of encoding errors this control will be placed below
272      * the status of the status editor.
273      *
274      * @param parent the parent control
275      * @param status the status
276      * @since 3.1
277      */

278     public void createStatusEncodingChangeControl(Composite parent, final IStatus status) {
279         final IAction action= fTextEditor.getAction(ITextEditorActionConstants.CHANGE_ENCODING);
280         if (action instanceof TextEditorAction)
281             ((TextEditorAction)action).update();
282
283         if (action == null || !action.isEnabled())
284             return;
285
286         Shell shell= parent.getShell();
287         Display display= shell.getDisplay();
288         Color bgColor= display.getSystemColor(SWT.COLOR_LIST_BACKGROUND);
289
290         Button button= new Button(parent, SWT.PUSH | SWT.FLAT);
291         button.setText(action.getText());
292         button.addSelectionListener(new SelectionAdapter() {
293             /*
294              * @see org.eclipse.swt.events.SelectionAdapter#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
295              */

296             public void widgetSelected(SelectionEvent e) {
297                 action.run();
298             }
299         });
300
301         Label filler= new Label(parent, SWT.NONE);
302         filler.setLayoutData(new GridData(GridData.FILL_BOTH));
303         filler.setBackground(bgColor);
304     }
305
306     /**
307      * Returns the shell of the active workbench window.
308      *
309      * @return the shell of the active workbench window or <code>null</code> if none
310      * @since 3.2
311      */

312     private static Shell getActiveWorkbenchShell() {
313          IWorkbenchWindow window= PlatformUI.getWorkbench().getActiveWorkbenchWindow();
314          if (window != null)
315             return window.getShell();
316          
317          return null;
318     }
319
320 }
321
Popular Tags