package hu.pazsitz.pacuse.pageObjects;

import hu.pazsitz.pacuse.pages.AbstractPage;
import hu.pazsitz.pacuse.pages.AbstractWidget;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.PageFactory;

/**
 * AbstractPageObject.java
 *
 * @author Zoltan Pazsit <pazsitz@pazsitz.hu>
 * @copyright Copyright (c) 2014, Zoltan Pazsit
 */
public abstract class AbstractPageObject<P extends AbstractPage> {
    protected WebDriver webDriver;
    protected P page;

    public AbstractPageObject(Class<P> pageClass, WebDriver webDriver) {
        this.webDriver = webDriver;
        instantiatePage(pageClass, webDriver);
    }

    public AbstractPageObject(Class<P> pageClass, WebDriver webDriver, boolean implicitLoadPage) {
        this.webDriver = webDriver;
        instantiatePage(pageClass, webDriver);
        if (implicitLoadPage) {
            loadPage();
        }
    }

    private void instantiatePage(Class<P> page, WebDriver webDriver) {
        try {
            this.page = page.getDeclaredConstructor(WebDriver.class).newInstance(webDriver);
        } catch (Exception e) {
            e.printStackTrace();
        }
        PageFactory.initElements(webDriver, this.page);
    }
    
    /**
     * Creates the widgetObject
     * @param clazz Class< ? extends {@link AbstractWidgetObject<?>} >
     * @param widget ideally comes from the Page Model object page.getXYWidget()
     * @return < ? extends {@link AbstractWidgetObject} >
     */
	protected <W extends AbstractWidgetObject<? extends AbstractWidget>> W registerWidgetObject(Class<W> clazz, AbstractWidget widget) {
		W widgetObject = null;
		
		try {
			widgetObject = clazz.getDeclaredConstructor(
					widget.getClass(), WebDriver.class).newInstance(
						widget, webDriver);
		} catch (Exception e) {
			// TODO log4j
			e.printStackTrace();
		}

		return widgetObject; 
	}

    protected void loadPage() {
        String pageUrl = page.getUrl();
        page.getWebDriver().get(pageUrl);
    }

    public P getPage() {
        return page;
    }
}

@@ -126,16 +126,63 @@
 idget;%0A%0A
+import org.openqa.selenium.JavascriptExecutor;%0A
 import o
@@ -211,16 +211,16 @@
 Driver;%0A
-
 import o
@@ -260,16 +260,61 @@
 ctory;%0A%0A
+import com.google.common.collect.Iterables;%0A%0A
 /**%0A * A
@@ -560,16 +560,51 @@
  P page;
+%0A    private String prevPageHandle;
 %0A%0A    pu
@@ -1991,16 +1991,76 @@
 t; %0A%09%7D%0A%0A
+%09/**%0A%09 * Loads the actual page by the defined page url%0A%09 */%0A
     prot
@@ -2119,24 +2119,24 @@
 e.getUrl();%0A
-
         page
@@ -2162,32 +2162,1921 @@
 (pageUrl);%0A    %7D
+%0A    %0A    /**%0A     * Loads the actual page by the defined page url%0A     * and appends the given parameters%0A     * @param additionalParams%0A     */%0A    protected void loadPage(String additionalParams) %7B%0A    %09String pageUrl = page.getUrl() + additionalParams;%0A    %09page.getWebDriver().get(pageUrl);%0A    %7D%0A    %0A    /**%0A     * Loads the actual page by the given page url%0A     * and appends the given parameters if any given%0A     * @param additionalParams%0A     */%0A    protected void loadPage(String url, String additionalParams) %7B%0A    %09if (additionalParams == null) additionalParams = %22%22;%0A    %09%0A    %09String pageUrl = url + additionalParams;%0A    %09page.getWebDriver().get(pageUrl);%0A    %7D%0A    %0A    /**%0A     * Opens and loads the given content in a new window%0A     * to navigate back use the this.goToPreviousPage()%0A     * @param url%0A     * @return newly opened page handle Identifier String%0A     */%0A    public String loadPageInNewWindow(String url) %7B%0A        prevPageHandle = webDriver.getWindowHandle();%0A        JavascriptExecutor jsExecutor = (JavascriptExecutor) webDriver;%0A        jsExecutor.executeScript(%22window.open(%5C%22%22 + url + %22%5C%22)%22);%0A        String connectorPageHandle = Iterables.getLast(webDriver.getWindowHandles());%0A       %0A        webDriver.switchTo().window(connectorPageHandle);%0A       %0A        return connectorPageHandle;%0A    %7D%0A    %0A    /**%0A     * Navigate back after page a page is loaded into new window%0A     * optionally closes the actual window%0A     * @param closeActual%0A     * @return boolean%0A     */%0A    public boolean goToPreviousPage(boolean closeActual) %7B%0A    %09if (!prevPageHandle.isEmpty() && webDriver.getWindowHandles().contains(prevPageHandle)) %7B%0A    %09%09if (closeActual && !webDriver.getWindowHandle().equals(prevPageHandle)) %7B%0A    %09%09%09webDriver.close();%0A    %09%09%7D%0A    %09%09webDriver.switchTo().window(prevPageHandle);%0A    %09%09return true;%0A    %09%7D%0A    %09%0A    %09return false;%0A    %7D
 %0A%0A    public P g

add pageload methods to AbstractPageObject