001 /*
002 * Copyright (C) 2003 Adam Olsen
003 *
004 * This program is free software; you can redistribute it and/or modify it under
005 * the terms of the GNU General Public License as published by the Free Software
006 * Foundation; either version 1, or (at your option) any later version.
007 *
008 * This program is distributed in the hope that it will be useful, but WITHOUT
009 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
010 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
011 * details.
012 *
013 * You should have received a copy of the GNU General Public License along with
014 * this program; if not, write to the Free Software Foundation, Inc., 675 Mass
015 * Ave, Cambridge, MA 02139, USA.
016 */
017
018 package com.valhalla.gui;
019
020 import java.awt.BorderLayout;
021 import java.awt.Font;
022 import java.awt.Point;
023 import java.awt.event.ActionEvent;
024 import java.awt.event.ActionListener;
025
026 import javax.swing.*;
027
028 public class DebugWindow extends JFrame {
029 private JButton ok = new JButton("OK");
030
031 private JButton clear = new JButton("Clear");
032
033 private MJTextArea log = new MJTextArea();
034
035 private JScrollPane pane = new JScrollPane(log);
036
037 public DebugWindow() {
038 super("Debug Window");
039
040 JPanel main = (JPanel) getContentPane();
041 main.setBorder(BorderFactory.createTitledBorder("Debug Window"));
042 main.setLayout(new BorderLayout());
043 log.setEditable(false);
044
045 main.add(pane, BorderLayout.CENTER);
046 JPanel buttons = new JPanel();
047 buttons.setLayout(new BoxLayout(buttons, BoxLayout.X_AXIS));
048 buttons.add(Box.createHorizontalGlue());
049 buttons.add(clear);
050 buttons.add(ok);
051 buttons.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
052 log.setFont(new Font("Default", Font.PLAIN, 9));
053
054 main.add(buttons, BorderLayout.SOUTH);
055
056 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
057
058 ok.addActionListener(new ActionListener() {
059 public void actionPerformed(ActionEvent e) {
060 setVisible(false);
061 }
062 });
063
064 clear.addActionListener(new ActionListener() {
065 public void actionPerformed(ActionEvent e) {
066 log.setText("");
067 }
068 });
069
070 pack();
071 setSize(400, 300);
072 setLocationRelativeTo(null);
073 }
074
075 public void append(String text) {
076 JScrollBar bar = pane.getVerticalScrollBar();
077
078 boolean end = bar.getValue()
079 - (bar.getMaximum() - bar.getModel().getExtent()) >= -16;
080 Point p = pane.getViewport().getViewPosition();
081
082 boolean scrFlag = bar.isVisible();
083 p.y += 50; // just so it's not the first line (might scroll a bit)
084 int pos = log.viewToModel(p);
085
086 if (pos < 0)
087 pos = 0;
088
089 try {
090
091 log.setText(log.getText() + text + "\n");
092
093 if (!end && scrFlag)
094 log.setCaretPosition(pos);
095 else
096 log.setCaretPosition(log.getText().length());
097 pane.repaint();
098 } catch (Exception e) {
099 //com.valhalla.Logger.debug( "ShowHandler.class Exception: " +
100 // e.toString() );
101 }
102 }
103 }