[Home] DMelt member access. Java source code: org.jhotdraw.draw.AbstractAttributedCompositeFigure
/*
* @(#)AbstractAttributedCompositeFigure.java
*
* Copyright (c) 1996-2010 by the original authors of JHotDraw and all its
* contributors. All rights reserved.
*
* You may not use, copy or modify this file, except in compliance with the
* license agreement you entered into with the copyright holders. For details
* see accompanying license terms.
*/
package org.jhotdraw.draw;
import java.awt.*;
import java.awt.geom.*;
import java.util.*;
import java.io.*;
import static org.jhotdraw.draw.AttributeKeys.*;
import org.jhotdraw.geom.*;
import org.jhotdraw.xml.DOMInput;
import org.jhotdraw.xml.DOMOutput;
/**
* This abstract class can be extended to implement a {@link CompositeFigure}
* which has its own attribute set.
*
* @author Werner Randelshofer
* @version $Id: AbstractAttributedCompositeFigure.java 717 2010-11-21 12:30:57Z rawcoder $
*/
public abstract class AbstractAttributedCompositeFigure extends AbstractCompositeFigure {
private HashMap attributes = new HashMap();
/**
* Forbidden attributes can't be put by the put() operation.
* They can only be changed by put().
*/
private HashSet forbiddenAttributes;
/** Creates a new instance. */
public AbstractAttributedCompositeFigure() {
}
public void setAttributeEnabled(AttributeKey key, boolean b) {
if (forbiddenAttributes == null) {
forbiddenAttributes = new HashSet();
}
if (b) {
forbiddenAttributes.remove(key);
} else {
forbiddenAttributes.add(key);
}
}
public boolean isAttributeEnabled(AttributeKey key) {
return forbiddenAttributes == null || !forbiddenAttributes.contains(key);
}
@SuppressWarnings("unchecked")
public void setAttributes(Map map) {
for (Map.Entry entry : map.entrySet()) {
set(entry.getKey(), entry.getValue());
}
}
@Override
public Map getAttributes() {
return new HashMap(attributes);
}
/**
* Sets an attribute of the figure.
* AttributeKey name and semantics are defined by the class implementing
* the figure interface.
*/
@Override
public void set(AttributeKey key, T newValue) {
if (forbiddenAttributes == null || !forbiddenAttributes.contains(key)) {
Object oldValue = attributes.put(key, newValue);
setAttributeOnChildren(key, newValue);
fireAttributeChanged(key, oldValue, newValue);
}
}
protected void setAttributeOnChildren(AttributeKey key, T newValue) {
for (Figure child : getChildren()) {
child.set(key, newValue);
}
}
/**
* Gets an attribute from the figure.
*/
@Override
public T get(AttributeKey key) {
return key.get(attributes);
}
@Override
public Object getAttributesRestoreData() {
LinkedList