001: /*
002: * @(#)AbstractGraphCell.java 1.0 03-JUL-04
003: *
004: * Copyright (c) 2001-2006 Gaudenz Alder
005: *
006: */
007: package org.jgraph.graph;
008:
009: import java.awt.geom.Point2D;
010: import java.util.Collections;
011: import java.util.List;
012: import java.util.Map;
013:
014: import javax.swing.tree.DefaultMutableTreeNode;
015: import javax.swing.tree.MutableTreeNode;
016:
017: /**
018: * The default implementation for the GraphCell interface.
019: *
020: * @version 1.0 1/1/02
021: * @author Gaudenz Alder
022: */
023:
024: public class DefaultGraphCell extends DefaultMutableTreeNode implements
025: GraphCell, Cloneable {
026:
027: /** Hashtable for properties. Initially empty */
028: protected AttributeMap attributes = null;
029:
030: /**
031: * Creates an empty cell.
032: */
033: public DefaultGraphCell() {
034: this (null);
035: }
036:
037: /**
038: * Creates a graph cell and initializes it with the specified user object.
039: *
040: * @param userObject an Object provided by the user that constitutes
041: * the cell's data
042: */
043: public DefaultGraphCell(Object userObject) {
044: this (userObject, null);
045: }
046:
047: /**
048: * Constructs a cell that holds a reference to the specified user object
049: * and contains the specified array of children and sets default values
050: * for the bounds attribute.
051: *
052: * @param userObject reference to the user object
053: * @param storageMap the storage attribute map for this cell
054: */
055: public DefaultGraphCell(Object userObject, AttributeMap storageMap) {
056: this (userObject, storageMap, null);
057:
058: }
059:
060: /**
061: * Creates a graph cell and initializes it with the specified user object.
062: * The GraphCell allows children only if specified.
063: *
064: * @param userObject an Object provided by the user that constitutes
065: * the cell's data
066: * @param storageMap the storage attribute map for this cell
067: * @param children array of children
068: */
069: public DefaultGraphCell(Object userObject, AttributeMap storageMap,
070: MutableTreeNode[] children) {
071: super (userObject, true);
072: setAttributes(storageMap);
073: if (children != null)
074: for (int i = 0; i < children.length; i++)
075: add(children[i]);
076: }
077:
078: /**
079: * Provides access to the children list to change ordering.
080: * This method returns a <code>Collections.EMPTY_LIST</code>
081: * if the list of childrenpoints to <code>null</code>.
082: */
083: public List getChildren() {
084: if (children == null)
085: return Collections.EMPTY_LIST;
086: return children;
087: }
088:
089: /**
090: * Returns the properies of the cell.
091: */
092: public AttributeMap getAttributes() {
093: return attributes;
094: }
095:
096: /**
097: * Changes the <code>attributes</code> of the cell.
098: *
099: * @deprecated Use getAttributes().applyMap
100: */
101: public Map changeAttributes(Map change) {
102: return getAttributes().applyMap(change);
103: }
104:
105: /**
106: * Sets the attributes.
107: * @param attributes The attributes to set
108: */
109: public void setAttributes(AttributeMap attributes) {
110: if (attributes == null)
111: attributes = new AttributeMap();
112: this .attributes = attributes;
113: }
114:
115: /**
116: * Utility method to create a port for this cell. This method adds
117: * a floating port.
118: * @return the port created
119: */
120: public Object addPort() {
121: return addPort(null);
122: }
123:
124: /**
125: * Utility method to create a port for this cell. The method adds a port
126: * at a fixed relative offset within the cell. If the offset is null
127: * then a floating port is added.
128: * @param offset the offset of the port within the cell
129: * @return the port created
130: */
131: public Object addPort(Point2D offset) {
132: return addPort(offset, null);
133: }
134:
135: /**
136: * Utility method to create a port for this cell. The method adds a port
137: * at a fixed relative offset within the cell. If the offset is null
138: * then a floating port is added.
139: * @param offset the offset of the port within the cell
140: * @param userObject the user object of the port cell
141: * @return the port created
142: */
143: public Object addPort(Point2D offset, Object userObject) {
144: DefaultPort port = new DefaultPort(userObject);
145: if (offset == null) {
146: add(port);
147: } else {
148: GraphConstants.setOffset(port.getAttributes(), offset);
149: add(port);
150: }
151: return port;
152: }
153:
154: /**
155: * Create a clone of the cell. This method uses the superclass
156: * implementation (which does not clone the children), then
157: * uses clone on the attribute map. This method does not
158: * clone the user object. You should override the
159: * cloneUserObject in the graph model to implement cloning
160: * of custom user objects.
161: *
162: * @return Object a clone of this object.
163: */
164: public Object clone() {
165: DefaultGraphCell c = (DefaultGraphCell) super .clone();
166: c.attributes = (AttributeMap) attributes.clone();
167: return c;
168: }
169:
170: }
|