File: ClassGraph.java

package info (click to toggle)
doc%2B%2B 3.2-1
  • links: PTS
  • area: non-free
  • in suites: slink
  • size: 1,844 kB
  • ctags: 1,925
  • sloc: cpp: 16,762; lex: 2,938; makefile: 278; java: 273; yacc: 139; perl: 20; sh: 17
file content (333 lines) | stat: -rw-r--r-- 9,475 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import java.applet.Applet;
import java.applet.AppletContext;
//import java.awt.BorderLayout;
import java.awt.*;
import java.util.StringTokenizer;
import java.net.URL;

class ClassLayout implements LayoutManager {

    public ClassLayout(){
    }

    public void addLayoutComponent(String name, Component comp) {
    }

    public void removeLayoutComponent(Component comp) {
    }
  
  int distance=30;
  int leftmax=2,rightmax=2;

    public Dimension preferredLayoutSize(Container parent) {
        Insets insets = parent.insets();
        int ncomponents = parent.countComponents();
        int w = 0;
        int h = ncomponents*distance;

        for (int i = 0; i < ncomponents; i++) {
            Component comp = parent.getComponent(i);
            Dimension d = comp.preferredSize();
            Point p = comp.location();
            if ((p.x + d.width) > w)
                w = p.x + d.width;
            if ((p.y + d.height) > h)
                h = p.y + d.height;
        }
	calcStuff(parent);
	w=(leftmax+rightmax)*distance;
        return new Dimension(insets.left + insets.right + w,
                             insets.top + insets.bottom + h);
    }

  void calcStuff(Container parent){
    int ncomponents = parent.countComponents();
        for (int i = 0; i < ncomponents; i++) {
	  NavigatorButton m=(NavigatorButton) parent.getComponent(i);
	  Dimension d = m.preferredSize();
	  int units=m.space+Math.max(2,(int)((d.width+distance-1)/distance));
	  if (m.growLeft){
	    if (units>leftmax)
	      leftmax=units;
	    } else 
	      if (units>rightmax)
		rightmax=units;
	}
  }

  public Dimension minimumLayoutSize(Container parent) {
    return preferredLayoutSize(parent);
  }
  boolean first=true;
    public void layoutContainer(Container parent) {
      calcStuff(parent);
      int     nmembers = parent.countComponents();
      /*System.out.println ("Hi, this is layout with # of comp="+nmembers+
	"left="+leftmax);    */
      int maxHeight=0;
      for (int i = 0; i < nmembers; i++) {
	NavigatorButton m=(NavigatorButton) parent.getComponent(i);
	Dimension d = m.preferredSize();
	if (d.height>maxHeight)
	  maxHeight=d.height;
      }	
      
      for (int i = 0; i < nmembers; i++) {
	NavigatorButton m=(NavigatorButton) parent.getComponent(i);
	Dimension d = m.preferredSize();
	if (d.width<2*distance)
	  d.width=2*distance;
	m.resize(d.width, d.height);
	Point l=m.location();
	// left border for right growing
	int left=leftmax-2;
	if (left<0) left=0;
	// right border for left growing
	int right=leftmax;

	if (m.growLeft)
	  l.x=(right-m.space)*distance-d.width;
	else 
	  l.x=(m.space+left)*distance;
	m.move(l.x,i*distance+(distance-maxHeight)/2);
	System.out.println (i+":"+m);    
      }
    }
}

class NavigatorButton extends Button{
  AppletContext context;
  URL url;
  int space,indent;
  // True if it grows to the left.
  boolean growLeft,bold,isInterface;
  Font boldFont,italicFont,normalFont;

