home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1998 December / PCO_1298.ISO / filesbbs / w95 / trial.exe / CLIPCONTROL.CLASS (.txt) < prev    next >
Encoding:
Java Class File  |  1998-06-01  |  9.2 KB  |  366 lines

  1. import java.applet.Applet;
  2. import java.applet.AudioClip;
  3. import java.awt.Button;
  4. import java.awt.Checkbox;
  5. import java.awt.Color;
  6. import java.awt.Component;
  7. import java.awt.Container;
  8. import java.awt.Dimension;
  9. import java.awt.Event;
  10. import java.net.MalformedURLException;
  11. import java.net.URL;
  12.  
  13. public class ClipControl extends Applet implements Runnable {
  14.    int width = 260;
  15.    int height = 40;
  16.    String url = "http://www.cs.indiana.edu/hyplan/kinzler/fun/shr_sounds/index.html";
  17.    String[][] pinfo = new String[][]{{"clip", "URL", "audio clip"}, {"bgcolor", "color", "background color"}, {"fgcolor", "color", "foreground color"}, {"bgstatus", "string", "background status string"}, {"noplay", "boolean", "don't show play button [false]"}, {"noloop", "boolean", "don't show loop button [false]"}, {"nostop", "boolean", "don't show stop button [false]"}, {"noload", "boolean", "don't show load button [false]"}, {"boxplay", "boolean", "use a checkbox for play button [false]"}, {"boxloop", "boolean", "use a checkbox for loop button [false]"}, {"boxstop", "boolean", "use a checkbox for stop button [false]"}, {"playlabel", "string", "play button label [Play]"}, {"looplabel", "string", "loop button label [Loop]"}, {"stoplabel", "string", "stop button label [Stop]"}, {"loadlabel", "string", "load button label [Load]"}, {"nextlabel", "string", "next load button label [Reload]"}, {"nextload", "boolean", "keep load button for reloads [false]"}, {"oninit", "actions", "actions on applet initialization []"}, {"onstart", "actions", "actions on applet start []"}, {"onstop", "actions", "actions on applet stop [quitstop]"}, {"ondestroy", "actions", "actions on applet destruction [stop]"}};
  18.    String clipname;
  19.    String status;
  20.    String nextlabel;
  21.    URL clipurl;
  22.    boolean nextload = false;
  23.    String oninit;
  24.    String onstart;
  25.    String onstop;
  26.    String ondestroy;
  27.    AudioClip clip;
  28.    Component play;
  29.    Component loop;
  30.    Component stop;
  31.    Component load;
  32.    Event pend;
  33.    Thread thrd;
  34.    boolean looping = false;
  35.    boolean loading = false;
  36.  
  37.    public String getAppletInfo() {
  38.       return "ClipControl - an AudioClip control applet with play/loop/stop/load buttons\nSteve Kinzler, kinzler@cs.indiana.edu, May 96/Sep 96\n" + this.url + "\nVersion 2.0.2, see source code for license\n" + "Copyright (c) 1996 Stephen B Kinzler.  All rights reserved.\n\n" + "color  := hexadecimal number prefixed with '#'\n" + "action := load = reload the clip\n" + "          play = play the clip, loading it first if needed\n" + "          loop = loop the clip, loading it first if needed\n" + "          quit = quit loading the clip\n" + "          stop = stop the clip";
  39.    }
  40.  
  41.    public String[][] getParameterInfo() {
  42.       return this.pinfo;
  43.    }
  44.  
  45.    public synchronized void init() {
  46.       Dimension var1 = ((Component)this).size();
  47.       if (var1.width > 0) {
  48.          this.width = var1.width;
  49.       }
  50.  
  51.       if (var1.height > 0) {
  52.          this.height = var1.height;
  53.       }
  54.  
  55.       ((Applet)this).resize(this.width, this.height);
  56.       String var2 = ((Applet)this).getParameter("BGCOLOR");
  57.       if (var2 != null && var2.startsWith("#")) {
  58.          int var3 = Integer.parseInt(var2.substring(1), 16);
  59.          ((Component)this).setBackground(new Color(var3));
  60.       }
  61.  
  62.       var2 = ((Applet)this).getParameter("FGCOLOR");
  63.       if (var2 != null && var2.startsWith("#")) {
  64.          int var12 = Integer.parseInt(var2.substring(1), 16);
  65.          ((Component)this).setForeground(new Color(var12));
  66.       }
  67.  
  68.       var2 = ((Applet)this).getParameter("BGSTATUS");
  69.       this.status = var2 != null ? var2 : "ClipControl: click, type 'h' for home page or '?' for keys";
  70.       var2 = this.clipname = ((Applet)this).getParameter("CLIP");
  71.  
  72.       try {
  73.          this.clipurl = var2 != null ? new URL(((Applet)this).getDocumentBase(), var2) : null;
  74.       } catch (MalformedURLException var4) {
  75.          this.clipurl = null;
  76.       }
  77.  
  78.       if (this.clipurl != null) {
  79.          var2 = ((Applet)this).getParameter("NEXTLABEL");
  80.          this.nextlabel = var2 != null ? var2 : "Reload";
  81.          var2 = ((Applet)this).getParameter("NEXTLOAD");
  82.          this.nextload = var2 != null ? var2.equals("true") : false;
  83.          this.play = this.makeButton(this.play, "PLAY", "Play");
  84.          this.loop = this.makeButton(this.loop, "LOOP", "Loop");
  85.          this.stop = this.makeButton(this.stop, "STOP", "Stop");
  86.          this.load = this.makeButton(this.load, "LOAD", "Load");
  87.          this.oninit = ((Applet)this).getParameter("ONINIT");
  88.          this.onstart = ((Applet)this).getParameter("ONSTART");
  89.          var2 = ((Applet)this).getParameter("ONSTOP");
  90.          this.onstop = var2 != null ? var2 : "quitstop";
  91.          var2 = ((Applet)this).getParameter("ONDESTROY");
  92.          this.ondestroy = var2 != null ? var2 : "stop";
  93.          this.doAction(this.oninit);
  94.       }
  95.    }
  96.  
  97.    synchronized Component makeButton(Component var1, String var2, String var3) {
  98.       if (var1 != null) {
  99.          ((Container)this).remove(var1);
  100.       }
  101.  
  102.       String var4 = ((Applet)this).getParameter(var2 + "LABEL");
  103.       if (var4 == null) {
  104.          var4 = var3;
  105.       }
  106.  
  107.       if (var2.equalsIgnoreCase("LOAD")) {
  108.          var1 = new Checkbox(this.clip == null ? var4 : this.nextlabel);
  109.          this.setButton(var1, this.loading);
  110.          String var9 = ((Applet)this).getParameter("NO" + var2);
  111.          if ((var9 == null || !var9.equals("true")) && (this.nextload || this.clip == null)) {
  112.             ((Container)this).add(var1);
  113.          }
  114.  
  115.          return var1;
  116.       } else {
  117.          String var5 = ((Applet)this).getParameter("BOX" + var2);
  118.          if (var5 != null && var5.equals("true")) {
  119.             var1 = new Checkbox(var4);
  120.          } else {
  121.             var1 = new Button(var4);
  122.          }
  123.  
  124.          this.setButton(var1, var2.equalsIgnoreCase("LOOP") ? this.looping : false);
  125.          var5 = ((Applet)this).getParameter("NO" + var2);
  126.          if (var5 == null || !var5.equals("true")) {
  127.             ((Container)this).add(var1);
  128.          }
  129.  
  130.          return var1;
  131.       }
  132.    }
  133.  
  134.    void setButton(Component var1, boolean var2) {
  135.       if (var1 instanceof Checkbox) {
  136.          ((Checkbox)var1).setState(var2);
  137.       }
  138.  
  139.    }
  140.  
  141.    public void start() {
  142.       this.doAction(this.onstart);
  143.    }
  144.  
  145.    void doAction(String var1) {
  146.       if (var1 != null) {
  147.          String var2 = var1.toLowerCase();
  148.          if (var2.indexOf("load") >= 0) {
  149.             this.quitLoad();
  150.             this.pressButton(this.load);
  151.          }
  152.  
  153.          if (var2.indexOf("play") >= 0) {
  154.             this.pressButton(this.play);
  155.          }
  156.  
  157.          if (var2.indexOf("loop") >= 0) {
  158.             if (this.loop instanceof Checkbox && this.looping) {
  159.                this.pressButton(this.loop);
  160.             }
  161.  
  162.             this.pressButton(this.loop);
  163.          }
  164.  
  165.          if (var2.indexOf("quit") >= 0) {
  166.             this.quitLoad();
  167.          }
  168.  
  169.          if (var2.indexOf("stop") >= 0) {
  170.             this.pressButton(this.stop);
  171.          }
  172.  
  173.       }
  174.    }
  175.  
  176.    void pressButton(Component var1) {
  177.       this.action(new Event(var1, 1001, this), this);
  178.    }
  179.  
  180.    public boolean mouseEnter(Event var1, int var2, int var3) {
  181.       ((Component)this).requestFocus();
  182.       ((Applet)this).showStatus(this.status);
  183.       return true;
  184.    }
  185.  
  186.    public boolean mouseExit(Event var1, int var2, int var3) {
  187.       ((Applet)this).showStatus((String)null);
  188.       return true;
  189.    }
  190.  
  191.    public boolean keyDown(Event var1, int var2) {
  192.       switch (var2) {
  193.          case 63:
  194.          default:
  195.             ((Applet)this).showStatus("(h)ome (p)lay (l)oop (s)top (r)eload (q)uit loading");
  196.             break;
  197.          case 72:
  198.          case 104:
  199.             this.toHome();
  200.             break;
  201.          case 76:
  202.          case 108:
  203.             this.doAction("loop");
  204.             break;
  205.          case 80:
  206.          case 112:
  207.             this.doAction("play");
  208.             break;
  209.          case 81:
  210.          case 113:
  211.             this.doAction("quit");
  212.             break;
  213.          case 82:
  214.          case 114:
  215.             this.doAction("load");
  216.             break;
  217.          case 83:
  218.          case 115:
  219.             this.doAction("stop");
  220.       }
  221.  
  222.       return true;
  223.    }
  224.  
  225.    void toHome() {
  226.       try {
  227.          ((Applet)this).getAppletContext().showDocument(new URL(this.url));
  228.       } catch (MalformedURLException var1) {
  229.          ((Applet)this).showStatus("Ignoring Malformed Home URL");
  230.       }
  231.    }
  232.  
  233.    public synchronized boolean action(Event var1, Object var2) {
  234.       if (var1.target == null) {
  235.          return true;
  236.       } else {
  237.          if (var1.target.equals(this.play)) {
  238.             this.setButton(this.play, true);
  239.             if (!this.getClip(var1)) {
  240.                ((Applet)this).showStatus("Playing " + this.clipname);
  241.                this.clip.play();
  242.             }
  243.  
  244.             this.setButton(this.play, false);
  245.          } else if (var1.target.equals(this.loop)) {
  246.             if (this.loop instanceof Checkbox && this.looping) {
  247.                ((Applet)this).showStatus("Stopping " + this.clipname);
  248.                this.clip.stop();
  249.                this.looping = false;
  250.                this.setButton(this.loop, false);
  251.                return true;
  252.             }
  253.  
  254.             this.setButton(this.loop, true);
  255.             if (!this.getClip(var1)) {
  256.                this.looping = true;
  257.                ((Applet)this).showStatus("Looping " + this.clipname);
  258.                this.clip.loop();
  259.             } else {
  260.                this.setButton(this.loop, false);
  261.             }
  262.          } else if (var1.target.equals(this.stop)) {
  263.             this.setButton(this.stop, true);
  264.             if (this.clip != null) {
  265.                ((Applet)this).showStatus("Stopping " + this.clipname);
  266.                this.clip.stop();
  267.                if (this.looping) {
  268.                   this.setButton(this.loop, false);
  269.                }
  270.             }
  271.  
  272.             this.pend = null;
  273.             this.looping = false;
  274.             this.setButton(this.stop, false);
  275.          } else if (var1.target.equals(this.load)) {
  276.             if (this.loading) {
  277.                this.quitLoad();
  278.             } else {
  279.                this.getClip((Event)null);
  280.             }
  281.          }
  282.  
  283.          return true;
  284.       }
  285.    }
  286.  
  287.    synchronized boolean getClip(Event var1) {
  288.       if (var1 != null && this.clip != null) {
  289.          return false;
  290.       } else {
  291.          if (var1 == null) {
  292.             this.quitLoad();
  293.          }
  294.  
  295.          if (this.loading) {
  296.             this.pend = var1;
  297.             ((Applet)this).showStatus("Still loading " + this.clipname);
  298.             return true;
  299.          } else {
  300.             this.loading = true;
  301.             this.setButton(this.load, true);
  302.             ((Applet)this).showStatus("Loading " + this.clipname);
  303.             if (var1 != null) {
  304.                this.pend = var1;
  305.             }
  306.  
  307.             this.thrd = new Thread(this);
  308.             this.thrd.start();
  309.             return true;
  310.          }
  311.       }
  312.    }
  313.  
  314.    public void run() {
  315.       AudioClip var1 = ((Applet)this).getAudioClip(this.clipurl);
  316.       synchronized(this){}
  317.  
  318.       try {
  319.          if (this.clip != null) {
  320.             this.doAction("stop");
  321.          }
  322.  
  323.          this.clip = var1;
  324.          Object var5 = null;
  325.          this.loading = false;
  326.          ((Applet)this).showStatus("Loaded " + this.clipname);
  327.          if (!this.nextload) {
  328.             ((Container)this).remove(this.load);
  329.          }
  330.  
  331.          ((Checkbox)this.load).setLabel(this.nextlabel);
  332.          this.setButton(this.load, false);
  333.          if (this.pend != null) {
  334.             this.action(this.pend, this);
  335.          }
  336.  
  337.          this.pend = null;
  338.          this.thrd = null;
  339.       } catch (Throwable var4) {
  340.          throw var4;
  341.       }
  342.  
  343.    }
  344.  
  345.    synchronized void quitLoad() {
  346.       if (this.thrd != null) {
  347.          this.thrd.stop();
  348.          ((Applet)this).showStatus("Quit loading " + this.clipname);
  349.       }
  350.  
  351.       this.loading = false;
  352.       this.pend = null;
  353.       this.thrd = null;
  354.       this.setButton(this.load, false);
  355.    }
  356.  
  357.    public void stop() {
  358.       this.doAction(this.onstop);
  359.    }
  360.  
  361.    public void destroy() {
  362.       this.doAction(this.ondestroy);
  363.       this.quitLoad();
  364.    }
  365. }
  366.