### Text file encoding

If your plug-in reads text files, it should honor the text file encoding preference in the workbench.

Text files are encoded differently depending on the platform and the locale. Most of the time, using the default text file encoding for the locale of the host operating system is good enough. However, a user may want to work with text files that originate from another source. Given the ability to use the platform in a networked team environment, it's certainly possible that users will want to work with text files that use a different encoding scheme than their native encoding scheme so that they can easily interchange files with another team.

For this reason, the workbench defines its own encoding profile that is specified by the user in the Preferences dialog. Users may choose from the available encoding choices in the General > Workspace preference page or type in their own encoding. Plug-ins that interpret text files, such as editors and builders, should consult the workbench encoding preference rather than assume that the installed operating system encoding is in use.

You can obtain the encoding preference using ResourcesPlugin.getEncoding(). This encoding should be passed to java.io readers instead of using the default system encoding. If you need to track changes to this preference, you can hook a listener on the ResourcesPlugin preferences and react to changes in ResourcesPlugin.PREF_ENCODING. The following example comes from the default text editor:

    public void initialize(StatusTextEditor textEditor) {

    	fTextEditor= textEditor;

    	fPropertyChangeListener= new Preferences.IPropertyChangeListener() {
    		public void propertyChange(Preferences.PropertyChangeEvent e) {
    			if (ResourcesPlugin.PREF_ENCODING.equals(e.getProperty()))
    				setEncoding(null, false);
    		}
    	};

    	Preferences p= ResourcesPlugin.getPlugin().getPluginPreferences();
    	p.addPropertyChangeListener(fPropertyChangeListener);

    	fEncodingActionGroup= new EncodingActionGroup(fTextEditor);
    	fEncodingActionGroup.update();
    }

Users may also change the encoding for a particular file in the Edit > Encoding menu of an editor. If you are manipulating text inside an open editor, you should use IEncodingSupport.getEncoding() instead in order to get the encoding for the particular editor. The following example shows how to obtain this information from an editor:

    IEncodingSupport encodingSupport = (IEncodingSupport) editor.getAdapter(IEncodingSupport.class);
    String encoding = encodingSupport.getEncoding();