View Javadoc

1   package com.eviware.soapui.support.components;
2   
3   import java.awt.Component;
4   import java.awt.Container;
5   import java.awt.Dimension;
6   import java.awt.Graphics;
7   import java.awt.Insets;
8   import java.awt.Point;
9   import java.awt.Rectangle;
10  import java.awt.event.MouseEvent;
11  import java.awt.event.MouseListener;
12  
13  import javax.swing.JComponent;
14  import javax.swing.SwingConstants;
15  import javax.swing.SwingUtilities;
16  import javax.swing.border.Border;
17  
18  /***
19   * MySwing: Advanced Swing Utilites Copyright (C) 2005 Santhosh Kumar T <p/>
20   * This library is free software; you can redistribute it and/or modify it under
21   * the terms of the GNU Lesser General Public License as published by the Free
22   * Software Foundation; either version 2.1 of the License, or (at your option)
23   * any later version. <p/> This library is distributed in the hope that it will
24   * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
25   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
26   * General Public License for more details.
27   */
28  
29  public class ComponentTitledBorder implements Border, MouseListener, SwingConstants
30  {
31  	int offset = 5;
32  
33  	Component comp;
34  	JComponent container;
35  	Rectangle rect;
36  	Border border;
37  
38  	public ComponentTitledBorder( Component comp, JComponent container, Border border )
39  	{
40  		this.comp = comp;
41  		this.container = container;
42  		this.border = border;
43  		container.addMouseListener( this );
44  	}
45  
46  	public boolean isBorderOpaque()
47  	{
48  		return true;
49  	}
50  
51  	public void paintBorder( Component c, Graphics g, int x, int y, int width, int height )
52  	{
53  		Insets borderInsets = border.getBorderInsets( c );
54  		Insets insets = getBorderInsets( c );
55  		int temp = ( insets.top - borderInsets.top ) / 2;
56  		border.paintBorder( c, g, x, y + temp, width, height - temp );
57  		Dimension size = comp.getPreferredSize();
58  		rect = new Rectangle( offset, 0, size.width, size.height );
59  		SwingUtilities.paintComponent( g, comp, ( Container ) c, rect );
60  	}
61  
62  	public Insets getBorderInsets( Component c )
63  	{
64  		Dimension size = comp.getPreferredSize();
65  		Insets insets = border.getBorderInsets( c );
66  		insets.top = Math.max( insets.top, size.height );
67  		return insets;
68  	}
69  
70  	private void dispatchEvent( MouseEvent me )
71  	{
72  		if( rect != null && rect.contains( me.getX(), me.getY() ) )
73  		{
74  			Point pt = me.getPoint();
75  			pt.translate( -offset, 0 );
76  			comp.setBounds( rect );
77  			comp.dispatchEvent( new MouseEvent( comp, me.getID(), me.getWhen(), me.getModifiers(), pt.x, pt.y, me
78  						.getClickCount(), me.isPopupTrigger(), me.getButton() ) );
79  			if( !comp.isValid() )
80  				container.repaint();
81  		}
82  	}
83  
84  	public void mouseClicked( MouseEvent me )
85  	{
86  		dispatchEvent( me );
87  	}
88  
89  	public void mouseEntered( MouseEvent me )
90  	{
91  		dispatchEvent( me );
92  	}
93  
94  	public void mouseExited( MouseEvent me )
95  	{
96  		dispatchEvent( me );
97  	}
98  
99  	public void mousePressed( MouseEvent me )
100 	{
101 		dispatchEvent( me );
102 	}
103 
104 	public void mouseReleased( MouseEvent me )
105 	{
106 		dispatchEvent( me );
107 	}
108 }