/***********************************************************************
 * MicrochipsCircus.java - Source Code (requires java 1.1)             *
 * Version release date : June 24, 2003                                *
 * Copyright (C) 2001-2003  Neural Semantics sprl, Belgium             *
 * Author : Michel Petre mpetre@neuralsemantics.com                    *
 *                                                                     *
 *     http://www.neuralsemantics.com/                                 *
 *                                                                     *
 * This code is released under the GNU GPL license, version 2 or       *
 * later, for educational and non-commercial purposes only.            *
 * If any part of the code is to be included in a commercial           *
 * software, please contact us first for a clearance at                *
 * info@neuralsemantics.com                                            *
 *                                                                     *
 *   This notice must remain intact in all copies of this code.        *
 *   This code is distributed WITHOUT ANY WARRANTY OF ANY KIND.        *
 *   The GNU GPL license can be found at :                             *
 *           http://www.gnu.org/copyleft/gpl.html                      *
 *                                                                     *
 ***********************************************************************/

 /* History & changes **************************************************
 *                                                                     *
 ******** July 20, 2001 ************************************************
 *   - First release                                                   *
 ******** June 24, 2003 ************************************************
 *   - Changed hyperlink panel                                         *
 *   - added welcome text parameter                                    *
 ***********************************************************************/


import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class MicrochipsCircus extends Applet 
                              implements ActionListener, 
                                         AdjustmentListener,
                                         MouseListener {
  // program name
  final static String TITLE = "The Microchips Circus";

  // Line separator char
  final static String LSEP = System.getProperty("line.separator");

  // standard foreground color for the labels
  private Color fgColor = Color.white; 

  // default font size
  private int fontSize = 12;

  // hyperlink objects
  private Panel linkPanel;
  private Label labelNS;
  private Color inactiveLinkColor = new Color(255,255,128);
  private Color activeLinkColor = Color.white; 
  private Font linkFont = new Font("Dialog", Font.PLAIN, fontSize);

  private Panel control;
  private MicrochipsPlayer player;
  private Button buttonClear = new Button("Clear");
  private Scrollbar barDelta, barMask;

  private int valueDelta = 5;
  private int valueMask = 0;

  private int chipWidth = 80;
  private int chipHeight = 80;

  private String sWelcome;

  private AppletContext appletContext;

//**********************************************************************
//                            Constructors
//**********************************************************************
  public MicrochipsCircus() {
  }

//**********************************************************************
//                              Methods
//**********************************************************************
  public void init() {
    setLayout(new BorderLayout());
    setBackground(Color.black);

    try {
      appletContext = getAppletContext();
      this.setAppletParams();
    }
    catch(Exception e) { 
      appletContext = null;
    }

    this.composeControl();
    this.composeLink();

    player = new MicrochipsPlayer();
    if (appletContext == null)
      player.setSize(500, 350);
    player.setChipSize(chipWidth, chipHeight);
    player.setDelta(valueDelta);
    player.setMask(valueMask);
    player.setText(sWelcome);

    Panel p = new Panel();
    p.setLayout(new BorderLayout());
    p.add("North", linkPanel);
    p.add("South", control);

    add("Center", player);
    add("South", p);

    player.start();
  }
//------------------------------------------------------------------------
  private void composeControl() {
    control = new Panel();

    Font f = new Font("Dialog", Font.PLAIN, fontSize);

    Label labelDelta = new Label("Sharpness");
    labelDelta.setForeground(fgColor);
    labelDelta.setFont(f);
    labelDelta.setAlignment(Label.CENTER);

    barDelta = new Scrollbar(Scrollbar.HORIZONTAL, valueDelta, 3, 0, 40);
    barDelta.addAdjustmentListener(this);
    // assign fixed size to the scrollbar
    Panel pBarDelta = new Panel();
    pBarDelta.setLayout(null);
    pBarDelta.add(barDelta);
    barDelta.setSize(90, 16);
    pBarDelta.setSize(90, 16);

    Panel pDelta = new Panel();
    pDelta.add(labelDelta);
    pDelta.add(pBarDelta);

    Label labelMask = new Label("Colors");
    labelMask.setForeground(fgColor);
    labelMask.setFont(f);
    labelMask.setAlignment(Label.CENTER);

    barMask = new Scrollbar(Scrollbar.HORIZONTAL, valueMask, 3, 0, 7);
    barMask.addAdjustmentListener(this);
    // assign fixed size to the scrollbar
    Panel pBarMask = new Panel();
    pBarMask.setLayout(null);
    pBarMask.add(barMask);
    barMask.setSize(90, 16);
    pBarMask.setSize(90, 16);

    Panel pMask = new Panel();
    pMask.add(labelMask);
    pMask.add(pBarMask);

    Panel pClear = new Panel();
    pClear.add(new Label("    "));
    pClear.add(buttonClear);
    buttonClear.setFont(new Font("Dialog", Font.PLAIN, 14));
    buttonClear.addActionListener(this);

    control.add(pDelta);
    control.add(pMask);
    control.add(pClear);
  }
//------------------------------------------------------------------------
  private void composeLink() {
    linkPanel = new Panel();
    linkPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 5));
    linkPanel.setFont(linkFont);
    linkPanel.setForeground(Color.yellow);

    Label labelName = new Label(TITLE + " \u00a9");
    labelName.setForeground(inactiveLinkColor);
    labelName.setFont(linkFont);
    labelName.setAlignment(Label.RIGHT);

    labelNS = new Label(" Neural Semantics   ");
    labelNS.setForeground(inactiveLinkColor);
    labelNS.setFont(linkFont);
    labelNS.setAlignment(Label.LEFT);

    Panel pNS = new Panel();
    pNS.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
    pNS.add(labelNS);
    linkPanel.add(labelName);
    linkPanel.add(pNS);

    // link to Neural Semantics website
    if (appletContext != null) {
      String h = getDocumentBase().getHost();
      if ((h.length() > 4) && (h.substring(0, 4).equals("www.")))
        h = h.substring(4);
      if ((h != null) && (! h.startsWith("neuralsemantics.com"))) {
        // create a hand cursor for the hyperlink area
        Cursor linkCursor = new Cursor(Cursor.HAND_CURSOR);
        pNS.setCursor(linkCursor);
        labelName.addMouseListener(this);
        labelNS.addMouseListener(this);
      }
    }
  }
