1 package org.coach.tracing.server.viewer; 2 3 import javax.swing.*; 4 import java.awt.*; 5 import java.awt.geom.Line2D ; 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 public final static int org_hspace = 20; 20 public final static int org_vspace = 5; 22 public final static int org_rect_width = 20; 24 public final static int org_rect_height = 5; 26 28 protected Dimension text_dimension = null; 29 protected String name; 30 protected Diagram diagram; 31 protected static int offset; 32 33 private ThreadNode threadNode; 34 35 public ThreadGui(Diagram v, String 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 (); 51 52 hideMenuItem = new javax.swing.JMenuItem (); 53 hideMenuItem.setText("hide"); 54 hideMenuItem.addActionListener(new java.awt.event.ActionListener () { 55 public void actionPerformed(java.awt.event.ActionEvent evt) { 56 hideMenuItemActionPerformed(evt); 57 } 58 }); 59 popup.add(hideMenuItem); 60 61 if (!threadNode.isLeaf()) { 62 expandMenuItem = new javax.swing.JMenuItem (); 63 expandMenuItem.setText("expand"); 64 expandMenuItem.addActionListener(new java.awt.event.ActionListener () { 65 public void actionPerformed(java.awt.event.ActionEvent evt) { 66 expandMenuItemActionPerformed(evt); 67 } 68 }); 69 popup.add(expandMenuItem); 70 } 71 72 if (!((ThreadNode)threadNode.getParent()).isRoot()) { 73 collapseMenuItem = new javax.swing.JMenuItem (); 74 collapseMenuItem.setText("collapse"); 75 collapseMenuItem.addActionListener(new java.awt.event.ActionListener () { 76 public void actionPerformed(java.awt.event.ActionEvent evt) { 77 collapseMenuItemActionPerformed(evt); 78 } 79 }); 80 popup.add(collapseMenuItem); 81 } 82 83 moveRightMenuItem = new javax.swing.JMenuItem (); 84 moveRightMenuItem.setText("move right"); 85 moveRightMenuItem.addActionListener(new java.awt.event.ActionListener () { 86 public void actionPerformed(java.awt.event.ActionEvent evt) { 87 moveRightMenuItemActionPerformed(evt); 88 } 89 }); 90 popup.add(moveRightMenuItem); 91 92 moveLeftMenuItem = new javax.swing.JMenuItem (); 93 moveLeftMenuItem.setText("move left"); 94 moveLeftMenuItem.addActionListener(new java.awt.event.ActionListener () { 95 public void actionPerformed(java.awt.event.ActionEvent evt) { 96 moveLeftMenuItemActionPerformed(evt); 97 } 98 }); 99 popup.add(moveLeftMenuItem); 100 101 addMouseListener(new java.awt.event.MouseAdapter () { 102 public void mousePressed(java.awt.event.MouseEvent e) { 103 if (e.isPopupTrigger()) { 104 popup.show(e.getComponent(), e.getX(), e.getY()); 105 } 106 } 107 public void mouseReleased(java.awt.event.MouseEvent 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 g2.drawString(name, (int)(org_hspace * scale), (text_dimension.height - 1)); 132 g2.setColor(Color.black); 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)); 135 g2.draw(new Line2D.Float (getWidth() / 2, text_dimension.height + (org_vspace * scale) + 1, getWidth() / 2, getHeight() - 1)); 137 } 138 139 protected void hideMenuItemActionPerformed(java.awt.event.ActionEvent evt) { 140 threadNode.setHidden(true); 141 diagram.repaintEvents(); 142 } 143 144 private void expandMenuItemActionPerformed(java.awt.event.ActionEvent evt) { 145 threadNode.expand(); 146 diagram.repaintEvents(); 147 } 148 149 private void collapseMenuItemActionPerformed(java.awt.event.ActionEvent evt) { 150 threadNode.collapse(); 151 diagram.repaintEvents(); 152 } 153 154 protected void moveRightMenuItemActionPerformed(java.awt.event.ActionEvent evt) { 155 ThreadNode.moveRight(threadNode); 156 diagram.repaintEvents(); 157 } 158 159 protected void moveLeftMenuItemActionPerformed(java.awt.event.ActionEvent evt) { 160 ThreadNode.moveLeft(threadNode); 161 diagram.repaintEvents(); 162 } 163 } 164 165 | Popular Tags |