1 package net.sf.mmapps.applications.developer.modules.appview;
2
3 import java.awt.*;
4 import java.awt.geom.Rectangle2D;
5
6 import net.sf.mmapps.modules.config.*;
7
8 import com.touchgraph.graphlayout.*;
9
10 /***
11 * @author Kees Jongenburger
12 * @version 1.21 $Id: MMBaseNode.java,v 1.1.1.1 2004/02/06 08:44:07 keesj Exp $
13 */
14 public class MMBaseNode extends Node {
15 NodeManagerConfiguration config;
16 FontMetrics fontMetrics = null;
17 Dimension dim;
18 boolean drawFields = false;
19
20 Font font;
21 FontMetrics fm;
22
23 public MMBaseNode(NodeManagerConfiguration nodeManagerConfiguration) {
24 super(
25 nodeManagerConfiguration.getName(),
26 Node.TYPE_ELLIPSE,
27 javax.swing.plaf.metal.MetalLookAndFeel.getControl(),
28 nodeManagerConfiguration.getName());
29 this.config = nodeManagerConfiguration;
30 font = new Font("Courrier", Font.BOLD, 10);
31 }
32
33 public void paint(Graphics g, TGPanel tgPanel) {
34 if (drawFields) {
35 draw((int) drawx, (int) drawy - getHeight() / 2, g);
36 } else {
37 super.paint(g, tgPanel);
38 }
39 }
40
41 public int getWidth() {
42 if (dim != null && drawFields)
43 return dim.width;
44 return super.getWidth();
45 }
46 public int getHeight() {
47 if (dim != null && drawFields)
48 return dim.height;
49 return super.getHeight();
50 }
51
52 public void showFields(boolean show) {
53 drawFields = show;
54 }
55
56 public void draw(int x, int y, Graphics g) {
57 Font font = g.getFont();
58 g.setFont(font);
59 int myWidth = 100;
60
61 String data = config.getName() + " extends " + config.getExtends();
62 if (config.getExtends().equals("object")) {
63 data = config.getName();
64 }
65
66 if (fm == null) {
67 fm = g.getFontMetrics();
68 }
69 Rectangle2D rect = fm.getStringBounds(data, g);
70 int textWidth = (int) rect.getWidth();
71 if (textWidth > myWidth) {
72 x -= (textWidth - myWidth) / 2;
73 myWidth = textWidth;
74 }
75
76 int textHeight = fm.getHeight();
77
78 g.setColor(Color.white);
79 g.fillRect(x, y, myWidth, 2 + (textHeight + 2) * (1 + config.getFieldConfigurations().size()));
80 g.setColor(Color.black);
81 g.drawRect(x, y, myWidth, 2 + (textHeight + 2) * (1 + config.getFieldConfigurations().size()));
82
83 y += textHeight + 2;
84 if (config.getName().equals("object")) {
85 g.drawString(config.getName(), x, y);
86 } else {
87 g.drawString(data, x, y);
88 }
89 g.drawLine(x, y + 1, x + myWidth, y + 1);
90 FieldConfigurations fieldConfigurations = config.getFieldConfigurations();
91 for (int i = 0; i < fieldConfigurations.size(); i++) {
92 FieldConfiguration fieldConfiguration = fieldConfigurations.getFieldConfiguration(i);
93 Rectangle2D r = fm.getStringBounds(fieldConfiguration.getName(), g);
94 int w = (int) rect.getWidth();
95 if (w > myWidth) {
96 myWidth = w;
97 }
98 y += textHeight + 2;
99 String fieldType = fieldConfiguration.getType();
100 if (fieldType.equals("STRING")) {
101 g.setColor(Color.black);
102 } else if (fieldType.equals("INTEGER")) {
103 g.setColor(Color.blue);
104 } else if (fieldType.equals("XML")) {
105 g.setColor(Color.cyan);
106 } else if (fieldType.equals("BYTE")) {
107 g.setColor(Color.orange);
108 } else if (fieldType.equals("NODE")) {
109 g.setColor(Color.magenta);
110 } else {
111 g.setColor(Color.green);
112 }
113 g.drawString(fieldConfiguration.getName(), x, y);
114
115 }
116
117 g.setFont(font);
118 dim = new Dimension(myWidth, 2 + (textHeight + 2) * (1 + config.getFieldConfigurations().size()));
119 }
120 }