

package org.dcm4chee.web.common.base;

import org.apache.wicket.Application;
import org.apache.wicket.IPageMap;
import org.apache.wicket.ResourceReference;
import org.apache.wicket.markup.html.CSSPackageResource;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.resources.CompressedResourceReference;
import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.resource.loader.ClassStringResourceLoader;
import org.apache.wicket.resource.loader.PackageStringResourceLoader;

/**
 * @author Franz Willer <franz.willer@gmail.com>
 * @version $Revision$ $Date$
 * @since July 7, 2009
 */
public class BaseWicketPage extends WebPage {

    private static final ResourceReference CSS = new CompressedResourceReference(BaseWicketPage.class, "base-style.css");
    
    protected ModuleSelectorPanel selectorPanel;
    
    public BaseWicketPage() {
        super();
        initLayout();
    }

    public BaseWicketPage(IPageMap pageMap) {
        super(pageMap);       
        initLayout();
    }
    
    private void initLayout() {

        if ( BaseWicketPage.CSS != null)
            add(CSSPackageResource.getHeaderContribution(BaseWicketPage.CSS));

        add( new Label("app_browser_title", new AbstractReadOnlyModel<Object>() {

            private static final long serialVersionUID = 1L;

            @Override
            public Object getObject() {
                return getBrowserTitle();
            }
        } ));

        add(selectorPanel = new ModuleSelectorPanel("modules"));
    }

    public ModuleSelectorPanel getModuleSelectorPanel() {
        return selectorPanel;
    }

    protected String getBrowserTitle() {
        Class<?> clazz = Application.get().getHomePage();
        String s = new ClassStringResourceLoader(clazz).loadStringResource(null, "application.browser_title");
        if (s==null) {
            s = new PackageStringResourceLoader().loadStringResource(clazz, "application.browser_title", 
                    getSession().getLocale(), null);
        }
        return s == null ? "DCM4CHEE" : s;
    }
}