  NavigatorButton(String name,URL _url,AppletContext _context,boolean gl,
		  boolean _isInterface, int _space,int _indent){
    super(name);
    isInterface=_isInterface;
    context=_context;
    url=_url;
    growLeft=gl;
    space=_space;
    indent=_indent;

    if (boldFont==null)
      boldFont=new Font("Helvetica",Font.BOLD, 12);
    if (italicFont==null)
      italicFont=new Font("Helvetica",Font.ITALIC, 12);
    if (isInterface) {
      setForeground(Color.blue);
      System.out.println (name+" is Interface");
    }
    
    if (url==null)
      setFont(italicFont);
      
    if (space==0){
      setFont(boldFont);
    }
  }

  public void init(){
    System.out.println ("Font="+getFont());
  }
  /*
  public String toString(){
    return("Button "+url+
	   " space="+space+" rect="+bounds());
  }
  */
  public boolean action(Event evt, Object arg) {
    if (url!=null){
      try {
	context.showDocument(url);
      }  catch (Exception e) {};
      return(true);
    }
    return(false);
  }
}

class ClassGraphPanel extends Panel{
  
  int ccnt=0;
  public Button buttons[];
  public String urls[],before[],after[];
  public static String dir;

  int indent[];
  ClassLayout layout;
  ClassGraphPanel(String _classes,String _indent,String _before,String _after,
		  AppletContext context,URL base){

    StringTokenizer tc = new StringTokenizer(_classes, ",");
    StringTokenizer tb = new StringTokenizer(_before, ",");
    StringTokenizer ti = new StringTokenizer(_indent, ",");
    StringTokenizer ta = new StringTokenizer(_after, ",");

    buttons=new Button[tb.countTokens()];
    urls=new String[tb.countTokens()];
    before=new String[tb.countTokens()];
    after=new String[tb.countTokens()];
    indent=new int[tb.countTokens()];
    for (; tc.hasMoreTokens();) {
      String tmp   = tc.nextToken();
      
      String type  = tmp.substring(0,1);
      String cname = tmp.substring(1);

      String file  = tc.nextToken().substring(1);
      before[ccnt] = tb.nextToken().substring(1);
      after[ccnt]  = ta.nextToken().substring(1);
      /*System.out.println ("Cnt="+ccnt+" name="+cname+
	" url="+file+"\nafter="+after[ccnt]+" before="+
	before[ccnt]);*/
      indent[ccnt]=Integer.valueOf(ti.nextToken()).intValue();
      URL url=null;
      if (file.length()>0)
	try {
	  url=new URL(base,file);
	} catch(Exception e) {};
	boolean growLeft=false;
	if (before[ccnt].length()<2)
	  growLeft=true;
	int space=(after[ccnt].length()+before[ccnt].length())/2;
	boolean isInterface=true;
	if (type.equals("C"))
	  isInterface=false;
	buttons[ccnt]=new NavigatorButton(cname,url,context,growLeft,isInterface,
					  space,indent[ccnt]);
	ccnt++;
    }

    setLayout(layout=new ClassLayout());
    for (int i=0 ; i< ccnt ; i++){
      add(buttons[i]);
      buttons[i].move(30*(indent[i]+before[i].length()/2),i*30);
    }
    //System.out.println ("Size:"+buttons[0].size());    
  }

  int h=30,w=30;
  void rightArrow(Graphics g,int x,int y){
    if (dir.equals("up"))
	return;
    g.drawLine(x-4, y-3,x,y);
    g.drawLine(x-4, y+3,x,y);
  }
  void downArrow(Graphics g,int x,int y){
    if (dir.equals("up"))
	return;
    g.drawLine(x-3, y-4,x,y);
    g.drawLine(x+3, y-4,x,y);
  }
  void leftArrow(Graphics g,int x,int y){
    if (dir.equals("down"))
	return;
    g.drawLine(x+4, y-3,x,y);
    g.drawLine(x+4, y+3,x,y);
  }
  void upArrow(Graphics g,int x,int y){
    if (dir.equals("down"))
	return;
    g.drawLine(x-3, y+4,x,y);
    g.drawLine(x+3, y+4,x,y);
  }
 
