home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / unuy2wen / cybcerone / utils / vectorscrollingpanel.java < prev   
Encoding:
Java Source  |  1996-08-14  |  2.2 KB  |  100 lines

  1. // VectorScrollingPanel.java
  2. // 25.03.96
  3. //
  4. // scrolls through a Vector of items
  5.  
  6. package cybcerone.utils;
  7.  
  8. import java.awt.Graphics;
  9. import java.util.Vector;
  10. import java.util.Enumeration;
  11.  
  12. /**
  13.  * Like a ScrollingPanel, but let's you give it a Vector of things
  14.  * to scroll and it'll do them one after another.
  15.  */
  16. public class VectorScrollingPanel extends ScrollingPanel {
  17.   private Vector allTheItems;
  18.   private Enumeration theItems;
  19.  
  20.   private String currentElem;
  21.   private String nextElem;
  22.   private Object nextObject;
  23.  
  24.   public VectorScrollingPanel (String id, String statusText, Appletlike app) {
  25.     super (id, statusText, app);
  26.   }
  27.   
  28.   public void setText (Vector items) {
  29.     stop ();
  30.  
  31.     if (items != null) {
  32.       allTheItems = items;
  33.       theItems = items.elements ();
  34.       setText ();
  35.       scrolling = true;
  36.       start ();
  37.     } else {
  38.       scrolling = false;
  39.     }
  40.   }
  41.   
  42.   /** if the current object changed, returns it new value, otherwise null */
  43.   protected Object setText () {
  44.     if (!theItems.hasMoreElements ())
  45.       theItems = allTheItems.elements ();
  46.  
  47.     if (currentElem == null) {
  48.       Object theElem = theItems.nextElement ();
  49.       setCurrent (getInfo (theElem));
  50.       x = getStartX ();
  51.       return theElem;
  52.  
  53.     } else if (nextElem == null) {
  54.       nextObject = theItems.nextElement ();
  55.       nextElem = getInfo (nextObject);
  56.       return null;
  57.  
  58.     } else {
  59.       x += fullWidth;
  60.       setCurrent (nextElem);
  61.       nextElem = null;
  62.       return nextObject;
  63.     }
  64.   }
  65.   
  66.   private void setCurrent (String text) {
  67.     currentElem = text;
  68.     if (metrics == null) setMetrics ();
  69.     textWidth = metrics.stringWidth (currentElem);
  70.     setOffScreen ();
  71.     setFullWidth ();
  72.   }
  73.  
  74.   protected int getStartX () {
  75.     return size().width / 2;
  76.   }
  77.   
  78.   protected String getInfo (Object theElem) {
  79.     return theElem.toString ();
  80.   }
  81.  
  82.   protected void step () {
  83.     x -= step;
  84.     if (x + fullWidth < size().width) {
  85.       if (nextElem == null)
  86.     setText ();
  87.       else if (x < offScreen)
  88.     setText ();
  89.     }
  90.   }
  91.  
  92.   protected void paintText (Graphics g) {
  93.     if (currentElem != null) {
  94.       g.drawString (currentElem, x, y);
  95.       if (nextElem != null) 
  96.     g.drawString (nextElem, x + fullWidth, y);
  97.     }
  98.   }
  99. }
  100.