KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > coach > tracing > server > viewer > ThreadGui


1 package org.coach.tracing.server.viewer;
2
3 import javax.swing.*;
4 import java.awt.*;
5 import java.awt.geom.Line2D JavaDoc;
6
7 public class ThreadGui extends JComponent {
8     protected static ScaleManager sm = ScaleManager.getScaleManager();
9     protected static float scale;
10     protected final JPopupMenu popup;
11     protected JMenuItem hideMenuItem;
12     private JMenuItem expandMenuItem;
13     private JMenuItem collapseMenuItem;
14     protected JMenuItem moveRightMenuItem;
15     protected JMenuItem moveLeftMenuItem;
16     
17     public final static int min_hsize = 50;
18     //the minimum horizontal size of the header component
19
public final static int org_hspace = 20;
20     //horizontal space between object text
21
public final static int org_vspace = 5;
22     //vertical space between the text and the rectangle
23
public final static int org_rect_width = 20;
24     //original width of the rectangle
25
public final static int org_rect_height = 5;
26     //original height of the rectangle
27

28     protected Dimension text_dimension = null;
29     protected String JavaDoc name;
30     protected Diagram diagram;
31     protected static int offset;
32     
33     private ThreadNode threadNode;
34
35     public ThreadGui(Diagram v, String JavaDoc n, ThreadNode threadNode) {
36         super();
37         this.diagram = v;
38         this.name = n;
39         this.threadNode = threadNode;
40         setFont(sm.getFont());
41         ToolTipManager.sharedInstance().registerComponent(this);
42         setToolTipText(n);
43         scale = sm.getScale();
44         text_dimension = sm.getStringDimension(name + "__", this);
45         setSize(Math.max((int)(text_dimension.width + (2 * org_hspace * scale)), (int)(min_hsize * scale)), diagram.getHeight() - 1);
46
47         setLocation(offset, 0);
48         offset += getWidth();
49
50         popup = new javax.swing.JPopupMenu JavaDoc();
51
52         hideMenuItem = new javax.swing.JMenuItem JavaDoc();
53         hideMenuItem.setText("hide");
54         hideMenuItem.addActionListener(new java.awt.event.ActionListener JavaDoc() {
55             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
56                 hideMenuItemActionPerformed(evt);
57             }
58         });
59         popup.add(hideMenuItem);
60
61         if (!threadNode.isLeaf()) {
62             expandMenuItem = new javax.swing.JMenuItem JavaDoc();
63             expandMenuItem.setText("expand");
64             expandMenuItem.addActionListener(new java.awt.event.ActionListener JavaDoc() {
65                 public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
66                     expandMenuItemActionPerformed(evt);
67                 }
68             });
69             popup.add(expandMenuItem);
70         }
71
72         if (!((ThreadNode)threadNode.getParent()).isRoot()) {
73             collapseMenuItem = new javax.swing.JMenuItem JavaDoc();
74             collapseMenuItem.setText("collapse");
75             collapseMenuItem.addActionListener(new java.awt.event.ActionListener JavaDoc() {
76                 public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
77                     collapseMenuItemActionPerformed(evt);
78                 }
79             });
80             popup.add(collapseMenuItem);
81         }
82
83         moveRightMenuItem = new javax.swing.JMenuItem JavaDoc();
84         moveRightMenuItem.setText("move right");
85         moveRightMenuItem.addActionListener(new java.awt.event.ActionListener JavaDoc() {
86             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
87                 moveRightMenuItemActionPerformed(evt);
88             }
89         });
90         popup.add(moveRightMenuItem);
91                 
92         moveLeftMenuItem = new javax.swing.JMenuItem JavaDoc();
93         moveLeftMenuItem.setText("move left");
94         moveLeftMenuItem.addActionListener(new java.awt.event.ActionListener JavaDoc() {
95             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
96                 moveLeftMenuItemActionPerformed(evt);
97             }
98         });
99         popup.add(moveLeftMenuItem);
100
101         addMouseListener(new java.awt.event.MouseAdapter JavaDoc() {
102             public void mousePressed(java.awt.event.MouseEvent JavaDoc e) {
103                 if (e.isPopupTrigger()) {
104                     popup.show(e.getComponent(), e.getX(), e.getY());
105                 }
106             }
107             public void mouseReleased(java.awt.event.MouseEvent JavaDoc e) {
108                 if (e.isPopupTrigger()) {
109                     popup.show(e.getComponent(), e.getX(), e.getY());
110                 }
111             }
112         });
113     }
114             
115     public static void reset() {
116         offset = 0;
117     }
118     
119     public int getXoffset() {
120         return getX() + getWidth() / 2;
121     }
122         
123     public void paint(Graphics g) {
124         super.paint(g);
125         Graphics2D g2 = (Graphics2D)g;
126         g2.setRenderingHints(sm.getHints());
127         g2.setStroke(sm.getStroke());
128         g2.setFont(sm.getFont());
129         g2.setColor(Color.blue);
130         //draw the string
131
g2.drawString(name, (int)(org_hspace * scale), (text_dimension.height - 1));
132         g2.setColor(Color.black);
133         //draw the black rectangle
134
g2.fillRect((int)(((getWidth() - 1) - (org_rect_width * scale)) / 2), (int)(text_dimension.height + (org_vspace * scale)), (int)(org_rect_width * scale), (int)(org_rect_height * scale));
135         // draw the line
136
g2.draw(new Line2D.Float JavaDoc(getWidth() / 2, text_dimension.height + (org_vspace * scale) + 1, getWidth() / 2, getHeight() - 1));
137     }
138         
139     protected void hideMenuItemActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
140         threadNode.setHidden(true);
141         diagram.repaintEvents();
142     }
143
144     private void expandMenuItemActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
145         threadNode.expand();
146         diagram.repaintEvents();
147     }
148
149     private void collapseMenuItemActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
150         threadNode.collapse();
151         diagram.repaintEvents();
152     }
153
154     protected void moveRightMenuItemActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
155         ThreadNode.moveRight(threadNode);
156         diagram.repaintEvents();
157     }
158
159     protected void moveLeftMenuItemActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
160         ThreadNode.moveLeft(threadNode);
161         diagram.repaintEvents();
162     }
163 }
164
165
Popular Tags