Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

Eclipse Plug-in Developer Guide
Previous Page Home Next Page

Text file encoding

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

text file encoding

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 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();

 
 
  Published under the terms of the Eclipse Public License Version 1.0 ("EPL") Design by Interspire