1 11 12 package org.eclipse.birt.chart.examples.api.data; 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.JComboBox ; 35 import javax.swing.JFrame ; 36 import javax.swing.JLabel ; 37 import javax.swing.JPanel ; 38 import org.eclipse.birt.chart.device.IDeviceRenderer; 39 import org.eclipse.birt.chart.device.IUpdateNotifier; 40 import org.eclipse.birt.chart.exception.ChartException; 41 import org.eclipse.birt.chart.factory.GeneratedChartState; 42 import org.eclipse.birt.chart.factory.Generator; 43 import org.eclipse.birt.chart.model.Chart; 44 import org.eclipse.birt.chart.model.attribute.Bounds; 45 import org.eclipse.birt.chart.model.attribute.impl.BoundsImpl; 46 import org.eclipse.birt.chart.util.PluginSettings; 47 import org.eclipse.birt.core.exception.BirtException; 48 49 53 public final class DataChartsViewer extends JPanel implements 54 IUpdateNotifier, 55 ComponentListener 56 { 57 58 private static final long serialVersionUID = 1L; 59 60 private boolean bNeedsGeneration = true; 61 62 private GeneratedChartState gcs = null; 63 64 private Chart cm = null; 65 66 private IDeviceRenderer idr = null; 67 68 private Map contextMap; 69 70 76 public static void main( String [] args ) 77 { 78 DataChartsViewer dcv = new DataChartsViewer( ); 79 80 JFrame jf = new JFrame ( ); 81 jf.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE ); 82 jf.addComponentListener( dcv ); 83 84 Container co = jf.getContentPane( ); 85 co.setLayout( new BorderLayout ( ) ); 86 co.add( dcv, BorderLayout.CENTER ); 87 88 Dimension dScreen = Toolkit.getDefaultToolkit( ).getScreenSize( ); 89 Dimension dApp = new Dimension ( 600, 400 ); 90 jf.setSize( dApp ); 91 jf.setLocation( ( dScreen.width - dApp.width ) / 2, 92 ( dScreen.height - dApp.height ) / 2 ); 93 94 jf.setTitle( dcv.getClass( ).getName( ) + " [device=" + dcv.idr.getClass( ).getName( ) + "]" ); 97 ControlPanel cp = dcv.new ControlPanel( dcv ); 98 co.add( cp, BorderLayout.SOUTH ); 99 100 jf.setVisible( true ); 101 } 102 103 106 DataChartsViewer( ) 107 { 108 contextMap = new HashMap (); 109 110 final PluginSettings ps = PluginSettings.instance( ); 111 try 112 { 113 idr = ps.getDevice( "dv.SWING" ); } 115 catch ( ChartException ex ) 116 { 117 ex.printStackTrace( ); 118 } 119 cm = DataCharts.createMinSliceChart( ); 120 } 121 122 127 public void regenerateChart( ) 128 { 129 bNeedsGeneration = true; 130 repaint( ); 131 } 132 133 138 public void repaintChart( ) 139 { 140 repaint( ); 141 } 142 143 148 public Object peerInstance( ) 149 { 150 return this; 151 } 152 153 158 public Chart getDesignTimeModel( ) 159 { 160 return cm; 161 } 162 163 168 public Chart getRunTimeModel( ) 169 { 170 return gcs.getChartModel( ); 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 public void paint( Graphics g ) 205 { 206 super.paint( g ); 207 208 Graphics2D g2d = (Graphics2D ) g; 209 idr.setProperty( IDeviceRenderer.GRAPHICS_CONTEXT, g2d ); 210 idr.setProperty( IDeviceRenderer.UPDATE_NOTIFIER, this ); 211 212 Dimension d = getSize( ); 213 Bounds bo = BoundsImpl.create( 0, 0, d.width, d.height ); 214 bo.scale( 72d / idr.getDisplayServer( ).getDpiResolution( ) ); 215 216 Generator gr = Generator.instance( ); 217 218 if ( bNeedsGeneration ) 220 { 221 bNeedsGeneration = false; 222 try 223 { 224 gcs = gr.build( idr.getDisplayServer( ), 225 cm, 226 bo, 227 null, 228 null, 229 null ); 230 } 231 catch ( ChartException ex ) 232 { 233 showException( g2d, ex ); 234 } 235 } 236 237 try 239 { 240 gr.render( idr, gcs ); 241 } 242 catch ( ChartException ex ) 243 { 244 showException( g2d, ex ); 245 } 246 } 247 248 254 private final void showException( Graphics2D g2d, Exception ex ) 255 { 256 String sWrappedException = ex.getClass( ).getName( ); 257 Throwable th = ex; 258 while ( ex.getCause( ) != null ) 259 { 260 ex = (Exception ) ex.getCause( ); 261 } 262 String sException = ex.getClass( ).getName( ); 263 if ( sWrappedException.equals( sException ) ) 264 { 265 sWrappedException = null; 266 } 267 268 String sMessage = null; 269 if ( th instanceof BirtException ) 270 { 271 sMessage = ( (BirtException) th ).getLocalizedMessage( ); 272 } 273 else 274 { 275 sMessage = ex.getMessage( ); 276 } 277 278 if ( sMessage == null ) 279 { 280 sMessage = "<null>"; } 282 283 StackTraceElement [] stea = ex.getStackTrace( ); 284 Dimension d = getSize( ); 285 286 Font fo = new Font ( "Monospaced", Font.BOLD, 14 ); g2d.setFont( fo ); 288 FontMetrics fm = g2d.getFontMetrics( ); 289 g2d.setColor( Color.WHITE ); 290 g2d.fillRect( 20, 20, d.width - 40, d.height - 40 ); 291 g2d.setColor( Color.BLACK ); 292 g2d.drawRect( 20, 20, d.width - 40, d.height - 40 ); 293 g2d.setClip( 20, 20, d.width - 40, d.height - 40 ); 294 int x = 25, y = 20 + fm.getHeight( ); 295 g2d.drawString( "Exception:", x, y ); x += fm.stringWidth( "Exception:" ) + 5; g2d.setColor( Color.RED ); 298 g2d.drawString( sException, x, y ); 299 x = 25; 300 y += fm.getHeight( ); 301 if ( sWrappedException != null ) 302 { 303 g2d.setColor( Color.BLACK ); 304 g2d.drawString( "Wrapped In:", x, y ); x += fm.stringWidth( "Wrapped In:" ) + 5; g2d.setColor( Color.RED ); 307 g2d.drawString( sWrappedException, x, y ); 308 x = 25; 309 y += fm.getHeight( ); 310 } 311 g2d.setColor( Color.BLACK ); 312 y += 10; 313 g2d.drawString( "Message:", x, y ); x += fm.stringWidth( "Message:" ) + 5; g2d.setColor( Color.BLUE ); 316 g2d.drawString( sMessage, x, y ); 317 x = 25; 318 y += fm.getHeight( ); 319 g2d.setColor( Color.BLACK ); 320 y += 10; 321 g2d.drawString( "Trace:", x, y ); x = 40; 323 y += fm.getHeight( ); 324 g2d.setColor( Color.GREEN.darker( ) ); 325 for ( int i = 0; i < stea.length; i++ ) 326 { 327 g2d.drawString( stea[i].getClassName( ) + ":" + stea[i].getMethodName( ) + "(...):" + stea[i].getLineNumber( ), x, y ); 330 x = 40; 331 y += fm.getHeight( ); 332 } 333 } 334 335 340 public void componentHidden( ComponentEvent e ) 341 { 342 344 } 345 346 351 public void componentMoved( ComponentEvent e ) 352 { 353 355 } 356 357 362 public void componentResized( ComponentEvent e ) 363 { 364 bNeedsGeneration = true; 365 } 366 367 372 public void componentShown( ComponentEvent e ) 373 { 374 376 } 377 378 382 private final class ControlPanel extends JPanel implements ActionListener 383 { 384 private static final long serialVersionUID = 1L; 385 386 private JComboBox jcbModels = null; 387 388 private JButton jbUpdate = null; 389 390 private final DataChartsViewer dcv; 391 392 ControlPanel( DataChartsViewer dcv ) 393 { 394 this.dcv = dcv; 395 396 setLayout( new GridLayout ( 0, 1, 0, 0 ) ); 397 398 JPanel jp = new JPanel ( ); 399 jp.setLayout( new FlowLayout ( FlowLayout.LEFT, 3, 3 ) ); 400 401 jp.add( new JLabel ( "Choose:" ) ); jcbModels = new JComboBox ( ); 403 404 jcbModels.addItem( "Min Slice" ); jcbModels.addItem( "Multiple Y Axis" ); jcbModels.addItem( "Multiple Y Series" ); 408 jcbModels.setSelectedIndex( 0 ); 409 jp.add( jcbModels ); 410 411 jbUpdate = new JButton ( "Update" ); jbUpdate.addActionListener( this ); 413 jp.add( jbUpdate ); 414 415 add( jp ); 416 } 417 418 423 public void componentHidden( ComponentEvent cev ) 424 { 425 setVisible( false ); 426 } 427 428 433 public void componentMoved( ComponentEvent cev ) 434 { 435 JFrame jf = (JFrame ) cev.getComponent( ); 436 Rectangle r = jf.getBounds( ); 437 setLocation( r.x, r.y + r.height ); 438 setSize( r.width, 50 ); 439 } 440 441 446 public void componentResized( ComponentEvent cev ) 447 { 448 JFrame jf = (JFrame ) cev.getComponent( ); 449 Rectangle r = jf.getBounds( ); 450 setLocation( r.x, r.y + r.height ); 451 setSize( r.width, 50 ); 452 } 453 454 459 public void componentShown( ComponentEvent cev ) 460 { 461 JFrame jf = (JFrame ) cev.getComponent( ); 462 Rectangle r = jf.getBounds( ); 463 setLocation( r.x, r.y + r.height ); 464 setSize( r.width, 50 ); 465 setVisible( true ); 466 } 467 468 473 public void actionPerformed( ActionEvent e ) 474 { 475 int i = jcbModels.getSelectedIndex( ); 476 cm = null; 477 switch ( i ) 478 { 479 case 0 : 480 cm = DataCharts.createMinSliceChart( ); 481 break; 482 case 1 : 483 cm = DataCharts.createMulitYSeriesChart( ); 484 break; 485 case 2 : 486 cm = DataCharts.createMultiYAxisChart( ); 487 break; 488 } 489 490 bNeedsGeneration = true; 491 dcv.repaint( ); 492 } 493 } 494 } 495 | Popular Tags |