001/*
002 * (C) Copyright 2006-2008 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     troger
016 *
017 * $Id$
018 */
019
020package org.nuxeo.ecm.platform.annotations.gwt.client;
021
022import org.nuxeo.ecm.platform.annotations.gwt.client.configuration.WebConfiguration;
023import org.nuxeo.ecm.platform.annotations.gwt.client.controler.AnnotationController;
024import org.nuxeo.ecm.platform.annotations.gwt.client.view.AnnotationManagerPanel;
025import org.nuxeo.ecm.platform.annotations.gwt.client.view.HideManagerButton;
026
027import com.google.gwt.dom.client.BaseElement;
028import com.google.gwt.dom.client.Document;
029import com.google.gwt.user.client.ui.DockPanel;
030import com.google.gwt.user.client.ui.Frame;
031import com.google.gwt.user.client.ui.RootPanel;
032
033/**
034 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
035 */
036public class AnnotationApplication {
037
038    private static WebConfiguration WEB_CONFIGURATION;
039
040    private static DockPanel applicationPanel = new DockPanel();
041
042    private static Frame PREVIEW_FRAME;
043
044    public static void build(WebConfiguration webConfiguration) {
045        WEB_CONFIGURATION = webConfiguration;
046        buildApplication();
047    }
048
049    private static void buildApplication() {
050        AnnotationConfiguration annotationConfiguration = AnnotationConfiguration.getInstance();
051        Document document = Document.get();
052        BaseElement baseElement = document.getElementsByTagName("base").getItem(0).cast();
053        registerBaseHref(baseElement.getHref());
054        registerAnnoteaServerUrl(annotationConfiguration.getAnnoteaServerUrl());
055        registerDocUrl(annotationConfiguration.getDocumentUrl());
056        registerDateFormatPattern(annotationConfiguration.getDateFormatPattern());
057        notifyAnnoteaServerUrlRegistered();
058
059        applicationPanel.setStyleName("annotationApplicationPanel");
060        applicationPanel.setWidth("100%");
061
062        RootPanel display = RootPanel.get("display");
063        String height = Integer.toString(display.getOffsetHeight()) + "px";
064        applicationPanel.setHeight(height);
065        applicationPanel.setHorizontalAlignment(DockPanel.ALIGN_LEFT);
066
067        PREVIEW_FRAME = new Frame(annotationConfiguration.getPreviewUrl());
068        PREVIEW_FRAME.setStyleName("previewFrame");
069        PREVIEW_FRAME.setWidth("100%");
070        PREVIEW_FRAME.setHeight(height);
071        applicationPanel.add(PREVIEW_FRAME, DockPanel.CENTER);
072        applicationPanel.setCellWidth(PREVIEW_FRAME, "100%");
073
074        AnnotationController controller = new AnnotationController(WEB_CONFIGURATION, false);
075        AnnotationManagerPanel annotationManagerPanel = new AnnotationManagerPanel(controller, WEB_CONFIGURATION);
076        controller.addModelChangeListener(annotationManagerPanel);
077
078        HideManagerButton hideManagerButton = new HideManagerButton(controller, annotationManagerPanel, PREVIEW_FRAME);
079
080        annotationManagerPanel.setWidth("250px");
081        applicationPanel.add(annotationManagerPanel, DockPanel.WEST);
082
083        hideManagerButton.setHeight(height);
084        applicationPanel.add(hideManagerButton, DockPanel.WEST);
085
086        display.add(applicationPanel);
087
088        controller.loadAnnotations();
089    }
090
091    private static native void registerBaseHref(String baseHref) /*-{
092                                                                 top['baseHref'] = baseHref;
093                                                                 }-*/;
094
095    private static native void registerAnnoteaServerUrl(String url) /*-{
096                                                                    top['annoteaServerUrl'] = url;
097                                                                    }-*/;
098
099    private static native void notifyAnnoteaServerUrlRegistered() /*-{
100                                                                  top['annoteaServerUrlRegistered'] = true;
101                                                                  }-*/;
102
103    private static native void registerDocUrl(String docUrl) /*-{
104                                                             top['docUrl'] = docUrl;
105                                                             }-*/;
106
107    private static native void registerDateFormatPattern(String dateFormatPattern) /*-{
108                                                                                   top['dateFormatPattern'] = dateFormatPattern;
109                                                                                   }-*/;
110
111}