/*
 * #%L
 * ImageJ software for multidimensional image processing and analysis.
 * %%
 * Copyright (C) 2009 - 2013 Board of Regents of the University of
 * Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
 * Institute of Molecular Cell Biology and Genetics.
 * %%
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 * 
 * The views and conclusions contained in the software and documentation are
 * those of the authors and should not be interpreted as representing official
 * policies, either expressed or implied, of any organization.
 * #L%
 */

package imagej.ui.swing.viewer.text;

import imagej.display.TextDisplay;
import imagej.ui.viewer.DisplayWindow;
import imagej.ui.viewer.text.TextDisplayPanel;

import java.awt.Font;

import javax.swing.JScrollPane;
import javax.swing.JTextArea;

/**
 * This is the display panel for {@link String}s.
 * 
 * @author Lee Kamentsky
 */
public class SwingTextDisplayPanel extends JScrollPane implements
	TextDisplayPanel
{

	private final DisplayWindow window;
	private final TextDisplay display;
	private final JTextArea textArea;

	public SwingTextDisplayPanel(final TextDisplay display,
		final DisplayWindow window)
	{
		this.display = display;
		this.window = window;
		textArea = new JTextArea();
		textArea.setEditable(false);
		textArea.setRows(25);
		textArea.setColumns(84);
		final Font font = new Font(Font.MONOSPACED, Font.PLAIN, 12);
		textArea.setFont(font);
		setViewportView(textArea);
		window.setContent(this);
	}

	// -- TextDisplayPanel methods --

	@Override
	public void append(final String text) {
		display.add(text);
	}

	@Override
	public void clear() {
		display.clear();
	}

	// -- DisplayPanel methods --

	@Override
	public TextDisplay getDisplay() {
		return display;
	}

	@Override
	public DisplayWindow getWindow() {
		return window;
	}

	@Override
	public void redoLayout() {
		// Nothing to layout
	}

	@Override
	public void setLabel(final String s) {
		// The label is not shown.
	}

	@Override
	public void redraw() {
		// The strategy is to compare the lines in the text area against
		// those in the display. We clear the control if we find a mismatch.

		final String currentText = textArea.getText();
		final StringBuffer targetText = new StringBuffer();
		for (final String line : display) {
			targetText.append(line + "\n");
		}
		if (targetText.toString().startsWith(currentText)) {
			if (targetText.length() > currentText.length()) {
				textArea.append(targetText.substring(currentText.length()));
			}
		}
		else {
			textArea.setText(targetText.toString());
		}
		// make sure the last line is always visible
		textArea.setCaretPosition(targetText.length());
	}

}

@@ -1999,14 +1999,73 @@
 awt.
-Font;%0A
+Dimension;%0Aimport java.awt.Font;%0A%0Aimport javax.swing.JEditorPane;
 %0Aimp
@@ -2096,38 +2096,8 @@
 ane;
-%0Aimport javax.swing.JTextArea;
 %0A%0A/*
@@ -2177,16 +2177,41 @@
 mentsky%0A
+ * @author Curtis Rueden%0A
  */%0Apubl
@@ -2382,24 +2382,26 @@
  final J
-TextArea
+EditorPane
  textAre
@@ -2566,16 +2566,18 @@
 ew J
-TextArea
+EditorPane
 ();%0A
@@ -2594,46 +2594,45 @@
 .set
-Editable(false);%0A%09%09textArea.setRows(25
+PreferredSize(new Dimension(600, 500)
 );%0A%09
@@ -2648,18 +2648,22 @@
 .set
-Columns(84
+Editable(false
 );%0A%09
@@ -3476,57 +3476,8 @@
 h.%0A%0A
-%09%09final String currentText = textArea.getText();%0A
 %09%09fi
@@ -3605,20 +3605,36 @@
 ;%0A%09%09%7D%0A%09%09
-if (
+final String text = 
 targetTe
@@ -3650,219 +3650,148 @@
 ng()
-.startsWith(currentText)) %7B%0A%09%09%09if (targetText.length() %3E currentText.length()) %7B%0A%09%09%09%09textArea.append(targetText.substring(currentText.length()));%0A%09%09%09%7D%0A%09%09%7D%0A%09%09else %7B%0A%09%09%09textArea.setText(targetText.toString());%0A%09%09%7D
+;%0A%09%09final boolean html = text.startsWith(%22%3Chtml%3E%22);%0A%09%09textArea.setContentType(html ? %22text/html%22 : %22text/plain%22);%0A%09%09textArea.setText(text);%0A
 %0A%09%09/

use a JEditorPane