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.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  		infoLabel = new JLabel();
56  		infoLabel.setVisible( false );
57  
58  		caretLabel.setBorder( BorderFactory.createCompoundBorder( BorderFactory.createMatteBorder( 0, 1, 0, 0,
59  				Color.LIGHT_GRAY ), BorderFactory.createMatteBorder( 0, 1, 0, 0, Color.WHITE ) ) );
60  
61  		ButtonBarBuilder builder = new ButtonBarBuilder( this );
62  		builder.addGriddedGrowing( infoLabel );
63  		builder.addStrut( Sizes.pixel( 2 ) );
64  
65  		statusPanel = new JPanel( new BorderLayout() );
66  		statusPanel.setPreferredSize( new Dimension( 60, 16 ) );
67  
68  		statusPanel.setBorder( BorderFactory.createCompoundBorder( BorderFactory.createMatteBorder( 0, 1, 0, 0,
69  				Color.LIGHT_GRAY ), BorderFactory.createMatteBorder( 0, 1, 0, 0, Color.WHITE ) ) );
70  
71  		builder.addFixed( statusPanel );
72  		builder.addFixed( caretLabel );
73  		builder.getPanel();
74  	}
75  
76  	public void addNotify()
77  	{
78  		super.addNotify();
79  
80  		if( target != null )
81  			target.addCaretListener( this );
82  	}
83  
84  	public void removeNotify()
85  	{
86  		super.removeNotify();
87  
88  		if( target != null )
89  			target.removeCaretListener( this );
90  	}
91  
92  	public void caretUpdate( CaretEvent e )
93  	{
94  		try
95  		{
96  			if( target == null )
97  				caretLabel.setText( "" );
98  
99  			int offset = target.getCaretPosition();
100 			int line = target.getLineOfOffset( offset );
101 			int column = offset - target.getLineStartOffset( line );
102 
103 			caretLabel.setText( " " + ( line + 1 ) + " : " + ( column + 1 ) );
104 		}
105 		catch( Exception e1 )
106 		{
107 			SoapUI.logError( e1 );
108 		}
109 	}
110 
111 	public void setTarget( JEditorStatusBarTarget target )
112 	{
113 		if( this.target != null )
114 			this.target.removeCaretListener( this );
115 
116 		this.target = target;
117 		this.target.addCaretListener( this );
118 
119 		caretUpdate( null );
120 	}
121 
122 	public void setInfo( String txt )
123 	{
124 		infoLabel.setText( txt );
125 		infoLabel.setVisible( txt != null );
126 	}
127 
128 	public void setStatusComponent( JComponent statusComponent )
129 	{
130 		statusPanel.removeAll();
131 		statusPanel.add( statusComponent, BorderLayout.CENTER );
132 		statusPanel.revalidate();
133 	}
134 
135 	/***
136 	 * Target for caret-status
137 	 * 
138 	 * @author Ole.Matzura
139 	 */
140 
141 	public interface JEditorStatusBarTarget
142 	{
143 		void addCaretListener( CaretListener listener );
144 
145 		int getCaretPosition();
146 
147 		void removeCaretListener( CaretListener listener );
148 
149 		int getLineStartOffset( int line ) throws Exception;
150 
151 		int getLineOfOffset( int offset ) throws Exception;;
152 	}
153 }