/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.opensim.console;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.prefs.Preferences;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import org.openide.util.Exceptions;
import org.openide.util.HelpCtx;
import org.openide.util.actions.CallableSystemAction;
import org.opensim.utils.TheApp;
import org.python.util.PythonInterpreter;

public final class ToolsRunScriptAction extends CallableSystemAction {

    public void performAction() {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    protected boolean asynchronous() {
        return false;
    }

    public String getName() {
        return "Scripts";
    }

   
   protected void initialize() {
      super.initialize();
      // see org.openide.util.actions.SystemAction.iconResource() javadoc for more details
      putValue("noIconInMenu", Boolean.TRUE);
   }
    
    public HelpCtx getHelpCtx() {
         return HelpCtx.DEFAULT_HELP;
    }

    public JMenuItem getMenuPresenter() {
        JMenu scriptsMenu = new JMenu("Scripts");
        FileFilter fileFilter = new FileFilter() {

            public boolean accept(File file) {
                return (!file.isDirectory() && file.getName().endsWith(".py"));
            }
        };
        final String ScriptsRootDirectory = Preferences.userNodeForPackage(TheApp.class).get("Scripts Path", "Scripts");
        File rootHelpDirectory = new File(ScriptsRootDirectory);
        String fullPath = rootHelpDirectory.getAbsolutePath();
        File[] files = rootHelpDirectory.listFiles(fileFilter);
        if (files == null) {
            
            return scriptsMenu;
        }

        for (int i = 0; i < files.length; i++) {
            final String fileName = files[i].getName();
            JMenuItem nextItem = new JMenuItem(fileName);
            nextItem.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    PythonInterpreter interp = ScriptingShellTopComponent.getDefault().getInterp();
                    interp.execfile(ScriptsRootDirectory+"/"+fileName);
                    File scriptFile= new File(ScriptsRootDirectory+"/"+fileName);
                    String fileContents = getContents(scriptFile);
                    ScriptingShellTopComponent.getDefault().logMessage(fileContents);
                    ScriptingShellTopComponent.getDefault().logMessage("Finished executing script file "+fileName);
                }
            });
            
            scriptsMenu.add(nextItem);
        }

        return scriptsMenu;
    }
 
    public String getContents(File aFile) {
        StringBuilder contents = new StringBuilder();

        try {
            //use buffering, reading one line at a time
            //FileReader always assumes default encoding is OK!
            BufferedReader input = new BufferedReader(new FileReader(aFile));
            try {
                String line = null; //not declared within while loop
        		/*
                 * readLine is a bit quirky :
                 * it returns the content of a line MINUS the newline.
                 * it returns null only for the END of the stream.
                 * it returns an empty String if two newlines appear in a row.
                 */
                while ((line = input.readLine()) != null) {
                    contents.append(line);
                    contents.append(System.getProperty("line.separator"));
                }
            } finally {
                input.close();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        return contents.toString();
    }
}

@@ -279,78 +279,8 @@
 er;%0A
-import java.io.FileInputStream;%0Aimport java.io.FileNotFoundException;%0A
 impo
@@ -334,36 +334,8 @@
 on;%0A
-import java.io.InputStream;%0A
 impo
@@ -426,44 +426,8 @@
 em;%0A
-import org.openide.util.Exceptions;%0A
 impo
@@ -505,24 +505,60 @@
 stemAction;%0A
+import org.opensim.utils.FileUtils;%0A
 import org.o
@@ -1865,28 +1865,16 @@
 null) %7B%0A
-            
 %0A       
@@ -2220,500 +2220,49 @@
     
-PythonInterpreter interp = ScriptingShellTopComponent.getDefault().getInterp();%0A                    interp.execfile(ScriptsRootDirectory+%22/%22+fileName);%0A                    File scriptFile= new File(ScriptsRootDirectory+%22/%22+fileName);%0A                    String fileContents = getContents(scriptFile);%0A                    ScriptingShellTopComponent.getDefault().logMessage(fileContents);%0A                    ScriptingShellTopComponent.getDefault().logMessage(%22Finished executing script file %22+
+executeFile(ScriptsRootDirectory + %22/%22 + 
 file
@@ -2302,28 +2302,16 @@
     %7D);%0A
-            
 %0A       
@@ -2344,32 +2344,512 @@
 tem);%0A        %7D%0A
+        JMenuItem browseItem = new JMenuItem(%22Browse...%22);%0A        browseItem.addActionListener(new ActionListener() %7B%0A%0A            public void actionPerformed(ActionEvent e) %7B%0A                String dataFilename = FileUtils.getInstance().browseForFilename(%22.py%22, %22Files containing script to execute%22, true);%0A                if (dataFilename != null) %7B%0A                    executeFile(dataFilename);%0A                %7D%0A            %7D%0A        %7D);%0A        scriptsMenu.add(browseItem);
 %0A        return 
@@ -2867,17 +2867,16 @@
 ;%0A    %7D%0A
- 
 %0A    pub
@@ -3949,10 +3949,607 @@
 ;%0A    %7D%0A
+    /**%0A     * execute passed in scriptFilename in Scripting shell and echo contents%0A     * @param scriptFilename %0A     */%0A    void executeFile(String scriptFilename) %7B%0A        PythonInterpreter interp = ScriptingShellTopComponent.getDefault().getInterp();%0A        interp.execfile(scriptFilename);%0A        File scriptFile = new File(scriptFilename);%0A        String fileContents = getContents(scriptFile);%0A        ScriptingShellTopComponent.getDefault().logMessage(fileContents);%0A        ScriptingShellTopComponent.getDefault().logMessage(%22Finished executing script file %22 + scriptFilename);%0A    %7D%0A
 %7D%0A

Allow browsing for script file from arbitrary directory