package leaf.ui;

import java.beans.PropertyChangeListener;

import javax.swing.JComponent;
import javax.swing.JTextArea;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;

public class LeafTextArea extends JTextArea
{

	private static final long serialVersionUID = 1L;
	
	private static final String PROPERTY = "CHANGED";
	
	private final ChangeThread thread = new ChangeThread();
	
	private boolean isRunning = true;
	
	public LeafTextArea( PropertyChangeListener parent )
	{
		this.addPropertyChangeListener( PROPERTY, parent );
		this.addCaretListener( new CaretListener()
		{
			@Override
			public void caretUpdate( CaretEvent e )
			{
				firePropertyChange( PROPERTY, false, true );
			}
		} );
	}
	
	@Override
	public void setEditable( boolean b )
	{
		super.setEditable( b );
		if( b )
		{
			startThread();
		}
		else
		{
			stopThread();
		}
	}
	
	private void startThread()
	{
		isRunning = true;
		thread.start();
	}
	
	private void stopThread()
	{
		isRunning = false;
	}
	
	private class ChangeThread extends Thread
	{
		@Override
		public synchronized void start()
		{
			while( isRunning )
			{
				
			}
		}
	}
	
}
