    001: import java.awt.Graphics2D;
    002: import java.awt.geom.Rectangle2D;
    003:
    004: /**
    005:    A class node in a class diagram.
    006: */
    007: public class ClassNode extends RectangularNode
    008: {
    009:    /**
    010:       Construct a class node with a default size
    011:    */
    012:    public ClassNode()
    013:    {
    014:       name = new MultiLineString();
    015:       name.setSize(MultiLineString.LARGE);
    016:       attributes = new MultiLineString();
    017:       attributes.setJustification(MultiLineString.LEFT);
    018:       methods = new MultiLineString();
    019:       methods.setJustification(MultiLineString.LEFT);
    020:       setBounds(new Rectangle2D.Double(0, 0, DEFAULT_WIDTH, DEFAULT_HEIGHT));
    021:       midHeight = DEFAULT_COMPARTMENT_HEIGHT;
    022:       botHeight = DEFAULT_COMPARTMENT_HEIGHT;
    023:    }
    024:
    025:    public void draw(Graphics2D g2)
    026:    {
    027:       layout(g2);
    028:       Rectangle2D top = new Rectangle2D.Double(getBounds().getX(),
    029:          getBounds().getY(), getBounds().getWidth(),
    030:          getBounds().getHeight() - midHeight - botHeight);
    031:       g2.draw(top);
    032:       name.draw(g2, top);
    033:       Rectangle2D mid = new Rectangle2D.Double(top.getX(),
    034:          top.getMaxY(), top.getWidth(), midHeight);
    035:       g2.draw(mid);
    036:       attributes.draw(g2, mid);
    037:       Rectangle2D bot = new Rectangle2D.Double(top.getX(),
    038:          mid.getMaxY(), top.getWidth(), botHeight);
    039:       g2.draw(bot);
    040:       methods.draw(g2, bot);
    041:    }
    042:
    043:    /**
    044:       Recomputes the layout of this node.
    045:       @param g2 the graphics context
    046:    */
    047:    public void layout(Graphics2D g2)
    048:    {
    049:       Rectangle2D min = new Rectangle2D.Double(0, 0,
    050:          DEFAULT_WIDTH, DEFAULT_COMPARTMENT_HEIGHT);
    051:       Rectangle2D top = name.getBounds(g2);
    052:       top.add(min);
    053:       Rectangle2D mid = attributes.getBounds(g2);
    054:       Rectangle2D bot = methods.getBounds(g2);
    055:
    056:       midHeight = mid.getHeight();
    057:       botHeight = bot.getHeight();
    058:       if (midHeight == 0 && botHeight == 0)
    059:       {
    060:          top.add(new Rectangle2D.Double(0, 0,
    061:                     DEFAULT_WIDTH,
    062:                     3 * DEFAULT_COMPARTMENT_HEIGHT));
    063:       }
    064:       else
    065:       {
    066:          mid.add(min);
    067:          bot.add(min);
    068:          midHeight = mid.getHeight();
    069:          botHeight = bot.getHeight();
    070:       }
    071:
    072:       Rectangle2D b = new Rectangle2D.Double(
    073:          getBounds().getX(), getBounds().getY(),
    074:          Math.max(top.getWidth(), Math.max(mid.getWidth(),
    075:             bot.getWidth())),
    076:          top.getHeight() + midHeight + botHeight);
    077:       setBounds(b);
    078:    }
    079:
    080:    /**
    081:       Sets the name property value.
    082:       @param newValue the class name
    083:    */
    084:    public void setName(MultiLineString newValue)
    085:    {
    086:       name = newValue;
    087:    }
    088:
    089:    /**
    090:       Gets the name property value.
    091:       @return the class name
    092:    */
    093:    public MultiLineString getName()
    094:    {
    095:       return name;
    096:    }
    097:
    098:    /**
    099:       Sets the attributes property value.
    100:       @param newValue the attributes of this class
    101:    */
    102:    public void setAttributes(MultiLineString newValue)
    103:    {
    104:       attributes = newValue;
    105:    }
    106:
    107:    /**
    108:       Gets the attributes property value.
    109:       @return the attributes of this class
    110:    */
    111:    public MultiLineString getAttributes()
    112:    {
    113:       return attributes;
    114:    }
    115:
    116:    /**
    117:       Sets the methods property value.
    118:       @param newValue the methods of this class
    119:    */
    120:    public void setMethods(MultiLineString newValue)
    121:    {
    122:       methods = newValue;
    123:    }
    124:
    125:    /**
    126:       Gets the methods property value.
    127:       @return the methods of this class
    128:    */
    129:    public MultiLineString getMethods()
    130:    {
    131:       return methods;
    132:    }
    133:
    134:    public Object clone()
    135:    {
    136:       ClassNode cloned = (ClassNode) super.clone();
    137:       cloned.name = (MultiLineString) name.clone();
    138:       cloned.methods = (MultiLineString) methods.clone();
    139:       cloned.attributes = (MultiLineString) attributes.clone();
    140:       return cloned;
    141:    }
    142:
    143:    private double midHeight;
    144:    private double botHeight;
    145:    private MultiLineString name;
    146:    private MultiLineString attributes;
    147:    private MultiLineString methods;
    148:
    149:    private static int DEFAULT_COMPARTMENT_HEIGHT = 20;
    150:    private static int DEFAULT_WIDTH = 80;
    151:    private static int DEFAULT_HEIGHT = 60;
    152: }