1 11 12 package org.eclipse.birt.chart.examples.api.format; 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 FormatChartsViewer 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 FormatChartsViewer fcv = new FormatChartsViewer( ); 79 80 JFrame jf = new JFrame ( ); 81 jf.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE ); 82 jf.addComponentListener( fcv ); 83 84 Container co = jf.getContentPane( ); 85 co.setLayout( new BorderLayout ( ) ); 86 co.add( fcv, 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( fcv.getClass( ).getName( ) + " [device=" + fcv.idr.getClass( ).getName( ) + "]" ); 97 ControlPanel cp = fcv.new ControlPanel( fcv ); 98 co.add( cp, BorderLayout.SOUTH ); 99 100 jf.setVisible( true ); 101 } 102 103 106 FormatChartsViewer( ) 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 = FormatCharts.createAxisFormatChart( ); 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 385 private static final long serialVersionUID = 1L; 386 387 private JComboBox jcbModels = null; 388 389 private JButton jbUpdate = null; 390 391 private final FormatChartsViewer fcv; 392 393 ControlPanel( FormatChartsViewer fcv ) 394 { 395 this.fcv = fcv; 396 397 setLayout( new GridLayout ( 0, 1, 0, 0 ) ); 398 399 JPanel jp = new JPanel ( ); 400 jp.setLayout( new FlowLayout ( FlowLayout.LEFT, 3, 3 ) ); 401 402 jp.add( new JLabel ( "Choose:" ) ); jcbModels = new JComboBox ( ); 404 405 jcbModels.addItem( "Axis Format" ); jcbModels.addItem( "Colored By Category" ); jcbModels.addItem( "Legend & Title Format" ); jcbModels.addItem( "Percentage Values" ); jcbModels.addItem( "Plot Format" ); jcbModels.addItem( "Series Format" ); 412 jcbModels.setSelectedIndex( 0 ); 413 jp.add( jcbModels ); 414 415 jbUpdate = new JButton ( "Update" ); jbUpdate.addActionListener( this ); 417 jp.add( jbUpdate ); 418 419 add( jp ); 420 } 421 422 427 public void componentHidden( ComponentEvent cev ) 428 { 429 setVisible( false ); 430 } 431 432 437 public void componentMoved( ComponentEvent cev ) 438 { 439 JFrame jf = (JFrame ) cev.getComponent( ); 440 Rectangle r = jf.getBounds( ); 441 setLocation( r.x, r.y + r.height ); 442 setSize( r.width, 50 ); 443 } 444 445 450 public void componentResized( ComponentEvent cev ) 451 { 452 JFrame jf = (JFrame ) cev.getComponent( ); 453 Rectangle r = jf.getBounds( ); 454 setLocation( r.x, r.y + r.height ); 455 setSize( r.width, 50 ); 456 } 457 458 463 public void componentShown( ComponentEvent cev ) 464 { 465 JFrame jf = (JFrame ) cev.getComponent( ); 466 Rectangle r = jf.getBounds( ); 467 setLocation( r.x, r.y + r.height ); 468 setSize( r.width, 50 ); 469 setVisible( true ); 470 } 471 472 477 public void actionPerformed( ActionEvent e ) 478 { 479 int i = jcbModels.getSelectedIndex( ); 480 cm = null; 481 switch ( i ) 482 { 483 case 0 : 484 cm = FormatCharts.createAxisFormatChart( ); 485 break; 486 case 1 : 487 cm = FormatCharts.createColoredByCategoryChart( ); 488 break; 489 case 2 : 490 cm = FormatCharts.createLegendTitleChart( ); 491 break; 492 case 3 : 493 cm = FormatCharts.createPercentageValueChart( ); 494 break; 495 case 4 : 496 cm = FormatCharts.createPlotFormatChart( ); 497 break; 498 case 5 : 499 cm = FormatCharts.createSeriesFormatChart( ); 500 break; 501 } 502 503 bNeedsGeneration = true; 504 fcv.repaint( ); 505 } 506 } 507 } 508 | Popular Tags |