1 11 12 package org.eclipse.birt.chart.examples.api.viewer; 13 14 import org.eclipse.birt.chart.device.IDeviceRenderer; 15 import org.eclipse.birt.chart.examples.api.script.JavaScriptViewer; 16 import org.eclipse.birt.chart.exception.ChartException; 17 import org.eclipse.birt.chart.factory.GeneratedChartState; 18 import org.eclipse.birt.chart.factory.Generator; 19 import org.eclipse.birt.chart.log.ILogger; 20 import org.eclipse.birt.chart.log.Logger; 21 import org.eclipse.birt.chart.model.Chart; 22 import org.eclipse.birt.chart.model.attribute.Bounds; 23 import org.eclipse.birt.chart.model.attribute.impl.BoundsImpl; 24 import org.eclipse.birt.chart.util.PluginSettings; 25 import org.eclipse.birt.core.exception.BirtException; 26 import org.eclipse.swt.SWT; 27 import org.eclipse.swt.events.PaintEvent; 28 import org.eclipse.swt.events.SelectionEvent; 29 import org.eclipse.swt.graphics.Device; 30 import org.eclipse.swt.graphics.Font; 31 import org.eclipse.swt.graphics.FontMetrics; 32 import org.eclipse.swt.graphics.GC; 33 import org.eclipse.swt.graphics.Image; 34 import org.eclipse.swt.graphics.Point; 35 import org.eclipse.swt.graphics.Rectangle; 36 import org.eclipse.swt.layout.GridData; 37 import org.eclipse.swt.layout.GridLayout; 38 import org.eclipse.swt.layout.RowLayout; 39 import org.eclipse.swt.widgets.Button; 40 import org.eclipse.swt.widgets.Combo; 41 import org.eclipse.swt.widgets.Composite; 42 import org.eclipse.swt.widgets.Display; 43 import org.eclipse.swt.widgets.Label; 44 import org.eclipse.swt.widgets.Shell; 45 import org.eclipse.swt.events.PaintListener; 46 import org.eclipse.swt.events.SelectionListener; 47 48 public class Chart3DViewer extends Composite implements 49 PaintListener, 50 SelectionListener 51 { 52 53 private IDeviceRenderer idr = null; 54 55 private Chart cm = null; 56 57 private static Combo cbType = null; 58 59 private static Button btn = null; 60 61 private GeneratedChartState gcs = null; 62 63 private boolean bNeedsGeneration = true; 64 65 private static ILogger logger = Logger.getLogger( JavaScriptViewer.class.getName( ) ); 66 71 public static void main( String [] args ) 72 { 73 Display display = Display.getDefault( ); 74 Shell shell = new Shell( display ); 75 shell.setSize( 600, 400 ); 76 shell.setLayout( new GridLayout( ) ); 77 78 Chart3DViewer c3dViewer = new Chart3DViewer( shell, SWT.NO_BACKGROUND ); 79 c3dViewer.setLayoutData( new GridData( GridData.FILL_BOTH ) ); 80 c3dViewer.addPaintListener( c3dViewer ); 81 82 Composite cBottom = new Composite( shell, SWT.NONE ); 83 cBottom.setLayoutData( new GridData( GridData.FILL_HORIZONTAL ) ); 84 cBottom.setLayout( new RowLayout( ) ); 85 86 Label la = new Label( cBottom, SWT.NONE ); 87 88 la.setText( "Choose: " ); cbType = new Combo( cBottom, SWT.DROP_DOWN | SWT.READ_ONLY ); 90 cbType.add( "3D Bar Chart" ); cbType.add( "3D Line Chart" ); cbType.add( "3D Area Chart" ); cbType.select( 0 ); 94 95 btn = new Button( cBottom, SWT.NONE ); 96 btn.setText( "Update" ); btn.addSelectionListener( c3dViewer ); 98 99 shell.open( ); 100 while ( !shell.isDisposed( ) ) 101 { 102 if ( !display.readAndDispatch( ) ) 103 display.sleep( ); 104 } 105 display.dispose( ); 106 } 107 108 111 Chart3DViewer( Composite parent, int style ) 112 { 113 super( parent, style ); 114 final PluginSettings ps = PluginSettings.instance( ); 115 try 116 { 117 idr = ps.getDevice( "dv.SWT" ); } 119 catch ( ChartException pex ) 120 { 121 logger.log( pex ); 122 } 123 cm = PrimitiveCharts.create3DBarChart( ); 124 } 125 126 131 public final void paintControl( PaintEvent e ) 132 { 133 Rectangle d = this.getClientArea( ); 134 Image imgChart = new Image( this.getDisplay( ), d ); 135 GC gcImage = new GC( imgChart ); 136 idr.setProperty( IDeviceRenderer.GRAPHICS_CONTEXT, gcImage ); 137 138 Bounds bo = BoundsImpl.create( 0, 0, d.width, d.height ); 139 bo.scale( 72d / idr.getDisplayServer( ).getDpiResolution( ) ); 140 141 Generator gr = Generator.instance( ); 142 if ( bNeedsGeneration ) 143 { 144 bNeedsGeneration = false; 145 try 146 { 147 gcs = gr.build( idr.getDisplayServer( ), 148 cm, 149 bo, 150 null, 151 null, 152 null ); 153 } 154 catch ( ChartException ce ) 155 { 156 ce.printStackTrace( ); 157 } 158 } 159 160 try 161 { 162 gr.render( idr, gcs ); 163 GC gc = e.gc; 164 gc.drawImage( imgChart, d.x, d.y ); 165 } 166 catch ( ChartException gex ) 167 { 168 showException( e.gc, gex ); 169 } 170 } 171 172 177 public void widgetSelected( SelectionEvent e ) 178 { 179 if ( e.widget.equals( btn ) ) 180 { 181 int iSelection = cbType.getSelectionIndex( ); 182 switch ( iSelection ) 183 { 184 case 0 : 185 cm = PrimitiveCharts.create3DBarChart( ); 186 break; 187 case 1 : 188 cm = PrimitiveCharts.create3DLineChart( ); 189 break; 190 case 2 : 191 cm = PrimitiveCharts.create3DAreaChart( ); 192 break; 193 } 194 bNeedsGeneration = true; 195 this.redraw( ); 196 } 197 } 198 199 204 public void widgetDefaultSelected( SelectionEvent e ) 205 { 206 208 } 209 210 private final void showException( GC g2d, Exception ex ) 211 { 212 String sWrappedException = ex.getClass( ).getName( ); 213 Throwable th = ex; 214 while ( ex.getCause( ) != null ) 215 { 216 ex = (Exception ) ex.getCause( ); 217 } 218 String sException = ex.getClass( ).getName( ); 219 if ( sWrappedException.equals( sException ) ) 220 { 221 sWrappedException = null; 222 } 223 224 String sMessage = null; 225 if ( th instanceof BirtException ) 226 { 227 sMessage = ( (BirtException) th ).getLocalizedMessage( ); 228 } 229 else 230 { 231 sMessage = ex.getMessage( ); 232 } 233 234 if ( sMessage == null ) 235 { 236 sMessage = "<null>"; } 238 StackTraceElement [] stea = ex.getStackTrace( ); 239 Point d = this.getSize( ); 240 241 Device dv = Display.getCurrent( ); 242 Font fo = new Font( dv, "Courier", SWT.BOLD, 16 ); g2d.setFont( fo ); 244 FontMetrics fm = g2d.getFontMetrics( ); 245 g2d.setBackground( dv.getSystemColor( SWT.COLOR_WHITE ) ); 246 g2d.fillRectangle( 20, 20, d.x - 40, d.y - 40 ); 247 g2d.setForeground( dv.getSystemColor( SWT.COLOR_BLACK ) ); 248 g2d.drawRectangle( 20, 20, d.x - 40, d.y - 40 ); 249 g2d.setClipping( 20, 20, d.x - 40, d.y - 40 ); 250 int x = 25, y = 20 + fm.getHeight( ); 251 g2d.drawString( "Exception:", x, y ); x += g2d.textExtent( "Exception:" ).x + 5; g2d.setForeground( dv.getSystemColor( SWT.COLOR_RED ) ); 254 g2d.drawString( sException, x, y ); 255 x = 25; 256 y += fm.getHeight( ); 257 if ( sWrappedException != null ) 258 { 259 g2d.setForeground( dv.getSystemColor( SWT.COLOR_BLACK ) ); 260 g2d.drawString( "Wrapped In:", x, y ); x += g2d.textExtent( "Wrapped In:" ).x + 5; g2d.setForeground( dv.getSystemColor( SWT.COLOR_RED ) ); 263 g2d.drawString( sWrappedException, x, y ); 264 x = 25; 265 y += fm.getHeight( ); 266 } 267 g2d.setForeground( dv.getSystemColor( SWT.COLOR_BLACK ) ); 268 y += 10; 269 g2d.drawString( "Message:", x, y ); x += g2d.textExtent( "Message:" ).x + 5; g2d.setForeground( dv.getSystemColor( SWT.COLOR_BLUE ) ); 272 g2d.drawString( sMessage, x, y ); 273 x = 25; 274 y += fm.getHeight( ); 275 g2d.setForeground( dv.getSystemColor( SWT.COLOR_BLACK ) ); 276 y += 10; 277 g2d.drawString( "Trace:", x, y ); x = 40; 279 y += fm.getHeight( ); 280 g2d.setForeground( dv.getSystemColor( SWT.COLOR_DARK_GREEN ) ); 281 for ( int i = 0; i < stea.length; i++ ) 282 { 283 g2d.drawString( stea[i].getClassName( ) + ":" + stea[i].getMethodName( ) 285 + "(...):" + stea[i].getLineNumber( ), x, y ); 287 x = 40; 288 y += fm.getHeight( ); 289 } 290 fo.dispose( ); 291 } 292 } 293 | Popular Tags |