diff --git a/src/main/java/istlab/KisoJikken/Editor.java b/src/main/java/istlab/KisoJikken/Editor.java index b6e93a3..d6451f4 100644 --- a/src/main/java/istlab/KisoJikken/Editor.java +++ b/src/main/java/istlab/KisoJikken/Editor.java @@ -31,6 +31,7 @@ import org.fife.ui.rtextarea.RTextScrollPane; public class Editor extends JFrame implements ActionListener, KeyListener { + static Editor lastOpened; File file; RSyntaxTextArea textArea; JPanel topP; @@ -46,15 +47,6 @@ textArea = new RSyntaxTextArea(20, 40); textArea.setFont(new FontUIResource("sansserif", Font.PLAIN, 16)); - // ファイルの拡張子によって、きりかえる - if (file.getName().endsWith(".java")) { - textArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JAVA); - textArea.setCodeFoldingEnabled(true); - } else if (file.getName().endsWith(".sh")) { - textArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_UNIX_SHELL); - } else if (file.getName().endsWith(".class")){ - textArea.setEditable(false); - } getContentPane().add(new RTextScrollPane(textArea)); textArea.addKeyListener(this); @@ -77,14 +69,26 @@ public void stateChanged(ChangeEvent e) { int size = fontSizeJS.getValue(); textArea.setFont(new FontUIResource("sansserif", Font.PLAIN, size)); - } }); saveB = new JButton("Save"); saveB.addActionListener(this); - topP.add(saveB); 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); @@ -109,6 +113,8 @@ setVisible(true); textArea.requestFocus(); textArea.setCaretPosition(0); + + lastOpened = this; } public void load() { diff --git a/src/main/java/istlab/KisoJikken/JTAConsole.java b/src/main/java/istlab/KisoJikken/JTAConsole.java index 9f96809..81837ee 100644 --- a/src/main/java/istlab/KisoJikken/JTAConsole.java +++ b/src/main/java/istlab/KisoJikken/JTAConsole.java @@ -1,19 +1,24 @@ package istlab.KisoJikken; +import java.awt.Dimension; import java.awt.Point; +import java.awt.event.KeyEvent; +import java.awt.event.KeyListener; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.io.OutputStream; import java.util.Stack; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTextArea; +import javax.swing.text.BadLocationException; -public class JTAConsole extends JTextArea implements Runnable, WindowListener { +public class JTAConsole extends JTextArea implements Runnable, WindowListener, KeyListener { // static JTAConsole lastWindow; // static Point lastActivePoint; static Stack winStack = new Stack(); @@ -21,7 +26,9 @@ Process process; Thread thread; InputStream instream; + InputStreamReader isreader; BufferedReader reader; + OutputStream outstream; // ProcessBuilder pb; // public JTAConsole(ProcessBuilder prob) { @@ -34,18 +41,27 @@ frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); frame.addWindowListener(this); - if (winStack.size()>0){ + if (winStack.size() > 0) { JTAConsole last = winStack.peek(); - if (last.isVisible()){ + if (last.isVisible()) { Point lastFrameP = last.frame.getLocationOnScreen(); - frame.setLocation(lastFrameP.x+470, lastFrameP.y); + frame.setLocation(lastFrameP.x + 470, lastFrameP.y); } + } else { + if (Editor.lastOpened != null) { + Point lastEditorP = Editor.lastOpened.getLocationOnScreen(); + Dimension lastEditorD = Editor.lastOpened.getSize(); + frame.setLocation(lastEditorP.x, lastEditorP.y + lastEditorD.height); + } + } + addKeyListener(this); + // if (lastActivePoint != null){ - // frame.setLocation(lastActivePoint.x+500, lastActivePoint.y); + // frame.setLocation(lastActivePoint.x+500, lastActivePoint.y); // } else if (lastWindow != null && lastWindow.isVisible()){ - // Point lastFrameP = lastWindow.frame.getLocationOnScreen(); - // frame.setLocation(lastFrameP.x+500, lastFrameP.y); + // Point lastFrameP = lastWindow.frame.getLocationOnScreen(); + // frame.setLocation(lastFrameP.x+500, lastFrameP.y); // } // lastWindow = this; winStack.push(this); @@ -60,8 +76,16 @@ public void startBR(Process proc) { process = proc; reader = new BufferedReader(new InputStreamReader(proc.getInputStream())); + // isreader = new InputStreamReader(proc.getInputStream()); + outstream = process.getOutputStream(); String line; try { + // while(true){ + // if (isreader.ready()){ + // char[] buf = new char[1000]; + // isreader.read(buf); + // } + // } while ((line = reader.readLine()) != null) { Systemoutprintln(line); } @@ -113,6 +137,14 @@ @Override public void windowClosing(WindowEvent e) { + try { + if (outstream != null) + outstream.close(); + if (instream != null) + instream.close(); + } catch (IOException e1) { + e1.printStackTrace(); + } process.destroy(); winStack.remove(this); setVisible(false); @@ -143,4 +175,60 @@ public void windowDeactivated(WindowEvent e) { } + + @Override + public void keyTyped(KeyEvent e) { + + } + + @Override + public void keyPressed(KeyEvent e) { + if (e.getKeyCode() == 10) { + sendLastLine(); + } + if (e.getKeyCode() > 64 && e.getKeyCode() < 64 + 32) { + if (e.isControlDown()) { + System.out.print("CTRL+"); + } + if (e.isAltDown()) { + System.out.print("ALT+"); + } + if (e.isAltGraphDown()) { + System.out.print("AltGraph+"); + } + if (e.isMetaDown()) { + System.out.print("META+"); + } + System.out.println(KeyEvent.getKeyText(e.getKeyCode())); + } + + if (e.isControlDown() || e.isAltDown() || e.isAltGraphDown() || e.isMetaDown()) { + if (e.getKeyCode() == 67 || e.getKeyCode() == 87) { // CTRL+C or Alt-W + windowClosing(null); + frame.dispose(); + } + } + } + + private void sendLastLine() { + int start; + try { + start = getLineStartOffset(getLineCount() - 1); + int end = getLineEndOffset(getLineCount() - 1); + String lastLineText = getText(start, end - start) + "\n"; + // System.out.println(lastLineText); + + byte[] sbyte = lastLineText.getBytes(); + outstream.write(sbyte); + outstream.flush(); + + } catch (BadLocationException | IOException e) { + e.printStackTrace(); + } + } + + @Override + public void keyReleased(KeyEvent e) { + + } } diff --git a/src/main/java/istlab/KisoJikken/ScriptRunner.java b/src/main/java/istlab/KisoJikken/ScriptRunner.java index 210c830..9083725 100644 --- a/src/main/java/istlab/KisoJikken/ScriptRunner.java +++ b/src/main/java/istlab/KisoJikken/ScriptRunner.java @@ -32,13 +32,20 @@ @Override public void run() { // Run script - System.out.println(scriptfile); + // System.out.println(scriptfile); ProcessBuilder processBuilder = new ProcessBuilder("bash", scriptfile); // ProcessBuilder processBuilder = new ProcessBuilder("pwd"); processBuilder.directory(new File(App.userhome + App.nwpsrc)); - processBuilder.inheritIO(); + // processBuilder.inheritIO(); + JTAConsole con = new JTAConsole("(Exec) " + scriptfile); + con.Systemoutprintln("== 実行開始 =="); + con.Systemoutprintln("> cd ~" + App.nwpsrc); + con.Systemoutprintln("> "+scriptfile); + try { process = processBuilder.start(); + con.startBR(process); + // BufferedReader reader = new BufferedReader(new // InputStreamReader(process.getInputStream())); // String line; @@ -49,7 +56,7 @@ } catch (IOException | InterruptedException e1) { e1.printStackTrace(); } - System.out.println("Exit"); + con.Systemoutprintln("== 終了 =="); thread = null; runB.setText("Run"); }