home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / in4wjcxu / src / como / awt / scrollpanel.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  3.9 KB  |  169 lines

  1. /*
  2.  
  3. ScrollPanel.java    1996 Jan Kautz
  4.  
  5. You can scroll a panel in vertical direction.
  6. Horizontally it gets as large as it may get.
  7. Only use in a BorderLayout() as "Center".
  8. After you added a new component to subject,
  9. you can call redo(), and everything will be
  10. fine.
  11.  
  12. Based on ScrollingPanel, done by Peris Brodsky (plb@idm.com).
  13. */
  14.  
  15. package como.awt;
  16.  
  17. import como.util.*;
  18. import java.util.*;
  19. import java.awt.*;
  20.  
  21. public class ScrollPanel extends Panel {
  22.  
  23.     Component subject;
  24.     Portal portal;
  25.     Scrollbar hbar, vbar;
  26.     Dimension subject_dim;
  27.     int hpos, vpos;
  28.     float line_fraction;
  29.     boolean configed = false;
  30.     boolean useinsets;
  31.  
  32.     public ScrollPanel( Component subject, float line_fraction ) {
  33.         this( subject, line_fraction, true );
  34.     }
  35.  
  36.     public ScrollPanel( Component subject, float line_fraction, boolean useinsets ) {
  37.         this.subject =  subject;
  38.         this.line_fraction =  line_fraction;
  39.         this.useinsets = useinsets;
  40.  
  41.         setLayout(new BorderLayout());
  42.      
  43.         portal = new Portal(this, subject);
  44.         portal.setLayout(null);
  45.         add("Center", portal);
  46.         portal.add(subject);
  47.      
  48.         /*
  49.         hbar =  new Scrollbar(Scrollbar.HORIZONTAL);
  50.         add("South", hbar);
  51.         */
  52.      
  53.         vbar =  new Scrollbar(Scrollbar.VERTICAL);
  54.         add("West", vbar);
  55.     }
  56.  
  57.     public Insets insets() {
  58.         Insets i = super.insets();
  59.  
  60.         if( useinsets )
  61.             return new Insets( i.top, i.left+5, i.bottom, i.right+5 );
  62.         else
  63.             return i;
  64.     }
  65.  
  66.     public void redo()
  67.     {
  68.         configed = false;
  69.  
  70.         paintAll( getGraphics() );
  71.         subject.paintAll( subject.getGraphics() );
  72.  
  73. // JK neu
  74.     
  75.         reconfigure( portal.size() );
  76.     }
  77.     
  78.     private void move_subject(int x, int y) {
  79.         subject.move(hpos =  x, vpos =  y);
  80.     }
  81.  
  82.     public boolean handleEvent(Event e) {
  83. /*
  84.         if (e.target == hbar &&
  85.             (e.id == Event.SCROLL_ABSOLUTE ||
  86.              e.id == Event.SCROLL_PAGE_UP ||
  87.              e.id == Event.SCROLL_PAGE_DOWN ||
  88.              e.id == Event.SCROLL_LINE_UP ||
  89.              e.id == Event.SCROLL_LINE_DOWN)) {
  90.             move_subject(-((Integer)e.arg).intValue(), vpos);
  91.         }
  92. */
  93.         if (e.target == vbar &&
  94.             (e.id == Event.SCROLL_ABSOLUTE ||
  95.              e.id == Event.SCROLL_PAGE_UP ||
  96.              e.id == Event.SCROLL_PAGE_DOWN ||
  97.              e.id == Event.SCROLL_LINE_UP ||
  98.              e.id == Event.SCROLL_LINE_DOWN)) {
  99.             move_subject(hpos, -((Integer)e.arg).intValue());
  100.         }
  101.  
  102.         return super.handleEvent(e);
  103. }
  104.  
  105.     public void reconfigure(Dimension portal_dim) {
  106.         if (portal_dim.width <= 0 || portal_dim.height <= 0)
  107.             return;
  108.  
  109.         if( !configed || portal_dim.width != subject_dim.width ) {
  110.             subject.resize( portal_dim.width, subject.preferredSize().height );
  111.             subject_dim = subject.size();
  112.  
  113.             configed = true;
  114.         }
  115.  
  116.         /* At the moment I need only vbars...
  117.         if (subject_dim.width + hpos < portal_dim.width)
  118.             move_subject(Math.min(0, portal_dim.width - subject_dim.width), vpos);
  119.      
  120.         hbar.setValues(-hpos, portal_dim.width, 0, subject_dim.width - portal_dim.width);
  121.      
  122.         // hbar.setPageIncrement(portal_dim.width);
  123.         // hbar.setLineIncrement(Math.round(line_fraction*portal_dim.width));
  124.         */
  125.      
  126.         if (subject_dim.height + vpos < portal_dim.height)
  127.             move_subject(hpos, Math.min(0, portal_dim.height - subject_dim.height));
  128.     
  129. // JK neu
  130.  
  131.         int max =  subject_dim.height;
  132.         int visible = portal_dim.height;
  133.  
  134.         if( System.getProperty( "os.name", "unknown" ).startsWith( "Windows") )
  135.             vbar.setValues(-vpos, visible, 0, max);
  136.         else
  137.             vbar.setValues(-vpos, visible, 0, max - visible);
  138.  
  139.         // vbar.setPageIncrement(portal_dim.height);
  140.         // vbar.setLineIncrement(Math.round(line_fraction*portal_dim.height));
  141.     }
  142.  
  143. class Portal extends Panel {
  144.  
  145.     ScrollPanel    parent;
  146.     Component subject;
  147.  
  148.     Portal(ScrollPanel parent, Component subject) {
  149.         this.parent =  parent;
  150.         this.subject = subject;
  151.         setLayout(null);
  152.     }
  153.  
  154.     public void reshape(int x, int y, int width, int height) {
  155.         super.reshape(x, y, width, height);
  156.         Dimension dim = size();
  157.         parent.reconfigure(dim);
  158.     }
  159.  
  160.     public Dimension preferredSize() {
  161.         return new Dimension( subject.preferredSize() );
  162.     }
  163.  
  164.     public Dimension minimumSize() {
  165.         return new Dimension( subject.minimumSize() );
  166.     }
  167. }
  168.