home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1996 December / PCO1296.ISO / filesbbs / frei / clip.arj / CLIP.EXE / ClipControl.java < prev    next >
Encoding:
Java Source  |  1996-06-09  |  7.6 KB  |  233 lines

  1. // Copyright (c) 1996 Stephen B Kinzler.  All rights reserved.
  2.  
  3. //
  4.  
  5. // Redistribution and use, with or without modification, are permitted
  6.  
  7. // provided that the following conditions are met:
  8.  
  9. // 1. Redistributions must retain the above copyright notice, this list of
  10.  
  11. //    conditions and the following disclaimer.
  12.  
  13. // 2. All advertising materials mentioning features or use of this software
  14.  
  15. //    must display the following acknowledgement:
  16.  
  17. //     This product includes software developed by the Stephen B Kinzler.
  18.  
  19. // 3. The name Stephen B Kinzler may not be used to endorse or promote
  20.  
  21. //    products derived from this software without specific prior written
  22.  
  23. //    permission.
  24.  
  25. //
  26.  
  27. // THIS SOFTWARE IS PROVIDED BY STEPHEN B KINZLER ``AS IS'' AND ANY EXPRESS
  28.  
  29. // OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  30.  
  31. // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  32.  
  33. // ARE DISCLAIMED.  IN NO EVENT SHALL STEPHEN B KINZLER BE LIABLE FOR
  34.  
  35. // ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  36.  
  37. // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  38.  
  39. // OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  40.  
  41. // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  42.  
  43. // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  44.  
  45. // IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  46.  
  47. // POSSIBILITY OF SUCH DAMAGE.
  48.  
  49.  
  50.  
  51. import java.applet.*;
  52.  
  53. import java.awt.*;
  54.  
  55. import java.net.*;
  56.  
  57.  
  58.  
  59. ///////////////////////////////////////////////////////////////////////////////
  60.  
  61. // ClipControl // Declaration /////////////////////////////////////////////////
  62.  
  63.  
  64.  
  65. public class ClipControl extends Applet implements Runnable {
  66.  
  67.  
  68.  
  69.     int width  = 260, height = 40;
  70.  
  71.     String url = "http://www.cs.indiana.edu/hyplan/kinzler/fun/"
  72.  
  73.            + "shr_sounds/index.html";
  74.  
  75.  
  76.  
  77.     public String getAppletInfo() {
  78.  
  79.         return
  80.  
  81. "ClipControl - a AudioClip control applet with play/loop/stop/load buttons\n"
  82.  
  83. + "Steve Kinzler, kinzler@cs.indiana.edu, May 96\n" + url
  84.  
  85. + "\nVersion 1.1.0";
  86.  
  87.     }
  88.  
  89.  
  90.  
  91.     String      clipname;
  92.  
  93.     URL      clipurl;
  94.  
  95.     boolean      backload = false, backplay = false;
  96.  
  97.  
  98.  
  99.     AudioClip clip = null;
  100.  
  101.     Button      play = null, loop = null, stop = null;
  102.  
  103.     Checkbox  load = null;
  104.  
  105.     Event      pend = null;
  106.  
  107.     Thread      thrd = null;
  108.  
  109.     boolean      loading = false;
  110.  
  111.  
  112.  
  113.     String pinfo[][] = {
  114.  
  115.         {"clip",      "URL",     "audio clip"},
  116.  
  117.         {"bgcolor",   "color",   "background color"},
  118.  
  119.         {"fgcolor",   "color",   "foreground color"},
  120.  
  121.         {"playlabel", "string",  "play button label [Play]"},
  122.  
  123.         {"looplabel", "string",  "loop button label [Loop]"},
  124.  
  125.         {"stoplabel", "string",  "stop button label [Stop]"},
  126.  
  127.         {"loadlabel", "string",  "load button label [Load]"},
  128.  
  129.         {"noload",    "boolean", "don't show load button [false]"},
  130.  
  131.         {"preload",   "boolean", "load clip at start [false]"},
  132.  
  133.         {"preplay",   "boolean", "play clip at start [false]"},
  134.  
  135.         {"preloop",   "boolean", "loop clip at start [false]"},
  136.  
  137.         {"backload",  "boolean", "keep loading on applet stop [false]"},
  138.  
  139.         {"backplay",  "boolean", "keep playing on applet stop [false]"}
  140.  
  141.     };
  142.  
  143.  
  144.  
  145.     public String[][] getParameterInfo() {
  146.  
  147.         return pinfo;
  148.  
  149.     }
  150.  
  151.  
  152.  
  153. // ClipControl // Initialization //////////////////////////////////////////////
  154.  
  155.  
  156.  
  157.     public synchronized void init() {
  158.  
  159.         Dimension d = size();
  160.  
  161.         if (d.width  > 0) width  = d.width;
  162.  
  163.         if (d.height > 0) height = d.height;
  164.  
  165.         resize(width, height);
  166.  
  167.  
  168.  
  169.         String s = getParameter("BGCOLOR");
  170.  
  171.         if (s != null && s.startsWith("#")) {
  172.  
  173.             int i = Integer.parseInt(s.substring(1), 16);
  174.  
  175.             setBackground(new Color(i));
  176.  
  177.         }
  178.  
  179.         s = getParameter("FGCOLOR");
  180.  
  181.         if (s != null && s.startsWith("#")) {
  182.  
  183.             int i = Integer.parseInt(s.substring(1), 16);
  184.  
  185.             setForeground(new Color(i));
  186.  
  187.         }
  188.  
  189.  
  190.  
  191.         s = clipname = getParameter("CLIP");
  192.  
  193.         try { clipurl = (s != null) ? new URL(getDocumentBase(), s)
  194.  
  195.                         : null; }
  196.  
  197.         catch (MalformedURLException e) { clipurl = null; }
  198.  
  199.         if (clipurl == null) return;
  200.  
  201.  
  202.  
  203.         s = getParameter("PLAYLABEL");
  204.  
  205.         if (play != null) remove(play);
  206.  
  207.         play = new Button((s != null) ? s : "Play"); add(play);
  208.  
  209.         s = getParameter("LOOPLABEL");
  210.  
  211.         if (loop != null) remove(loop);
  212.  
  213.         loop = new Button((s != null) ? s : "Loop"); add(loop);
  214.  
  215.         s = getParameter("STOPLABEL");
  216.  
  217.         if (stop != null) remove(stop);
  218.  
  219.         stop = new Button((s != null) ? s : "Stop"); add(stop);
  220.  
  221.         s = getParameter("LOADLABEL");
  222.  
  223.         if (load != null) remove(load);
  224.  
  225.         load = new Checkbox((s != null) ? s : "Load");
  226.  
  227.         if (loading) load.setState(true);
  228.  
  229.         s = getParameter("NOLOAD");
  230.  
  231.         if (clip == null && ((s != null) ? ! s.equals("true") : true))
  232.  
  233.             add(load);
  234.  
  235.  
  236.  
  237.         s = getParameter("BACKLOAD");
  238.  
  239.         backload = (s != null) ? s.equals("true") : false;
  240.  
  241.         s = getParameter("BACKPLAY");
  242.  
  243.         backplay = (s != null) ? s.equals("true") : false;
  244.  
  245.  
  246.  
  247.         preAction("PRELOAD", load);
  248.  
  249.         preAction("PRELOOP", loop);
  250.  
  251.         preAction("PREPLAY", play);
  252.  
  253.     }
  254.  
  255.  
  256.  
  257.     void preAction(String param, Component button) {
  258.  
  259.         String s = getParameter(param);
  260.  
  261.         if ((s != null) ? ! s.equals("true") : true) return;
  262.  
  263.         action(new Event(button, Event.ACTION_EVENT, this), this);
  264.  
  265.     }
  266.  
  267.  
  268.  
  269. // ClipControl // Events //////////////////////////////////////////////////////
  270.  
  271.  
  272.  
  273.     public boolean mouseEnter(Event evt, int x, int y) {
  274.  
  275.         showStatus(url);
  276.  
  277.         return true;
  278.  
  279.     }
  280.  
  281.  
  282.  
  283.     public boolean mouseExit(Event evt, int x, int y) {
  284.  
  285.         showStatus(null);
  286.  
  287.         return true;
  288.  
  289.     }
  290.  
  291.  
  292.  
  293.     public boolean mouseUp(Event evt, int x, int y) {
  294.  
  295.         if (url != null) {
  296.  
  297.             try { getAppletContext().showDocument(new URL(url)); }
  298.  
  299.             catch (MalformedURLException e) {
  300.  
  301.                 url = null;
  302.  
  303.                 showStatus("Ignoring Malformed Home URL");
  304.  
  305.             }
  306.  
  307.         }
  308.  
  309.         return true;
  310.  
  311.     }
  312.  
  313.  
  314.  
  315.     public synchronized boolean keyDown(Event evt, int key) {
  316.  
  317.         if (clip != null) clip.stop();
  318.  
  319.         if (thrd != null) thrd.stop();
  320.  
  321.         load.setState(false);
  322.  
  323.         clip = null; pend = null; thrd = null; loading = false;
  324.  
  325.         getClip(null);
  326.  
  327.         return true;
  328.  
  329.     }
  330.  
  331.  
  332.  
  333.     public boolean action(Event e, Object o) {
  334.  
  335.         if (e.target == null) return true;
  336.  
  337.         if (e.target.equals(play)) {
  338.  
  339.             if (! getClip(e)) {
  340.  
  341.                 showStatus("Playing " + clipname);
  342.  
  343.                 clip.play();
  344.  
  345.             }
  346.  
  347.         } else if (e.target.equals(loop)) {
  348.  
  349.             if (! getClip(e)) {
  350.  
  351.                 showStatus("Looping " + clipname);
  352.  
  353.                 clip.loop();
  354.  
  355.             }
  356.  
  357.         } else if (e.target.equals(stop)) {
  358.  
  359.             pend = null;
  360.  
  361.             if (clip != null) {
  362.  
  363.                 showStatus("Stopping " + clipname);
  364.  
  365.                 clip.stop();
  366.  
  367.             }
  368.  
  369.         } else if (e.target.equals(load)) {
  370.  
  371.             getClip(null);
  372.  
  373.         }
  374.  
  375.         return true;
  376.  
  377.     }
  378.  
  379.  
  380.  
  381. // ClipControl // Loading /////////////////////////////////////////////////////
  382.  
  383.  
  384.  
  385.     synchronized boolean getClip(Event e) {
  386.  
  387.         load.setState(true);
  388.  
  389.         if (! loading) {
  390.  
  391.             loading = true;
  392.  
  393.             if (clip == null) {
  394.  
  395.                 showStatus("Loading " + clipname);
  396.  
  397.                 if (e != null) pend = e;
  398.  
  399.                 thrd = new Thread(this);
  400.  
  401.                 thrd.start();
  402.  
  403.                 return true;
  404.  
  405.             }
  406.  
  407.         }
  408.  
  409.         if (clip == null) {
  410.  
  411.             if (e != null) pend = e;
  412.  
  413.             showStatus("Still loading " + clipname);
  414.  
  415.             return true;
  416.  
  417.         }
  418.  
  419.         return false;
  420.  
  421.     }
  422.  
  423.  
  424.  
  425.     public void run() {
  426.  
  427.         clip = getAudioClip(clipurl);
  428.  
  429.         showStatus("Loaded " + clipname);
  430.  
  431.         remove(load);
  432.  
  433.         repaint();
  434.  
  435.         if (pend != null) action(pend, this);
  436.  
  437.         pend = null; thrd = null;
  438.  
  439.     }
  440.  
  441.  
  442.  
  443. // ClipControl // Suspension //////////////////////////////////////////////////
  444.  
  445.  
  446.  
  447.     public synchronized void stop() {
  448.  
  449.         if (clip != null && ! backplay) clip.stop();
  450.  
  451.         if (thrd != null && ! backload) {
  452.  
  453.             thrd.stop();
  454.  
  455.             load.setState(false);
  456.  
  457.             clip = null; pend = null; thrd = null; loading = false;
  458.  
  459.         }
  460.  
  461.     }
  462.  
  463. }
  464.  
  465.