コード例 #1
0
ファイル: EditorWindow.java プロジェクト: Janl1/RedJ
  private void save() {
    // The name of the file to open.
    String fileName = project.getPath() + File.separator + jClass.fileName;

    try {
      // Assume default encoding.
      FileWriter fileWriter = new FileWriter(fileName);

      // Always wrap FileWriter in BufferedWriter.
      BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);

      // Note that write() does not automatically
      // append a newline character.
      bufferedWriter.write(editPane.getText());

      // Always close files.
      bufferedWriter.close();
      System.out.println("Saved!");
    } catch (IOException ex) {
      System.out.println("Error writing to file '" + fileName + "'");
      // Or we could just do this:
      // ex.printStackTrace();
    }
    if (RedJ.compiledClasses.contains(jClass.getName())) {
      RedJ.compiledClasses.remove(jClass.getName());
    }
  }
コード例 #2
0
ファイル: EditorWindow.java プロジェクト: Janl1/RedJ
  public EditorWindow(RedJClass jClass, RedJProject project) {
    addWindowListener(
        new WindowAdapter() {
          @Override
          public void windowClosed(WindowEvent e) {
            RedJ.openedWindows.remove(jClass.getName());
            RedJ.openedClasses.remove(jClass.getName());
          }
        });

    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    setBounds(100, 100, 899, 522);
    setTitle(jClass.getName() + " - " + project.getName());
    JMenuBar menuBar = new JMenuBar();
    setJMenuBar(menuBar);

    this.jClass = jClass;
    this.project = project;

    // mnClass.add(mntmClose);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    contentPane.setLayout(new BorderLayout(0, 0));
    setContentPane(contentPane);

    JTextPane editorPane = new JTextPane(/*SyntaxColor.getDoc()*/ );
    editorPane.setFont(new Font("Swis721 BT", Font.PLAIN, 14));
    contentPane.add(editorPane, BorderLayout.CENTER);
    editPane = editorPane;

    JScrollPane scrollPane = new JScrollPane(editPane);
    getContentPane().add(scrollPane);
    scrollPane.setBackground(new Color(45, 50, 59));

    JPanel panel = new JPanel();
    panel.setBackground(new Color(211, 211, 211));
    contentPane.add(panel, BorderLayout.NORTH);

    JPanel panel2 = new JPanel();
    panel2.setBackground(new Color(211, 211, 211));
    contentPane.add(panel2, BorderLayout.SOUTH);

    JTextPane textPane = new JTextPane();
    textPane.setEditable(false);
    textPane.setSize(new Dimension(100, 1000));
    textPane.setVisible(false);

    panel2.add(textPane);

    errorPane = textPane;

    JButton btnNewButton = new JButton("Compile");
    btnNewButton.setToolTipText("(F11)");
    btnNewButton.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            CompilerJob job = new CompilerJob(jClass, project);
            job.doJob();
          }
        });

    JButton btnSave = new JButton("Save");
    btnSave.setToolTipText("(CTRL + S)");
    btnSave.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            // The name of the file to open.
            String fileName = project.getPath() + File.separator + jClass.fileName;

            try {
              // Assume default encoding.
              FileWriter fileWriter = new FileWriter(fileName);

              // Always wrap FileWriter in BufferedWriter.
              BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);

              // Note that write() does not automatically
              // append a newline character.
              bufferedWriter.write(editPane.getText());

              // Always close files.
              bufferedWriter.close();
            } catch (IOException ex) {
              System.out.println("Error writing to file '" + fileName + "'");
              // Or we could just do this:
              // ex.printStackTrace();
            }
          }
        });
    panel.add(btnSave);
    panel.add(btnNewButton);

    JButton btnNewButton_1 = new JButton("Cut");
    btnNewButton_1.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {

            StringSelection selection = new StringSelection(editPane.getSelectedText());
            Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
            clipboard.setContents(selection, selection);
          }
        });
    btnNewButton_1.setToolTipText("CTRL + X");
    panel.add(btnNewButton_1);

    JButton btnNewButton_2 = new JButton("Copy");
    btnNewButton_2.setToolTipText("(CTRL + C)");
    btnNewButton_2.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            StringSelection selection = new StringSelection(editPane.getSelectedText());
            Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
            clipboard.setContents(selection, selection);
          }
        });
    panel.add(btnNewButton_2);

    JButton btnPaste = new JButton("Paste");
    btnPaste.setToolTipText("(CTRL + V)");
    btnPaste.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            String data = "";
            try {
              data =
                  (String)
                      Toolkit.getDefaultToolkit()
                          .getSystemClipboard()
                          .getData(DataFlavor.stringFlavor);
            } catch (HeadlessException e1) {
              // TODO Auto-generated catch block
              e1.printStackTrace();
            } catch (UnsupportedFlavorException e1) {
              // TODO Auto-generated catch block
              e1.printStackTrace();
            } catch (IOException e1) {
              // TODO Auto-generated catch block
              e1.printStackTrace();
            }

            try {
              editPane.getDocument().insertString(editPane.getCaretPosition(), data, null);
            } catch (BadLocationException e1) {
              // TODO Auto-generated catch block
              e1.printStackTrace();
            }
          }
        });
    panel.add(btnPaste);

    JButton btnClose = new JButton("Close");
    btnClose.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            setVisible(false);
          }
        });
    panel.add(btnClose);

    completeManager = new CompleteManager(editPane);
    completeManager.addAction("{", "{}");
    completeManager.addAction("(", "()");
    completeManager.addAction("[", "[]");
    completeManager.addAction("<", "<>");
    completeManager.addAction("\"", "\"\"");
    completeManager.addAction("'", "''");

    editPane.addKeyListener(this);
  }