  void paintLine(Graphics g,int x,int y,String type){

    int dh=(h-buttons[0].size().height+1)/2;    
    for (int i=0 ; i<type.length()-1 ; i+=2){
      String sub=type.substring(i,i+1);
      String prot=type.substring(i+1,i+2);

      if (sub.equals("|")){
	g.drawLine(x+w/2, y-dh ,x+w/2,y+h);
	//if (!upArrows)
	if (!prot.equals("|")){
	  g.drawLine(x+w/2, y+h/2 ,x+w,y+h/2);
	  rightArrow(g,x+w-1,y+h/2);
	  upArrow(g,x+w/2,y-dh+1);
	}
      } else if (sub.equals("r")){
	g.drawLine(x+w/2, y ,x+w/2,y+h/2);
	g.drawLine(x+w/2, y+h/2 ,x+w,y+h/2);
	rightArrow(g,x+w-1,y+h/2);
      } else if (sub.equals("l")){
	g.drawLine(x, y+h/2 ,x+w/2,y+h/2);
	g.drawLine(x+w/2, y+h/2 ,x+w/2,y+h);
	leftArrow(g,x+1,y+w/2);
      } else if (sub.equals("L")){
	g.drawLine(x, y+h/2 ,x+w/2,y+h/2);
	g.drawLine(x+w/2, y ,x+w/2,y+h);
	leftArrow(g,x+1,y+w/2);
      } else if (sub.equals("R")){
	g.drawLine(x+w/2, y+h/2 ,x+w,y+h/2);
	g.drawLine(x+w/2, y ,x+w/2,y+h);
	rightArrow(g,x+w-1,y+h/2);
      } else if (sub.equals("D")){
	g.drawLine(x, y+h/2 ,x+w/2,y+h/2);
	g.drawLine(x+w/2, y ,x+w/2,y+h);
	downArrow(g,x+w/2, y+h);
	leftArrow(g,x+1,y+w/2);
      } else if (sub.equals("d")){
	g.drawLine(x, y+h/2 ,x+w/2,y+h/2);
	g.drawLine(x+w/2, y+h/2 ,x+w/2,y+h);
	downArrow(g,x+w/2, y+h);
	leftArrow(g,x+1,y+w/2);
      } else if (sub.equals("^")){
	g.drawLine(x+w/2, y - dh ,x+w/2,y+h/2);
	g.drawLine(x+w/2, y+h/2 ,x+w,y+h/2);
	rightArrow(g,x+w-1,y+h/2);
	upArrow(g,x+w/2,y-dh+1);
      }
      x+=w;      
    }
  }
 boolean first=true;
public void paint(Graphics g)
  { 
    int x,y;
    // 24 pt red font used here
    /*g.setFont(new Font("TimesRoman", Font.BOLD, 24));
      String test=new String("Malte");
      g.drawString(test, 0,40);
      */
      g.setColor(Color.black);
      for (int i=0 ; i<ccnt ; i++){
      x=(Math.max((layout.leftmax-2),0))*30;
      /*if (first)
	System.out.println ("x:"+x);*/
      
      y=i*30;
      paintLine(g,x,y,before[i]);
      Rectangle r=buttons[i].bounds();
      x=r.x+r.width;
      paintLine(g,x,y,after[i]);
    }
    first=false;
  }
}

public class ClassGraph extends Applet{
  ClassGraphPanel panel;

  public void init() {
    String classes = getParameter("classes");
    String before = getParameter("before");
    String after = getParameter("after");
    String indent = getParameter("indent");
    ClassGraphPanel.dir = getParameter("arrowdir");

    setLayout(new FlowLayout(FlowLayout.LEFT));
    //setLayout(new GridLayout(0,1,0,1));

    panel = new ClassGraphPanel(classes,indent,before,after,
				getAppletContext(),getDocumentBase());
    //add("Center", panel);
    add(panel);
    resize(panel.preferredSize());    
  }
}