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