1 /* 2 * Copyright (c) 1998, 2011, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 /* 27 * This source code is provided to illustrate the usage of a given feature 28 * or technique and has been deliberately simplified. Additional steps 29 * required for a production-quality application, such as security checks, 30 * input validation and proper error handling, might not be present in 31 * this sample code. 32 */ 33 34 35 package com.sun.tools.example.debug.gui; 36 37 import java.awt.*; 38 import java.awt.event.*; 39 import javax.swing.*; 40 41 public class TypeScript extends JPanel { 42 43 private static final long serialVersionUID = -983704841363534885L; 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 54 private static String newline = System.getProperty("line.separator"); 55 56 public TypeScript(String prompt) { 57 this(prompt, true); 58 } 59 60 public TypeScript(String prompt, boolean echoInput) { 61 this.echoInput = echoInput; 62 63 setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); 64 //setBorder(new EmptyBorder(5, 5, 5, 5)); 65 66 history = new JTextArea(0, 0); 67 history.setEditable(false); 68 JScrollPane scroller = new JScrollPane(history); 69 historyVScrollBar = scroller.getVerticalScrollBar(); 70 historyHScrollBar = scroller.getHorizontalScrollBar(); 71 72 add(scroller); 73 74 JPanel cmdLine = new JPanel(); 75 cmdLine.setLayout(new BoxLayout(cmdLine, BoxLayout.X_AXIS)); 76 //cmdLine.setBorder(new EmptyBorder(5, 5, 0, 0)); 77 78 promptLabel = new JLabel(prompt + " "); 79 cmdLine.add(promptLabel); 80 entry = new JTextField(); 81 //### Swing bug workaround. 82 entry.setMaximumSize(new Dimension(1000, 20)); 83 cmdLine.add(entry); 84 add(cmdLine); 85 } 86 87 /****** 88 public void setFont(Font f) { 89 entry.setFont(f); 90 history.setFont(f); 91 } 92 ******/ 93 94 public void setPrompt(String prompt) { 95 promptLabel.setText(prompt + " "); 96 } 97 98 public void append(String text) { 99 history.append(text); 100 historyVScrollBar.setValue(historyVScrollBar.getMaximum()); 101 historyHScrollBar.setValue(historyHScrollBar.getMinimum()); 102 } 103 104 public void newline() { 105 history.append(newline); 106 historyVScrollBar.setValue(historyVScrollBar.getMaximum()); 107 historyHScrollBar.setValue(historyHScrollBar.getMinimum()); 108 } 109 110 public void flush() {} 111 112 public void addActionListener(ActionListener a) { 113 entry.addActionListener(a); 114 } 115 116 public void removeActionListener(ActionListener a) { 117 entry.removeActionListener(a); 118 } 119 120 public String readln() { 121 String text = entry.getText(); 122 entry.setText(""); 123 if (echoInput) { 124 history.append(">>>"); 125 history.append(text); 126 history.append(newline); 127 historyVScrollBar.setValue(historyVScrollBar.getMaximum()); 128 historyHScrollBar.setValue(historyHScrollBar.getMinimum()); 129 } 130 return text; 131 } 132 }