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.swing;
14  
15  import java.awt.Component;
16  import java.awt.Insets;
17  import java.awt.Point;
18  import java.awt.Rectangle;
19  import java.awt.dnd.Autoscroll;
20  
21  import javax.swing.JViewport;
22  import javax.swing.SwingUtilities;
23  
24  public class AutoscrollSupport implements Autoscroll
25  {
26  	private static final int AUTOSCROLL_MARGIN = 12;
27  
28  	Component comp;
29  	Insets insets;
30  	Insets scrollUnits;
31  
32  	public AutoscrollSupport( Component comp, Insets insets )
33  	{
34  		this( comp, insets, insets );
35  	}
36  
37  	public AutoscrollSupport( Component comp, Insets insets, Insets scrollUnits )
38  	{
39  		this.comp = comp;
40  		this.insets = insets;
41  		this.scrollUnits = scrollUnits;
42  	}
43  
44  	public AutoscrollSupport( Component comp )
45  	{
46  		this( comp, new Insets( AUTOSCROLL_MARGIN, AUTOSCROLL_MARGIN, AUTOSCROLL_MARGIN, AUTOSCROLL_MARGIN ) );
47  	}
48  
49  	public void autoscroll( Point cursorLoc )
50  	{
51  		JViewport viewport = getViewport();
52  		if( viewport == null )
53  			return;
54  		Point viewPos = viewport.getViewPosition();
55  		int viewHeight = viewport.getExtentSize().height;
56  		int viewWidth = viewport.getExtentSize().width;
57  
58  		// resolve scrolling
59  		if( ( cursorLoc.y - viewPos.y ) < insets.top )
60  		{ // scroll up
61  			viewport.setViewPosition( new Point( viewPos.x, Math.max( viewPos.y - scrollUnits.top, 0 ) ) );
62  		}
63  		else if( ( viewPos.y + viewHeight - cursorLoc.y ) < insets.bottom )
64  		{ // scroll down
65  			viewport.setViewPosition( new Point( viewPos.x, Math.min( viewPos.y + scrollUnits.bottom, comp.getHeight()
66  					- viewHeight ) ) );
67  		}
68  		else if( ( cursorLoc.x - viewPos.x ) < insets.left )
69  		{ // scroll left
70  			viewport.setViewPosition( new Point( Math.max( viewPos.x - scrollUnits.left, 0 ), viewPos.y ) );
71  		}
72  		else if( ( viewPos.x + viewWidth - cursorLoc.x ) < insets.right )
73  		{ // scroll right
74  			viewport.setViewPosition( new Point( Math.min( viewPos.x + scrollUnits.right, comp.getWidth() - viewWidth ),
75  					viewPos.y ) );
76  		}
77  	}
78  
79  	public Insets getAutoscrollInsets()
80  	{
81  		Rectangle raOuter = comp.getBounds();
82  		Rectangle raInner = comp.getParent().getBounds();
83  		return new Insets( raInner.y - raOuter.y + AUTOSCROLL_MARGIN, raInner.x - raOuter.x + comp.getWidth(),
84  				raOuter.height - raInner.height - raInner.y + raOuter.y + AUTOSCROLL_MARGIN, raOuter.width - raInner.width
85  						- raInner.x + raOuter.x + AUTOSCROLL_MARGIN );
86  	}
87  
88  	JViewport getViewport()
89  	{
90  		return ( JViewport )SwingUtilities.getAncestorOfClass( JViewport.class, comp );
91  	}
92  }