All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jsoar.debugger.actions.SourceFileAction Maven / Gradle / Ivy

/*
 * Copyright (c) 2008  Dave Ray 
 *
 * Created on Oct 25, 2008
 */
package org.jsoar.debugger.actions;

import java.awt.event.ActionEvent;
import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.KeyStroke;
import javax.swing.filechooser.FileFilter;

import org.jsoar.debugger.JSoarDebugger;
import org.jsoar.kernel.SoarException;
import org.jsoar.util.FileTools;
import org.jsoar.util.commands.SoarCommandInterpreter;

/**
 * @author ray
 */
public class SourceFileAction extends AbstractDebuggerAction
{
    private static final long serialVersionUID = -7639843952865259437L;
    
    private String lastDir = System.getProperty("user.dir");

    public SourceFileAction(ActionManager manager)
    {
        super(manager, "Source File ...");
        
        setAcceleratorKey(KeyStroke.getKeyStroke("ctrl O"));
        lastDir = JSoarDebugger.getPreferences().get("lastSourceDir", System.getProperty("user.dir"));
    }

    /* (non-Javadoc)
     * @see org.jsoar.debugger.actions.AbstractDebuggerAction#update()
     */
    @Override
    public void update()
    {
    }

    /* (non-Javadoc)
     * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
     */
    @Override
    public void actionPerformed(ActionEvent arg0)
    {
        JFileChooser chooser = new JFileChooser(lastDir);
        chooser.setFileFilter(new FileFilter() {

            @Override
            public boolean accept(File f)
            {
                return f.isDirectory() || (f.isFile() && "soar".equals(FileTools.getExtension(f.getName())));
            }

            @Override
            public String getDescription()
            {
                return "Soar Files (*.soar)";
            }});
        
        if(JFileChooser.CANCEL_OPTION == chooser.showOpenDialog(getApplication().getTopLevelAncestor()))
        {
            return;
        }
        
        final File f = chooser.getSelectedFile();
        lastDir = f.getParentFile().getAbsolutePath();
        JSoarDebugger.getPreferences().put("lastSourceDir", lastDir);
        
        final SoarCommandInterpreter interp = getApplication().getAgent().getInterpreter();
        
        getApplication().getAgent().execute(() ->
        {
            try
            {
                interp.source(f);
            }
            catch (SoarException e)
            {
                // TODO this is a little smelly.
                getApplication().getAgent().getPrinter().error(e.getMessage());
            }
            getApplication().getAgent().getPrinter().flush();
            return null;
        }, getApplication().newUpdateCompleter(false));
        
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy