public void actionPerformed(ActionEvent a) { String command = a.getActionCommand(); if (command.equals("e")) { // Log toggle. if (consoleDisplayed = !consoleDisplayed) { splitter.add(outputScroll); splitter.setDividerLocation(.8); } else { splitter.remove(outputScroll); } } else if (command.equals("k")) { if (text.getText().contains("class")) { // This means we should try to compile this as normal. // Pulls out class name String code = text.getText(); int firstPos = code.indexOf("class"); int secondPos = code.indexOf("{"); String name = code.substring(firstPos + "class".length() + 1, secondPos).trim(); compileAndRun(name, text.getText()); } else { // This means we should compile this as a playground. String code = text.getText(); // Common import statements built-in String importDump = new String(); importDump += ("import java.util.*;\n" + "import javax.swing.*;\n" + "import javax.swing.event.*;\n" + "import java.awt.*;\n" + "import java.awt.event.*;\n" + "import java.io.*;\n"); // Pulls out any "import" statements and appends them to the import dump. int i = code.indexOf("import"); while (i >= 0) { String s = code.substring(i, code.indexOf(";", i) + 1); code = code.replaceFirst(s, ""); importDump += s + "\n"; i = code.indexOf("import", i + 1); } // Inject the class header and main method code = "//User and auto-imports pre-defined\n" + importDump + "//Autogenerated class\npublic class Main {\npublic static void main(String[] args) {\n" + code + "\n}\n}"; compileAndRun("Main", code); } } }
private void updateDisplay() { // first, set block colours textView.setBackground(getColour(backgroundColour("text-background-colour"))); textView.setForeground(getColour(foregroundColour("text-foreground-colour"))); textView.setCaretColor(getColour(foregroundColour("text-caret-colour"))); problemsView.setBackground(getColour(backgroundColour("problems-background-colour"))); problemsView.setForeground(getColour(foregroundColour("problems-foreground-colour"))); consoleView.setBackground(getColour(backgroundColour("console-background-colour"))); consoleView.setForeground(getColour(foregroundColour("console-foreground-colour"))); // second, set colours on the code! java.util.List<Lexer.Token> tokens = Lexer.tokenise(textView.getText(), true); int pos = 0; for (Lexer.Token t : tokens) { int len = t.toString().length(); if (t instanceof Lexer.RightBrace || t instanceof Lexer.LeftBrace) { highlightArea(pos, len, foregroundColour("text-brace-colour")); } else if (t instanceof Lexer.Strung) { highlightArea(pos, len, foregroundColour("text-string-colour")); } else if (t instanceof Lexer.Comment) { highlightArea(pos, len, foregroundColour("text-comment-colour")); } else if (t instanceof Lexer.Quote) { highlightArea(pos, len, foregroundColour("text-quote-colour")); } else if (t instanceof Lexer.Comma) { highlightArea(pos, len, foregroundColour("text-comma-colour")); } else if (t instanceof Lexer.Identifier) { highlightArea(pos, len, foregroundColour("text-identifier-colour")); } else if (t instanceof Lexer.Integer) { highlightArea(pos, len, foregroundColour("text-integer-colour")); } pos += len; } }
private boolean checkForSave() { // build warning message String message; if (file == null) { message = "File has been modified. Save changes?"; } else { message = "File \"" + file.getName() + "\" has been modified. Save changes?"; } // show confirm dialog int r = JOptionPane.showConfirmDialog( this, new JLabel(message), "Warning!", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE); if (r == JOptionPane.YES_OPTION) { // Save File if (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) { // write the file physWriteTextFile(fileChooser.getSelectedFile(), textView.getText()); } else { // user cancelled save after all return false; } } return r != JOptionPane.CANCEL_OPTION; }
public void evaluate() { try { // clear problems and console messages problemsView.setText(""); consoleView.setText(""); // update status view statusView.setText(" Parsing ..."); tabbedPane.setSelectedIndex(0); LispExpr root = Parser.parse(textView.getText()); statusView.setText(" Running ..."); tabbedPane.setSelectedIndex(1); // update run button runButton.setIcon(stopImage); runButton.setActionCommand("Stop"); // start run thread runThread = new RunThread(root); runThread.start(); } catch (SyntaxError e) { tabbedPane.setSelectedIndex(0); System.err.println( "Syntax Error at " + e.getLine() + ", " + e.getColumn() + " : " + e.getMessage()); } catch (Error e) { // parsing error System.err.println(e.getMessage()); statusView.setText(" Errors."); } }
private void _setTagForSelectedText(String tagElement) { try { System.out.println(mTextEditor.getText()); } catch (Exception e) { e.printStackTrace(); } }
public void saveFile() { if (file == null) { // first save file, so prompt for name. saveFileAs(); } else { // file already named so just write it. physWriteTextFile(file, textView.getText()); // update status statusView.setText(" Saved file \"" + file.getName() + "\"."); // reset dirty bit dirty = false; } }
public void saveFileAs() { // Force user to enter new file name if (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) { file = fileChooser.getSelectedFile(); } else { // user cancelled save after all return; } // file selected, so write it. physWriteTextFile(file, textView.getText()); // update status statusView.setText(" Saved file \"" + file.getName() + "\"."); // reset dirty bit dirty = false; }
public void prettyPrint() { try { // clear old problem messages problemsView.setText(""); // update status view statusView.setText(" Parsing ..."); LispExpr root = Parser.parse(textView.getText()); statusView.setText(" Pretty Printing ..."); String newText = PrettyPrinter.prettyPrint(root); textView.setText(newText); statusView.setText(" Done."); } catch (Error e) { System.err.println(e.getMessage()); statusView.setText(" Errors."); } }
// This is used for when the user's code has something wrong. // This has checks in place to make some more sense of the error messages. // Internal errors should be logged using println and the progErr font. private static void logError(String message) { if (message.contains("Playground$FrameAction")) { // This is a reflection error, so that means that we have a malformed class or method. String code = text.getText(); if (!code.startsWith("public class")) { println( "Error: You defined a private class. Please use \"public class <classname>\".", progErr); } else { println( "Error: Malformed method. Make sure your main method is defined as \"public static void main(<any args>)\".", progErr); } } else { println(message, progErr); } }
public void actionPerformed(ActionEvent a) { // Note that this only works on *nix OSes. // For windows, get the first character of the action command, cast it to int, and compare // that on a case-by-case basis. String command = a.getActionCommand(); if (command.equals("e")) { // Log toggle. if (consoleDisplayed = !consoleDisplayed) { splitter.add(outputScroll); splitter.setDividerLocation(defaultSliderPosition); } else { splitter.remove(outputScroll); } } else if (command.equals("r")) { // Gets the code into a string. String code = text.getText(); // Marks certain areas as "dirty". // Dirty areas are places that shouldn't be considered for any keywords, // including "import", "extend", and so on. ArrayList<Integer> dirtyBounds = getDirty(code); if (text.getText().contains("class")) { // This means we should try to compile this as normal. // Pulls out class name int firstPos = code.indexOf("public class"); int secondPos = code.indexOf("{"); String name = code.substring(firstPos + "public class".length() + 1, secondPos).trim(); // Just a safety check to make sure you don't try to modify this program while it's // running. if (name.equals("Playground")) { System.out.println( "I know what you're doing and I don't approve. I won't even compile that."); return; } compileAndRun(name, code); } else { // This means we should compile this as a playground. // Common import statements built-in String importDump = new String(); importDump += ( // "import java.util.*;\n" + "import javax.swing.*;\n" + "import javax.swing.event.*;\n" + "import java.awt.*;\n" + "import java.awt.event.*;\n" + "import java.io.*;\n"); // User-defined or auto-generated methods String methodDump = new String(); // dirtyBounds.forEach(System.out::println); // Pulls out any "import" statements and appends them to the import dump. int i = code.indexOf("import"); while (i >= 0) { // Ignores comments and string literals if (isDirty(dirtyBounds, i)) { i = code.indexOf("import", i + 1); continue; } String s = code.substring(i, code.indexOf(";", i) + 1); // System.out.println("Found import: " + s); code = code.replaceFirst(s, ""); importDump += s + "\n"; i = code.indexOf("import", i + 1); } // Pulls out methods- these are defined by the keyword "method" until a closing bracket. /* i = code.indexOf("method"); while(i >= 0) { //Ignores comments and string literals if (isDirty(dirtyBounds, i)) { i = code.indexOf("method", i+1); continue; } //This scans from the first '{' until the last '}' to pull out the full method declaration. char temp = 0; int pos = code.indexOf("{", i+1), count = 1; if(pos != -1) { while(++pos < code.length()) { if (count == 0) break; temp = code.charAt(pos); if (temp == '{') count++; if (temp == '}') count--; } } else { //Missing an opening bracket, so just remove "method" and hope it compiles. code = code.replaceFirst("method", ""); i = code.indexOf("method", i+1); continue; } String s = code.substring(i, pos); //System.out.println("Found method: " + s); code = code.replace(s, ""); methodDump+=(s.replaceFirst("method ", "static ")) + "\n"; i = code.indexOf("method", i+1); }*/ // Pulls out all methods i = code.indexOf("("); while (i >= 0) { if (isDirty(dirtyBounds, i)) { i = code.indexOf("(", i + 1); continue; } // Move backwards first char temp = 0; int pos = i; boolean shouldSkip = false; while (--pos > 0) { temp = code.charAt(pos); if (temp == '.') { shouldSkip = true; break; } // This is a method call, ie String.charAt(); if (temp == ';') { ++pos; break; } // This is most likely a method declaration, since we had no errors // If we hit the start of the file, that's probable a method dec. too! } if (shouldSkip || isDirty(dirtyBounds, pos)) { i = code.indexOf("(", i + 1); continue; } // If this def. isn't a method or it's in a comment int start = pos; temp = 0; pos = code.indexOf("{", i + 1); int count = 1; if (pos != -1) { while (++pos < code.length()) { if (count == 0) break; temp = code.charAt(pos); if (temp == '{') count++; if (temp == '}') count--; } } else { i = code.indexOf("(", i + 1); continue; } int end = pos; String s = code.substring(start, end); code = code.replace(s, ""); // Just to make it look nicer s = s.trim(); System.out.println("Found method: " + s); methodDump += (s + "\n"); i = code.indexOf("(", i + 1); } // Inject the class header and main method, imports, and methods code = "//User and auto-imports pre-defined\n" + importDump + "//Autogenerated class\n" + "public class Main {\n" + "public static void main(String[] args) {\n" + code + "}\n" + methodDump + "}"; // Run as normal compileAndRun("Main", code); } } else if (command.equals("k")) { kill(); } else if (command.equals("o")) { new OptionFrame(frame); } else if (command.equals("/")) { new HelpFrame(frame); } }