1 19 20 package org.apache.excalibur.instrument.client; 21 22 import java.awt.Color ; 23 import java.awt.Dimension ; 24 25 import javax.swing.ImageIcon ; 26 import javax.swing.JLabel ; 27 import javax.swing.SwingConstants ; 28 import javax.swing.event.InternalFrameEvent ; 29 import javax.swing.tree.DefaultMutableTreeNode ; 30 31 import org.apache.avalon.framework.configuration.Configuration; 32 import org.apache.avalon.framework.configuration.ConfigurationException; 33 import org.apache.avalon.framework.configuration.DefaultConfiguration; 34 35 41 public class InstrumentSampleFrame 42 extends AbstractInternalFrame 43 { 44 public static final String FRAME_TYPE = "sample-frame"; 45 46 private static final int STATE_NONE = 0; 47 private static final int STATE_DISCONNECTED = 1; 48 private static final int STATE_MISSING = 2; 49 private static final int STATE_SNAPSHOT = 3; 50 private static final int STATE_EXPIRED = 4; 51 52 private static final ImageIcon m_iconDisconnected; 53 private static final ImageIcon m_iconMissing; 54 private static final ImageIcon m_iconExpired; 55 56 private int m_state = STATE_NONE; 57 private InstrumentManagerConnection m_connection; 58 private String m_instrumentSampleName; 59 private String m_fullName; 60 61 63 private long m_lastSnapshotTime; 64 65 66 private int[] m_samples; 67 68 69 private LineChart m_lineChart; 70 71 74 static 75 { 76 ClassLoader cl = InstrumentManagerTreeCellRenderer.class.getClassLoader(); 78 m_iconDisconnected = 79 new ImageIcon ( cl.getResource( NodeData.MEDIA_PATH + "sample_disconnected.gif") ); 80 m_iconMissing = 81 new ImageIcon ( cl.getResource( NodeData.MEDIA_PATH + "sample_missing.gif") ); 82 m_iconExpired = 83 new ImageIcon ( cl.getResource( NodeData.MEDIA_PATH + "sample_expired.gif") ); 84 } 85 86 89 InstrumentSampleFrame( Configuration stateConfig, 90 InstrumentManagerConnection connection, 91 InstrumentClientFrame frame ) 92 throws ConfigurationException 93 { 94 super( stateConfig, true, true, true, true, frame ); 95 96 m_instrumentSampleName = stateConfig.getAttribute( "sample" ); 97 m_fullName = m_instrumentSampleName; 98 99 m_connection = connection; 100 } 101 102 InstrumentSampleFrame( InstrumentManagerConnection connection, 103 String sampleName, 104 InstrumentClientFrame frame ) 105 { 106 super( "", true, true, true, true, frame ); 107 108 m_connection = connection; 109 m_instrumentSampleName = sampleName; 110 m_fullName = m_instrumentSampleName; 111 112 setSize( new Dimension ( 600, 120 ) ); 113 } 114 115 118 122 protected void getState( DefaultConfiguration stateConfig ) 123 { 124 stateConfig.setAttribute( "type", FRAME_TYPE ); 125 stateConfig.setAttribute( "url", m_connection.getKey().toString() ); 126 stateConfig.setAttribute( "sample", m_instrumentSampleName ); 127 } 128 129 void hideFrame() 130 { 131 133 super.hideFrame(); 134 } 135 136 139 public void internalFrameClosed( InternalFrameEvent event ) 140 { 141 m_connection.hideSampleFrame( this ); 144 145 super.internalFrameClosed( event ); 146 } 147 148 151 156 public String getInstrumentSampleName() 157 { 158 return m_instrumentSampleName; 159 } 160 161 167 public long getLastSnapshotTime() 168 { 169 return m_lastSnapshotTime; 170 } 171 172 175 private void updateIcon() 176 { 177 ImageIcon icon; 178 DefaultMutableTreeNode sampleNode = 179 m_connection.getInstrumentSampleTreeNode( m_instrumentSampleName ); 180 if ( sampleNode != null ) 181 { 182 InstrumentSampleNodeData sampleNodeData = 184 (InstrumentSampleNodeData)sampleNode.getUserObject(); 185 186 icon = sampleNodeData.getIcon(); 188 } 189 else if ( m_state == STATE_MISSING ) 190 { 191 icon = m_iconMissing; 192 } 193 else if ( m_state == STATE_EXPIRED ) 194 { 195 icon = m_iconExpired; 196 } 197 else 198 { 199 icon = m_iconDisconnected; 200 } 201 202 if ( getFrameIcon() != icon ) 204 { 205 setFrameIcon( icon ); 206 } 207 } 208 209 216 private void updateTitle() 217 { 218 DefaultMutableTreeNode sampleNode = 219 m_connection.getInstrumentSampleTreeNode( m_instrumentSampleName ); 220 if ( sampleNode != null ) 221 { 222 StringBuffer sb = new StringBuffer (); 224 InstrumentSampleNodeData sampleNodeData = 225 (InstrumentSampleNodeData)sampleNode.getUserObject(); 226 227 sb.append( sampleNodeData.getDescription() ); 228 229 DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode )sampleNode.getParent(); 231 while( parentNode != null ) 232 { 233 Object userObject = parentNode.getUserObject(); 234 if ( ( userObject == null ) || !( userObject instanceof NodeData) ) 235 { 236 parentNode = null; 237 } 238 else 239 { 240 sb.append( " / " ); 241 sb.append( ((NodeData)userObject).getDescription() ); 242 parentNode = (DefaultMutableTreeNode )parentNode.getParent(); 243 } 244 } 245 246 m_fullName = sb.toString(); 248 } 249 250 StringBuffer sb = new StringBuffer (); 252 switch ( m_state ) 253 { 254 case STATE_SNAPSHOT: 255 break; 256 257 case STATE_MISSING: 258 sb.append( "[Missing] " ); 259 break; 260 261 case STATE_EXPIRED: 262 sb.append( "[Expired] " ); 263 break; 264 265 default: 266 sb.append( "[Disconnected] " ); 267 break; 268 } 269 270 sb.append( m_fullName ); 272 273 sb.append( " / " ); 275 sb.append( m_connection.getTitle() ); 276 277 String title = sb.toString(); 278 279 if ( !getTitle().equals( title ) ) 281 { 282 setTitle( title ); 283 } 284 } 285 286 291 private void initChart( InstrumentSampleSnapshotData snapshot ) 292 { 293 long interval = Math.max( 1, snapshot.getInterval() ); 295 int hInterval; 296 String format; 297 String detailFormat; 298 if( interval < 1000 ) 299 { 300 hInterval = (int)( 10000 / interval ); 302 format = "{3}:{4}:{5}"; 303 detailFormat = "{1}/{2} {3}:{4}:{5}.{6}"; 304 } 305 else if( interval < 60000 ) 306 { 307 hInterval = (int)( 60000 / interval ); 309 format = "{3}:{4}:{5}"; 310 detailFormat = "{1}/{2} {3}:{4}:{5}"; 311 } 312 else if( interval < 600000 ) 313 { 314 hInterval = (int)( 600000 / interval ); 316 format = "{1}/{2} {3}:{4}"; 317 detailFormat = "{1}/{2} {3}:{4}"; 318 } 319 else if( interval < 3600000 ) 320 { 321 hInterval = (int)( 3600000 / interval ); 323 format = "{1}/{2} {3}:{4}"; 324 detailFormat = "{1}/{2} {3}:{4}"; 325 } 326 else if( interval < 86400000 ) 327 { 328 hInterval = (int)( 86400000 / interval ); 330 format = "{1}/{2}"; 331 detailFormat = "{1}/{2} {3}:{4}"; 332 } 333 else if( interval < 604800000 ) 334 { 335 hInterval = (int)( 604800000 / interval ); 337 format = "{0}/{1}/{2}"; 338 detailFormat = "{0}/{1}/{2}"; 339 } 340 else 341 { 342 hInterval = 10; 344 format = "{0}/{1}/{2}"; 345 detailFormat = "{0}/{1}/{2}"; 346 } 347 348 getContentPane().removeAll(); 350 351 m_lineChart = new LineChart( 353 hInterval, interval, format, detailFormat, 20, getFrame().isAntialias() ); 354 getContentPane().add( m_lineChart ); 355 } 356 357 private void setStateSnapshot( InstrumentSampleSnapshotData snapshot ) 358 { 359 if ( m_state != STATE_SNAPSHOT ) 360 { 361 initChart( snapshot ); 362 363 m_state = STATE_SNAPSHOT; 364 365 updateTitle(); 366 } 367 368 int size = snapshot.getSize(); 369 370 int[] newSamples = snapshot.getSamples(); 373 if ( newSamples.length == size ) 374 { 375 m_samples = newSamples; 377 } 378 else if ( newSamples.length > size ) 379 { 380 m_samples = new int[size]; 383 System.arraycopy( newSamples, newSamples.length - size, m_samples, 0, size ); 384 } 385 else 386 { 387 390 if ( m_samples == null ) 392 { 393 m_samples = new int[size]; 394 } 395 else if ( m_samples.length != size ) 396 { 397 int[] tmpSamples = new int[size]; 399 if ( m_samples.length > size ) 400 { 401 System.arraycopy( m_samples, m_samples.length - size, tmpSamples, 0, size ); 403 } 404 else 405 { 406 System.arraycopy( 408 m_samples, 0, tmpSamples, size - m_samples.length, m_samples.length ); 409 } 410 m_samples = tmpSamples; 411 } 412 413 long age = snapshot.getTime() - m_lastSnapshotTime; 415 int intervalAge = (int)( age / snapshot.getInterval() ); 416 if ( intervalAge > m_samples.length ) 417 { 418 intervalAge = m_samples.length; 419 } 420 421 if ( ( intervalAge > 0 ) && ( intervalAge < m_samples.length ) ) 423 { 424 System.arraycopy( m_samples, intervalAge, m_samples, 0, size - intervalAge ); 426 } 427 428 if ( intervalAge > newSamples.length ) 431 { 432 for ( int i = size - intervalAge; i < size - newSamples.length; i++ ) 433 { 434 m_samples[i] = 0; 435 } 436 } 437 438 System.arraycopy( 440 newSamples, 0, m_samples, size - newSamples.length, newSamples.length ); 441 } 442 443 m_lineChart.setValues( m_samples, snapshot.getTime() ); 445 446 m_lastSnapshotTime = snapshot.getTime(); 448 449 updateIcon(); 451 } 452 453 456 private void setStateDisconnected() 457 { 458 if ( m_state != STATE_DISCONNECTED ) 459 { 460 getContentPane().removeAll(); 461 462 JLabel label = new JLabel ( "Not connected" ); 464 label.setForeground( Color.red ); 465 label.setHorizontalAlignment( SwingConstants.CENTER ); 466 label.setVerticalAlignment( SwingConstants.CENTER ); 467 468 getContentPane().add( label ); 469 470 m_state = STATE_DISCONNECTED; 471 472 updateTitle(); 473 updateIcon(); 474 } 475 } 476 477 480 private void setStateSampleMissing() 481 { 482 if ( m_state != STATE_MISSING ) 483 { 484 getContentPane().removeAll(); 485 486 JLabel label = new JLabel ( "Sample not found" ); 488 label.setForeground( Color.red ); 489 label.setHorizontalAlignment( SwingConstants.CENTER ); 490 label.setVerticalAlignment( SwingConstants.CENTER ); 491 492 getContentPane().add( label ); 493 494 m_state = STATE_MISSING; 495 496 updateTitle(); 497 updateIcon(); 498 } 499 } 500 501 504 private void setStateSampleExpired() 505 { 506 if ( m_state != STATE_EXPIRED ) 507 { 508 m_lineChart.setBackground( new Color ( 220, 220, 220 ) ); 511 m_lineChart.repaint(); 512 513 m_state = STATE_EXPIRED; 514 515 updateTitle(); 516 updateIcon(); 517 } 518 } 519 520 525 public void updateSnapshot( InstrumentSampleSnapshotData snapshot ) 526 { 527 if ( m_connection.isDeleted() ) 528 { 529 hideFrame(); 531 } 532 else 533 { 534 if ( m_lineChart != null ) 535 { 536 m_lineChart.setAntialias( getFrame().isAntialias() ); 537 } 538 539 if ( snapshot == null ) 540 { 541 if ( !m_connection.isConnected() ) 543 { 544 setStateDisconnected(); 546 } 547 else if ( ( m_state == STATE_SNAPSHOT ) || ( m_state == STATE_EXPIRED ) ) 548 { 549 setStateSampleExpired(); 551 } 552 else 553 { 554 setStateSampleMissing(); 556 } 557 } 558 else 559 { 560 setStateSnapshot( snapshot ); 561 } 562 } 563 } 564 565 568 void update() 569 { 570 InstrumentSampleSnapshotData snapshot = null; 571 if ( !m_connection.isDeleted() ) 572 { 573 snapshot = m_connection.getSampleSnapshot( m_instrumentSampleName ); 574 } 575 576 updateSnapshot( snapshot ); 577 } 578 } 579 580 | Popular Tags |