// NMI's Java Code Viewer 5.1 © 1997-2001 B. Lemaire
// http://njcv.htmlplanet.com - info@njcv.htmlplanet.com

// Copy registered to Evaluation Copy                                   

// Source File Name:   dadexc.java

import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.*;
import java.awt.event.*;
import java.io.PrintStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Vector;

public class dadexc extends Applet
    implements MouseListener, MouseMotionListener {

    protected static final String version_string = "2000.05.07";
    dadexc myApplet;
    Image image;
    int imageWidth;
    int imageHeight;
    int wordgap;
    dadexc$Word dragWord;
    Point wordStartLocation;
    Point mouseStartLocation;
    protected Label lblStatus;
    protected String default_correct_feedback;
    protected String default_wrong_feedback;
    protected int nWrong;
    protected boolean isFinished;
    protected AudioClip welcome_snd;
    protected AudioClip finished_snd;
    protected AudioClip correct_snd;
    protected AudioClip wrong_snd;
    protected AudioClip bumpy_snd;
    protected dadexc$MyFeedback myFeedback;
    protected Vector drops;
    protected dadexc$DraggerThread dragger;
    protected Vector words;

    public void init() {
        myApplet = this;
        setBackground(Color.white);
        setLayout(new dadexc$MyLayout2(this));
        myFeedback = new dadexc$MyFeedback(this);
        try {
            String s1 = getParameter("image");
            image = getImage(new URL(getCodeBase(), s1));
        }
        catch(MalformedURLException malformedurlexception) {
            image = null;
            System.out.println("MalformedURLException: " + malformedurlexception.getMessage());
            return;
        }
        String s;
        if((s = getParameter("welcome_snd")) != null)
            welcome_snd = getAudioClip(getDocumentBase(), s);
        if((s = getParameter("finished_snd")) != null)
            finished_snd = getAudioClip(getDocumentBase(), s);
        if((s = getParameter("correct_snd")) != null)
            correct_snd = getAudioClip(getDocumentBase(), s);
        if((s = getParameter("wrong_snd")) != null)
            wrong_snd = getAudioClip(getDocumentBase(), s);
        if((s = getParameter("bumpy_snd")) != null)
            bumpy_snd = getAudioClip(getDocumentBase(), s);
        Color color = Color.gray;
        if((s = getParameter("wordbg")) != null)
            color = new Color(Integer.parseInt(s, 16));
        if((s = getParameter("wordgap")) != null)
            wordgap = Integer.parseInt(s);
        for(int i = 1; i < 100; i++) {
            String s2 = getParameter("word" + i);
            if(s2 == null)
                break;
            dadexc$Word dadexc$word = new dadexc$Word(this, i, s2, color);
            words.addElement(dadexc$word);
            add(dadexc$word);
            dadexc$word.setVisible(true);
        }

        for(int j = 1; j < 100; j++) {
            String s3 = getParameter("drop" + j);
            if(s3 == null)
                break;
            dadexc$Drop dadexc$drop = new dadexc$Drop(this, j, s3);
            drops.addElement(dadexc$drop);
        }

        if((s = getParameter("correct")) != null)
            default_correct_feedback = s;
        if((s = getParameter("wrong")) != null)
            default_wrong_feedback = s;
        add(lblStatus = new Label());
        addMouseListener(this);
        addMouseMotionListener(this);
    }

    protected void init_feedback() {
        int i = getSize().width;
        int j = getSize().height;
        myFeedback.pack();
        Dimension dimension = getToolkit().getScreenSize();
        myFeedback.setLocation((dimension.width - i) / 3, (dimension.height - j) / 3);
    }

    public void start() {
        super.start();
        mylayout();
        for(int i = 0; i < words.size(); i++)
            ((dadexc$Word)words.elementAt(i)).reset();

        for(int j = 0; j < drops.size(); j++)
            ((dadexc$Drop)drops.elementAt(j)).reset();

        nWrong = 0;
        isFinished = false;
        (dragger = new dadexc$DraggerThread(this)).start();
        lblStatus.setText("Bienvenue  !");
        welcome_feedback();
    }

    public void stop() {
        dragger.setStopFlag();
        super.stop();
        myFeedback.close();
    }

    protected void mylayout() {
        imageWidth = image.getWidth(this);
        imageHeight = image.getHeight(this);
        int i = 0;
        int j = 3;
        int k = 3;
        for(; i < words.size(); i++) {
            dadexc$Word dadexc$word = (dadexc$Word)words.elementAt(i);
            dadexc$word.setSize(dadexc$word.getPreferredSize());
            if(k + dadexc$word.getSize().width > imageWidth) {
                j += dadexc$word.getSize().height;
                k = 3;
            }
            dadexc$word.setLocation(k, j);
            dadexc$word.setVisible(true);
            k += dadexc$word.getSize().width + wordgap;
        }

        lblStatus.setSize(getSize().width, getSize().height - imageHeight);
        lblStatus.setLocation(0, imageHeight + 1);
        lblStatus.setVisible(true);
        validate();
        repaint();
        init_feedback();
    }

    public void paint(Graphics g) {
        if(image != null) {
            g.drawImage(image, 0, 0, this);
            g.draw3DRect(0, 0, imageWidth - 1, imageHeight - 1, true);
        }
        for(int i = 0; i < drops.size(); i++)
            ((dadexc$Drop)drops.elementAt(i)).paint(g);

    }

    public void mouseClicked(MouseEvent mouseevent) {
        if(!myFeedback.isVisible()) {
            myFeedback.popup(null, "Vous avez fait " + nWrong + " erreur" + (nWrong == 1 ? "" : "s") + "\n\n" + "Recharger la page pour rejouer");
            return;
        } else {
            myFeedback.close();
            return;
        }
    }

    public void mouseEntered(MouseEvent mouseevent) {
    }

    public void mouseExited(MouseEvent mouseevent) {
    }

    public void mousePressed(MouseEvent mouseevent) {
    }

    public void mouseReleased(MouseEvent mouseevent) {
        if(dragWord != null)
            dragWord.onDrop();
    }

    public void mouseDragged(MouseEvent mouseevent) {
        if(dragWord == null)
            return;
        if(!contains(mouseevent.getX(), mouseevent.getY())) {
            return;
        } else {
            dragger.setLocation((wordStartLocation.x - mouseStartLocation.x) + mouseevent.getX(), (wordStartLocation.y - mouseStartLocation.y) + mouseevent.getY());
            return;
        }
    }

    public void mouseMoved(MouseEvent mouseevent) {
    }

    public boolean imageUpdate(Image image1, int i, int j, int k, int l, int i1) {
        mylayout();
        return super.imageUpdate(image1, i, j, k, l, i1);
    }

    protected String getFeedback(int i, int j) {
        String s = getParameter(i + "on" + j);
        if(s == null)
            s = i != j ? default_wrong_feedback : default_correct_feedback;
        return s.replace('\\', '\n');
    }

    protected void welcome_feedback() {
        if(welcome_snd != null)
            welcome_snd.play();
        myFeedback.popup(null, "Commencer", "- Schéma \340 annoter -\n\nMettre les mots \340 la bonne place\n\n\n\n\n Version 2000.05.07\n\n (c) 2000 T. Poschwatta, tp@fonz.de\n");
    }

    protected void finished_feedback() {
        if(finished_snd != null)
            finished_snd.play();
        myFeedback.popup(null, "Fermer", "FELICITATION!\nL exercice est fini !\nVous avez fait " + nWrong + (nWrong != 1 ? " erreurs." : " erreur."));
    }

    protected void correct_feedback(int i) {
        if(correct_snd != null)
            correct_snd.play();
        update_status();
        for(int j = 0; j < words.size(); j++)
            if(!((dadexc$Word)words.elementAt(j)).correct)
                return;

        isFinished = true;
        update_status();
        finished_feedback();
    }

    protected void wrong_feedback(int i, int j) {
        if(wrong_snd != null)
            wrong_snd.play();
        nWrong++;
        update_status();
        myFeedback.popup(null, null, getFeedback(i, j));
    }

    protected void bumpy_feedback() {
        if(bumpy_snd != null)
            bumpy_snd.play();
        myFeedback.popup(null, "Continue", "The word overlaps with two or more placeholders, it's not\nclear where you think the right position is.\nPlease move the word so that it only touches one single\nplaceholder.");
    }

    protected void update_status() {
        if(isFinished) {
            lblStatus.setText(nWrong + " erreur" + (nWrong == 1 ? "" : "s") + ". recharger la page pour rejouer.");
            return;
        } else {
            lblStatus.setText(nWrong + " erreur" + (nWrong == 1 ? "" : "s"));
            return;
        }
    }

    public dadexc() {
        wordgap = 1;
        default_correct_feedback = "Correct";
        default_wrong_feedback = "Faux";
        drops = new Vector();
        words = new Vector();
    }
}

