View Javadoc

    1   /*
    2    *  soapUI, copyright (C) 2004-2007 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(
    59  			BorderFactory.createMatteBorder( 0, 0, 0, 1, Color.LIGHT_GRAY ),
    60  			BorderFactory.createEmptyBorder(2, 2, 0, 1)));
    61  		add( lineNumberTable, BorderLayout.CENTER );
    62
    63  		lineNumberTable.getColumnModel().getColumn( 0 ).setCellRenderer( new LineNumberCellRenderer() );
    64  		lineNumberTable.getSelectionModel().addListSelectionListener( new ListSelectionListener() {
    65
    66  			public void valueChanged( ListSelectionEvent e )
    67  			{
    68  				int row = lineNumberTable.getSelectedRow();
    69  				if( row != -1 )
    70  				{
    71  					LineNumbersPanel.this.editArea.setCaretPosition( LineNumbersPanel.this.editArea.getLineStartOffset( row ));
    72  					LineNumbersPanel.this.editArea.requestFocusInWindow();
    73  				}
    74  			}} );
    75  	}
    76
    77  	private class LineNumberCellRenderer extends DefaultTableCellRenderer
    78  	{
    79  		public LineNumberCellRenderer()
    80  		{
    81  			super();
    82
    83  			setForeground( Color.DARK_GRAY );
    84  			setHorizontalAlignment( SwingConstants.RIGHT );
    85  		}
    86
    87  		@Override
    88  		public Component getTableCellRendererComponent( JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column )
    89  		{
    90  			setValue(value);
    91  			return this;
    92  		}
    93  	}
    94
    95  	private class LineNumberTableModel extends AbstractTableModel
    96  	{
    97  		private int lastLineCount;
    98
    99  		public LineNumberTableModel()
    100 		{
    101 			editArea.getDocument().addDocumentListener( new DocumentListenerAdapter() {
    102
    103 				@Override
    104 				public void update( Document document )
    105 				{
    106 					if( lastLineCount != editArea.getLineCount())
    107 						fireTableDataChanged();
    108 				}} );
    109 		}
    110
    111 		public int getColumnCount()
    112 		{
    113 			return 1;
    114 		}
    115
    116 		public int getRowCount()
    117 		{
    118 			lastLineCount = editArea.getLineCount();
    119 			return lastLineCount;
    120 		}
    121
    122 		public Object getValueAt( int rowIndex, int columnIndex )
    123 		{
    124 			return String.valueOf( rowIndex+1 );
    125 		}
    126 	}
    127 }