KickJava   Java API By Example, From Geeks To Geeks.

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


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