View Javadoc

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