All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.geotools.styling.ExternalGraphicImpl Maven / Gradle / Ivy

/*
 *    GeoTools - The Open Source Java GIS Toolkit
 *    http://geotools.org
 *
 *    (C) 2002-2008, Open Source Geospatial Foundation (OSGeo)
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License as published by the Free Software Foundation;
 *    version 2.1 of the License.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 */
package org.geotools.styling;

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import javax.swing.Icon;
import org.geotools.metadata.iso.citation.OnLineResourceImpl;
import org.geotools.util.Utilities;
import org.opengis.metadata.citation.OnLineResource;
import org.opengis.style.ColorReplacement;
import org.opengis.style.GraphicalSymbol;
import org.opengis.style.StyleVisitor;
import org.opengis.util.Cloneable;

/**
 * @author Ian Turton, CCG
 * @version $Id$
 */
public class ExternalGraphicImpl implements ExternalGraphic, Symbol, Cloneable {
    /** The logger for the default core module. */
    // private static final java.util.logging.Logger LOGGER =
    // org.geotools.util.logging.Logging.getLogger(ExternalGraphicImpl.class);
    private Icon inlineContent;

    private OnLineResource online;

    private URL location = null;
    private String format = null;
    private String uri = null;
    private Map customProps = null;

    private final Set colorReplacements;

    public ExternalGraphicImpl() {
        this(null, null, null);
    }

    public ExternalGraphicImpl(
            Icon icon, Collection replaces, OnLineResource source) {
        this.inlineContent = icon;
        if (replaces == null) {
            colorReplacements = new TreeSet();
        } else {
            colorReplacements = new TreeSet(replaces);
        }
        this.online = source;
    }

    public void setURI(String uri) {
        this.uri = uri;
    }

    @Override
    public String getURI() {
        return this.uri;
    }

    /**
     * Provides the format of the external graphic.
     *
     * @return The format of the external graphic. Reported as its MIME type in a String object.
     */
    public String getFormat() {
        return format;
    }

    /**
     * Provides the URL for where the external graphic resource can be located.
     *
     * @return The URL of the ExternalGraphic
     * @throws MalformedURLException If unable to represent external graphic as a URL
     */
    public java.net.URL getLocation() throws MalformedURLException {
        if (uri == null) {
            return null;
        }
        if (location == null) {
            location = new URL(uri);
        }

        return location;
    }

    /**
     * Setter for property Format.
     *
     * @param format New value of property Format.
     */
    public void setFormat(java.lang.String format) {
        this.format = format;
    }

    /**
     * Setter for property location.
     *
     * @param location New value of property location.
     */
    public void setLocation(java.net.URL location) {
        if (location == null) {
            throw new IllegalArgumentException("ExternalGraphic location URL cannot be null");
        }
        this.uri = location.toString();
        this.location = location;
    }

    public Object accept(StyleVisitor visitor, Object data) {
        return visitor.visit(this, data);
    }

    public void accept(org.geotools.styling.StyleVisitor visitor) {
        visitor.visit(this);
    }

    /**
     * Returns a clone of the ExternalGraphic
     *
     * @see org.geotools.styling.ExternalGraphic#clone()
     */
    public Object clone() {
        try {
            return super.clone();
        } catch (CloneNotSupportedException e) {
            // This will never happen
            throw new AssertionError(e);
        }
    }

    /**
     * Generates a hashcode for the ExternalGraphic
     *
     * @return The hash code.
     */
    public int hashCode() {
        final int PRIME = 1000003;
        int result = 0;

        if (format != null) {
            result = (PRIME * result) + format.hashCode();
        }

        if (uri != null) {
            result = (PRIME * result) + uri.hashCode();
        }

        //        if (inlineContent != null) {
        //            result = (PRIME * result) + inlineContent.hashCode();
        //        }
        //
        //        if (online != null) {
        //            result = (PRIME * result) + online.hashCode();
        //        }
        //
        //        if (replacements != null) {
        //            result = (PRIME * result) + replacements.hashCode();
        //        }

        return result;
    }

    /**
     * Compares this ExternalGraphi with another.
     *
     * 

Two external graphics are equal if they have the same uri and format. * * @param oth The other External graphic. * @return True if this and the other external graphic are equal. */ public boolean equals(Object oth) { if (this == oth) { return true; } if (oth instanceof ExternalGraphicImpl) { ExternalGraphicImpl other = (ExternalGraphicImpl) oth; return Utilities.equals(uri, other.uri) && Utilities.equals(format, other.format); } return false; } public java.util.Map getCustomProperties() { return customProps; } public void setCustomProperties(java.util.Map list) { customProps = list; } public OnLineResource getOnlineResource() { if (online == null) { OnLineResourceImpl impl = new OnLineResourceImpl(); try { impl.setLinkage(new URI(uri)); } catch (URISyntaxException e) { throw new IllegalArgumentException(e); } online = impl; } return online; } public void setOnlineResource(OnLineResource online) { this.online = online; } public Icon getInlineContent() { return inlineContent; } public void setInlineContent(Icon inlineContent) { this.inlineContent = inlineContent; } public Collection getColorReplacements() { return Collections.unmodifiableCollection(colorReplacements); } public Set colorReplacements() { return this.colorReplacements; } static GraphicalSymbol cast(GraphicalSymbol item) { if (item == null) { return null; } else if (item instanceof ExternalGraphicImpl) { return (ExternalGraphic) item; } else if (item instanceof org.opengis.style.ExternalGraphic) { org.opengis.style.ExternalGraphic graphic = (org.opengis.style.ExternalGraphic) item; ExternalGraphicImpl copy = new ExternalGraphicImpl(); copy.colorReplacements().addAll(graphic.getColorReplacements()); copy.setFormat(graphic.getFormat()); copy.setInlineContent(graphic.getInlineContent()); copy.setOnlineResource(graphic.getOnlineResource()); return copy; } return null; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy