/*
 * This is part of Geomajas, a GIS framework, http://www.geomajas.org/.
 *
 * Copyright 2008-2013 Geosparc nv, http://www.geosparc.com/, Belgium.
 *
 * The program is available in open source according to the GNU Affero
 * General Public License. All contributions in this program are covered
 * by the Geomajas Contributors License Agreement. For full licensing
 * details, see LICENSE.txt in the project root.
 */
package org.geomajas.puregwt.client.gfx;

import org.geomajas.puregwt.client.service.DomService;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.Widget;

/**
 * <p>
 * Basic HTML element definition used in map rendering. Note that all HtmlObjects are actually GWT widgets.This class
 * will create an HTML element in the constructor and set as many style info as is provided. Also all HtmlObjects will
 * have an absolute positioning.
 * </p>
 * <p>
 * All getters and setters work immediately on the underlying HTML element for performance reasons. Extensions of this
 * class can represent individual HTML tags such as images, but also groups (DIV). In this case of a group (see
 * HtmlContainer) it is possible to attach multiple other HtmlObjects to create a tree structure.
 * </p>
 * 
 * @author Pieter De Graef
 */
public abstract class AbstractHtmlObject implements HtmlObject {

	private Widget widget;

	private double opacity = 1;

	// ------------------------------------------------------------------------
	// Constructors:
	// ------------------------------------------------------------------------

	/**
	 * Create an HtmlObject widget that represents a certain HTML element.
	 * 
	 * @param tagName
	 *            The tag-name of the HTML that should be created (DIV, IMG, ...).
	 */
	public AbstractHtmlObject(Widget widget) {
		this.widget = widget;
		DOM.setStyleAttribute(widget.getElement(), "position", "absolute");
		DOM.setStyleAttribute(widget.getElement(), "width", "100%");
		DOM.setStyleAttribute(widget.getElement(), "height", "100%");
	}

	/**
	 * Create an HtmlObject widget that represents a certain HTML element.
	 * 
	 * @param tagName
	 *            The tag-name of the HTML that should be created (DIV, IMG, ...).
	 * @param width
	 *            The width for this element, expressed in pixels.
	 * @param height
	 *            The height for this element, expressed in pixels.
	 */
	public AbstractHtmlObject(Widget widget, int width, int height) {
		this.widget = widget;
		DOM.setStyleAttribute(widget.getElement(), "position", "absolute");
		DOM.setStyleAttribute(widget.getElement(), "width", width + "px");
		DOM.setStyleAttribute(widget.getElement(), "height", height + "px");
	}

	/**
	 * Create an HtmlObject widget that represents a certain HTML element.
	 * 
	 * @param tagName
	 *            The tag-name of the HTML that should be created (DIV, IMG, ...).
	 * @param width
	 *            The width for this element, expressed in pixels.
	 * @param height
	 *            The height for this element, expressed in pixels.
	 * @param top
	 *            How many pixels should this object be placed from the top (relative to the parent origin).
	 * @param left
	 *            How many pixels should this object be placed from the left (relative to the parent origin).
	 */
	public AbstractHtmlObject(Widget widget, int width, int height, int top, int left) {
		this.widget = widget;
		DOM.setStyleAttribute(widget.getElement(), "position", "absolute");
		DOM.setStyleAttribute(widget.getElement(), "width", width + "px");
		DOM.setStyleAttribute(widget.getElement(), "height", height + "px");
		DomService.setTop(widget.getElement(), top);
		DomService.setLeft(widget.getElement(), left);
	}

	// ------------------------------------------------------------------------
	// Getters and setters:
	// ------------------------------------------------------------------------

	public Widget getParent() {
		return widget.getParent();
	}

	public int getWidth() {
		return sizeToInt(DOM.getStyleAttribute(widget.getElement(), "width"));
	}

	public void setWidth(int width) {
		DOM.setStyleAttribute(widget.getElement(), "width", width + "px");
	}

	public int getHeight() {
		return sizeToInt(DOM.getStyleAttribute(widget.getElement(), "height"));
	}

	public void setHeight(int height) {
		DOM.setStyleAttribute(widget.getElement(), "height", height + "px");
	}

	public int getLeft() {
		return sizeToInt(DOM.getStyleAttribute(widget.getElement(), "left"));
	}

	public void setLeft(int left) {
		DomService.setLeft(widget.getElement(), left);
	}

	public int getTop() {
		return sizeToInt(DOM.getStyleAttribute(widget.getElement(), "top"));
	}

	public void setTop(int top) {
		DomService.setTop(widget.getElement(), top);
	}

	public double getOpacity() {
		return opacity;
	}

	public void setOpacity(double opacity) {
		this.opacity = opacity;
		DOM.setStyleAttribute(widget.getElement(), "filter", "alpha(opacity=" + (opacity * 100) + ")");
		DOM.setStyleAttribute(widget.getElement(), "opacity", Double.toString(opacity));
	}

	// ------------------------------------------------------------------------
	// Private methods:
	// ------------------------------------------------------------------------

	@Override
	public Widget asWidget() {
		return widget;
	}

	@Override
	public void setVisible(boolean visible) {
		widget.setVisible(visible);
	}

	@Override
	public boolean isVisible() {
		return widget.isVisible();
	}

	private int sizeToInt(String size) {
		int position = size.indexOf('p');
		if (position < 0) {
			return Integer.parseInt(size);
		}
		return Integer.parseInt(size.substring(0, position));
	}
}
@@ -1797,220 +1797,25 @@
 this
-.widget = widget;%0A%09%09DOM.setStyleAttribute(widget.getElement(), %22position%22, %22absolute%22);%0A%09%09DOM.setStyleAttribute(widget.getElement(), %22width%22, %22100%25%22);%0A%09%09DOM.setStyleAttribute(widget.getElement(), %22height%22, %22
+(widget, 100, 
 100
-%25%22
 );%0A%09

Fix PURE-162 - A constructor of AbstractHtmlObject creates an invalid instance (can trigger a NumberFormatException)