Newer
Older
KisoJikkenNWP / src / main / java / istlab / KisoJikken / Editor.java
package istlab.KisoJikken;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.plaf.FontUIResource;

import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
import org.fife.ui.rsyntaxtextarea.SyntaxConstants;
import org.fife.ui.rtextarea.RTextScrollPane;

public class Editor extends JFrame implements ActionListener, KeyListener {
    static Editor lastOpened;
    File file;
    RSyntaxTextArea textArea;
    JPanel topP;
    JSlider fontSizeJS;
    JButton saveB;
    JButton runB; // for ShellScript
    JButton compileB;
    JButton execB;
    JTextField comlineOption;
    ScriptRunner runner;

    public Editor(File f) {
        file = f;

        textArea = new RSyntaxTextArea(20, 40);
        textArea.setFont(new FontUIResource("sansserif", Font.PLAIN, 16));
        getContentPane().add(new RTextScrollPane(textArea));
        textArea.addKeyListener(this);

        try {
            setTitle(file.getCanonicalPath().replaceAll(App.userhome, "~"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        setSize(new Dimension(800, 400));// pack();
        setLocationRelativeTo(Launcher.theapp);
        setLocation(Launcher.theapp.getWidth(), Launcher.theapp.file2editor.size() * 40);
        setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
        load();

        topP = new JPanel();
        topP.setLayout(new FlowLayout(FlowLayout.CENTER, 1,2));
        topP.add(new JLabel("size:"));
        fontSizeJS = new JSlider(10, 50, 16);
        topP.add(fontSizeJS);
        fontSizeJS.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
                int size = fontSizeJS.getValue();
                textArea.setFont(new FontUIResource("sansserif", Font.PLAIN, size));
            }
        });
        saveB = new JButton("Save");
        saveB.addActionListener(this);
        saveB.setEnabled(false);

        // ファイルの拡張子によって、きりかえる
        if (file.getName().endsWith(".java")) {
            textArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JAVA);
            textArea.setCodeFoldingEnabled(true);
            topP.add(saveB);
        } else if (file.getName().endsWith(".sh")) {
            textArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_UNIX_SHELL);
            topP.add(saveB);
        } else if (file.getName().endsWith(".class")) {
            textArea.setEditable(false);
        } else { // txt fileなど
            topP.add(saveB);
        }

        JButton loadB = new JButton("Reload");
        loadB.addActionListener(this);
        topP.add(loadB);

        // 実行ボタン
        if (textArea.getSyntaxEditingStyle() == SyntaxConstants.SYNTAX_STYLE_UNIX_SHELL) {
            runB = new JButton("Run");
            runB.addActionListener(this);
            topP.add(runB);
            runner = new ScriptRunner(file.getAbsolutePath(), runB);
        } else if (textArea.getSyntaxEditingStyle() == SyntaxConstants.SYNTAX_STYLE_JAVA) {
            compileB = new JButton("Compile");
            execB = new JButton("Exec");
            compileB.addActionListener(this);
            execB.addActionListener(this);
            topP.add(compileB);
            topP.add(execB);
            comlineOption = new JTextField("arg1 arg2 ...", 15);
            comlineOption.setForeground(Color.GRAY);
            comlineOption.addFocusListener(new FocusListener() {
                @Override
                public void focusGained(FocusEvent e) {
                    if (comlineOption.getText().equals("arg1 arg2 ...")) {
                        comlineOption.setText("");
                        comlineOption.setForeground(Color.BLACK);
                    }
                }

                @Override
                public void focusLost(FocusEvent e) {
                    if (comlineOption.getText().isEmpty()) {
                        comlineOption.setForeground(Color.GRAY);
                        comlineOption.setText("arg1 arg2 ...");
                    }
                }
            });

            topP.add(comlineOption);
        }

        getContentPane().add(topP, BorderLayout.NORTH);

        setVisible(true);
        textArea.requestFocus();
        textArea.setCaretPosition(0);

        lastOpened = this;
    }

    public void load() {
        InputStream is = null;
        try {
            is = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        BufferedReader br = null;
        try {
            br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        StringBuilder sb = new StringBuilder();
        String line;
        try {
            while ((line = br.readLine()) != null) {
                sb.append(line + "\n");
            }
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        textArea.setText(sb.toString());
    }

    public void save() {
        String src = textArea.getText();
        // System.out.println(src);
        try {
            PrintWriter out = new PrintWriter(file.getAbsolutePath(), "UTF-8");
            out.println(src);
            out.flush();
        } catch (FileNotFoundException | UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        System.out.println("保存しました");
        saveB.setEnabled(false);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("Save")) {
            save();
        } else if (e.getActionCommand().equals("Reload")) {
            load();
        } else if (e.getActionCommand().equals("Run")) {
            runner.startstop();
            runB.setText("Stop");
        } else if (e.getActionCommand().equals("Stop")) {
            runner.startstop();
            runB.setText("Run");
        } else if (e.getActionCommand().equals("Compile")) {
            new JCompiler(file.getAbsolutePath());
        } else if (e.getActionCommand().equals("Exec")) {
            new JExecutor(file.getAbsolutePath(), comlineOption.getText());
        }

    }

    @Override
    public void keyTyped(KeyEvent e) {
        saveB.setEnabled(true);
    }

    @Override
    public void keyPressed(KeyEvent e) {
        System.out.println(e.getKeyCode());

        if (e.isControlDown() || e.isAltDown() || e.isAltGraphDown() || e.isMetaDown()) {
            if (e.getKeyCode() == 83) { // Save
                save();
            }
            if (e.getKeyCode() == 87) { // W (Close)
                setVisible(false);
            }
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }
}