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