package ca.phon.shell.actions;

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

import ca.hedlund.jiss.ui.bindings.RunCommand;
import ca.phon.app.hooks.HookableAction;
import ca.phon.shell.PhonShellWindow;
import ca.phon.ui.CommonModuleFrame;

/**
 * Open a new PhonShell window for the current frame
 * and execute the specified script.
 *
 */
public class PhonShellScriptAction extends HookableAction {

	private final String scriptLocation;
	
	private boolean useBuffer = false;
	
	public PhonShellScriptAction(String scriptLocation) {
		this.scriptLocation = scriptLocation;
	}
	
	public boolean isUseBuffer() {
		return this.useBuffer;
	}
	
	public void setUseBuffer(boolean useBuffer) {
		this.useBuffer = useBuffer;
	}
	
	@Override
	public void hookableActionPerformed(ActionEvent ae) {
		PhonShellWindow window = null;
		
		CommonModuleFrame cmf = CommonModuleFrame.getCurrentFrame();
		if(cmf instanceof PhonShellWindow) {
			window = (PhonShellWindow)cmf;
		} else {
			window = new PhonShellWindow();
			window.pack();
			window.setVisible(true);
		}
		
		(new RunCommand(window.getConsole(), createExecCommand())).runCommand();
	}

	public String createExecCommand() {
		final StringBuilder sb = new StringBuilder();
		sb.append("::exec ").append('\"').append(scriptLocation).append('\"');
		if(useBuffer) {
			sb.append('>').append(getValue(NAME));
		}
		return sb.toString();
	}
	
}

@@ -63,29 +63,8 @@
 ent;
-%0Aimport java.io.File;
 %0A%0Aim
@@ -1318,11 +1318,13 @@
 end(
-'%3E'
+%22 %3E %22
 ).ap

Fix useBuffer flag for scripts