1 11 12 package org.eclipse.birt.chart.examples.api.viewer; 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.util.HashMap ; 31 import java.util.Map ; 32 33 import javax.swing.JButton ; 34 import javax.swing.JCheckBox ; 35 import javax.swing.JComboBox ; 36 import javax.swing.JFrame ; 37 import javax.swing.JLabel ; 38 import javax.swing.JPanel ; 39 40 import org.eclipse.birt.chart.device.IDeviceRenderer; 41 import org.eclipse.birt.chart.device.IUpdateNotifier; 42 import org.eclipse.birt.chart.exception.ChartException; 43 import org.eclipse.birt.chart.factory.GeneratedChartState; 44 import org.eclipse.birt.chart.factory.Generator; 45 import org.eclipse.birt.chart.model.Chart; 46 import org.eclipse.birt.chart.model.ChartWithAxes; 47 import org.eclipse.birt.chart.model.ChartWithoutAxes; 48 import org.eclipse.birt.chart.model.attribute.AxisType; 49 import org.eclipse.birt.chart.model.attribute.Bounds; 50 import org.eclipse.birt.chart.model.attribute.ChartDimension; 51 import org.eclipse.birt.chart.model.attribute.impl.BoundsImpl; 52 import org.eclipse.birt.chart.model.attribute.impl.JavaNumberFormatSpecifierImpl; 53 import org.eclipse.birt.chart.model.component.Axis; 54 import org.eclipse.birt.chart.util.PluginSettings; 55 import org.eclipse.birt.core.exception.BirtException; 56 57 61 public final class SwingChartViewerSelector extends JPanel implements 62 IUpdateNotifier, 63 ComponentListener 64 { 65 66 private static final long serialVersionUID = 1L; 67 68 private boolean bNeedsGeneration = true; 69 70 private GeneratedChartState gcs = null; 71 72 private Chart cm = null; 73 74 private IDeviceRenderer idr = null; 75 76 private Map contextMap; 77 78 84 public static void main( String [] args ) 85 { 86 SwingChartViewerSelector scv = new SwingChartViewerSelector( ); 87 88 JFrame jf = new JFrame ( ); 89 jf.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE ); 90 jf.addComponentListener( scv ); 91 92 Container co = jf.getContentPane( ); 93 co.setLayout( new BorderLayout ( ) ); 94 co.add( scv, BorderLayout.CENTER ); 95 96 Dimension dScreen = Toolkit.getDefaultToolkit( ).getScreenSize( ); 97 Dimension dApp = new Dimension ( 800, 600 ); 98 jf.setSize( dApp ); 99 jf.setLocation( ( dScreen.width - dApp.width ) / 2, 100 ( dScreen.height - dApp.height ) / 2 ); 101 102 jf.setTitle( scv.getClass( ).getName( ) + " [device=" + scv.idr.getClass( ).getName( ) 104 + "]" ); 106 ControlPanel cp = scv.new ControlPanel( scv ); 107 co.add( cp, BorderLayout.SOUTH ); 108 109 jf.setVisible( true ); 110 } 111 112 115 SwingChartViewerSelector( ) 116 { 117 contextMap = new HashMap ( ); 118 119 final PluginSettings ps = PluginSettings.instance( ); 120 try 121 { 122 idr = ps.getDevice( "dv.SWING" ); } 124 catch ( ChartException ex ) 125 { 126 ex.printStackTrace( ); 127 } 128 cm = PrimitiveCharts.createBarChart( ); 130 } 131 132 137 public void regenerateChart( ) 138 { 139 bNeedsGeneration = true; 140 repaint( ); 141 } 142 143 148 public void repaintChart( ) 149 { 150 repaint( ); 151 } 152 153 158 public Object peerInstance( ) 159 { 160 return this; 161 } 162 163 168 public Chart getDesignTimeModel( ) 169 { 170 return cm; 171 } 172 173 178 public Object getContext( Object key ) 179 { 180 return contextMap.get( key ); 181 } 182 183 189 public Object putContext( Object key, Object value ) 190 { 191 return contextMap.put( key, value ); 192 } 193 194 199 public Object removeContext( Object key ) 200 { 201 return contextMap.remove( key ); 202 } 203 204 209 public Chart getRunTimeModel( ) 210 { 211 return gcs.getChartModel( ); 212 } 213 214 public void paint( Graphics g ) 215 { 216 super.paint( g ); 217 218 Graphics2D g2d = (Graphics2D ) g; 219 idr.setProperty( IDeviceRenderer.GRAPHICS_CONTEXT, g2d ); 220 idr.setProperty( IDeviceRenderer.UPDATE_NOTIFIER, this ); 221 222 Dimension d = getSize( ); 223 Bounds bo = BoundsImpl.create( 0, 0, d.width, d.height ); 224 bo.scale( 72d / idr.getDisplayServer( ).getDpiResolution( ) ); 225 226 Generator gr = Generator.instance( ); 227 228 if ( bNeedsGeneration ) 230 { 231 bNeedsGeneration = false; 232 try 233 { 234 gcs = gr.build( idr.getDisplayServer( ), 235 cm, 236 bo, 237 null, 238 null, 239 null ); 240 } 241 catch ( ChartException ex ) 242 { 243 showException( g2d, ex ); 244 } 245 } 246 247 try 249 { 250 gr.render( idr, gcs ); 251 } 252 catch ( ChartException ex ) 253 { 254 showException( g2d, ex ); 255 } 256 } 257 258 264 private final void showException( Graphics2D g2d, Exception ex ) 265 { 266 String sWrappedException = ex.getClass( ).getName( ); 267 Throwable th = ex; 268 while ( ex.getCause( ) != null ) 269 { 270 ex = (Exception ) ex.getCause( ); 271 } 272 String sException = ex.getClass( ).getName( ); 273 if ( sWrappedException.equals( sException ) ) 274 { 275 sWrappedException = null; 276 } 277 278 String sMessage = null; 279 if ( th instanceof BirtException ) 280 { 281 sMessage = ( (BirtException) th ).getLocalizedMessage( ); 282 } 283 else 284 { 285 sMessage = ex.getMessage( ); 286 } 287 288 if ( sMessage == null ) 289 { 290 sMessage = "<null>"; } 292 293 StackTraceElement [] stea = ex.getStackTrace( ); 294 Dimension d = getSize( ); 295 296 Font fo = new Font ( "Monospaced", Font.BOLD, 14 ); g2d.setFont( fo ); 298 FontMetrics fm = g2d.getFontMetrics( ); 299 g2d.setColor( Color.WHITE ); 300 g2d.fillRect( 20, 20, d.width - 40, d.height - 40 ); 301 g2d.setColor( Color.BLACK ); 302 g2d.drawRect( 20, 20, d.width - 40, d.height - 40 ); 303 g2d.setClip( 20, 20, d.width - 40, d.height - 40 ); 304 int x = 25, y = 20 + fm.getHeight( ); 305 g2d.drawString( "Exception:", x, y ); x += fm.stringWidth( "Exception:" ) + 5; g2d.setColor( Color.RED ); 308 g2d.drawString( sException, x, y ); 309 x = 25; 310 y += fm.getHeight( ); 311 if ( sWrappedException != null ) 312 { 313 g2d.setColor( Color.BLACK ); 314 g2d.drawString( "Wrapped In:", x, y ); x += fm.stringWidth( "Wrapped In:" ) + 5; g2d.setColor( Color.RED ); 317 g2d.drawString( sWrappedException, x, y ); 318 x = 25; 319 y += fm.getHeight( ); 320 } 321 g2d.setColor( Color.BLACK ); 322 y += 10; 323 g2d.drawString( "Message:", x, y ); x += fm.stringWidth( "Message:" ) + 5; g2d.setColor( Color.BLUE ); 326 g2d.drawString( sMessage, x, y ); 327 x = 25; 328 y += fm.getHeight( ); 329 g2d.setColor( Color.BLACK ); 330 y += 10; 331 g2d.drawString( "Trace:", x, y ); x = 40; 333 y += fm.getHeight( ); 334 g2d.setColor( Color.GREEN.darker( ) ); 335 for ( int i = 0; i < stea.length; i++ ) 336 { 337 g2d.drawString( stea[i].getClassName( ) + ":" + stea[i].getMethodName( ) 339 + "(...):" + stea[i].getLineNumber( ), x, y ); 341 x = 40; 342 y += fm.getHeight( ); 343 } 344 } 345 346 351 public void componentHidden( ComponentEvent e ) 352 { 353 355 } 356 357 362 public void componentMoved( ComponentEvent e ) 363 { 364 366 } 367 368 373 public void componentResized( ComponentEvent e ) 374 { 375 bNeedsGeneration = true; 376 } 377 378 383 public void componentShown( ComponentEvent e ) 384 { 385 387 } 388 389 393 private final class ControlPanel extends JPanel implements ActionListener 394 { 395 396 private static final long serialVersionUID = 1L; 397 398 private JComboBox jcbModels = null; 399 400 private JButton jbUpdate = null; 401 402 private JComboBox jcbDimensions = null; 403 404 private JCheckBox jcbTransposed = null; 405 406 private JCheckBox jcbPercent = null; 407 408 private JCheckBox jcbLogarithmic = null; 409 410 private final SwingChartViewerSelector scv; 411 412 ControlPanel( SwingChartViewerSelector scv ) 413 { 414 this.scv = scv; 415 416 setLayout( new GridLayout ( 0, 1, 0, 0 ) ); 417 418 JPanel jp1 = new JPanel ( ); 419 jp1.setLayout( new FlowLayout ( FlowLayout.LEFT, 5, 5 ) ); 420 421 jp1.add( new JLabel ( "Choose:" ) ); jcbModels = new JComboBox ( ); 423 424 jcbModels.addItem( "Bar Chart" ); jcbModels.addItem( "Bar Chart(2 Series)" ); jcbModels.addItem( "Pie Chart" ); jcbModels.addItem( "Pie Chart(4 Series)" ); jcbModels.addItem( "Line Chart" ); jcbModels.addItem( "Bar/Line Stacked Chart" ); jcbModels.addItem( "Scatter Chart" ); jcbModels.addItem( "Stock Chart" ); jcbModels.addItem( "Area Chart" ); jcbModels.addItem( "Open Chart File" ); 435 jcbModels.setSelectedIndex( 0 ); 436 jp1.add( jcbModels ); 437 438 jcbDimensions = new JComboBox ( ); 439 jcbDimensions.addItem( "2D" ); jcbDimensions.addItem( "2D with Depth" ); jp1.add( jcbDimensions ); 442 443 jcbTransposed = new JCheckBox ( "Transposed", false ); jp1.add( jcbTransposed ); 445 446 jcbPercent = new JCheckBox ( "Percent", false ); jp1.add( jcbPercent ); 448 449 jcbLogarithmic = new JCheckBox ( "Logarithmic", false ); jp1.add( jcbLogarithmic ); 451 452 jbUpdate = new JButton ( "Update" ); jbUpdate.addActionListener( this ); 454 jp1.add( jbUpdate ); 455 456 add( jp1 ); 457 } 458 459 464 public void componentHidden( ComponentEvent cev ) 465 { 466 setVisible( false ); 467 } 468 469 474 public void componentMoved( ComponentEvent cev ) 475 { 476 JFrame jf = (JFrame ) cev.getComponent( ); 477 Rectangle r = jf.getBounds( ); 478 setLocation( r.x, r.y + r.height ); 479 setSize( r.width, 50 ); 480 } 481 482 487 public void componentResized( ComponentEvent cev ) 488 { 489 JFrame jf = (JFrame ) cev.getComponent( ); 490 Rectangle r = jf.getBounds( ); 491 setLocation( r.x, r.y + r.height ); 492 setSize( r.width, 50 ); 493 } 494 495 500 public void componentShown( ComponentEvent cev ) 501 { 502 JFrame jf = (JFrame ) cev.getComponent( ); 503 Rectangle r = jf.getBounds( ); 504 setLocation( r.x, r.y + r.height ); 505 setSize( r.width, 50 ); 506 setVisible( true ); 507 } 508 509 514 public void actionPerformed( ActionEvent e ) 515 { 516 int i = jcbModels.getSelectedIndex( ); 517 cm = null; 518 switch ( i ) 519 { 520 case 0 : 521 cm = PrimitiveCharts.createBarChart( ); 522 break; 523 case 1 : 524 cm = PrimitiveCharts.createMultiBarChart( ); 525 break; 526 case 2 : 527 cm = PrimitiveCharts.createPieChart( ); 528 break; 529 case 3 : 530 cm = PrimitiveCharts.createMultiPieChart( ); 531 break; 532 case 4 : 533 cm = PrimitiveCharts.createLineChart( ); 534 break; 535 case 5 : 536 cm = PrimitiveCharts.createStackedChart( ); 537 break; 538 case 6 : 539 cm = PrimitiveCharts.createScatterChart( ); 540 break; 541 case 7 : 542 cm = PrimitiveCharts.createStockChart( ); 543 break; 544 case 8 : 545 cm = PrimitiveCharts.createAreaChart( ); 546 break; 547 case 9 : 548 cm = PrimitiveCharts.openChart( ); 549 break; 550 } 551 552 if ( cm instanceof ChartWithAxes ) 553 { 554 jcbTransposed.setEnabled( true ); 555 jcbLogarithmic.setEnabled( true ); 556 jcbPercent.setEnabled( true ); 557 558 ChartWithAxes cwa = ( (ChartWithAxes) cm ); 559 cwa.setTransposed( jcbTransposed.isSelected( ) ); 560 Axis ax = cwa.getPrimaryOrthogonalAxis( cwa.getPrimaryBaseAxes( )[0] ); 561 562 if ( jcbLogarithmic.isSelected( ) ) 563 { 564 if ( ax.getType( ) == AxisType.LINEAR_LITERAL ) 565 { 566 ax.setType( AxisType.LOGARITHMIC_LITERAL ); 567 } 568 } 569 else 570 { 571 if ( ax.getType( ) == AxisType.LOGARITHMIC_LITERAL ) 572 { 573 ax.setType( AxisType.LINEAR_LITERAL ); 574 } 575 } 576 577 if ( jcbPercent.isSelected( ) == true ) 578 { 579 ax.setFormatSpecifier( JavaNumberFormatSpecifierImpl.create( "0'%'" ) ); } 581 else 582 { 583 ax.setFormatSpecifier( null ); 584 } 585 } 586 else if ( cm instanceof ChartWithoutAxes ) 587 { 588 jcbTransposed.setEnabled( false ); 589 jcbLogarithmic.setEnabled( false ); 590 jcbPercent.setEnabled( false ); 591 } 592 593 if ( jcbModels.getSelectedIndex( ) == 7 ) 594 { 595 cm.setDimension( ChartDimension.TWO_DIMENSIONAL_LITERAL ); 596 } 597 else 598 { 599 switch ( jcbDimensions.getSelectedIndex( ) ) 600 { 601 case 0 : 602 cm.setDimension( ChartDimension.TWO_DIMENSIONAL_LITERAL ); 603 break; 604 case 1 : 605 cm.setDimension( ChartDimension.TWO_DIMENSIONAL_WITH_DEPTH_LITERAL ); 606 break; 607 } 608 } 609 610 bNeedsGeneration = true; 611 scv.repaint( ); 612 } 613 } 614 } | Popular Tags |