Activator.java
Go to the documentation of this file.
00001 package jp.go.aist.hrp.joystick;
00002 
00003 import java.io.File;
00004 import java.io.FileOutputStream;
00005 import java.io.IOException;
00006 import java.io.InputStream;
00007 import java.io.OutputStream;
00008 import java.net.URL;
00009 
00010 import org.eclipse.core.runtime.FileLocator;
00011 import org.eclipse.jface.resource.ImageDescriptor;
00012 import org.eclipse.ui.plugin.AbstractUIPlugin;
00013 import org.osgi.framework.BundleContext;
00014 
00015 
00019 public class Activator extends AbstractUIPlugin {
00020     // The plug-in ID
00021         public static final String PLUGIN_ID = "JoystickPlugin";
00022 
00023         // The shared instance
00024         private static Activator plugin;
00025 
00026     private static final String  LINUX_HOME_DIR   = System.getenv("HOME") + File.separator ;
00027     private static final String  WIN_HOME_DIR     = System.getenv("APPDATA") + File.separator;
00028     private static final String  LINUX_TMP_DIR   = LINUX_HOME_DIR + ".Joystick_aist" + File.separator;
00029     private static final String  WIN_TMP_DIR     = WIN_HOME_DIR + "Joystick_aist" + File.separator;
00030     private static final File TMP_DIR = initTempDir();
00031     
00035         public Activator() {
00036                 plugin = this;
00037         }
00038 
00039         /*
00040          * (non-Javadoc)
00041          * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
00042          */
00043         public void start(BundleContext context) throws Exception {
00044                 super.start(context);
00045         }
00046 
00047         /*
00048          * (non-Javadoc)
00049          * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
00050          */
00051         public void stop(BundleContext context) throws Exception {
00052                 plugin = null;
00053                 super.stop(context);
00054         }
00055         
00061     public static String getPath() {
00062         URL entry = getDefault().getBundle().getEntry("/");
00063         String pluginDirectory = "";
00064         try {
00065             pluginDirectory = FileLocator.resolve(entry).getPath();
00066         } catch (IOException e) {
00067             e.printStackTrace();
00068         }
00069         return pluginDirectory;
00070     }
00071     
00078     public File getTempDir(){
00079         return TMP_DIR;
00080     }
00081 
00087         public static Activator getDefault() {
00088                 return plugin;
00089         }
00090 
00091         
00100     public static void resourceToFile(Class<jp.go.aist.hrp.joystick.Activator> pluginManager, String srcName, File destFile)
00101     throws IOException {
00102         InputStream in = pluginManager.getResourceAsStream(srcName.toString());
00103         OutputStream out = new FileOutputStream(destFile.toString());
00104         try {
00105             byte[] buf = new byte[1024];
00106             int len;
00107             while ((len = in.read(buf)) > 0) {
00108                 out.write(buf, 0, len);
00109             }
00110         } catch (IOException e) {
00111             e.printStackTrace();
00112         } finally {
00113             in.close();
00114             out.close();
00115         }
00116     }
00117         
00122     private static File createConfigFile() {
00123         File rtcFile = new File( Activator.getDefault().getTempDir(),"rtc.conf");
00124         if (!rtcFile.exists()){
00125                 try{
00126                         resourceToFile(Activator.class, "/default_rtc.conf", rtcFile);
00127                 }catch (IOException ex){
00128                         ex.printStackTrace();
00129                 }
00130         }
00131         return rtcFile;
00132     }
00133         
00138         public static String getConfigFilePath(){
00139                 String confPath=""; //$NON-NLS-1$
00140         File defualtRtcFile = new File(Activator.getDefault().getTempDir() + File.separator + "rtc.conf");
00141         if( defualtRtcFile.isFile() ){
00142             confPath = defualtRtcFile.getPath(); 
00143         } else {
00144                 confPath = createConfigFile().getPath();
00145         }
00146         System.out.println("[Joystick] default Config File path="+confPath); //$NON-NLS-1$
00147                 return confPath;
00148         }
00149 
00157         public static ImageDescriptor getImageDescriptor(String path) {
00158                 return imageDescriptorFromPlugin(PLUGIN_ID, path);
00159         }
00160         
00161     
00162         static private File initTempDir() {
00163         File ret = null;
00164         
00165         if ( System.getProperty("os.name").equals("Linux") ||
00166              System.getProperty("os.name").equals("Mac OS X")) {
00167             ret = new File(LINUX_TMP_DIR);
00168         } else { //Windows と 仮定
00169             ret = new File(WIN_TMP_DIR);
00170         }
00171         if( !ret.exists() ){
00172             ret.mkdirs();
00173         }
00174         
00175         return ret;
00176     }
00177 
00178 }


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Thu Apr 11 2019 03:30:15