/*
 *   
 *
 * Copyright  1990-2006 Sun Microsystems, Inc. All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License version
 * 2 only, as published by the Free Software Foundation. 
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License version 2 for more details (a copy is
 * included at /legal/license.txt). 
 * 
 * You should have received a copy of the GNU General Public License
 * version 2 along with this work; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA 
 * 
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
 * Clara, CA 95054 or visit www.sun.com if you need additional
 * information or have any questions. 
 */

package com.sun.midp.lcdui;

import com.sun.midp.events.EventTypes;
import com.sun.midp.events.Event;

import javax.microedition.lcdui.Display;

/**
 * An subclass for events of REPAINT_EVENT type. These events are generated by
 * LCDUI for its own purposes.
 */
class RepaintEvent extends Event {
    /** X1 for the paint event. */
    int paintX1;
    /** Y1 for the paint event. */
    int paintY1;
    /** X2 for the paint event. */
    int paintX2;
    /** Y2 for the paint event. */
    int paintY2;
    /** Target for the paint event. */
    Object paintTarget;
    /** Per use ID for tracking events in serviceRepaints. */
    int perUseID;
    /** Target display of the event. */
    DisplayEventConsumer display;

    /**
     * Construct an event that has no parameters.
     *
     * @param type event ID type
     */
    private RepaintEvent(int type) {
        super(type);
    }

    /**
     * Create a repaint event.
     *
     * @param d The Display
     * @param x The x origin coordinate
     * @param y The y origin coordinate
     * @param w The width
     * @param h The height
     * @param target The optional paint target
     *
     * @return initialized event
     */
    static RepaintEvent createRepaintEvent(DisplayEventConsumer d,
                                         int x, int y, int w, int h,
                                         Object target) {
        RepaintEvent e = new RepaintEvent(EventTypes.REPAINT_EVENT);
        e.setRepaintFields(d, x, y, w, h, target);
        return e;
    }

    /**
     * Set the fields of a repaint event.
     *
     * @param d The Display
     * @param x The x origin coordinate
     * @param y The y origin coordinate
     * @param w The width
     * @param h The height
     * @param target The optional paint target
     */
    void setRepaintFields(DisplayEventConsumer d,
                          int x, int y, int w, int h, Object target) {
        display = d;

        w += x; // convert from width, height to absolute
        h += y; //  x2, y2

        if (x < 0) {
            x = 0;
        }

        if (y < 0) {
            y = 0;
        }

        paintX1 = x;
        paintY1 = y;
        paintX2 = w;
        paintY2 = h;
        paintTarget = target;
    }
}

