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.support.components;
    14
    15  import java.awt.BorderLayout;
    16  import java.awt.Color;
    17  import java.awt.Dimension;
    18
    19  import javax.swing.BorderFactory;
    20  import javax.swing.JComponent;
    21  import javax.swing.JLabel;
    22  import javax.swing.JPanel;
    23  import javax.swing.event.CaretEvent;
    24  import javax.swing.event.CaretListener;
    25
    26  import com.eviware.soapui.SoapUI;
    27  import com.jgoodies.forms.builder.ButtonBarBuilder;
    28  import com.jgoodies.forms.layout.Sizes;
    29
    30  /***
    31   * A simple status bar for editors
    32   *
    33   * @author Ole.Matzura
    34   */
    35
    36  public class JEditorStatusBar extends JPanel implements CaretListener
    37  {
    38  	private JLabel caretLabel;
    39  	private JLabel infoLabel;
    40  	private JEditorStatusBarTarget target;
    41  	private JPanel statusPanel;
    42
    43  	public JEditorStatusBar()
    44  	{
    45  		this( null );
    46  	}
    47
    48  	public JEditorStatusBar( JEditorStatusBarTarget target )
    49  	{
    50  		this.target = target;
    51
    52  		caretLabel = new JLabel();
    53  		caretLabel.setPreferredSize( new Dimension( 60, 16 ));
    54
    55
    56  		infoLabel = new JLabel();
    57  		infoLabel.setVisible( false );
    58
    59  		caretLabel.setBorder(
    60  				BorderFactory.createCompoundBorder(
    61  						BorderFactory.createMatteBorder( 0, 1, 0, 0, Color.LIGHT_GRAY ),
    62  						BorderFactory.createMatteBorder( 0, 1, 0, 0, Color.WHITE )));
    63
    64
    65  		ButtonBarBuilder builder = new ButtonBarBuilder( this );
    66  		builder.addGriddedGrowing( infoLabel );
    67  		builder.addStrut( Sizes.pixel( 2 ));
    68
    69  		statusPanel = new JPanel( new BorderLayout() );
    70  		statusPanel.setPreferredSize( new Dimension( 60, 16 ));
    71
    72  		statusPanel.setBorder(
    73  				BorderFactory.createCompoundBorder(
    74  						BorderFactory.createMatteBorder( 0, 1, 0, 0, Color.LIGHT_GRAY ),
    75  						BorderFactory.createMatteBorder( 0, 1, 0, 0, Color.WHITE )));
    76
    77  		builder.addFixed( statusPanel );
    78  		builder.addFixed( caretLabel );
    79  		builder.getPanel();
    80  	}
    81
    82  	public void addNotify()
    83  	{
    84  		super.addNotify();
    85
    86  		if( target != null )
    87  			target.addCaretListener( this );
    88  	}
    89
    90  	public void removeNotify()
    91  	{
    92  		super.removeNotify();
    93
    94  		if( target != null )
    95  			target.removeCaretListener( this );
    96  	}
    97
    98  	public void caretUpdate(CaretEvent e)
    99  	{
    100 		try
    101 		{
    102 			if( target == null )
    103 				caretLabel.setText( "" );
    104
    105 			int offset = target.getCaretPosition();
    106 			int line = target.getLineOfOffset( offset );
    107 			int column = offset - target.getLineStartOffset( line );
    108
    109 			caretLabel.setText( " " + (line+1) + " : " + (column+1) );
    110 		}
    111 		catch (Exception e1)
    112 		{
    113 			SoapUI.logError( e1 );
    114 		}
    115 	}
    116
    117 	public void setTarget( JEditorStatusBarTarget target )
    118 	{
    119 		if( this.target != null )
    120 			this.target.removeCaretListener( this );
    121
    122 		this.target = target;
    123 		this.target.addCaretListener( this );
    124
    125 		caretUpdate( null );
    126 	}
    127
    128 	public void setInfo( String txt )
    129 	{
    130 		infoLabel.setText( txt );
    131 		infoLabel.setVisible( txt != null );
    132 	}
    133
    134 	public void setStatusComponent( JComponent statusComponent )
    135 	{
    136 		statusPanel.removeAll();
    137 		statusPanel.add( statusComponent, BorderLayout.CENTER );
    138 		statusPanel.revalidate();
    139 	}
    140
    141 	/***
    142 	 * Target for caret-status
    143 	 *
    144 	 * @author Ole.Matzura
    145 	 */
    146
    147 	public interface JEditorStatusBarTarget
    148 	{
    149 		void addCaretListener(CaretListener listener);
    150
    151 		int getCaretPosition();
    152
    153 		void removeCaretListener(CaretListener listener);
    154
    155 		int getLineStartOffset(int line) throws Exception;
    156
    157 		int getLineOfOffset(int offset) throws Exception;;
    158 	}
    159 }