1 7 34 35 package com.sun.tools.example.debug.gui; 36 37 import java.awt.*; 38 import java.awt.event.*; 39 import javax.swing.*; 40 import javax.swing.border.*; 41 42 public class TypeScript extends JPanel { 43 44 private JTextArea history; 45 private JTextField entry; 46 47 private JLabel promptLabel; 48 49 private JScrollBar historyVScrollBar; 50 private JScrollBar historyHScrollBar; 51 52 private boolean echoInput = false; 53 private boolean nlPending = false; 54 55 private static String newline = System.getProperty("line.separator"); 56 57 public TypeScript(String prompt) { 58 this(prompt, true); 59 } 60 61 public TypeScript(String prompt, boolean echoInput) { 62 this.echoInput = echoInput; 63 64 setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); 65 67 history = new JTextArea(0, 0); 68 history.setEditable(false); 69 JScrollPane scroller = new JScrollPane(history); 70 historyVScrollBar = scroller.getVerticalScrollBar(); 71 historyHScrollBar = scroller.getHorizontalScrollBar(); 72 73 add(scroller); 74 75 JPanel cmdLine = new JPanel(); 76 cmdLine.setLayout(new BoxLayout(cmdLine, BoxLayout.X_AXIS)); 77 79 promptLabel = new JLabel(prompt + " "); 80 cmdLine.add(promptLabel); 81 entry = new JTextField(); 82 entry.setMaximumSize(new Dimension(1000, 20)); 84 cmdLine.add(entry); 85 add(cmdLine); 86 } 87 88 94 95 public void setPrompt(String prompt) { 96 promptLabel.setText(prompt + " "); 97 } 98 99 public void append(String text) { 100 history.append(text); 101 historyVScrollBar.setValue(historyVScrollBar.getMaximum()); 102 historyHScrollBar.setValue(historyHScrollBar.getMinimum()); 103 } 104 105 public void newline() { 106 history.append(newline); 107 historyVScrollBar.setValue(historyVScrollBar.getMaximum()); 108 historyHScrollBar.setValue(historyHScrollBar.getMinimum()); 109 } 110 111 public void flush() {} 112 113 public void addActionListener(ActionListener a) { 114 entry.addActionListener(a); 115 } 116 117 public void removeActionListener(ActionListener a) { 118 entry.removeActionListener(a); 119 } 120 121 public String readln() { 122 String text = entry.getText(); 123 entry.setText(""); 124 if (echoInput) { 125 history.append(">>>"); 126 history.append(text); 127 history.append(newline); 128 historyVScrollBar.setValue(historyVScrollBar.getMaximum()); 129 historyHScrollBar.setValue(historyHScrollBar.getMinimum()); 130 } 131 return text; 132 } 133 } 134 | Popular Tags |