home *** CD-ROM | disk | FTP | other *** search
/ CD Ware Multimedia 1999 February / CDW0299.iso / Demos / Cafe / Source.bin / ScrollingPanel.java < prev    next >
Encoding:
Java Source  |  1998-03-18  |  13.8 KB  |  449 lines

  1. package symantec.itools.awt;
  2.  
  3. import java.awt.Dimension;
  4. import java.awt.Point;
  5. import java.awt.Component;
  6. import java.awt.Adjustable;
  7.  
  8. //     02/15/97    RKM    Changed add to call setComponent
  9. //                    Changed setComponent to add the component before the VBar, HBar, & cornerRect
  10. //                    Removed bOsFlag and added isSolaris to OS
  11. //                    Added handling of correct scrollbar width for the Mac
  12. //  05/08/97    CAR Changed handleEvent to check that event target is either the HBar OR the VBar
  13. //                  before executing a scroll.  This fixed the bug where a scroll event that was
  14. //                  not targeted at the VBar would cause the VBar logic to execute.
  15. //  05/11/97    CAR Changed setComponent to not call super.removeAll and addInternalComponents.
  16. //                  Replaced all calls to repaint with calls to placeComponents (except in reshape).
  17. //                  Changed visibility of placeComponents from default to private.
  18. //  05/16/97    CAR Changed scrollVerticalAbsolute and scrollHorizontalAbsolute to account for
  19. //                  the visible page size
  20. //  05/19/97    CAR Changed reshape to call placeComponents
  21. //  05/20/97    CAR Added vendor/version check to placeComponents, scrollDown/Right/PageDown/PageRight
  22. //     07/15/97    RKM    Changed call to super.setLayout so it would bypass our override
  23. //     07/19/97    RKM    Hide components immediately after creation, instead of waiting to post add (eliminate flicker)
  24. //                    Removed hard coded scrollbar size (called getPreferredSize())
  25. //                    Removed add overrides and added addImpl (this is the 1.1 way of doing this)
  26. //                    Changed removeAll to not call super, but remove the one component that could be there
  27. //  07/25/97    RKM Added getScrollPosition a la ScrollPane
  28. //  07/29/97    RKM Made bSun1_1 static
  29. //  07/30/97    CAR marked fields transient as needed
  30. //  08/21/97    CAR Ripped out guts of ScrollingPanel, now extend directly from java.awt.ScrollPane
  31. //    08/30/97    LAB    Deprecated class (Addresses Mac Bug #7651).  Updated version to 1.1.
  32. //    09/04/97    LAB    Backed out deprecation.  Interactions are more useful than baseclass.
  33.  
  34. /**
  35.  * Similar to java.awt.ScrollPane, but with better Interaction Wizard support.
  36.  * <p>
  37.  * The ScrollingPanel is a panel with automatic scroll bars.
  38.  * It may contain only one component/container/panel/etc.
  39.  * It automatically tracks the size of this (usually large) component and
  40.  * provides scroll bars so the entire component may be viewed within a
  41.  * panel of limited size.
  42.  * Typically, the added component would be a panel (or some other container)
  43.  * which would then contain a variety of other components.
  44.  *
  45.  * @see java.awt.ScrollPane
  46.  * @version 1.1, August 30, 1997
  47.  * @author Symantec
  48.  */
  49. public class ScrollingPanel extends java.awt.ScrollPane
  50. {
  51.     /**
  52.      * Constructs a default ScrollingPanel.
  53.      * The panel is initialized with a null component, zero
  54.      * minimum height and zero minimum width.
  55.      */
  56.     public ScrollingPanel()
  57.     {
  58.         super();
  59.     }
  60.  
  61.     /**
  62.      * Constructs a new ScrollingPanel initialized with the
  63.      * specified scrollbarDisplayPolicy.
  64.      * @param scrollbarDisplayPolicy policy for when scrollbars should be shown
  65.      */
  66.     public ScrollingPanel(int scrollbarDisplayPolicy)
  67.     {
  68.         super(scrollbarDisplayPolicy);
  69.     }
  70.  
  71.     /**
  72.      * Constructs a new ScrollingPanel initialized with the
  73.      * specified component, minimum height and minimum width.
  74.      * @param component the component (usually a Panel) to be
  75.      * scrolled
  76.      * @param minWidth the value to be used for the minimumSize()
  77.      * width of the ScrollingPanel
  78.      * @param minHeight the value to be used for the minimumSize()
  79.      * height of the ScrollingPanel
  80.      */
  81.     public ScrollingPanel(Component component, int minWidth, int minHeight)
  82.     {
  83.         super();
  84.         addImpl(component, null, -1);
  85.         setSize(minWidth, minHeight);
  86.     }
  87.  
  88.     /**
  89.      * Sets the value to be used for the minimumSize() width
  90.      * of the ScrollingPanel.
  91.      * @param minWidth the value to be used for the minimumSize()
  92.      * width of the ScrollingPanel
  93.      * @see #getMinimumWidth
  94.      */
  95.     public void setMinimumWidth(int minWidth)
  96.     {
  97.         this.width = minWidth;
  98.     }
  99.  
  100.     /**
  101.      * Gets the current value used for the minimumSize()
  102.      * width of the ScrollingPanel.
  103.      * @return the current minimum width value
  104.      */
  105.     public int getMinimumWidth()
  106.     {
  107.         return this.width;
  108.     }
  109.  
  110.     /**
  111.      * Sets the value to be used for the minimumSize()
  112.      * height of the ScrollingPanel.
  113.      * @param minHeight the value to be used for the minimumSize()
  114.      * height of the ScrollingPanel
  115.      * @see #getMinimumHeight
  116.      */
  117.     public void setMinimumHeight(int minHeight)
  118.     {
  119.         this.height = minHeight;
  120.     }
  121.  
  122.     /**
  123.      * Gets the value used for the minimumSize() height
  124.      * of the ScrollingPanel.
  125.      * @return current minimum height value
  126.      * @see #setMinimumHeight
  127.      */
  128.     public int getMinimumHeight()
  129.     {
  130.         return this.height;
  131.     }
  132.  
  133.     /**
  134.      * @deprecated
  135.      * removed functionality
  136.      */
  137.     public void setVerticalGap(int gapPixels)
  138.     {
  139.     }
  140.  
  141.     /**
  142.      * @deprecated
  143.      * removed functionality
  144.      */
  145.     public int getVerticalGap()
  146.     {
  147.         return 0;
  148.     }
  149.  
  150.     /**
  151.      * @deprecated
  152.      * removed functionality
  153.      */
  154.     public void setHorizontalGap(int gapPixels)
  155.     {
  156.     }
  157.  
  158.     /**
  159.      * @deprecated
  160.      * removed functionality
  161.      */
  162.     public int getHorizontalGap()
  163.     {
  164.         return 0;
  165.     }
  166.  
  167.     /**
  168.      * @deprecated
  169.      * replaced by constructor which takes a scrollbarDisplayPolicy
  170.      */
  171.     public void setShowVerticalScroll(boolean cond)
  172.     {
  173.     }
  174.  
  175.     /**
  176.      * @deprecated
  177.      * replaced by getScrollbarDisplayPolicy
  178.      */
  179.     public boolean getShowVerticalScroll()
  180.     {
  181.         int i = getScrollbarDisplayPolicy();
  182.         if (i == SCROLLBARS_ALWAYS || i == SCROLLBARS_AS_NEEDED)
  183.             return true;
  184.         else
  185.             return false;
  186.     }
  187.  
  188.     /**
  189.      * @deprecated
  190.      * replaced by constructor which takes a scrollbarDisplayPolicy
  191.      */
  192.     public void setShowHorizontalScroll(boolean cond)
  193.     {
  194.     }
  195.  
  196.     /**
  197.      * @deprecated
  198.      * replaced by getScrollbarDisplayPolicy
  199.      */
  200.     public boolean getShowHorizontalScroll()
  201.     {
  202.         int i = getScrollbarDisplayPolicy();
  203.         if (i == SCROLLBARS_ALWAYS || i == SCROLLBARS_AS_NEEDED)
  204.             return true;
  205.         else
  206.             return false;
  207.     }
  208.  
  209.     /**
  210.      * @deprecated
  211.      * replaced by Adjustable.setUnitIncrement
  212.      */
  213.     public void setScrollLineIncrement(int scrollLineIncrement)
  214.     {
  215.         getHAdjustable().setUnitIncrement(scrollLineIncrement);
  216.         getVAdjustable().setUnitIncrement(scrollLineIncrement);
  217.     }
  218.  
  219.     /**
  220.      * @deprecated
  221.      * replaced by Adjustable.getUnitIncrement
  222.      */
  223.     public int getScrollLineIncrement()
  224.     {
  225.         return Math.max(getHAdjustable().getUnitIncrement(),
  226.                         getVAdjustable().getUnitIncrement());
  227.     }
  228.  
  229.     /**
  230.      * Sets the component in this ScrollingPanel. This is the component
  231.      * that gets scrolled. The ScrollingPanel can only contain one component.
  232.      * Any previous component will be removed before the new one is added.
  233.      * @param comp the component to add
  234.      * @see #getComponent
  235.      */
  236.     public void setComponent(Component comp)
  237.     {
  238.         addImpl(comp, null, -1);
  239.     }
  240.  
  241.     /**
  242.      * Gets the current component in the ScrollingPanel.
  243.      * This is the component that gets scrolled.
  244.      * @return the current component in the ScrollingPanel
  245.      * @see #setComponent
  246.      */
  247.     public Component getComponent()
  248.     {
  249.         if (getComponentCount() > 0) {
  250.             return getComponent(0);
  251.         }
  252.         else
  253.             return null;
  254.     }
  255.  
  256.     /**
  257.      * Scrolls up by the number of pixels specified in the method setScrollLineIncrement().
  258.      * The default is one pixel.
  259.      * @see #scrollDown
  260.      * @see #scrollLeft
  261.      * @see #scrollRight
  262.      * @see #setScrollLineIncrement
  263.      * @see #getScrollLineIncrement
  264.      */
  265.     public void scrollUp()
  266.     {
  267.         Point p = getScrollPosition();
  268.         setScrollPosition(new Point(p.x, p.y - getVAdjustable().getUnitIncrement()));
  269.     }
  270.  
  271.     /**
  272.      * Scrolls left by the number of pixels specified in the method setScrollLineIncrement().
  273.      * The default is one pixel.
  274.      * @see #scrollRight
  275.      * @see #scrollUp
  276.      * @see #scrollDown
  277.      * @see #setScrollLineIncrement
  278.      * @see #getScrollLineIncrement
  279.      */
  280.     public void scrollLeft()
  281.     {
  282.         Point p = getScrollPosition();
  283.         setScrollPosition(new Point(p.x - getHAdjustable().getUnitIncrement(), p.y));
  284.     }
  285.  
  286.     /**
  287.      * Scrolls down by the number of pixels specified in the method setScrollLineIncrement().
  288.      * The default is one pixel.
  289.      * @see #scrollUp
  290.      * @see #scrollLeft
  291.      * @see #scrollRight
  292.      * @see #setScrollLineIncrement
  293.      * @see #getScrollLineIncrement
  294.      */
  295.     public void scrollDown()
  296.     {
  297.         Point p = getScrollPosition();
  298.         setScrollPosition(new Point(p.x, p.y + getVAdjustable().getUnitIncrement()));
  299.     }
  300.  
  301.     /**
  302.      * Scrolls right by the number of pixels specified in the method setScrollLineIncrement().
  303.      * The default is one pixel.
  304.      * @see #scrollLeft
  305.      * @see #scrollUp
  306.      * @see #scrollDown
  307.      * @see #setScrollLineIncrement
  308.      * @see #getScrollLineIncrement
  309.      */
  310.     public void scrollRight()
  311.     {
  312.         Point p = getScrollPosition();
  313.         setScrollPosition(new Point(p.x + getHAdjustable().getUnitIncrement(), p.y));
  314.     }
  315.  
  316.     /**
  317.      * Scrolls one "page" up.
  318.      * The page size is the size of this ScrollingPanel not including the
  319.      * scroll bar, if present.
  320.      * @see #scrollPageDown
  321.      * @see #scrollPageLeft
  322.      * @see #scrollPageRight
  323.      */
  324.     public void scrollPageUp()
  325.     {
  326.         Point p = getScrollPosition();
  327.         setScrollPosition(new Point(p.x, p.y - getVAdjustable().getBlockIncrement()));
  328.     }
  329.  
  330.     /**
  331.      * Scrolls one "page" left.
  332.      * The page size is the size of this ScrollingPanel not including the
  333.      * scroll bar, if present.
  334.      * @see #scrollPageRight
  335.      * @see #scrollPageUp
  336.      * @see #scrollPageDown
  337.      */
  338.     public void scrollPageLeft()
  339.     {
  340.         Point p = getScrollPosition();
  341.         setScrollPosition(new Point(p.x - getHAdjustable().getBlockIncrement(), p.y));
  342.     }
  343.  
  344.     /**
  345.      * Scrolls one "page" down.
  346.      * The page size is the size of this ScrollingPanel not including the
  347.      * scroll bar, if present.
  348.      * @see #scrollPageUp
  349.      * @see #scrollPageLeft
  350.      * @see #scrollPageRight
  351.      */
  352.     public void scrollPageDown()
  353.     {
  354.         Point p = getScrollPosition();
  355.         setScrollPosition(new Point(p.x, p.y + getVAdjustable().getBlockIncrement()));
  356.     }
  357.  
  358.     /**
  359.      * Scrolls one "page" right.
  360.      * The page size is the size of this ScrollingPanel not including the
  361.      * scroll bar, if present.
  362.      * @see #scrollPageLeft
  363.      * @see #scrollPageUp
  364.      * @see #scrollPageDown
  365.      */
  366.     public void scrollPageRight()
  367.     {
  368.         Point p = getScrollPosition();
  369.         setScrollPosition(new Point(p.x + getHAdjustable().getBlockIncrement(), p.y));
  370.     }
  371.  
  372.     /**
  373.      * Scrolls to an absolute vertical position.
  374.      * The contained component's given pixel position will be scrolled to the
  375.      * top line of the scrolling panel. The provided position is constrained to the range
  376.      * [0, component's height - view port height].
  377.      * @param position the pixel position to scroll to
  378.      * @see #scrollHorizontalAbsolute
  379.      */
  380.     public void scrollVerticalAbsolute(int position)
  381.     {
  382.         Point p = getScrollPosition();
  383.         setScrollPosition(new Point(p.x, position));
  384.     }
  385.  
  386.     /**
  387.      * Scrolls to an absolute horizontal position.
  388.      * The contained component's given pixel position will be scrolled to the
  389.      * leftmost part of the scrolling panel. The provided position is constrained to the range
  390.      * [0, component's width - view port width].
  391.      * @param position the pixel position to scroll to
  392.      * @see #scrollVerticalAbsolute
  393.      */
  394.     public void scrollHorizontalAbsolute(int position)
  395.     {
  396.         Point p = getScrollPosition();
  397.         setScrollPosition(new Point(position, p.y));
  398.     }
  399.  
  400.     /**
  401.      * Returns the recommended dimensions to properly display this component.
  402.      * This is a standard Java AWT method which gets called to determine
  403.      * the recommended size of this component.
  404.      *
  405.      * For each axis, it returns the larger of the current size or the
  406.      * minimum size.
  407.      *
  408.      * @see #minimumSize
  409.      */
  410.     public Dimension preferredSize()
  411.     {
  412.         Dimension s = size();
  413.         Dimension m = minimumSize();
  414.         return new Dimension(Math.max(s.width, m.width), Math.max(s.height, m.height));
  415.     }
  416.  
  417.     public Dimension getPreferredSize()
  418.     {
  419.         return preferredSize();
  420.     }
  421.  
  422.     /**
  423.      * Returns the minimum dimensions to properly display this component.
  424.      * This is a standard Java AWT method which gets called to determine
  425.      * the minimum size of this component.
  426.      *
  427.      * The value returned is set using the setMinimumHeight() and
  428.      * setMinimumWidth() methods or when this ScrollingPanel is constructed.
  429.      *
  430.      * @see #preferredSize
  431.      * @see #setMinimumHeight
  432.      * @see #setMinimumWidth
  433.      */
  434.     public Dimension minimumSize()
  435.     {
  436.         return new Dimension(width, height);
  437.     }
  438.  
  439.     public Dimension getMinimumSize()
  440.     {
  441.         return minimumSize();
  442.     }
  443.  
  444.     private int width = 100;
  445.     private int    height = 100;
  446.  
  447. }
  448.  
  449.