/*
 * @(#)Log.java
 *
 * This file is part of webCDwriter - Network CD Writing.
 *
 * Copyright (C) 1999-2003 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 20030304
 * @author Jörg P. M. Haeger
 */
public class Log {
	private static String buf = "";
	private static JTextArea textArea = new JTextArea();
	private static JScrollPane scrollPane = init();

	public static void clear() {
		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) {
			buf = buf + str + "\n";
		}
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				put2();
			}
		});
	}

	private static void put2() {
		synchronized(buf) {
			textArea.append(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 = "";
		}
	}
}
