
import javax.swing.*;
import java.util.*;
import javax.swing.text.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseEvent;
import java.awt.Color;

import javax.swing.JLabel;
import java.awt.BorderLayout;



//public class exonViewer extends JTextArea implements exonViewerInterface
public class exonViewer extends JPanel implements exonViewerInterface
{
	JTextArea jta_ = new JTextArea(5,100);

	public exonViewer()
	{
		//super(10,80);
		JLabel jl = new JLabel("Exon Intervals:");
		jta_.setWrapStyleWord(false);
		jta_.setEditable(false);
		jta_.setSelectionColor(Color.red);
		//jta_.addMouseListener(new SelectionMouseListener());
		//jta_.addMouseMotionListener(new SelectionMouseMotionListener());
		this.setLayout(new BorderLayout());
		this.add(jl, BorderLayout.NORTH);
		this.add(jta_, BorderLayout.CENTER);
	}
	
	public void updateExons(ArrayList exons, int activePoint)
	{
		jta_.grabFocus();
		Exon exon;
		int selectionStart=0;
		int selectionEnd=0;
		Document doc = jta_.getDocument();
		int docLength = doc.getLength();
		StringBuffer buf = new StringBuffer();
		
		List list = (List)exons;
		Collections.sort(exons);
		Object[] lista = exons.toArray();
		int listLength = lista.length;
		
		for (int i=0; i<listLength; i++)
		{
			exon = (Exon)lista[listLength-(1+i)];
			if (exon.isCurrent(activePoint))
			{
				selectionStart = buf.length();
				buf.append("(");
                        	buf.append(exon.getStart());
                        	buf.append(", ");
                        	buf.append(exon.getEnd());
                        	buf.append(")\t\n");
				selectionEnd = buf.length();
			}
			else
			{
				buf.append("(");
				buf.append(exon.getStart());
				buf.append(", ");
				buf.append(exon.getEnd());
				buf.append(")\t\n");
			}
			//buf.append(exon.get);
		}
		jta_.replaceRange(buf.toString(), 0, docLength); 
		jta_.setSelectionStart(selectionStart);
		jta_.setSelectionEnd(selectionEnd);
		jta_.select(selectionStart, selectionEnd);
		jta_.repaint();
	}
/*
	private class SelectionMouseMotionListener extends MouseMotionAdapter {
        	public void mouseDragged(MouseEvent e) {
			System.out.println("selection start = "+getSelectionStart());
			System.out.println("selection end = "+getSelectionEnd());
		}
	}

 	private class SelectionMouseListener extends MouseAdapter {
        	public void mousePressed(MouseEvent e) {
			System.out.println("selection start = "+getSelectionStart());
			System.out.println("selection end = "+getSelectionEnd());
		}
	}
 */ 
}
		