コード例 #3
0
ファイル: CompilerJob.java プロジェクト: Janl1/RedJ
  public CompilerJob(RedJClass jClass, RedJProject project) {
    this.setName("CompilerJob-" + jClass.getName());
    this.setRunnable(
        new Runnable() {

          @Override
          public void run() {
            // JOptionPane.showMessageDialog(ProjectWindow.contentPane, "Compiling!");
            String sourceFile = project.getPath() + File.separator + jClass.getName() + ".java";
            /*JFrame frame = new JFrame(sourceFile);
            frame.setSize(800, 800);
            frame.setVisible(true);*/

            JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
            // JOptionPane.showMessageDialog(ProjectWindow.contentPane, "Compiling1!");
            DiagnosticCollector<JavaFileObject> diagnostics =
                new DiagnosticCollector<JavaFileObject>();
            StandardJavaFileManager fileManager =
                compiler.getStandardFileManager(diagnostics, null, null);
            // JOptionPane.showMessageDialog(ProjectWindow.contentPane, "Compiling2!");
            // prepare the source file(s) to compile
            List<File> sourceFileList = new ArrayList<File>();
            sourceFileList.add(new File(sourceFile));
            // JOptionPane.showMessageDialog(ProjectWindow.contentPane, "Compiling3!");
            Iterable<? extends JavaFileObject> compilationUnits =
                fileManager.getJavaFileObjectsFromFiles(sourceFileList);
            CompilationTask task =
                compiler.getTask(null, fileManager, diagnostics, null, null, compilationUnits);
            // JOptionPane.showMessageDialog(ProjectWindow.contentPane, "Compiling4!");
            boolean success = task.call();
            // JOptionPane.showMessageDialog(ProjectWindow.contentPane, "Compiling5!");
            try {
              fileManager.close();
            } catch (IOException e) {
            }
            // JOptionPane.showMessageDialog(ProjectWindow.contentPane, "Compiling failed222!");
            if (success) {
              // JOptionPane.showMessageDialog(ProjectWindow.contentPane, "Compiled!");
              System.out.println("Compiled!");
              if (!RedJ.compiledClasses.contains(jClass.getName())) {
                RedJ.compiledClasses.add(jClass.getName());
              }
              try {
                RedJ.openedWindows.get(jClass.getName()).compilingSuccessful();

                Thread thread =
                    new Thread(
                        new Runnable() {

                          @Override
                          public void run() {
                            RedJ.openedWindows.get(jClass.getName()).clearError();
                          }
                        });
                try {
                  thread.sleep(1000 * 10);
                } catch (InterruptedException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
                }
                thread.start();
              } catch (Exception e) {

              }
            } else {
              try {
                System.err.println("Compiling failed.");
                // JOptionPane.showMessageDialog(ProjectWindow.contentPane, "Compiling failed!");
                if (RedJ.openedClasses.contains(jClass.getName())) {
                  RedJ.openedWindows
                      .get(jClass.getName())
                      .displayError(diagnostics.getDiagnostics().toString());
                  RedJ.openedWindows.get(jClass.getName()).requestFocus();
                  for (Diagnostic<? extends JavaFileObject> obj : diagnostics.getDiagnostics()) {
                    RedJ.openedWindows.get(jClass.getName()).errorPane.grabFocus();

                    System.out.println(obj.getLineNumber());
                    System.out.println(obj.getPosition());
                    RedJ.openedWindows.get(jClass.getName()).gotoError((int) obj.getPosition());
                  }
                } else {
                  // JOptionPane.showMessageDialog(ProjectWindow.contentPane, "Compiling failed!");
                  EditorWindow window = new EditorWindow(jClass, RedJ.openedProject);
                  window.setVisible(true);
                  window.load(jClass.getContent());
                  RedJ.openedClasses.add(jClass.getName());
                  RedJ.openedWindows.put(jClass.getName(), window);
                  RedJ.openedWindows
                      .get(jClass.getName())
                      .displayError(diagnostics.getDiagnostics().toString());
                  RedJ.openedWindows.get(jClass.getName()).requestFocus();
                  for (Diagnostic<? extends JavaFileObject> obj : diagnostics.getDiagnostics()) {
                    RedJ.openedWindows.get(jClass.getName()).errorPane.grabFocus();

                    System.out.println(obj.getLineNumber());
                    System.out.println(obj.getPosition());
                    RedJ.openedWindows.get(jClass.getName()).gotoError((int) obj.getPosition());
                  }
                }
              } catch (Exception e) {

              }
            }
          }
        });
  }