Click
Click Examples

Source Viewer : WEB-INF/classes/org/apache/click/examples/page/control/ImageDemo.java

package org.apache.click.examples.page.control;

import org.apache.click.control.Form;
import org.apache.click.control.ImageSubmit;
import org.apache.click.control.Label;
import org.apache.click.examples.page.BorderPage;

/**
 * Provides an ImageSubmit control example.
 */
public class ImageDemo extends BorderPage {

    private static final long serialVersionUID = 1L;

    private ImageSubmit colorSubmit;

    private Form buttonsForm = new Form("buttonsForm");
    private Form form = new Form("form");

    // Constructor ------------------------------------------------------------

    public ImageDemo() {
        addControl(form);
        addControl(buttonsForm);

        // Buttons Form
        ImageSubmit editSubmit = new ImageSubmit("edit", "/assets/images/edit.gif");
        editSubmit.setListener(this, "onEditClick");
        editSubmit.setTitle("Edit");
        buttonsForm.add(editSubmit);

        ImageSubmit deleteSubmit = new ImageSubmit("delete", "/assets/images/delete.gif");
        deleteSubmit.setListener(this, "onDeleteClick");
        deleteSubmit.setTitle("Delete");
        buttonsForm.add(deleteSubmit);

        // Colors Form
        form.add(new Label("label", "<b>Color Chooser</b>"));

        colorSubmit = new ImageSubmit("save", "/assets/images/colors.gif");
        colorSubmit.setListener(this, "onColorClick");
        form.add(colorSubmit);
    }

    // Event Handlers ---------------------------------------------------------

    public boolean onEditClick() {
        addModel("buttonMsg", "Edit");
        return true;
    }

    public boolean onDeleteClick() {
        addModel("buttonMsg", "Delete");
        return true;
    }

    public boolean onColorClick() {
        int x = colorSubmit.getX();
        int y = colorSubmit.getY();

        String color = "no color";

        if (x > 3 && x < 31) {
            if (y > 3 && y < 31) {
                color = "Red";
            } else if (y > 44 && y < 71) {
                color = "Green";
            }
        } else if (x > 44 && x < 71) {
            if (y > 3 && y < 31) {
                color = "Blue";
            } else if (y > 44 && y < 71) {
                color = "White";
            }
        }

        String colorMsg = "<b>" + color + "</b>. <p/> " +
                          "[ x=" + x + ", y=" + y + " ]";

        addModel("colorMsg", colorMsg);

        return true;
    }
}