001/* A GraphicElement is an atomic piece of a graphical representation.
002
003 Copyright (c) 1999-2014 The Regents of the University of California.
004 All rights reserved.
005 Permission is hereby granted, without written agreement and without
006 license or royalty fees, to use, copy, modify, and distribute this
007 software and its documentation for any purpose, provided that the above
008 copyright notice and the following two paragraphs appear in all copies
009 of this software.
010
011 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
012 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
013 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
014 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
015 SUCH DAMAGE.
016
017 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
018 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
019 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
020 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
021 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
022 ENHANCEMENTS, OR MODIFICATIONS.
023
024 PT_COPYRIGHT_VERSION_2
025 COPYRIGHTENDKEY
026
027 */
028package ptolemy.vergil.toolbox;
029
030import java.io.IOException;
031import java.io.Writer;
032import java.util.HashMap;
033import java.util.Iterator;
034import java.util.Map;
035import java.util.Set;
036
037import diva.canvas.toolbox.GraphicsParser;
038import diva.util.java2d.PaintedObject;
039import diva.util.java2d.PaintedString;
040import diva.util.xml.XmlElement;
041
042//////////////////////////////////////////////////////////////////////////
043//// GraphicElement
044
045/**
046 An GraphicElement is an atomic piece of a graphical representation.
047 i.e. a line, box, textbox, etc.
048
049 @author Steve Neuendorffer, John Reekie
050 @version $Id$
051 @since Ptolemy II 1.0
052 @Pt.ProposedRating Yellow (neuendor)
053 @Pt.AcceptedRating Red (johnr)
054 */
055public class GraphicElement {
056    /**
057     * Create a new GraphicElement with the given type.
058     * By default, the GraphicElement contains no attributes and an empty
059     * label
060     * @param type The type.
061     */
062    public GraphicElement(String type) {
063        _attributes = new HashMap();
064        _type = type;
065        _label = "";
066    }
067
068    /**
069     * Return a set of all the attribute names, where each element of
070     * the set is a String.
071     * @return The set of all attribute names
072     */
073    public Set attributeNameSet() {
074        return _attributes.keySet();
075    }
076
077    /**
078     * Write the GraphicElement in XML format to the given writer.
079     * @param out The writer.
080     * @param prefix The prefix, usually a string of spaces.
081     * @exception IOException If there is a problem writing the MoML.
082     */
083    public void exportMoML(Writer out, String prefix) throws IOException {
084        XmlElement element = new XmlElement(_type, _attributes);
085        element.setPCData(_label);
086        element.writeXML(out, prefix);
087    }
088
089    /** Return the value of the attribute with the given name.
090     *  Throw an exception if there is no attribute with the
091     *  given name in this schematic.
092     *  @param name The name of the attribute.
093     *  @return The value of the attribute with the given name.
094     *  @see #setAttribute(String, String)
095     */
096    public String getAttribute(String name) {
097        return (String) _attributes.get(name);
098    }
099
100    /**
101     * Return the label of this graphic element. This is
102     * primarily useful for textual elements, but may be used for other
103     * objects that have a label.
104     * @return The label.
105     * @see #setLabel(String)
106     */
107    public String getLabel() {
108        return _label;
109    }
110
111    /**
112     * Return a new painted object that looks like this graphic element.
113     * If the attributes are not consistent, or another error occurs, then
114     * return a painted string containing "Error!".
115     * @return The painted object.
116     */
117    public PaintedObject getPaintedObject() {
118        String type = getType();
119        String label = getLabel();
120        PaintedObject paintedObject = GraphicsParser.createPaintedObject(type,
121                _attributes, label);
122
123        if (paintedObject == null) {
124            return GraphicElement._errorObject;
125        }
126
127        return paintedObject;
128    }
129
130    /**
131     * Return the type of this graphic element.
132     * The type is immutably set when the element is created.
133     * @return The type.
134     */
135    public String getType() {
136        return _type;
137    }
138
139    /**
140     * Test if this element has an attribute with the given name.
141     * @param name The name.
142     * @return true if this element contains an attribute with the given
143     * name.
144     */
145    public boolean containsAttribute(String name) {
146        return _attributes.containsKey(name);
147    }
148
149    /**
150     * Remove an attribute from this element.
151     * @param name The name of the attribute to remove
152     */
153    public void removeAttribute(String name) {
154        _attributes.remove(name);
155    }
156
157    /**
158     * Set the attribute with the given name to the given value.
159     * @param name The name of the attribute.
160     * @param value The value of the attribute.
161     * @see #getAttribute(String)
162     */
163    public void setAttribute(String name, String value) {
164        _attributes.put(name, value);
165    }
166
167    /**
168     * Set the label for this graphic element.
169     * @param name The name.
170     * @see #getLabel()
171     */
172    public void setLabel(String name) {
173        _label = name;
174    }
175
176    /**
177     * Return a string this representing this GraphicElement.
178     */
179    @Override
180    public String toString() {
181        StringBuffer result = new StringBuffer("{");
182        result.append(
183                getClass().getName() + " {" + _type + "}" + " attributes {");
184
185        Set attributeSet = attributeNameSet();
186        Iterator names = attributeSet.iterator();
187
188        while (names.hasNext()) {
189            String p = (String) names.next();
190            result.append(" {" + p + "=" + getAttribute(p) + "}");
191        }
192
193        result.append("} label {" + getLabel() + "}}");
194
195        return result.toString();
196    }
197
198    // The painted object that is returned if an error occurs.
199    private static final PaintedString _errorObject = new PaintedString(
200            "ERROR!");
201
202    // The attributes of this graphic element.
203    private Map _attributes;
204
205    // The type of this graphic element.
206    private String _type;
207
208    // The label of this graphic element.
209    private String _label;
210}