DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

SmartTextArea

12.22.2005
| 2010 views |
  • submit to reddit
        
/**
 *SmartArea.java is a somehow skilled java text component.
 *Pluggable into any swing application.
 *I did not made the awt compatibility tests yet.
 *if anyone improves or makes changes over this 
 *please send me a copy to: fatihkeles@gmail.com
 *It can show the text with line numbers, and select entire 
 *row by clicking number pane on left;
 *can set a point to indicate current caret position
 */
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.*;
/*
 *@author fkeles
 *@version 0.1 Key listener for numbering & caret listener used for display and selction
 *@version 0.2 key listener replaced with document listener
 *
 */

public class SmartTextArea extends JPanel{
	private JTextArea accessibleTextComponent;
	private NumberPane numberPane;
	private JScrollPane scroller;
	private JPanel rootPane,contentPane;
	private Point caretPosition;
	
	
	public SmartTextArea(){
		contentPane=new JPanel(new BorderLayout());
		scroller=new JScrollPane(contentPane);
		accessibleTextComponent=new JTextArea();
		numberPane=new NumberPane();
		rootPane=this;
		caretPosition=new Point();
		super.setLayout(new GridLayout(1,1));
		
		contentPane.add(accessibleTextComponent,BorderLayout.CENTER);
		contentPane.add(numberPane,BorderLayout.WEST);
		
		rootPane.add(scroller);
	}
	public SmartTextArea(Point caretReports){
		this();
		caretPosition=caretReports;
	}
	public SmartTextArea(String text){
		this();
		setText(text);
	}
	
	public JTextArea getAccessibleTextComponent(){
		return accessibleTextComponent;
	}
	public void setLayout(LayoutManager lm){
		//super.setLayout(new BorderLayout());//never let another layout
	}
	public String getText(){
		return accessibleTextComponent.getText();
	}
	public void setText(String text){
		accessibleTextComponent.setText(text);
	}
	public int getRowCount(){
		return accessibleTextComponent.getRows();
	}

	public class NumberPane extends JPanel implements DocumentListener,MouseListener,CaretListener{
		private int width=20,lineCount=0,d_height=0;
        private Color foregroundColor,backgroundColor;
        private Color defaultForegroundColor=new Color(128,0,0);
        private Color defaultBackgroundColor;
        private Point selectionIndicater;
        private int selection=-1;
        
        public NumberPane(){
        	foregroundColor=defaultForegroundColor;
        	setPreferredSize(new Dimension(width,-1));
        	addMouseListener(this);
        	accessibleTextComponent.getDocument().addDocumentListener(this);
        	accessibleTextComponent.addCaretListener(this);
        }
        
		public void insertUpdate(DocumentEvent de){repaint();}
		public void removeUpdate(DocumentEvent de){repaint();}
		public void changedUpdate(DocumentEvent de){repaint();}
		
		public void mouseClicked(MouseEvent me){}
		public void mouseReleased(MouseEvent me){}
		public void mouseEntered(MouseEvent me){}
		public void mouseExited(MouseEvent me){}
		public void mousePressed(MouseEvent me){
			if(SwingUtilities.isLeftMouseButton(me)){//only left click
				selection=(me.getY())/d_height;
				//System.out.println(selection);
				if(selection>=0&&selection<lineCount){
					select(selection);
					selection=-1;
				}
			}
		}
		
		public void caretUpdate(CaretEvent ce){
			try{
        		int l=accessibleTextComponent.getLineOfOffset(ce.getDot());
        		int c=accessibleTextComponent.getLineStartOffset(l);
        		l+=1;
        		c=ce.getDot()-c;
        		caretPosition.x=l;
        		caretPosition.y=c;
        	}catch(Exception e){}
		}
		
		public void select(int i){
			try{
				int s1=accessibleTextComponent.getLineStartOffset(i);
				int s2=accessibleTextComponent.getLineEndOffset(i);
				accessibleTextComponent.setCaretPosition(s1);
				accessibleTextComponent.moveCaretPosition(s2);
				//accessibleTextComponent.requestFocus();
				//source.select(s1,s2);
			}catch(Exception r){
				System.out.println(r);
			}
		}
				
		public void paint(Graphics g){
			super.paint(g);
			Color old=g.getColor();
            g.setColor(foregroundColor);
            lineCount=accessibleTextComponent.getLineCount();
            FontMetrics fm=accessibleTextComponent.getFontMetrics(accessibleTextComponent.getFont());
            d_height=fm.getHeight();
            int pwidth=fm.stringWidth(""+(lineCount+1))+4;
            
            if(pwidth-width>=0){
                width+=10;
                setPreferredSize(new Dimension(width,-1));
            }else{
            	if(pwidth-width<-10){
            		width-=10;
                	setPreferredSize(new Dimension(width,-1));
            	}
            }
            for(int i=0;i<lineCount+1;i++){
                g.drawString((i+1)+"",2,i*d_height+d_height-4);
            }
            g.setColor(old);
		}
		
	}



	public static void main(String[] a){
		JFrame f=new JFrame();
		f.getContentPane().setLayout(new GridLayout(1,1));
		f.getContentPane().add(new SmartTextArea());
		f.setSize(400,400);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.setVisible(true);
	}

}