Пример #1
0
    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);
        }
      }
    }
Пример #2
0
  public static void main() {
    // Main
    frame = new JFrame("Java Playground");
    frame.setSize(640, 480);
    // Make sure the divider is properly resized
    frame.addComponentListener(
        new ComponentAdapter() {
          public void componentResized(ComponentEvent c) {
            splitter.setDividerLocation(.8);
          }
        });
    // Make sure the JVM is reset on close
    frame.addWindowListener(
        new WindowAdapter() {
          public void windowClosed(WindowEvent w) {
            new FrameAction().kill();
          }
        });

    // Setting up the keybinding
    // Ctrl+k or Cmd+k -> compile
    bind(KeyEvent.VK_K);

    // Ctrl+e or Cmd+e -> console
    bind(KeyEvent.VK_E);

    // Save, New file, Open file, Print.
    // Currently UNUSED until I figure out how normal java files and playground files will
    // interface.
    bind(KeyEvent.VK_S);
    bind(KeyEvent.VK_N);
    bind(KeyEvent.VK_O);
    bind(KeyEvent.VK_P);

    // Binds the keys to the action defined in the FrameAction class.
    frame.getRootPane().getActionMap().put("console", new FrameAction());

    // The main panel for typing code in.
    text = new JTextPane();
    textScroll = new JScrollPane(text);
    textScroll.setBorder(null);
    textScroll.setPreferredSize(new Dimension(640, 480));

    // Document with syntax highlighting. Currently unfinished.
    doc = text.getStyledDocument();
    doc.addDocumentListener(
        new DocumentListener() {
          public void changedUpdate(DocumentEvent d) {}

          public void insertUpdate(DocumentEvent d) {}

          public void removeUpdate(DocumentEvent d) {}
        });

    ((AbstractDocument) doc).setDocumentFilter(new NewLineFilter());

    // The output log; a combination compiler warning/error/runtime error/output log.
    outputText = new JTextPane();
    outputScroll = new JScrollPane(outputText);
    outputScroll.setBorder(null);

    // "Constant" for the error font
    error = new SimpleAttributeSet();
    error.addAttribute(StyleConstants.CharacterConstants.Italic, Boolean.TRUE);
    error.addAttribute(StyleConstants.Foreground, Color.RED);

    // "Constant" for the warning message font
    warning = new SimpleAttributeSet();
    warning.addAttribute(StyleConstants.CharacterConstants.Italic, Boolean.TRUE);
    warning.addAttribute(StyleConstants.Foreground, Color.PINK);

    // "Constant" for the debugger error font
    progErr = new SimpleAttributeSet();
    progErr.addAttribute(StyleConstants.Foreground, Color.BLUE);

    // Print streams to redirect System.out and System.err.
    out = new TextOutputStream(outputText, null);
    err = new TextOutputStream(outputText, error);
    System.setOut(new PrintStream(out));
    System.setErr(new PrintStream(err));

    // Sets up the output log
    outputText.setEditable(false);
    outputScroll.setVisible(true);

    // File input/output setup
    chooser = new JFileChooser();

    // Setting up miscellaneous stuff
    compiler = ToolProvider.getSystemJavaCompiler();
    JVMrunning = false;
    redirectErr = null;
    redirectOut = null;
    redirectIn = null;

    // Sets up the splitter pane and opens the program up
    splitter = new JSplitPane(JSplitPane.VERTICAL_SPLIT, textScroll, outputScroll);
    consoleDisplayed = false;
    splitter.remove(outputScroll); // Initially hides terminal until it is needed
    splitter.setOneTouchExpandable(true);
    frame.add(splitter);
    frame.setVisible(true);

    // Sets the divider to the proper one, for debugging
    // splitter.setDividerLocation(.8);
  }
Пример #3
0
    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);
      }
    }