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 *     Alexandre Russel
016 *
017 * $Id$
018 */
019
020package org.nuxeo.ecm.platform.pictures.tiles.gwt.client.controller;
021
022import org.nuxeo.ecm.platform.pictures.tiles.gwt.client.TilingPreviewConstant;
023import org.nuxeo.ecm.platform.pictures.tiles.gwt.client.model.TilingInfo;
024import org.nuxeo.ecm.platform.pictures.tiles.gwt.client.model.TilingModel;
025
026import com.google.gwt.user.client.Element;
027
028import static org.nuxeo.ecm.platform.pictures.tiles.gwt.client.TilingPreviewConstant.ORG_NUXEO_ECM_PLATFORM_PICTURES_TILES_GWT_CLIENT_POINTER_ADAPTER;
029import static org.nuxeo.ecm.platform.pictures.tiles.gwt.client.TilingPreviewConstant.ORG_NUXEO_ECM_PLATFORM_PICTURES_TILES_GWT_CLIENT_XPOINTER_FILTER;
030
031/**
032 * @author Alexandre Russel
033 */
034public class TilingController {
035
036    private static TilingInfo sourceTilingInfo;
037
038    private static TilingModel model;
039
040    public TilingController(TilingInfo tilingInfo, TilingModel tilingModel) {
041        sourceTilingInfo = new TilingInfo(tilingInfo);
042        model = tilingModel;
043
044        setfilterPath(ORG_NUXEO_ECM_PLATFORM_PICTURES_TILES_GWT_CLIENT_XPOINTER_FILTER);
045        setPointerAdapter(ORG_NUXEO_ECM_PLATFORM_PICTURES_TILES_GWT_CLIENT_POINTER_ADAPTER);
046    }
047
048    public static TilingInfo getSourcetilingInfo() {
049        return sourceTilingInfo;
050    }
051
052    private native void setPointerAdapter(String adapter)/*-{
053                                                         top[adapter] = function(i,j,k,l) {
054                                                         return @org.nuxeo.ecm.platform.pictures.tiles.gwt.client.controller.TilingController::filterPoint(IIII)(i,j,k,l);
055                                                         };
056                                                         }-*/;
057
058    public static String filterPoint(int i, int j, int k, int l) {
059        double zoom = model.getCurrentZoom();
060
061        int startX = model.getViewAreaLeft();
062        int endX = model.getViewAreaLeft() + model.getViewAreaWidth();
063        int startY = model.getViewAreaTop();
064        int endY = model.getViewAreaTop() + model.getViewAreaHeight();
065
066        float zoomedAx = (float) (i * zoom);
067        float zoomedAy = (float) (j * zoom);
068        float zoomedBx = (float) (k * zoom);
069        float zoomedBy = (float) (l * zoom);
070
071        // Window.alert("filterPoint: startX=" + startX + " ,endX=" + endX
072        // + " ,startY=" + startY + " ,endY=" + endY + " ,zoomedAx="
073        // + zoomedAx + " ,zoomedAy=" + zoomedAy + " ,zoomedBx="
074        // + zoomedBx + " ,zoomedBy=" + zoomedBy);
075
076        if (zoomedAx > startX && zoomedBx < endX && zoomedAy > startY && zoomedBy < endY) {
077            int Ax = (int) Math.round(zoomedAx);
078            int Ay = (int) Math.round(zoomedAy);
079            int Bx = Math.round(Ax + (zoomedBx - zoomedAx));
080            int By = Math.round(Ay + (zoomedBy - zoomedAy));
081            // Window.alert("filterPoint: [" + Ax + "," + Ay + "]:[" + Bx + ","
082            // + By + "]");
083            return "[" + Ax + "," + Ay + "]:[" + Bx + "," + By + "]";
084        }
085        return "";
086    }
087
088    public static String filterPath(Element image, String xpath, int i, int j, int k, int l) {
089        String src = image.getAttribute("src");
090        int x = Integer.parseInt(src.substring(src.lastIndexOf("?x=") + 3, src.lastIndexOf("&y=")));
091        int y = Integer.parseInt(src.substring(src.lastIndexOf("&y=") + 3, src.lastIndexOf("&date=")));
092        float zommedAx = i + x * model.getTileWidth();
093        float zommedAy = j + y * model.getTileHeight();
094        float zommedBx = (k - i) + zommedAx;
095        float zommedBy = (l - j) + zommedAy;
096
097        double zoom = model.getCurrentZoom();
098        int finalAx = (int) Math.round(zommedAx / zoom);
099        int finalAy = (int) Math.round(zommedAy / zoom);
100        int finalBx = (int) Math.round(zommedBx / zoom);
101        int finalBy = (int) Math.round(zommedBy / zoom);
102        // Window.alert("filterPath: #xpointer(image-range(//img,[" + finalAx
103        // + "," + finalAy + "],[" + finalBx + "," + finalBy + "]))");
104        return "#xpointer(image-range(//img,[" + finalAx + "," + finalAy + "],[" + finalBx + "," + finalBy + "]))";
105    }
106
107    public void updateAnnotationDecoration() {
108        updateAnnotationView(TilingPreviewConstant.ORG_NUXEO_ECM_PLATFORM_PICTURES_TILES_GWT_CLIENT_UPDATE_ANNOTATED_DOCUMENT);
109    }
110
111    public native void updateAnnotationView(String functionName)/*-{
112                                                                if(functionName && top[functionName]) {
113                                                                top[functionName](true);
114                                                                }
115                                                                }-*/;
116
117    public native void setfilterPath(String xfilter)/*-{
118                                                    top[xfilter] = function(image, xpath, i, j, k, l){
119                                                    return @org.nuxeo.ecm.platform.pictures.tiles.gwt.client.controller.TilingController::filterPath(Lcom/google/gwt/user/client/Element;Ljava/lang/String;IIII)(image,xpath,i,j,k,l);
120                                                    };
121                                                    }-*/;
122}