preferencepage.java

来自「plugin for eclipse」· Java 代码 · 共 214 行

JAVA
214
字号
package isis.tinydt.preferences;

import isis.anp.config.PlatformProfile;
import isis.tinydt.TinydtPlugin;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.TreeMap;

import org.eclipse.core.runtime.IPath;
import org.eclipse.jface.preference.FieldEditorPreferencePage;
import org.eclipse.jface.preference.FileFieldEditor;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;


public class PreferencePage extends FieldEditorPreferencePage implements IWorkbenchPreferencePage
{
    Map controls = new TreeMap();
    
    public PreferencePage()
    {
        super(GRID);
        setPreferenceStore(TinydtPlugin.getDefault().getPreferenceStore());
        setDescription("Shell and platform specific C preprocessors executables:");
        initializeDefaults();
    }
        
    public boolean performOk()
    {
        IPreferenceStore store = getPreferenceStore();        
        IPath base_path = TinydtPlugin.getDefault().getStateLocation();        
        try
        {
            // for each platform
            Iterator it = PlatformProfile.getPlatforms().iterator();
            while( it.hasNext() )
            {
                String platform = (String)it.next();
                String file_name = base_path.append(platform + ".properties").toString();
                try
                {
                    PlatformProfile profile = PlatformProfile.load(platform);
                    
                    FileFieldEditor f = (FileFieldEditor)controls.get(platform);
                    
                    String s = f.getStringValue();
                    System.out.println(s);
            		s=s.replaceAll("\\\\","/");
            		System.out.println(s);
                    store.setValue(platform,s);                   
                    profile.setCppExecutable(s);
                    profile.save(new FileOutputStream( file_name ));
                }
                catch( IOException e )
                {
                    e.printStackTrace();                           
                }
            }  

            
            try
            {
            // store path to bash
            FileFieldEditor f = (FileFieldEditor)controls.get("bash");
            
            String s = f.getStringValue();
            System.out.println(s);
    		s=s.replaceAll("\\\\","/");
    		System.out.println(s);
            store.setValue("bash",s);
            
            
            Properties p = new Properties();

            p.put("bashExecutable",s);
            String file_name = base_path.append("bash.properties").toString();
            p.store(new FileOutputStream( file_name ), null );
            }
            catch( IOException e )
            {
            	e.printStackTrace();          
            }
            
        }
        catch( Exception e )
        {            
            e.printStackTrace();            
        }
        return true;        
    }
    
    public String getBashPath()
    {
        IPath base_path = TinydtPlugin.getDefault().getStateLocation();
        String file_name = base_path.append("bash.properties").toString();
        try {
        	InputStream is = new FileInputStream( file_name );
        	Properties p = new Properties();
        	p.load(is);
        	if(p.getProperty("bashExecutable")!=null)
        		return p.getProperty("bashExecutable").trim();
        } catch (Exception e) {
        	// sorry - bash.properties does not exist - we return a default value        	
        }
        
        return "c:/tinyos/cygwin/bin/bash";        
    }    
    
    private void initializeDefaults()
    {
        IPreferenceStore store = getPreferenceStore();        
//        IPath base_path = TinydtPlugin.getDefault().getStateLocation();
        
        try
        {
            // for each platform
            Iterator it = PlatformProfile.getPlatforms().iterator();
            while( it.hasNext() )
            {
                try
                {                
                    String platform = (String)it.next();
                    PlatformProfile profile = PlatformProfile.load(platform);               
                    store.setDefault(platform, profile.getCppExecutable());
                }
                catch( Exception e )
                {
                    e.printStackTrace();
                }
            }        

            // set default for bash path
            store.setDefault("bash", getBashPath());
            
        }
        catch( Exception e )
        {            
            e.printStackTrace();            
        }
    }
    
    public void createFieldEditors()
    {
        try
        {
            Iterator it = PlatformProfile.getPlatforms().iterator();
            while( it.hasNext() )
            {
                String platform = (String)it.next();
                FileFieldEditor f = new FileFieldEditor(platform, "C preprocessor for "+platform+":\n", getFieldEditorParent());
                controls.put(platform,f);
                addField(f);
            }
            
            // add field for bash path
            FileFieldEditor f = new FileFieldEditor("bash", "Bash executable:\n", getFieldEditorParent());
            controls.put("bash",f);
            addField(f);
            
        }
        catch( Exception e )
        {
            e.printStackTrace();
        }        
    }

    public void init(IWorkbench workbench)
    {
        IPreferenceStore store = getPreferenceStore();       
        IPath base_path = TinydtPlugin.getDefault().getStateLocation();            
        try
        {
            // for each platform
            Iterator it = PlatformProfile.getPlatforms().iterator();
            while( it.hasNext() )
            {
                String platform = (String)it.next();
                String file_name = base_path.append(platform + ".properties").toString();
                PlatformProfile profile = null;                
                try
                {
                    InputStream is = new FileInputStream( file_name );
                    profile = PlatformProfile.readObject(is);
                }
                catch( IOException e )
                {
                    try
                    {
                        profile = PlatformProfile.load(platform);
                    }
                    catch( IOException e2 )
                    {
                        e2.printStackTrace();                
                    }            
                }                
                store.setValue(platform, profile.getCppExecutable());
            }
            
            // init bash field
            store.setValue("bash", getBashPath());
        }
        catch( Exception e )
        {            
            e.printStackTrace();            
        }
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?