1 17 package org.apache.jmeter.visualizers; 18 19 import java.util.HashMap ; 20 import java.awt.BorderLayout ; 21 import java.awt.Color ; 22 import java.awt.Dimension ; 23 import java.awt.Font ; 24 import java.awt.FlowLayout ; 25 26 import javax.swing.ImageIcon ; 27 import javax.swing.JLabel ; 28 import javax.swing.JPanel ; 29 import javax.swing.JScrollPane ; 30 import javax.swing.JSplitPane ; 31 import javax.swing.JTree ; 32 import javax.swing.tree.DefaultMutableTreeNode ; 33 import javax.swing.tree.DefaultTreeModel ; 34 import javax.swing.tree.TreeSelectionModel ; 35 import javax.swing.event.TreeSelectionListener ; 36 import javax.swing.event.TreeSelectionEvent ; 37 38 39 import org.apache.jmeter.samplers.Clearable; 40 import org.apache.jmeter.samplers.SampleResult; 41 import org.apache.jmeter.util.JMeterUtils; 42 43 public class MonitorPerformancePanel extends JSplitPane 44 implements TreeSelectionListener , MonitorListener, Clearable 45 { 46 47 private JScrollPane TREEPANE; 48 private JPanel GRAPHPANEL; 49 private JTree SERVERTREE; 50 private DefaultTreeModel TREEMODEL; 51 private MonitorGraph GRAPH; 52 private DefaultMutableTreeNode ROOTNODE; 53 private HashMap SERVERMAP; 54 private MonitorAccumModel MODEL; 55 private SampleResult ROOTSAMPLE; 56 57 public static final String LEGEND_HEALTH = 58 JMeterUtils.getResString("monitor_legend_health"); 59 public static final String LEGEND_LOAD = 60 JMeterUtils.getResString("monitor_legend_load"); 61 public static final String LEGEND_MEM = 62 JMeterUtils.getResString("monitor_legend_memory_per"); 63 public static final String LEGEND_THREAD = 64 JMeterUtils.getResString("monitor_legend_thread_per"); 65 public static final ImageIcon LEGEND_HEALTH_ICON = 66 JMeterUtils.getImage("monitor-green-legend.gif"); 67 public static final ImageIcon LEGEND_LOAD_ICON = 68 JMeterUtils.getImage("monitor-blue-legend.gif"); 69 public static final ImageIcon LEGEND_MEM_ICON = 70 JMeterUtils.getImage("monitor-orange-legend.gif"); 71 public static final ImageIcon LEGEND_THREAD_ICON = 72 JMeterUtils.getImage("monitor-red-legend.gif"); 73 public static final String GRID_LABEL_TOP = 74 JMeterUtils.getResString("monitor_label_left_top"); 75 public static final String GRID_LABEL_MIDDLE = 76 JMeterUtils.getResString("monitor_label_left_middle"); 77 public static final String GRID_LABEL_BOTTOM = 78 JMeterUtils.getResString("monitor_label_left_bottom"); 79 public static final String GRID_LABEL_HEALTHY = 80 JMeterUtils.getResString("monitor_label_right_healthy"); 81 public static final String GRID_LABEL_ACTIVE = 82 JMeterUtils.getResString("monitor_label_right_active"); 83 public static final String GRID_LABEL_WARNING = 84 JMeterUtils.getResString("monitor_label_right_warning"); 85 public static final String GRID_LABEL_DEAD = 86 JMeterUtils.getResString("monitor_label_right_dead"); 87 88 public static final String PERF_TITLE = 89 JMeterUtils.getResString("monitor_performance_title"); 90 public static final String SERVER_TITLE = 91 JMeterUtils.getResString("monitor_performance_servers"); 92 93 protected Font plaintext = new Font ("plain", Font.TRUETYPE_FONT, 10); 94 95 99 public MonitorPerformancePanel() 100 { 101 } 103 104 107 public MonitorPerformancePanel(MonitorAccumModel model, MonitorGraph graph) 108 { 109 super(); 110 this.SERVERMAP = new HashMap (); 111 this.MODEL = model; 112 this.MODEL.addListener(this); 113 this.GRAPH = graph; 114 init(); 115 } 116 117 121 protected void init(){ 122 ROOTSAMPLE = new SampleResult(); 123 ROOTSAMPLE.setSampleLabel(SERVER_TITLE); 124 ROOTSAMPLE.setSuccessful(true); 125 ROOTNODE = new DefaultMutableTreeNode (ROOTSAMPLE); 126 TREEMODEL = new DefaultTreeModel (ROOTNODE); 127 SERVERTREE = new JTree (TREEMODEL); 128 SERVERTREE.getSelectionModel().setSelectionMode( 129 TreeSelectionModel.SINGLE_TREE_SELECTION); 130 SERVERTREE.addTreeSelectionListener(this); 131 SERVERTREE.setShowsRootHandles(true); 132 TREEPANE = new JScrollPane (SERVERTREE); 133 TREEPANE.setPreferredSize(new Dimension (150,200)); 134 this.add(TREEPANE,JSplitPane.LEFT); 135 this.setDividerLocation(0.18); 136 137 JPanel right = new JPanel (); 138 right.setLayout(new BorderLayout ()); 139 JLabel title = new JLabel (" " + PERF_TITLE); 140 title.setPreferredSize(new Dimension (200,40)); 141 GRAPHPANEL = new JPanel (); 142 GRAPHPANEL.setLayout(new BorderLayout ()); 143 GRAPHPANEL.setMaximumSize( 144 new Dimension (MODEL.getBufferSize(),MODEL.getBufferSize())); 145 GRAPHPANEL.setBackground(Color.white); 146 GRAPHPANEL.add(GRAPH,BorderLayout.CENTER); 147 right.add(GRAPHPANEL,BorderLayout.CENTER); 148 149 right.add(title,BorderLayout.NORTH); 150 right.add(createLegend(),BorderLayout.SOUTH); 151 right.add(createLeftGridLabels(),BorderLayout.WEST); 152 right.add(createRightGridLabels(),BorderLayout.EAST); 153 this.add(right,JSplitPane.RIGHT); 154 } 155 156 162 public JPanel createLegend(){ 163 Dimension lsize = new Dimension (130,18); 164 165 JPanel legend = new JPanel (); 166 legend.setLayout(new FlowLayout ()); 167 JLabel health = new JLabel (LEGEND_HEALTH); 168 health.setFont(plaintext); 169 health.setPreferredSize(lsize); 170 health.setIcon(LEGEND_HEALTH_ICON); 171 legend.add(health); 172 173 JLabel load = new JLabel (LEGEND_LOAD); 174 load.setFont(plaintext); 175 load.setPreferredSize(lsize); 176 load.setIcon(LEGEND_LOAD_ICON); 177 legend.add(load); 178 179 JLabel mem = new JLabel (LEGEND_MEM); 180 mem.setFont(plaintext); 181 mem.setPreferredSize(lsize); 182 mem.setIcon(LEGEND_MEM_ICON); 183 legend.add(mem); 184 185 JLabel thd = new JLabel (LEGEND_THREAD); 186 thd.setFont(plaintext); 187 thd.setPreferredSize(lsize); 188 thd.setIcon(LEGEND_THREAD_ICON); 189 legend.add(thd); 190 return legend; 191 } 192 193 198 public JPanel createLeftGridLabels(){ 199 Dimension lsize = new Dimension (33,20); 200 JPanel labels = new JPanel (); 201 labels.setLayout(new BorderLayout ()); 202 203 JLabel top = new JLabel (" " + GRID_LABEL_TOP); 204 top.setFont(plaintext); 205 top.setPreferredSize(lsize); 206 labels.add(top,BorderLayout.NORTH); 207 208 JLabel mid = new JLabel (" " + GRID_LABEL_MIDDLE); 209 mid.setFont(plaintext); 210 mid.setPreferredSize(lsize); 211 labels.add(mid,BorderLayout.CENTER); 212 213 JLabel bottom = new JLabel (" " + GRID_LABEL_BOTTOM); 214 bottom.setFont(plaintext); 215 bottom.setPreferredSize(lsize); 216 labels.add(bottom,BorderLayout.SOUTH); 217 return labels; 218 } 219 220 225 public JPanel createRightGridLabels(){ 226 JPanel labels = new JPanel (); 227 labels.setLayout(new BorderLayout ()); 228 labels.setPreferredSize(new Dimension (40,GRAPHPANEL.getWidth() - 100)); 229 Dimension lsize = new Dimension (40,20); 230 JLabel h = new JLabel (GRID_LABEL_HEALTHY); 231 h.setFont(plaintext); 232 h.setPreferredSize(lsize); 233 labels.add(h,BorderLayout.NORTH); 234 235 JLabel d = new JLabel (GRID_LABEL_DEAD); 236 d.setFont(plaintext); 237 d.setPreferredSize(lsize); 238 labels.add(d,BorderLayout.SOUTH); 239 return labels; 240 } 241 242 246 public void addSample(MonitorModel model){ 247 if (!SERVERMAP.containsKey(model.getURL())){ 248 DefaultMutableTreeNode newnode = 249 new DefaultMutableTreeNode (model); 250 newnode.setAllowsChildren(false); 251 SERVERMAP.put(model.getURL(),newnode); 252 ROOTNODE.add(newnode); 253 this.TREEPANE.updateUI(); 254 } 255 DefaultMutableTreeNode node = 256 (DefaultMutableTreeNode ) SERVERTREE.getLastSelectedPathComponent(); 257 Object usrobj = node.getUserObject(); 258 if (usrobj instanceof MonitorModel){ 259 GRAPH.updateGui((MonitorModel)usrobj); 260 } 261 } 262 263 269 public void valueChanged(TreeSelectionEvent e) 270 { 271 if (SERVERTREE.getLastSelectedPathComponent() != null){ 274 DefaultMutableTreeNode node = 275 (DefaultMutableTreeNode ) SERVERTREE.getLastSelectedPathComponent(); 276 Object usrobj = node.getUserObject(); 277 if ( usrobj != null && usrobj instanceof MonitorModel){ 278 MonitorModel mo = (MonitorModel)usrobj; 279 GRAPH.updateGui(mo); 280 this.updateUI(); 281 } 282 TREEPANE.updateUI(); 283 } 284 } 285 286 291 public void clear(){ 292 this.SERVERMAP.clear(); 293 ROOTNODE.removeAllChildren(); 294 SERVERTREE.updateUI(); 295 GRAPH.clear(); 296 } 297 } 298 | Popular Tags |