//------------------------------------------------------------------------
  public void stop() {
    player.stop();
  }
//------------------------------------------------------------------------
  public void destroy() {
    player.destroy();
  }

//**********************************************************************
//                     ActionListener Interface
//**********************************************************************
  public void actionPerformed(ActionEvent e) {
    Object source = e.getSource();
    if (source == buttonClear) {
      player.clear();
    }
  }

//**********************************************************************
//                     AdjustmentListener Interface
//**********************************************************************
  public void adjustmentValueChanged(AdjustmentEvent e) {
    Object source = e.getSource();
    if (source == barDelta) {
      player.setDelta(barDelta.getValue());
    }
    else if (source == barMask) {
      player.setMask(barMask.getValue());
    }
  }

//**********************************************************************
//                     MouseListener Interface
//**********************************************************************
  public void mouseClicked(MouseEvent e) {
  }
//-------------------------------------------
  public void mouseEntered(MouseEvent e) {
    // text color rollover
    labelNS.setForeground(activeLinkColor);
    if (appletContext != null)
      showStatus("Click to connect to Neural Semantics home page");
  }
//-------------------------------------------
  public void mouseExited(MouseEvent e) {
    // text color rollover
    labelNS.setForeground(inactiveLinkColor);
    if (appletContext != null)
      showStatus("");
  }
//-------------------------------------------
  public void mousePressed(MouseEvent e) {
    // link to Neural Semantics website
    if (appletContext != null) {
      String h = getDocumentBase().getHost();
      if ((h.length() > 4) && (h.substring(0, 4).equals("www.")))
        h = h.substring(4);
      if ((h != null) && (! h.startsWith("neuralsemantics.com"))) {
        try {
          java.net.URL url = 
            new java.net.URL("http://www.neuralsemantics.com/");
          appletContext.showDocument(url);
        }
        catch(Exception ex){ }
      }
    }
  }
//-------------------------------------------
  public void mouseReleased(MouseEvent e) {
  }

//**********************************************************************
//                             Applet info
//**********************************************************************
  public String getAppletInfo() {
    String s = "The microchips circus" + LSEP + LSEP +
               "A digital art generator applet" + LSEP +
               "Copyright (c) Neural Semantics, 2001-2003" + LSEP + LSEP +
               "Home page : http://www.neuralsemantics.com/";
    return s;
  }
//------------------------------------------------------------------------
  private void setAppletParams() {
    // read the HTML parameters
    String param = getParameter("clipW");
    if (param != null) {
      try {
        chipWidth = Integer.valueOf(param).intValue();
      }
      catch (NumberFormatException e) { }
    }
    param = getParameter("clipH");
    if (param != null) {
      try {
        chipHeight = Integer.valueOf(param).intValue();
      }
      catch (NumberFormatException e) { }
    }
    sWelcome = getParameter("text");
  }

//**********************************************************************
//                           Main method
//**********************************************************************
  public static void main(String[] args) {
    WindowListener w = new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    };
    Frame frame = new Frame();
    MicrochipsCircus applet = new MicrochipsCircus();
    applet.init();
    frame.add(applet);
    frame.addWindowListener(w);
    frame.pack();
    frame.show();
  }
}