View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2009 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of version 2.1 of the GNU Lesser General Public License as published by 
6    *  the Free Software Foundation.
7    *
8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
10   *  See the GNU Lesser General Public License for more details at gnu.org.
11   */
12  
13  package com.eviware.soapui.impl.wsdl.panels.teststeps.support;
14  
15  import java.awt.BorderLayout;
16  import java.awt.Color;
17  import java.awt.Component;
18  import java.awt.Dimension;
19  
20  import javax.swing.BorderFactory;
21  import javax.swing.JPanel;
22  import javax.swing.JTable;
23  import javax.swing.ListSelectionModel;
24  import javax.swing.SwingConstants;
25  import javax.swing.event.ListSelectionEvent;
26  import javax.swing.event.ListSelectionListener;
27  import javax.swing.table.AbstractTableModel;
28  import javax.swing.table.DefaultTableCellRenderer;
29  import javax.swing.text.Document;
30  
31  import com.eviware.soapui.StandaloneSoapUICore;
32  import com.eviware.soapui.support.DocumentListenerAdapter;
33  import com.eviware.soapui.support.xml.JXEditTextArea;
34  
35  /***
36   * Panel for displaying line-numbers next to a JXEditTextArea
37   * 
38   * @author ole.matzura
39   */
40  
41  public class LineNumbersPanel extends JPanel
42  {
43  	private JXEditTextArea editArea;
44  	private JTable lineNumberTable;
45  
46  	public LineNumbersPanel( JXEditTextArea editArea )
47  	{
48  		super( new BorderLayout() );
49  		this.editArea = editArea;
50  
51  		lineNumberTable = new JTable( new LineNumberTableModel() );
52  		lineNumberTable.setBackground( StandaloneSoapUICore.SoapUITheme.BACKGROUND_COLOR );
53  		lineNumberTable.setRowHeight( editArea.getLineHeight() );
54  		lineNumberTable.getColumnModel().getColumn( 0 ).setWidth( 30 );
55  		lineNumberTable.setPreferredSize( new Dimension( 30, 0 ) );
56  		lineNumberTable.setSelectionMode( ListSelectionModel.SINGLE_SELECTION );
57  
58  		setBorder( BorderFactory.createCompoundBorder( BorderFactory.createMatteBorder( 0, 0, 0, 1, Color.LIGHT_GRAY ),
59  				BorderFactory.createEmptyBorder( 2, 2, 0, 1 ) ) );
60  		add( lineNumberTable, BorderLayout.CENTER );
61  
62  		lineNumberTable.getColumnModel().getColumn( 0 ).setCellRenderer( new LineNumberCellRenderer() );
63  		lineNumberTable.getSelectionModel().addListSelectionListener( new ListSelectionListener()
64  		{
65  
66  			public void valueChanged( ListSelectionEvent e )
67  			{
68  				int row = lineNumberTable.getSelectedRow();
69  				if( row != -1 )
70  				{
71  					LineNumbersPanel.this.editArea
72  							.setCaretPosition( LineNumbersPanel.this.editArea.getLineStartOffset( row ) );
73  					LineNumbersPanel.this.editArea.requestFocusInWindow();
74  				}
75  			}
76  		} );
77  	}
78  
79  	private class LineNumberCellRenderer extends DefaultTableCellRenderer
80  	{
81  		public LineNumberCellRenderer()
82  		{
83  			super();
84  
85  			setForeground( Color.DARK_GRAY );
86  			setHorizontalAlignment( SwingConstants.RIGHT );
87  		}
88  
89  		@Override
90  		public Component getTableCellRendererComponent( JTable table, Object value, boolean isSelected, boolean hasFocus,
91  				int row, int column )
92  		{
93  			setValue( value );
94  			return this;
95  		}
96  	}
97  
98  	private class LineNumberTableModel extends AbstractTableModel
99  	{
100 		private int lastLineCount;
101 
102 		public LineNumberTableModel()
103 		{
104 			editArea.getDocument().addDocumentListener( new DocumentListenerAdapter()
105 			{
106 
107 				@Override
108 				public void update( Document document )
109 				{
110 					if( lastLineCount != editArea.getLineCount() )
111 						fireTableDataChanged();
112 				}
113 			} );
114 		}
115 
116 		public int getColumnCount()
117 		{
118 			return 1;
119 		}
120 
121 		public int getRowCount()
122 		{
123 			lastLineCount = editArea.getLineCount();
124 			return lastLineCount;
125 		}
126 
127 		public Object getValueAt( int rowIndex, int columnIndex )
128 		{
129 			return String.valueOf( rowIndex + 1 );
130 		}
131 	}
132 }