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.annotations.gwt.client.util;
021
022import com.google.gwt.dom.client.Document;
023import com.google.gwt.dom.client.Element;
024import com.google.gwt.dom.client.ImageElement;
025
026/**
027 * @author <a href="mailto:arussel@nuxeo.com">Alexandre Russel</a>
028 */
029public class ImageRangeXPointer implements XPointer {
030
031    private final String url;
032
033    private final String path;
034
035    private final Point topLeft;
036
037    private final Point bottomRight;
038
039    private String xpointerString;
040
041    public ImageRangeXPointer(String xpointer) {
042        xpointerString = xpointer;
043        this.url = xpointer.substring(0, xpointer.indexOf("#"));
044        xpointer = xpointer.replaceFirst(".*image-range\\(", "");
045        xpointer = xpointer.replaceFirst("\\)\\)$", "");
046        String[] args = xpointer.split(",");
047        this.path = args[0];
048        this.topLeft = new Point(args[1] + "," + args[2]);
049        this.bottomRight = new Point(args[3] + "," + args[4]);
050    }
051
052    public String getMethod() {
053        return "image-range";
054    }
055
056    public String getUrl() {
057        return url;
058    }
059
060    public String getXPath() {
061        return path;
062    }
063
064    public ImageElement getImage() {
065        return getImage(false);
066    }
067
068    public ImageElement getImage(boolean multiImage) {
069        Document document = Document.get();
070        if (!multiImage) {
071            String idablePath = XPathUtil.toIdableName(path);
072            Element div = document.getElementById(idablePath);
073            if (div == null) {
074                return null;
075            }
076            return (ImageElement) div.getFirstChild();
077        } else {
078            return (ImageElement) document.getElementById("annotationRootImage");
079        }
080    }
081
082    public Point getTopLeft() {
083        return topLeft;
084    }
085
086    public Point getBottomRight() {
087        return bottomRight;
088    }
089
090    public String getXpointerString() {
091        return xpointerString;
092    }
093
094    @Override
095    public boolean equals(Object obj) {
096        if (!(obj instanceof ImageRangeXPointer)) {
097            return false;
098        }
099        ImageRangeXPointer xp = (ImageRangeXPointer) obj;
100        return xpointerString.equals(xp.xpointerString);
101    }
102
103    @Override
104    public int hashCode() {
105        int result = 17;
106        result += 17 * xpointerString.hashCode();
107        return result;
108    }
109
110}