/* * @(#)FigureAttributes.java * * Project: JHotdraw - a GUI framework for technical drawings * http://www.jhotdraw.org * http://jhotdraw.sourceforge.net * Copyright: © by the original author(s) and all contributors * License: Lesser GNU Public License (LGPL) * http://www.opensource.org/licenses/lgpl-license.html */ package CH.ifa.draw.figures; import CH.ifa.draw.util.*; import CH.ifa.draw.framework.*; import java.awt.Color; import java.io.IOException; import java.io.Serializable; import java.util.Map; import java.util.Iterator; /** * A container for a figure's attributes. The attributes are stored * as key/value pairs. * * @see Figure * * @version <$CURRENT_VERSION$> */ public class FigureAttributes extends Object implements Cloneable, Serializable { private Map fMap; /* * Serialization support. */ private static final long serialVersionUID = -6886355144423666716L; private int figureAttributesSerializedDataVersion = 1; /** * Constructs the FigureAttributes. */ public FigureAttributes() { fMap = CollectionsFactory.current().createMap(); } /** * Gets the attribute with the given name. * @return attribute or null if the key is not defined */ public Object get(FigureAttributeConstant attributeConstant) { return fMap.get(attributeConstant); } /** * Sets the attribute with the given name and * overwrites its previous value. */ public void set(FigureAttributeConstant attributeConstant, Object value) { if (value != null) { fMap.put(attributeConstant, value); } else { fMap.remove(attributeConstant); } } /** * Tests if an attribute is defined. */ public boolean hasDefined(FigureAttributeConstant attributeConstant) { return fMap.containsKey(attributeConstant); } /** * Clones the attributes. */ public Object clone() { try { FigureAttributes a = (FigureAttributes) super.clone(); a.fMap = CollectionsFactory.current().createMap(fMap); return a; } catch (CloneNotSupportedException e) { throw new InternalError(); } } /** * Reads the attributes from a StorableInput. * FigureAttributes store the following types directly: * Color, Boolean, String, Int. Other attribute types * have to implement the Storable interface or they * have to be wrapped by an object that implements Storable. * @see Storable * @see #write */ public void read(StorableInput dr) throws IOException { String s = dr.readString(); if (!s.toLowerCase().equals("attributes")) { throw new IOException("Attributes expected"); } fMap = CollectionsFactory.current().createMap(); int size = dr.readInt(); for (int i=0; i