public class ClassNode extends RectangularNode
1: import java.awt.Graphics2D;
2: import java.awt.geom.Rectangle2D;
4: /**
5: A class node in a class diagram.
6: */
7: public class ClassNode extends RectangularNode
8: {
9: /**
10: Construct a class node with a default size
11: */
12: public ClassNode()
13: {
14: name = new MultiLineString();
15: name.setSize(MultiLineString.LARGE);
16: attributes = new MultiLineString();
17: attributes.setJustification(MultiLineString.LEFT);
18: methods = new MultiLineString();
19: methods.setJustification(MultiLineString.LEFT);
20: setBounds(new Rectangle2D.Double(0, 0, DEFAULT_WIDTH, DEFAULT_HEIGHT));
21: midHeight = DEFAULT_COMPARTMENT_HEIGHT;
22: botHeight = DEFAULT_COMPARTMENT_HEIGHT;
23: }
25: public void draw(Graphics2D g2)
26: {
27: layout(g2);
28: Rectangle2D top = new Rectangle2D.Double(getBounds().getX(),
29: getBounds().getY(), getBounds().getWidth(),
30: getBounds().getHeight() - midHeight - botHeight);
31: g2.draw(top);
32: name.draw(g2, top);
33: Rectangle2D mid = new Rectangle2D.Double(top.getX(),
34: top.getMaxY(), top.getWidth(), midHeight);
35: g2.draw(mid);
36: attributes.draw(g2, mid);
37: Rectangle2D bot = new Rectangle2D.Double(top.getX(),
38: mid.getMaxY(), top.getWidth(), botHeight);
39: g2.draw(bot);
40: methods.draw(g2, bot);
41: }
43: /**
44: Recomputes the layout of this node.
45: @param g2 the graphics context
46: */
47: public void layout(Graphics2D g2)
48: {
49: Rectangle2D min = new Rectangle2D.Double(0, 0,
50: DEFAULT_WIDTH, DEFAULT_COMPARTMENT_HEIGHT);
51: Rectangle2D top = name.getBounds(g2);
52: top.add(min);
53: Rectangle2D mid = attributes.getBounds(g2);
54: Rectangle2D bot = methods.getBounds(g2);
56: midHeight = mid.getHeight();
57: botHeight = bot.getHeight();
58: if (midHeight == 0 && botHeight == 0)
59: {
60: top.add(new Rectangle2D.Double(0, 0,
61: DEFAULT_WIDTH,
62: 3 * DEFAULT_COMPARTMENT_HEIGHT));
63: }
64: else
65: {
66: mid.add(min);
67: bot.add(min);
68: midHeight = mid.getHeight();
69: botHeight = bot.getHeight();
70: }
72: Rectangle2D b = new Rectangle2D.Double(
73: getBounds().getX(), getBounds().getY(),
74: Math.max(top.getWidth(), Math.max(mid.getWidth(),
75: bot.getWidth())),
76: top.getHeight() + midHeight + botHeight);
77: setBounds(b);
78: }
80: /**
81: Sets the name property value.
82: @param newValue the class name
83: */
84: public void setName(MultiLineString newValue)
85: {
86: name = newValue;
87: }
89: /**
90: Gets the name property value.
91: @return the class name
92: */
93: public MultiLineString getName()
94: {
95: return name;
96: }
98: /**
99: 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: }
107: /**
108: Gets the attributes property value.
109: @return the attributes of this class
110: */
111: public MultiLineString getAttributes()
112: {
113: return attributes;
114: }
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: }
125: /**
126: Gets the methods property value.
127: @return the methods of this class
128: */
129: public MultiLineString getMethods()
130: {
131: return methods;
132: }
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: }
143: private double midHeight;
144: private double botHeight;
145: private MultiLineString name;
146: private MultiLineString attributes;
147: private MultiLineString methods;
149: private static int DEFAULT_COMPARTMENT_HEIGHT = 20;
150: private static int DEFAULT_WIDTH = 80;
151: private static int DEFAULT_HEIGHT = 60;
152: }