    001/* ===========================================================
    002 * Orson Charts : a 3D chart library for the Java(tm) platform
    003 * ===========================================================
    004 *
    005 * (C)opyright 2013-2016, by Object Refinery Limited.  All rights reserved.
    006 *
    007 * http://www.object-refinery.com/orsoncharts/index.html
    008 *
    009 * This program is free software: you can redistribute it and/or modify
    010 * it under the terms of the GNU General Public License as published by
    011 * the Free Software Foundation, either version 3 of the License, or
    012 * (at your option) any later version.
    013 *
    014 * This program is distributed in the hope that it will be useful,
    015 * but WITHOUT ANY WARRANTY; without even the implied warranty of
    016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    017 * GNU General Public License for more details.
    018 *
    019 * You should have received a copy of the GNU General Public License
    020 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    021 *
    022 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates.
    023 * Other names may be trademarks of their respective owners.]
    024 *
    025 * If you do not wish to be bound by the terms of the GPL, an alternative
    026 * commercial license can be purchased.  For details, please see visit the
    027 * Orson Charts home page:
    028 *
    029 * http://www.object-refinery.com/orsoncharts/index.html
    030 *
    031 */
    032
    033package com.orsoncharts.graphics3d;
    034
    035import java.awt.Shape;
    036import java.util.HashMap;
    037import java.util.Map;
    038import com.orsoncharts.util.ArgChecks;
    039
    040/**
    041 * Represents an item or element that has been rendered by the graphics engine.
    042 * Properties can be assigned to the elements in order to provide the
    043 * information required for such things as end user interactivity.
    044 *
    045 * @since 1.3
    046 */
    047public class RenderedElement {
    048
    049    /** A key for the 'type' property.  The value is an Object. */
    050    public static final String TYPE = "type";
    051
    052    /**
    053     * A key for the 'bounds' property (the value is a {@code Shape}).
    054     */
    055    public static final String BOUNDS = "bounds";
    056
    057    /** Properties for the element. */
    058    private final Map<String, Object> properties;
    059
    060    /**
    061     * Creates a new interactive element with the specified type.
    062     *
    063     * @param type  the type ({@code null} not permitted).
    064     * @param bounds  the bounds ({@code null} permitted).
    065     */
    066    public RenderedElement(Object type, Shape bounds) {
    067        ArgChecks.nullNotPermitted(type, "type");
    068        this.properties = new HashMap<String, Object>();
    069        this.properties.put(TYPE, type);
    070        this.properties.put(RenderedElement.BOUNDS, bounds);
    071    }
    072
    073    /**
    074     * Returns the element type, as specified in the constructor.
    075     *
    076     * @return The element type.
    077     */
    078    public Object getType() {
    079        return this.properties.get(TYPE);
    080    }
    081
    082    /**
    083     * Returns the value of the property with the specified key, or
    084     * {@code null}).
    085     *
    086     * @param key  the key ({@code null} not permitted).
    087     *
    088     * @return The property value.
    089     */
    090    public Object getProperty(String key) {
    091        return this.properties.get(key);
    092    }
    093
    094    /**
    095     * Sets the value for a property.
    096     *
    097     * @param key  the property key ({@code null} not permitted).
    098     * @param value  the property value.
    099     */
    100    public void setProperty(String key, Object value) {
    101        this.properties.put(key, value);
    102    }
    103
    104    /**
    105     * Clears all properties for this element.
    106     */
    107    public void clearProperties() {
    108        this.properties.clear();
    109    }
    110
    111    /**
    112     * Returns a string representation of the element, primarily for
    113     * debugging purposes.
    114     *
    115     * @return A string.
    116     */
    117    @Override
    118    public String toString() {
    119        StringBuilder sb = new StringBuilder();
    120        Object type = this.properties.get(TYPE);
    121        sb.append("RenderedElement[").append(type).append(",")
    122                .append(this.properties.entrySet()).append("]");
    123        return sb.toString();
    124    }
    125
    126}