001/*
002 * (C) Copyright 2006-2008 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Alexandre Russel
018 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.platform.pictures.tiles.gwt.client.controller;
023
024import org.nuxeo.ecm.platform.pictures.tiles.gwt.client.TilingPreviewConstant;
025import org.nuxeo.ecm.platform.pictures.tiles.gwt.client.model.TilingInfo;
026import org.nuxeo.ecm.platform.pictures.tiles.gwt.client.model.TilingModel;
027
028import com.google.gwt.user.client.Element;
029
030import static org.nuxeo.ecm.platform.pictures.tiles.gwt.client.TilingPreviewConstant.ORG_NUXEO_ECM_PLATFORM_PICTURES_TILES_GWT_CLIENT_POINTER_ADAPTER;
031import static org.nuxeo.ecm.platform.pictures.tiles.gwt.client.TilingPreviewConstant.ORG_NUXEO_ECM_PLATFORM_PICTURES_TILES_GWT_CLIENT_XPOINTER_FILTER;
032
033/**
034 * @author Alexandre Russel
035 */
036public class TilingController {
037
038    private static TilingInfo sourceTilingInfo;
039
040    private static TilingModel model;
041
042    public TilingController(TilingInfo tilingInfo, TilingModel tilingModel) {
043        sourceTilingInfo = new TilingInfo(tilingInfo);
044        model = tilingModel;
045
046        setfilterPath(ORG_NUXEO_ECM_PLATFORM_PICTURES_TILES_GWT_CLIENT_XPOINTER_FILTER);
047        setPointerAdapter(ORG_NUXEO_ECM_PLATFORM_PICTURES_TILES_GWT_CLIENT_POINTER_ADAPTER);
048    }
049
050    public static TilingInfo getSourcetilingInfo() {
051        return sourceTilingInfo;
052    }
053
054    private native void setPointerAdapter(String adapter)/*-{
055                                                         top[adapter] = function(i,j,k,l) {
056                                                         return @org.nuxeo.ecm.platform.pictures.tiles.gwt.client.controller.TilingController::filterPoint(IIII)(i,j,k,l);
057                                                         };
058                                                         }-*/;
059
060    public static String filterPoint(int i, int j, int k, int l) {
061        double zoom = model.getCurrentZoom();
062
063        int startX = model.getViewAreaLeft();
064        int endX = model.getViewAreaLeft() + model.getViewAreaWidth();
065        int startY = model.getViewAreaTop();
066        int endY = model.getViewAreaTop() + model.getViewAreaHeight();
067
068        float zoomedAx = (float) (i * zoom);
069        float zoomedAy = (float) (j * zoom);
070        float zoomedBx = (float) (k * zoom);
071        float zoomedBy = (float) (l * zoom);
072
073        // Window.alert("filterPoint: startX=" + startX + " ,endX=" + endX
074        // + " ,startY=" + startY + " ,endY=" + endY + " ,zoomedAx="
075        // + zoomedAx + " ,zoomedAy=" + zoomedAy + " ,zoomedBx="
076        // + zoomedBx + " ,zoomedBy=" + zoomedBy);
077
078        if (zoomedAx > startX && zoomedBx < endX && zoomedAy > startY && zoomedBy < endY) {
079            int Ax = (int) Math.round(zoomedAx);
080            int Ay = (int) Math.round(zoomedAy);
081            int Bx = Math.round(Ax + (zoomedBx - zoomedAx));
082            int By = Math.round(Ay + (zoomedBy - zoomedAy));
083            // Window.alert("filterPoint: [" + Ax + "," + Ay + "]:[" + Bx + ","
084            // + By + "]");
085            return "[" + Ax + "," + Ay + "]:[" + Bx + "," + By + "]";
086        }
087        return "";
088    }
089
090    public static String filterPath(Element image, String xpath, int i, int j, int k, int l) {
091        String src = image.getAttribute("src");
092        int x = Integer.parseInt(src.substring(src.lastIndexOf("?x=") + 3, src.lastIndexOf("&y=")));
093        int y = Integer.parseInt(src.substring(src.lastIndexOf("&y=") + 3, src.lastIndexOf("&date=")));
094        float zommedAx = i + x * model.getTileWidth();
095        float zommedAy = j + y * model.getTileHeight();
096        float zommedBx = (k - i) + zommedAx;
097        float zommedBy = (l - j) + zommedAy;
098
099        double zoom = model.getCurrentZoom();
100        int finalAx = (int) Math.round(zommedAx / zoom);
101        int finalAy = (int) Math.round(zommedAy / zoom);
102        int finalBx = (int) Math.round(zommedBx / zoom);
103        int finalBy = (int) Math.round(zommedBy / zoom);
104        // Window.alert("filterPath: #xpointer(image-range(//img,[" + finalAx
105        // + "," + finalAy + "],[" + finalBx + "," + finalBy + "]))");
106        return "#xpointer(image-range(//img,[" + finalAx + "," + finalAy + "],[" + finalBx + "," + finalBy + "]))";
107    }
108
109    public void updateAnnotationDecoration() {
110        updateAnnotationView(TilingPreviewConstant.ORG_NUXEO_ECM_PLATFORM_PICTURES_TILES_GWT_CLIENT_UPDATE_ANNOTATED_DOCUMENT);
111    }
112
113    public native void updateAnnotationView(String functionName)/*-{
114                                                                if(functionName && top[functionName]) {
115                                                                top[functionName](true);
116                                                                }
117                                                                }-*/;
118
119    public native void setfilterPath(String xfilter)/*-{
120                                                    top[xfilter] = function(image, xpath, i, j, k, l){
121                                                    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);
122                                                    };
123                                                    }-*/;
124}