/* * @(#)Log.java * * This file is part of webCDwriter - Network CD/DVD Writing. * * Copyright (C) 1999-2005 Jörg P. M. Haeger * * webCDwriter is free software. See CDcreator.java for details. * * Jörg Haeger, 10.08.2000 */ import javax.swing.*; /** * An output window with auto scrolldown * * @version 20050826 * @author Jörg P. M. Haeger */ public class Log { private static StringBuffer buf = new StringBuffer(); private static JTextArea textArea = new JTextArea(); private static JScrollPane scrollPane = init(); public static void clear() { SwingUtilities.invokeLater(new Runnable() { public void run() { synchronized(buf) { buf.delete(0, buf.length()); textArea.setText(""); } } }); } public static JComponent getView() { return scrollPane; } private static JScrollPane init() { scrollPane = new JScrollPane(textArea); scrollPane.setHorizontalScrollBarPolicy( JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); return scrollPane; } public static void put(String str) { synchronized(buf) { boolean refresh = buf.length() == 0; buf.append(str); buf.append("\n"); if (!refresh) return; } SwingUtilities.invokeLater(new Runnable() { public void run() { put2(); } }); } private static void put2() { synchronized(buf) { Version.debug("Log.put", "<" + buf + ">"); textArea.append(new String(buf)); // scroll down if the user has not moved the cursor int p = textArea.getCaretPosition(); if (p == textArea.getText().length() - buf.length()) textArea.setCaretPosition(p + buf.length()); buf.delete(0, buf.length()); } } }