1 11 12 package org.eclipse.birt.chart.examples.api.interactivity; 13 14 import java.awt.BorderLayout ; 15 import java.awt.Color ; 16 import java.awt.Container ; 17 import java.awt.Dimension ; 18 import java.awt.FlowLayout ; 19 import java.awt.Font ; 20 import java.awt.FontMetrics ; 21 import java.awt.Graphics ; 22 import java.awt.Graphics2D ; 23 import java.awt.GridLayout ; 24 import java.awt.Rectangle ; 25 import java.awt.Toolkit ; 26 import java.awt.event.ActionEvent ; 27 import java.awt.event.ActionListener ; 28 import java.awt.event.ComponentEvent ; 29 import java.awt.event.ComponentListener ; 30 import java.awt.event.WindowAdapter ; 31 import java.awt.event.WindowEvent ; 32 import java.awt.image.BufferedImage ; 33 import java.util.HashMap ; 34 import java.util.Map ; 35 36 import javax.swing.JButton ; 37 import javax.swing.JComboBox ; 38 import javax.swing.JFrame ; 39 import javax.swing.JLabel ; 40 import javax.swing.JOptionPane ; 41 import javax.swing.JPanel ; 42 43 import org.eclipse.birt.chart.device.IDeviceRenderer; 44 import org.eclipse.birt.chart.device.ICallBackNotifier; 45 import org.eclipse.birt.chart.exception.ChartException; 46 import org.eclipse.birt.chart.factory.GeneratedChartState; 47 import org.eclipse.birt.chart.factory.Generator; 48 import org.eclipse.birt.chart.model.Chart; 49 import org.eclipse.birt.chart.model.attribute.Bounds; 50 import org.eclipse.birt.chart.model.attribute.CallBackValue; 51 import org.eclipse.birt.chart.model.attribute.impl.BoundsImpl; 52 import org.eclipse.birt.chart.util.PluginSettings; 53 import org.eclipse.birt.core.exception.BirtException; 54 55 62 public final class SwingInteractivityViewer extends JPanel implements 63 ICallBackNotifier, 64 ComponentListener 65 { 66 67 private static final long serialVersionUID = 1L; 68 69 private boolean bNeedsGeneration = true; 70 71 private GeneratedChartState gcs = null; 72 73 private Chart cm = null; 74 75 private IDeviceRenderer idr = null; 76 77 private BufferedImage bi = null; 78 79 private Map contextMap; 80 81 87 public static void main( String [] args ) 88 { 89 final SwingInteractivityViewer siv = new SwingInteractivityViewer( ); 90 91 JFrame jf = new JFrame ( ); 92 jf.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE ); 93 jf.addComponentListener( siv ); 94 95 Container co = jf.getContentPane( ); 96 co.setLayout( new BorderLayout ( ) ); 97 co.add( siv, BorderLayout.CENTER ); 98 99 Dimension dScreen = Toolkit.getDefaultToolkit( ).getScreenSize( ); 100 Dimension dApp = new Dimension ( 600, 400 ); 101 jf.setSize( dApp ); 102 jf.setLocation( ( dScreen.width - dApp.width ) / 2, 103 ( dScreen.height - dApp.height ) / 2 ); 104 105 jf.setTitle( siv.getClass( ).getName( ) + " [device=" + siv.idr.getClass( ).getName( ) + "]" ); 108 ControlPanel cp = siv.new ControlPanel( siv ); 109 co.add( cp, BorderLayout.SOUTH ); 110 111 siv.idr.setProperty( IDeviceRenderer.UPDATE_NOTIFIER, siv ); 112 113 jf.addWindowListener( new WindowAdapter ( ) { 114 115 public void windowClosing( WindowEvent e ) 116 { 117 siv.idr.dispose( ); 118 } 119 120 } ); 121 122 jf.setVisible( true ); 123 } 124 125 128 SwingInteractivityViewer( ) 129 { 130 contextMap = new HashMap (); 131 132 final PluginSettings ps = PluginSettings.instance( ); 133 try 134 { 135 idr = ps.getDevice( "dv.SWING" ); } 137 catch ( ChartException ex ) 138 { 139 ex.printStackTrace( ); 140 } 141 cm = InteractivityCharts.createHSChart( ); 142 } 143 144 149 public void regenerateChart( ) 150 { 151 bNeedsGeneration = true; 152 updateBuffer( ); 153 repaint( ); 154 } 155 156 161 public void repaintChart( ) 162 { 163 repaint( ); 164 } 165 166 171 public Object peerInstance( ) 172 { 173 return this; 174 } 175 176 181 public Chart getDesignTimeModel( ) 182 { 183 return cm; 184 } 185 186 191 public Chart getRunTimeModel( ) 192 { 193 return gcs.getChartModel( ); 194 } 195 196 201 public Object getContext( Object key ) 202 { 203 return contextMap.get( key ); 204 } 205 206 212 public Object putContext( Object key, Object value ) 213 { 214 return contextMap.put( key, value ); 215 } 216 217 222 public Object removeContext( Object key ) 223 { 224 return contextMap.remove( key ); 225 } 226 227 public void updateBuffer( ) 228 { 229 Dimension d = getSize( ); 230 231 if ( bi == null 232 || bi.getWidth( ) != d.width 233 || bi.getHeight( ) != d.height ) 234 { 235 bi = new BufferedImage ( d.width, 236 d.height, 237 BufferedImage.TYPE_INT_ARGB ); 238 } 239 240 Graphics2D g2d = (Graphics2D ) bi.getGraphics( ); 241 242 idr.setProperty( IDeviceRenderer.GRAPHICS_CONTEXT, g2d ); 243 Bounds bo = BoundsImpl.create( 0, 0, d.width, d.height ); 244 bo.scale( 72d / idr.getDisplayServer( ).getDpiResolution( ) ); 251 Generator gr = Generator.instance( ); 252 if ( bNeedsGeneration ) 253 { 254 bNeedsGeneration = false; 255 try 256 { 257 gcs = gr.build( idr.getDisplayServer( ), 258 cm, 259 bo, 260 null, 261 null, 262 null ); 263 } 264 catch ( ChartException ex ) 265 { 266 showException( g2d, ex ); 267 } 268 } 269 270 try 271 { 272 gr.render( idr, gcs ); 273 } 274 catch ( ChartException rex ) 275 { 276 showException( g2d, rex ); 277 } 278 finally 279 { 280 g2d.dispose( ); 281 } 282 283 } 284 285 290 public void paint( Graphics g ) 291 { 292 super.paint( g ); 293 294 if ( bi == null ) 295 { 296 updateBuffer( ); 297 } 298 299 g.drawImage( bi, 0, 0, this ); 300 } 301 302 308 private final void showException( Graphics2D g2d, Exception ex ) 309 { 310 String sWrappedException = ex.getClass( ).getName( ); 311 Throwable th = ex; 312 while ( ex.getCause( ) != null ) 313 { 314 ex = (Exception ) ex.getCause( ); 315 } 316 String sException = ex.getClass( ).getName( ); 317 if ( sWrappedException.equals( sException ) ) 318 { 319 sWrappedException = null; 320 } 321 322 String sMessage = null; 323 if ( th instanceof BirtException ) 324 { 325 sMessage = ( (BirtException) th ).getLocalizedMessage( ); 326 } 327 else 328 { 329 sMessage = ex.getMessage( ); 330 } 331 332 if ( sMessage == null ) 333 { 334 sMessage = "<null>"; } 336 337 StackTraceElement [] stea = ex.getStackTrace( ); 338 Dimension d = getSize( ); 339 340 Font fo = new Font ( "Monospaced", Font.BOLD, 14 ); g2d.setFont( fo ); 342 FontMetrics fm = g2d.getFontMetrics( ); 343 g2d.setColor( Color.WHITE ); 344 g2d.fillRect( 20, 20, d.width - 40, d.height - 40 ); 345 g2d.setColor( Color.BLACK ); 346 g2d.drawRect( 20, 20, d.width - 40, d.height - 40 ); 347 g2d.setClip( 20, 20, d.width - 40, d.height - 40 ); 348 int x = 25, y = 20 + fm.getHeight( ); 349 g2d.drawString( "Exception:", x, y ); x += fm.stringWidth( "Exception:" ) + 5; g2d.setColor( Color.RED ); 352 g2d.drawString( sException, x, y ); 353 x = 25; 354 y += fm.getHeight( ); 355 if ( sWrappedException != null ) 356 { 357 g2d.setColor( Color.BLACK ); 358 g2d.drawString( "Wrapped In:", x, y ); x += fm.stringWidth( "Wrapped In:" ) + 5; g2d.setColor( Color.RED ); 361 g2d.drawString( sWrappedException, x, y ); 362 x = 25; 363 y += fm.getHeight( ); 364 } 365 g2d.setColor( Color.BLACK ); 366 y += 10; 367 g2d.drawString( "Message:", x, y ); x += fm.stringWidth( "Message:" ) + 5; g2d.setColor( Color.BLUE ); 370 g2d.drawString( sMessage, x, y ); 371 x = 25; 372 y += fm.getHeight( ); 373 g2d.setColor( Color.BLACK ); 374 y += 10; 375 g2d.drawString( "Trace:", x, y ); x = 40; 377 y += fm.getHeight( ); 378 g2d.setColor( Color.GREEN.darker( ) ); 379 for ( int i = 0; i < stea.length; i++ ) 380 { 381 g2d.drawString( stea[i].getClassName( ) + ":" + stea[i].getMethodName( ) + "(...):" + stea[i].getLineNumber( ), x, y ); 384 x = 40; 385 y += fm.getHeight( ); 386 } 387 } 388 389 394 public void componentHidden( ComponentEvent e ) 395 { 396 398 } 399 400 405 public void componentMoved( ComponentEvent e ) 406 { 407 409 } 410 411 416 public void componentResized( ComponentEvent e ) 417 { 418 bNeedsGeneration = true; 419 } 420 421 426 public void componentShown( ComponentEvent e ) 427 { 428 430 } 431 432 436 private final class ControlPanel extends JPanel implements ActionListener 437 { 438 439 private static final long serialVersionUID = 1L; 440 441 private JComboBox jcbModels = null; 442 443 private JButton jbUpdate = null; 444 445 private final SwingInteractivityViewer siv; 446 447 ControlPanel( SwingInteractivityViewer siv ) 448 { 449 this.siv = siv; 450 451 setLayout( new GridLayout ( 0, 1, 0, 0 ) ); 452 453 JPanel jp = new JPanel ( ); 454 jp.setLayout( new FlowLayout ( FlowLayout.LEFT, 3, 3 ) ); 455 456 jp.add( new JLabel ( "Choose:" ) ); jcbModels = new JComboBox ( ); 458 459 jcbModels.addItem( "Highlight Series" ); jcbModels.addItem( "Show Tooltip" ); jcbModels.addItem( "Toggle Visibility" ); jcbModels.addItem( "URL Redirect" ); jcbModels.addItem( "Call Back" ); 465 jcbModels.setSelectedIndex( 0 ); 466 jp.add( jcbModels ); 467 468 jbUpdate = new JButton ( "Update" ); jbUpdate.addActionListener( this ); 470 jp.add( jbUpdate ); 471 472 add( jp ); 473 } 474 475 480 public void componentHidden( ComponentEvent cev ) 481 { 482 setVisible( false ); 483 } 484 485 490 public void componentMoved( ComponentEvent cev ) 491 { 492 JFrame jf = (JFrame ) cev.getComponent( ); 493 Rectangle r = jf.getBounds( ); 494 setLocation( r.x, r.y + r.height ); 495 setSize( r.width, 50 ); 496 } 497 498 503 public void componentResized( ComponentEvent cev ) 504 { 505 JFrame jf = (JFrame ) cev.getComponent( ); 506 Rectangle r = jf.getBounds( ); 507 setLocation( r.x, r.y + r.height ); 508 setSize( r.width, 50 ); 509 } 510 511 516 public void componentShown( ComponentEvent cev ) 517 { 518 JFrame jf = (JFrame ) cev.getComponent( ); 519 Rectangle r = jf.getBounds( ); 520 setLocation( r.x, r.y + r.height ); 521 setSize( r.width, 50 ); 522 setVisible( true ); 523 } 524 525 530 public void actionPerformed( ActionEvent e ) 531 { 532 int i = jcbModels.getSelectedIndex( ); 533 cm = null; 534 switch ( i ) 535 { 536 case 0 : 537 cm = InteractivityCharts.createHSChart( ); 538 break; 539 case 1 : 540 cm = InteractivityCharts.createSTChart( ); 541 break; 542 case 2 : 543 cm = InteractivityCharts.createTVChart( ); 544 break; 545 case 3 : 546 cm = InteractivityCharts.createURChart( ); 547 break; 548 case 4 : 549 cm = InteractivityCharts.createCBChart( ); 550 break; 551 } 552 553 bNeedsGeneration = true; 554 siv.updateBuffer( ); 555 siv.repaint( ); 556 } 557 } 558 559 public void callback( Object event, Object source, CallBackValue value ) 560 { 561 JOptionPane.showMessageDialog( SwingInteractivityViewer.this, 562 value.getIdentifier( ) ); 563 } 564 } | Popular Tags |