Skip to content

Commit

Permalink
Refactored FIJI plugin organization and added Javadocs
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcel Stefko committed May 30, 2017
1 parent daf27ae commit 2143dc8
Show file tree
Hide file tree
Showing 16 changed files with 1,379 additions and 1,224 deletions.
21 changes: 4 additions & 17 deletions src/beanshell/ConsoleFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ public ConsoleFrame() {
interpreter.setShowResults(true);
}

/**
*
* @return BeanShell interpreter associated with this ConsoleFrame
*/
public Interpreter getInterpreter() {
return interpreter;
}
Expand Down Expand Up @@ -109,23 +113,6 @@ private void console_panelComponentResized(java.awt.event.ComponentEvent evt) {/
this.console.setSize(this.console_panel.getWidth(), this.console_panel.getHeight());
}//GEN-LAST:event_console_panelComponentResized

/**
* Accessed from FIJI. Initializes console and prints welcome text.
* @param args the command line arguments
*/
public static void main(String args[]) {
final ConsoleFrame console = new ConsoleFrame();
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {

console.setVisible(true);
}
});
CommandLineInterface.printWelcomeText(console.getInterpreter().getOut());
new Thread(console.getInterpreter()).start();
}


// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JPanel console_panel;
// End of variables declaration//GEN-END:variables
Expand Down
32 changes: 28 additions & 4 deletions src/commandline/CommandLineInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,32 @@
import org.apache.commons.cli.ParseException;

/**
*
* @author stefko
* Main class of the project, launches the BeanShell script interface.
* @author Marcel Stefko
*/
public final class CommandLineInterface {

private static final Options options = constructOptions();
private static final URL urlToWelcomeText = CommandLineInterface.class.getResource("/beanshell/welcome_text.txt");

/**
*
* @return all understood options for ALICA execution
*/
public static Options constructOptions() {
final Options options = new Options();
options.addOption("i", "interpreter", false, "run BeanShell interpreter inside current terminal window");
options.addOption("s", "script", true, "execute BeanShell script");
options.addOption("s", "script", true, "execute BeanShell script (can be combined with -i)");
options.addOption("h", "help", false, "show this help");
return options;
}

/**
* Shows help, launches the interpreter and executes scripts according to input args.
* @param args input arguments
*/
public static void main(String args[]) {
// parse input arguments
CommandLineParser parser = new DefaultParser();
CommandLine line = null;
try {
Expand All @@ -66,14 +75,20 @@ public static void main(String args[]) {
System.err.println("Use -help for usage.");
System.exit(1);
}

// decide how do we make the interpreter available based on options
Interpreter interpreter = null;
// show help and exit
if (line.hasOption("help")) {
HelpFormatter helpFormatter = new HelpFormatter();
helpFormatter.printHelp("java -jar <jar-name>", options, true);
System.exit(0);
// launch interpreter inside current terminal
} else if (line.hasOption("interpreter")) {
// assign in, out and err streams to the interpreter
interpreter = new Interpreter(new InputStreamReader(System.in), System.out, System.err, true);
interpreter.setShowResults(true);
// if a script was given, execute it before giving access to user
if (line.hasOption("script")) {
try {
interpreter.source(line.getOptionValue("script"));
Expand All @@ -83,7 +98,9 @@ public static void main(String args[]) {
Logger.getLogger(ConsoleFrame.class.getName()).log(Level.SEVERE, "EvalError while executing shell script.", ex);
}
}
// give access to user
new Thread( interpreter ).start();
// only execute script and exit
} else if (line.hasOption("script")) {
interpreter = new Interpreter();
try {
Expand All @@ -96,22 +113,30 @@ public static void main(String args[]) {
Logger.getLogger(ConsoleFrame.class.getName()).log(Level.SEVERE, "EvalError while executing shell script.", ex);
System.exit(1);
}
// if System.console() returns null, it means we were launched by
// double-clicking the .jar, so launch own ConsoleFrame
} else if (System.console() == null) {
ConsoleFrame cframe = new ConsoleFrame();
cframe.setVisible(true);
System.setOut(cframe.getInterpreter().getOut());
System.setErr(cframe.getInterpreter().getErr());
new Thread(cframe.getInterpreter()).start();
// otherwise, show help
} else {
HelpFormatter helpFormatter = new HelpFormatter();
helpFormatter.printHelp("java -jar <jar-name>", options, true);
System.exit(0);
}

if (interpreter != null) {
printWelcomeText(interpreter.getOut());
}
}

/**
* Reads the welcome_text file and prints it to a PrintStream.
* @param out stream to print to
*/
public static void printWelcomeText(PrintStream out) {
try {
InputStream inputStream = urlToWelcomeText.openStream();
Expand All @@ -121,7 +146,6 @@ public static void printWelcomeText(PrintStream out) {
while ((length = inputStream.read(buffer)) != -1) {
result.write(buffer, 0, length);
}
// StandardCharsets.UTF_8.name() > JDK 7
String message = result.toString("UTF-8");
out.print(message+"\n");
} catch (IOException ex) {
Expand Down
Loading

0 comments on commit 2143dc8

Please sign in to comment.