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