nrubin29

Untitled

Aug 27th, 2013
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.39 KB | None | 0 0
  1. package me.nrubin29.terminal;
  2.  
  3. import javax.swing.*;
  4. import javax.swing.text.*;
  5. import java.awt.*;
  6. import java.awt.event.KeyEvent;
  7. import java.awt.event.KeyListener;
  8. import java.io.*;
  9. import java.util.ArrayList;
  10.  
  11. public class Terminal extends JFrame {
  12.  
  13.     private JTextPane area = new JTextPane();
  14.     private JTextField input = new JTextField("Input");
  15.  
  16.     private SimpleAttributeSet inputSAS = new SimpleAttributeSet(), output = new SimpleAttributeSet(), error = new SimpleAttributeSet();
  17.  
  18.     public Terminal() throws IOException {
  19.         super("Terminal");
  20.  
  21.         getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
  22.  
  23.         StyleConstants.setForeground(inputSAS, Color.GREEN);
  24.         StyleConstants.setBackground(inputSAS, Color.BLACK);
  25.  
  26.         StyleConstants.setForeground(output, Color.WHITE);
  27.         StyleConstants.setBackground(output, Color.BLACK);
  28.  
  29.         StyleConstants.setForeground(error, Color.RED);
  30.         StyleConstants.setBackground(error, Color.BLACK);
  31.  
  32.         final Process bash = new ProcessBuilder("bash").start();
  33.  
  34.         input.addKeyListener(new KeyListener() {
  35.             public void keyPressed(KeyEvent e) {
  36.                 if (e.getKeyCode() == KeyEvent.VK_ENTER) {
  37.                     try {
  38.                         String command = input.getText();
  39.                         if (command.equals("")) return;
  40.  
  41.                         input.setText("");
  42.                         input.setEditable(false);
  43.  
  44.                         write(inputSAS, command);
  45.  
  46.                         OutputStreamWriter outputStreamWriter = new OutputStreamWriter(bash.getOutputStream());
  47.                         outputStreamWriter.write(command);
  48.  
  49.                         bash.waitFor();
  50.  
  51.                         writeStream(bash.getErrorStream());
  52.                         writeStream(bash.getInputStream());
  53.  
  54.                         input.setEditable(true);
  55.                     } catch (Exception ex) {
  56.                         error(ex);
  57.                     }
  58.                 }
  59.             }
  60.  
  61.             public void keyTyped(KeyEvent e) {}
  62.             public void keyReleased(KeyEvent e) {}
  63.         });
  64.  
  65.         area.setBackground(Color.black);
  66.         area.setCaretColor(Color.green);
  67.         area.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 14));
  68.         area.setEditable(false);
  69.  
  70.         JScrollPane pane = new JScrollPane(area);
  71.         pane.setBorder(BorderFactory.createLineBorder(Color.GREEN));
  72.         pane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
  73.         pane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
  74.         pane.setPreferredSize(new Dimension(640, 460));
  75.  
  76.         input.setBackground(Color.black);
  77.         input.setForeground(Color.green);
  78.         input.setCaretColor(Color.green);
  79.         input.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 14));
  80.         input.setBorder(BorderFactory.createLineBorder(Color.GREEN));
  81.  
  82.         add(pane);
  83.         add(input);
  84.  
  85.         Dimension DIM = new Dimension(640, 480);
  86.         setPreferredSize(DIM);
  87.         setSize(DIM);
  88.         setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  89.         setLocationRelativeTo(null);
  90.         setResizable(true);
  91.         pack();
  92.         setVisible(true);
  93.  
  94.         input.requestFocus();
  95.     }
  96.  
  97.     public static void main(String[] args) throws IOException {
  98.         new Terminal();
  99.     }
  100.  
  101.     private void write(SimpleAttributeSet attributeSet, String... lines) {
  102.         try {
  103.             for (String line : lines) area.getStyledDocument().insertString(area.getStyledDocument().getLength(), "\n" + line, attributeSet);
  104.             area.getStyledDocument().insertString(area.getStyledDocument().getLength(), "\n", attributeSet);
  105.         }
  106.         catch (Exception e) { error(e); }
  107.     }
  108.  
  109.     private void error(Exception e) {
  110.         write(error, "An error has occured: " + e.getLocalizedMessage());
  111.         e.printStackTrace(); //TODO: temp.
  112.     }
  113.  
  114.     private void writeStream(InputStream s) {
  115.         try {
  116.             BufferedReader reader = new BufferedReader(new InputStreamReader(s));
  117.  
  118.             ArrayList<String> strs = new ArrayList<String>();
  119.  
  120.             while(reader.ready()) strs.add(reader.readLine());
  121.  
  122.             write(output, strs.toArray(new String[strs.size()]));
  123.  
  124.             s.close();
  125.         }
  126.         catch (Exception e) { error(e); }
  127.     }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment