home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-08-14 | 2.2 KB | 100 lines |
- // VectorScrollingPanel.java
- // 25.03.96
- //
- // scrolls through a Vector of items
-
- package cybcerone.utils;
-
- import java.awt.Graphics;
- import java.util.Vector;
- import java.util.Enumeration;
-
- /**
- * Like a ScrollingPanel, but let's you give it a Vector of things
- * to scroll and it'll do them one after another.
- */
- public class VectorScrollingPanel extends ScrollingPanel {
- private Vector allTheItems;
- private Enumeration theItems;
-
- private String currentElem;
- private String nextElem;
- private Object nextObject;
-
- public VectorScrollingPanel (String id, String statusText, Appletlike app) {
- super (id, statusText, app);
- }
-
- public void setText (Vector items) {
- stop ();
-
- if (items != null) {
- allTheItems = items;
- theItems = items.elements ();
- setText ();
- scrolling = true;
- start ();
- } else {
- scrolling = false;
- }
- }
-
- /** if the current object changed, returns it new value, otherwise null */
- protected Object setText () {
- if (!theItems.hasMoreElements ())
- theItems = allTheItems.elements ();
-
- if (currentElem == null) {
- Object theElem = theItems.nextElement ();
- setCurrent (getInfo (theElem));
- x = getStartX ();
- return theElem;
-
- } else if (nextElem == null) {
- nextObject = theItems.nextElement ();
- nextElem = getInfo (nextObject);
- return null;
-
- } else {
- x += fullWidth;
- setCurrent (nextElem);
- nextElem = null;
- return nextObject;
- }
- }
-
- private void setCurrent (String text) {
- currentElem = text;
- if (metrics == null) setMetrics ();
- textWidth = metrics.stringWidth (currentElem);
- setOffScreen ();
- setFullWidth ();
- }
-
- protected int getStartX () {
- return size().width / 2;
- }
-
- protected String getInfo (Object theElem) {
- return theElem.toString ();
- }
-
- protected void step () {
- x -= step;
- if (x + fullWidth < size().width) {
- if (nextElem == null)
- setText ();
- else if (x < offScreen)
- setText ();
- }
- }
-
- protected void paintText (Graphics g) {
- if (currentElem != null) {
- g.drawString (currentElem, x, y);
- if (nextElem != null)
- g.drawString (nextElem, x + fullWidth, y);
- }
- }
- }
-