package info.istlab.Zemi01.gitmonitor;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
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.io.UnsupportedEncodingException;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.text.BadLocationException;
public class JTAConsole extends MyRSJTextArea
implements Runnable, WindowListener, KeyListener, MouseListener, ActionListener {
// static JTAConsole lastWindow;
// static Point lastActivePoint;
// static Stack<JTAConsole> winStack = new Stack<JTAConsole>();
JFrame frame;
Process process;
Thread thread;
InputStream instream;
InputStreamReader isreader;
BufferedReader reader;
OutputStream outstream;
// StringBuilder originalContent;
// String mainSrc;
// ProcessBuilder pb;
CommandRunner comRunner;
//左側に表示するコマンド。クリックorハイライトで表示する。
JList<CommandRunner> comList;
// public JTAConsole(ProcessBuilder prob) {
public JTAConsole() {
// comRunner = runner;
// pb = prob;
// if (winStack.size() > 0) {
// JTAConsole last = winStack.peek();
// if (last.isVisible()) {
// Point lastFrameP = last.frame.getLocationOnScreen();
// 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);
addMouseListener(this);
// if (lastActivePoint != null){
// 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);
// }
// lastWindow = this;
// winStack.push(this);
// originalContent = new StringBuilder();
}
public void Systemoutprintln(String s) {
append(s + "\n");
// originalContent.append(s + "\n");
int len = getDocument().getLength();
if (getSelectedText() == null)
setCaretPosition(len);
}
public void startBR(Process proc) {
process = proc;
try {
reader = new BufferedReader(new InputStreamReader(proc.getInputStream(), "UTF-8"));
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
// 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);
}
process.waitFor();
} catch (IOException | InterruptedException e) {
// e.printStackTrace();
System.out.print(e.getClass().getCanonicalName() + " : ");
System.out.println(e.getLocalizedMessage());
}
}
@Override
public void run() {
String line;
try {
while ((line = reader.readLine()) != null) {
Systemoutprintln(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void windowOpened(WindowEvent e) {
}
private void destroyProcess() {
try {
if (outstream != null)
outstream.close();
if (instream != null)
instream.close();
} catch (IOException e1) {
e1.printStackTrace();
}
process.destroyForcibly();
}
@Override
public void windowClosing(WindowEvent e) {
destroyProcess();
// winStack.remove(this);
setVisible(false);
// if (!process.isAlive()) {
// Launcher.allProcs.remove(process);
// }
}
@Override
public void windowClosed(WindowEvent e) {
}
@Override
public void windowIconified(WindowEvent e) {
}
@Override
public void windowDeiconified(WindowEvent e) {
}
@Override
public void windowActivated(WindowEvent e) {
// JTAConsole.lastActivePoint = frame.getLocationOnScreen();
}
@Override
public void windowDeactivated(WindowEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
// if (e.isShiftDown()){
// setText(""); //reset content
// }
if (e.getKeyCode() == 10) {
setText(""); //reset content
comRunner.invoke();
}
if (e.getKeyCode()== KeyEvent.VK_B){
setText(""); //reset content
comRunner.invoke("git branch -avv");
}
if (e.getKeyCode()== KeyEvent.VK_S){
setText(""); //reset content
comRunner.invoke("git status");
}
if (e.getKeyCode()== KeyEvent.VK_R){
setText(""); //reset content
comRunner.invoke("git remote -v");
}
// 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) { // CTRL+C
if (getSelectedText() != null) {
// 文字列をクリップボードにコピーする
Toolkit kit = Toolkit.getDefaultToolkit();
Clipboard clip = kit.getSystemClipboard();
StringSelection ss = new StringSelection(getSelectedText());
clip.setContents(ss, ss);
System.out.println("選択範囲をコピーしました");
} else {
// プロセスのみ終了
destroyProcess();
}
}
if (e.getKeyCode() == 68 || e.getKeyCode() == 87) { // CTRL+D 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();
// originalContent.append("")
} catch (BadLocationException | IOException e) {
System.out.println(e.getLocalizedMessage());
}
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void mouseClicked(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON3) {
// JTAConsolePopup popupMenu = new JTAConsolePopup(this);
// popupMenu.show(this, e.getX(), e.getY());
}
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
/**
* PopupMenuの選択によるコマンド実行
*
* @param e
*/
@Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Copy")) {
Toolkit kit = Toolkit.getDefaultToolkit();
Clipboard clip = kit.getSystemClipboard();
StringSelection ss = new StringSelection(getSelectedText());
clip.setContents(ss, ss);
} else if (e.getActionCommand().equals("Show Diff")) {
} else if (e.getActionCommand().equals("Submit")) {
}
}
public void setMainSrcByFileName(String absolutePath) {
}
}