View Javadoc

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