import java.applet.*;
import java.awt.*;
import java.awt.image.*;
import java.net.*;
import java.io.*;
import java.util.*;

/**
 * JVtxView is an applet which shows teletext (Videotext in german)
 * files. It is "designed" to show those *.vtx files created
 * by vbidecode; thus allowing some sort of teletext server.
 * @author Martin Denn <mdenn@unix-ag.uni-kl.de>
 * @version 0.02
 */
public class JVtxView extends Applet {
    private Font font;
    JVtxFont vtx_font;
    JVtxPage vtx_page=null;
    Vector channels=new Vector();
    int current_channel;

    /**
     * Class constructor. 
     * First builds the font, then parses the available channels
     * and finally starts up the first page.
     */
    public void init() {
        MediaTracker tracker;
        Image img;
        int w, h;
        URL u;
        InputStream input;
        int c;

        String fontfile = this.getParameter("font");
        if (fontfile == null) {
            fontfile = "vtx10x12.gif";
        }
        tracker = new MediaTracker(this);
        img = getImage(getCodeBase(), fontfile);
        tracker.addImage(img,0);
        try {
            tracker.waitForID(0);
        }
        catch (InterruptedException e) {
            return;
        }
        w = img.getWidth(this);
        h = img.getHeight(this);
        vtx_font = new JVtxFont(img, w, h);

        String parameter = this.getParameter("channels");
        if (parameter == null) {
             System.out.println("must give at least one channel with the \"channels\" attribute");
            return;
        }
        try {
            StringTokenizer st=new StringTokenizer(parameter, " ");
            while (st.hasMoreTokens()) {
                channels.addElement(st.nextToken());
            }
        }
        catch (NoSuchElementException e) {
            // should not occur
        }
        if (channels.isEmpty()) {
            System.out.println("must give at least one channel with the \"channels\" attribute");
            return;
        }


        resize(w/32*40, h/8*25);
        
        vtx_page = new JVtxPage((Applet) this, vtx_font);
        this.setLayout(new GridLayout(1, 1));
        this.add(vtx_page);
    }
    
    /**
     * @return the current channel's name.
     */
    public String currentChannel() {
        String result=null;
        try {
            result=(String) channels.elementAt(current_channel);
        }
        catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("index out of bounds in currentChannel");
        }
        return result;
    }
    
    /**
     * selects the next channel.
     * Wraps around after reaching the last channel.
     */
    public void nextChannel() {
        current_channel++;
        if (current_channel >= channels.size()) {
            current_channel = 0;
        }
    }

    /**
     * selects the previous channel.
     * Wraps around after reaching the first channel.
     */
    public void previousChannel() {
        current_channel--;
        if (current_channel < 0) {
            current_channel = channels.size()-1;
        }
    }
    
}



