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.ChartWithAxes; 23 import org.eclipse.birt.chart.model.ChartWithoutAxes; 24 import org.eclipse.birt.chart.model.attribute.AxisType; 25 import org.eclipse.birt.chart.model.attribute.Bounds; 26 import org.eclipse.birt.chart.model.attribute.ChartDimension; 27 import org.eclipse.birt.chart.model.attribute.impl.BoundsImpl; 28 import org.eclipse.birt.chart.model.attribute.impl.JavaNumberFormatSpecifierImpl; 29 import org.eclipse.birt.chart.model.component.Axis; 30 import org.eclipse.birt.chart.util.PluginSettings; 31 import org.eclipse.birt.core.exception.BirtException; 32 33 import org.eclipse.swt.SWT; 34 import org.eclipse.swt.events.PaintEvent; 35 import org.eclipse.swt.events.PaintListener; 36 import org.eclipse.swt.events.SelectionEvent; 37 import org.eclipse.swt.events.SelectionListener; 38 import org.eclipse.swt.graphics.Device; 39 import org.eclipse.swt.graphics.Font; 40 import org.eclipse.swt.graphics.FontMetrics; 41 import org.eclipse.swt.graphics.GC; 42 import org.eclipse.swt.graphics.Image; 43 import org.eclipse.swt.graphics.Point; 44 import org.eclipse.swt.graphics.Rectangle; 45 import org.eclipse.swt.layout.GridData; 46 import org.eclipse.swt.layout.GridLayout; 47 import org.eclipse.swt.layout.RowLayout; 48 import org.eclipse.swt.widgets.Button; 49 import org.eclipse.swt.widgets.Combo; 50 import org.eclipse.swt.widgets.Composite; 51 import org.eclipse.swt.widgets.Display; 52 import org.eclipse.swt.widgets.Label; 53 import org.eclipse.swt.widgets.Shell; 54 55 59 public final class SwtChartViewerSelector extends Composite implements 60 PaintListener, 61 SelectionListener 62 { 63 64 private IDeviceRenderer idr = null; 65 66 private Chart cm = null; 67 68 private static Combo cbType = null; 69 70 private static Combo cbDimension = null; 71 72 private static Button btn = null; 73 74 private GeneratedChartState gcs = null; 75 76 private boolean bNeedsGeneration = true; 77 78 private static Button cbPercent = null; 79 80 private static Button cbLogarithmic = null; 81 82 private static Button cbTransposed = null; 83 84 private static ILogger logger = Logger.getLogger( JavaScriptViewer.class.getName( ) ); 85 90 public static void main( String [] args ) 91 { 92 Display display = Display.getDefault( ); 93 Shell shell = new Shell( display ); 94 shell.setSize( 800, 600 ); 95 shell.setLayout( new GridLayout( ) ); 96 97 SwtChartViewerSelector scv = new SwtChartViewerSelector( shell, SWT.NO_BACKGROUND ); 98 scv.setLayoutData( new GridData( GridData.FILL_BOTH ) ); 99 scv.addPaintListener( scv ); 100 101 Composite cBottom = new Composite( shell, SWT.NONE ); 102 cBottom.setLayoutData( new GridData( GridData.FILL_HORIZONTAL ) ); 103 cBottom.setLayout( new RowLayout( ) ); 104 105 Label la = new Label( cBottom, SWT.NONE ); 106 107 la.setText( "Choose: " ); cbType = new Combo( cBottom, SWT.DROP_DOWN | SWT.READ_ONLY ); 109 cbType.add( "Bar Chart" ); cbType.add( "Bar Chart(2 Series)" ); cbType.add( "Pie Chart" ); cbType.add( "Pie Chart(4 Series)" ); cbType.add( "Line Chart" ); cbType.add( "Bar/Line Stacked Chart" ); cbType.add( "Scatter Chart" ); cbType.add( "Stock Chart" ); cbType.add( "Area Chart" ); cbType.select( 0 ); 119 120 cbDimension = new Combo( cBottom, SWT.DROP_DOWN | SWT.READ_ONLY ); 121 cbDimension.add( "2D" ); cbDimension.add( "2D with Depth" ); cbDimension.select( 0 ); 124 125 cbTransposed = new Button( cBottom, SWT.CHECK ); 126 cbTransposed.setText( "Transposed" ); 128 cbPercent = new Button( cBottom, SWT.CHECK ); 129 cbPercent.setText( "Percent" ); 131 cbLogarithmic = new Button( cBottom, SWT.CHECK ); 132 cbLogarithmic.setText( "Logarithmic" ); 134 btn = new Button( cBottom, SWT.NONE ); 135 btn.setText( "Update" ); btn.addSelectionListener( scv ); 137 138 shell.open( ); 139 while ( !shell.isDisposed( ) ) 140 { 141 if ( !display.readAndDispatch( ) ) 142 display.sleep( ); 143 } 144 display.dispose( ); 145 } 146 147 150 SwtChartViewerSelector( Composite parent, int style ) 151 { 152 super( parent, style ); 153 final PluginSettings ps = PluginSettings.instance( ); 154 try 155 { 156 idr = ps.getDevice( "dv.SWT" ); } 158 catch ( ChartException ex ) 159 { 160 logger.log( ex ); 161 } 162 cm = PrimitiveCharts.createBarChart( ); 163 164 } 165 166 171 public final void paintControl( PaintEvent e ) 172 { 173 Rectangle d = this.getClientArea( ); 174 Image imgChart = new Image( this.getDisplay( ), d ); 175 GC gcImage = new GC( imgChart ); 176 idr.setProperty( IDeviceRenderer.GRAPHICS_CONTEXT, gcImage ); 177 178 Bounds bo = BoundsImpl.create( 0, 0, d.width, d.height ); 179 bo.scale( 72d / idr.getDisplayServer( ).getDpiResolution( ) ); 180 181 Generator gr = Generator.instance( ); 182 if ( bNeedsGeneration ) 183 { 184 bNeedsGeneration = false; 185 try 186 { 187 gcs = gr.build( idr.getDisplayServer( ), 188 cm, 189 bo, 190 null, 191 null, 192 null ); 193 } 194 catch ( ChartException ce ) 195 { 196 ce.printStackTrace( ); 197 } 198 } 199 200 try 201 { 202 gr.render( idr, gcs ); 203 GC gc = e.gc; 204 gc.drawImage( imgChart, d.x, d.y ); 205 } 206 catch ( ChartException gex ) 207 { 208 showException( e.gc, gex ); 209 } 210 } 211 212 217 public void widgetSelected( SelectionEvent e ) 218 { 219 if ( e.widget.equals( btn ) ) 220 { 221 int iSelection = cbType.getSelectionIndex( ); 222 switch ( iSelection ) 223 { 224 case 0 : 225 cm = PrimitiveCharts.createBarChart( ); 226 break; 227 case 1 : 228 cm = PrimitiveCharts.createMultiBarChart( ); 229 break; 230 case 2 : 231 cm = PrimitiveCharts.createPieChart( ); 232 break; 233 case 3 : 234 cm = PrimitiveCharts.createMultiPieChart( ); 235 break; 236 case 4 : 237 cm = PrimitiveCharts.createLineChart( ); 238 break; 239 case 5 : 240 cm = PrimitiveCharts.createStackedChart( ); 241 break; 242 case 6 : 243 cm = PrimitiveCharts.createScatterChart( ); 244 break; 245 case 7 : 246 cm = PrimitiveCharts.createStockChart( ); 247 break; 248 case 8 : 249 cm = PrimitiveCharts.createAreaChart( ); 250 break; 251 } 252 253 if ( cm instanceof ChartWithAxes ) 254 { 255 256 cbTransposed.setEnabled( true ); 257 cbLogarithmic.setEnabled( true ); 258 cbPercent.setEnabled( true ); 259 260 ChartWithAxes cwa = ( (ChartWithAxes) cm ); 261 cwa.setTransposed( cbTransposed.getSelection( ) ); 262 Axis ax = cwa.getPrimaryOrthogonalAxis( cwa.getPrimaryBaseAxes( )[0] ); 263 264 if ( cbLogarithmic.getSelection( ) ) 265 { 266 if ( ax.getType( ) == AxisType.LINEAR_LITERAL ) 267 { 268 ax.setType( AxisType.LOGARITHMIC_LITERAL ); 269 } 270 } 271 else 272 { 273 if ( ax.getType( ) == AxisType.LOGARITHMIC_LITERAL ) 274 { 275 ax.setType( AxisType.LINEAR_LITERAL ); 276 } 277 } 278 279 if ( cbPercent.getSelection( ) == true ) 280 { 281 ax.setFormatSpecifier( JavaNumberFormatSpecifierImpl.create( "0'%'" ) ); } 283 else 284 { 285 ax.setFormatSpecifier( null ); 286 } 287 288 } 289 else if ( cm instanceof ChartWithoutAxes ) 290 { 291 cbTransposed.setEnabled( false ); 292 cbLogarithmic.setEnabled( false ); 293 cbPercent.setEnabled( false ); 294 } 295 296 if ( cbType.getSelectionIndex( ) == 7 ) 297 { 298 cm.setDimension( ChartDimension.TWO_DIMENSIONAL_LITERAL ); 299 } 300 else 301 { 302 switch ( cbDimension.getSelectionIndex( ) ) 303 { 304 305 case 0 : 306 cm.setDimension( ChartDimension.TWO_DIMENSIONAL_LITERAL ); 307 break; 308 case 1 : 309 cm.setDimension( ChartDimension.TWO_DIMENSIONAL_WITH_DEPTH_LITERAL ); 310 break; 311 } 312 } 313 314 bNeedsGeneration = true; 315 this.redraw( ); 316 } 317 } 318 319 324 public void widgetDefaultSelected( SelectionEvent e ) 325 { 326 328 } 329 330 private final void showException( GC g2d, Exception ex ) 331 { 332 String sWrappedException = ex.getClass( ).getName( ); 333 Throwable th = ex; 334 while ( ex.getCause( ) != null ) 335 { 336 ex = (Exception ) ex.getCause( ); 337 } 338 String sException = ex.getClass( ).getName( ); 339 if ( sWrappedException.equals( sException ) ) 340 { 341 sWrappedException = null; 342 } 343 344 String sMessage = null; 345 if ( th instanceof BirtException ) 346 { 347 sMessage = ( (BirtException) th ).getLocalizedMessage( ); 348 } 349 else 350 { 351 sMessage = ex.getMessage( ); 352 } 353 354 if ( sMessage == null ) 355 { 356 sMessage = "<null>"; } 358 StackTraceElement [] stea = ex.getStackTrace( ); 359 Point d = this.getSize( ); 360 361 Device dv = Display.getCurrent( ); 362 Font fo = new Font( dv, "Courier", SWT.BOLD, 16 ); g2d.setFont( fo ); 364 FontMetrics fm = g2d.getFontMetrics( ); 365 g2d.setBackground( dv.getSystemColor( SWT.COLOR_WHITE ) ); 366 g2d.fillRectangle( 20, 20, d.x - 40, d.y - 40 ); 367 g2d.setForeground( dv.getSystemColor( SWT.COLOR_BLACK ) ); 368 g2d.drawRectangle( 20, 20, d.x - 40, d.y - 40 ); 369 g2d.setClipping( 20, 20, d.x - 40, d.y - 40 ); 370 int x = 25, y = 20 + fm.getHeight( ); 371 g2d.drawString( "Exception:", x, y ); x += g2d.textExtent( "Exception:" ).x + 5; g2d.setForeground( dv.getSystemColor( SWT.COLOR_RED ) ); 374 g2d.drawString( sException, x, y ); 375 x = 25; 376 y += fm.getHeight( ); 377 if ( sWrappedException != null ) 378 { 379 g2d.setForeground( dv.getSystemColor( SWT.COLOR_BLACK ) ); 380 g2d.drawString( "Wrapped In:", x, y ); x += g2d.textExtent( "Wrapped In:" ).x + 5; g2d.setForeground( dv.getSystemColor( SWT.COLOR_RED ) ); 383 g2d.drawString( sWrappedException, x, y ); 384 x = 25; 385 y += fm.getHeight( ); 386 } 387 g2d.setForeground( dv.getSystemColor( SWT.COLOR_BLACK ) ); 388 y += 10; 389 g2d.drawString( "Message:", x, y ); x += g2d.textExtent( "Message:" ).x + 5; g2d.setForeground( dv.getSystemColor( SWT.COLOR_BLUE ) ); 392 g2d.drawString( sMessage, x, y ); 393 x = 25; 394 y += fm.getHeight( ); 395 g2d.setForeground( dv.getSystemColor( SWT.COLOR_BLACK ) ); 396 y += 10; 397 g2d.drawString( "Trace:", x, y ); x = 40; 399 y += fm.getHeight( ); 400 g2d.setForeground( dv.getSystemColor( SWT.COLOR_DARK_GREEN ) ); 401 for ( int i = 0; i < stea.length; i++ ) 402 { 403 g2d.drawString( stea[i].getClassName( ) + ":" + stea[i].getMethodName( ) + "(...):" + stea[i].getLineNumber( ), x, y ); 406 x = 40; 407 y += fm.getHeight( ); 408 } 409 fo.dispose( ); 410 } 411 } | Popular Tags |