1 11 12 package org.eclipse.birt.chart.examples.api.script; 13 14 import org.eclipse.birt.chart.device.IDeviceRenderer; 15 import org.eclipse.birt.chart.exception.ChartException; 16 import org.eclipse.birt.chart.factory.GeneratedChartState; 17 import org.eclipse.birt.chart.factory.Generator; 18 import org.eclipse.birt.chart.log.ILogger; 19 import org.eclipse.birt.chart.log.Logger; 20 import org.eclipse.birt.chart.model.Chart; 21 import org.eclipse.birt.chart.model.attribute.Bounds; 22 import org.eclipse.birt.chart.model.attribute.impl.BoundsImpl; 23 import org.eclipse.birt.chart.util.PluginSettings; 24 import org.eclipse.birt.core.exception.BirtException; 25 import org.eclipse.swt.SWT; 26 import org.eclipse.swt.events.PaintEvent; 27 import org.eclipse.swt.events.PaintListener; 28 import org.eclipse.swt.events.SelectionEvent; 29 import org.eclipse.swt.events.SelectionListener; 30 import org.eclipse.swt.graphics.Device; 31 import org.eclipse.swt.graphics.Font; 32 import org.eclipse.swt.graphics.FontMetrics; 33 import org.eclipse.swt.graphics.GC; 34 import org.eclipse.swt.graphics.Image; 35 import org.eclipse.swt.graphics.Point; 36 import org.eclipse.swt.graphics.Rectangle; 37 import org.eclipse.swt.layout.GridData; 38 import org.eclipse.swt.layout.GridLayout; 39 import org.eclipse.swt.layout.RowLayout; 40 import org.eclipse.swt.widgets.Button; 41 import org.eclipse.swt.widgets.Combo; 42 import org.eclipse.swt.widgets.Composite; 43 import org.eclipse.swt.widgets.Display; 44 import org.eclipse.swt.widgets.Label; 45 import org.eclipse.swt.widgets.Shell; 46 47 public class JavaScriptViewer extends Composite implements 48 PaintListener, 49 SelectionListener 50 { 51 52 private IDeviceRenderer idr = null; 53 54 private Chart cm = null; 55 56 private transient static Label description = null; 57 58 private static Combo cbType = null; 59 60 private static Button btn = null; 61 62 private GeneratedChartState gcs = null; 63 64 private boolean bNeedsGeneration = true; 65 66 private static ILogger logger = Logger.getLogger( JavaScriptViewer.class.getName( ) ); 67 72 public static void main( String [] args ) 73 { 74 75 Display display = Display.getDefault( ); 76 Shell shell = new Shell( display ); 77 shell.setSize( 600, 400 ); 78 shell.setLayout( new GridLayout( ) ); 79 80 JavaScriptViewer jsViewer = new JavaScriptViewer( shell, 81 SWT.NO_BACKGROUND ); 82 jsViewer.setLayoutData( new GridData( GridData.FILL_BOTH ) ); 83 jsViewer.addPaintListener( jsViewer ); 84 85 Composite cBottom = new Composite( shell, SWT.NONE ); 86 cBottom.setLayoutData( new GridData( GridData.FILL_HORIZONTAL ) ); 87 cBottom.setLayout( new RowLayout( ) ); 88 89 description = new Label( shell, SWT.NONE ); 90 description.setLayoutData( new GridData( GridData.FILL_HORIZONTAL ) ); 91 description.setText( "beforeDrawAxisLabel( Axis, Label, IChartScriptContext )" + "\nbeforeDrawAxisTitle( Axis, Label, IChartScriptContext )" ); 94 Label la = new Label( cBottom, SWT.NONE ); 95 96 la.setText( "Choose: " ); cbType = new Combo( cBottom, SWT.DROP_DOWN | SWT.READ_ONLY ); 98 cbType.add( "Axis" ); cbType.add( "DataPoints" ); cbType.add( "Marker" ); cbType.add( "Series" ); cbType.add( "Series Title" ); cbType.add( "Block" ); cbType.add( "Legend" ); cbType.select( 0 ); 106 107 btn = new Button( cBottom, SWT.NONE ); 108 btn.setText( "Update" ); btn.addSelectionListener( jsViewer ); 110 111 shell.open( ); 112 while ( !shell.isDisposed( ) ) 113 { 114 if ( !display.readAndDispatch( ) ) 115 display.sleep( ); 116 } 117 display.dispose( ); 118 } 119 120 123 JavaScriptViewer( Composite parent, int style ) 124 { 125 super( parent, style ); 126 final PluginSettings ps = PluginSettings.instance( ); 127 try 128 { 129 idr = ps.getDevice( "dv.SWT" ); } 131 catch ( ChartException pex ) 132 { 133 logger.log( pex ); 134 } 135 cm = ScriptCharts.createChart_Axis( ); 136 } 137 138 143 public final void paintControl( PaintEvent e ) 144 { 145 Rectangle d = this.getClientArea( ); 146 Image imgChart = new Image( this.getDisplay( ), d ); 147 GC gcImage = new GC( imgChart ); 148 idr.setProperty( IDeviceRenderer.GRAPHICS_CONTEXT, gcImage ); 149 150 Bounds bo = BoundsImpl.create( 0, 0, d.width, d.height ); 151 bo.scale( 72d / idr.getDisplayServer( ).getDpiResolution( ) ); 152 153 Generator gr = Generator.instance( ); 154 if ( bNeedsGeneration ) 155 { 156 bNeedsGeneration = false; 157 try 158 { 159 gcs = gr.build( idr.getDisplayServer( ), 160 cm, 161 bo, 162 null, 163 null, 164 null ); 165 } 166 catch ( ChartException ce ) 167 { 168 ce.printStackTrace( ); 169 } 170 } 171 172 try 173 { 174 gr.render( idr, gcs ); 175 GC gc = e.gc; 176 gc.drawImage( imgChart, d.x, d.y ); 177 } 178 catch ( ChartException gex ) 179 { 180 showException( e.gc, gex ); 181 } 182 } 183 184 189 public void widgetSelected( SelectionEvent e ) 190 { 191 if ( e.widget.equals( btn ) ) 192 { 193 int iSelection = cbType.getSelectionIndex( ); 194 switch ( iSelection ) 195 { 196 case 0 : 197 cm = ScriptCharts.createChart_Axis( ); 198 description.setText( "beforeDrawAxisLabel( Axis, Label, IChartScriptContext )" + "\nbeforeDrawAxisTitle( Axis, Label, IChartScriptContext )" ); break; 201 case 1 : 202 cm = ScriptCharts.createChart_DataPoints( ); 203 description.setText( "beforeDrawDataPointLabel( DataPointHint, Label, IChartScriptContext )" ); break; 205 case 2 : 206 cm = ScriptCharts.createChart_Marker( ); 207 description.setText( "beforeDrawMarkerLine( Axis, MarkerLine, IChartScriptContext )" + "\nbeforeDrawMarkerRange( Axis, MarkerRange, IChartScriptContext )" ); break; 210 case 3 : 211 cm = ScriptCharts.createChart_Series( ); 212 description.setText( "beforeDrawSeries( Series, ISeriesRenderer, IChartScriptContext )" ); break; 214 case 4 : 215 cm = ScriptCharts.createChart_SeriesTitle( ); 216 description.setText( "beforeDrawSeriesTitle( Series, Label, IChartScriptContext )" ); break; 218 case 5 : 219 cm = ScriptCharts.createChart_Block( ); 220 description.setText( "beforeDrawBlock( Block, IChartScriptContext )" ); break; 222 case 6 : 223 cm = ScriptCharts.createChart_Legend( ); 224 description.setText( "beforeDrawLegendEntry( Label, IChartScriptContext )" ); break; 226 } 227 bNeedsGeneration = true; 228 this.redraw( ); 229 } 230 } 231 232 237 public void widgetDefaultSelected( SelectionEvent e ) 238 { 239 } 241 242 private final void showException( GC g2d, Exception ex ) 243 { 244 String sWrappedException = ex.getClass( ).getName( ); 245 Throwable th = ex; 246 while ( ex.getCause( ) != null ) 247 { 248 ex = (Exception ) ex.getCause( ); 249 } 250 String sException = ex.getClass( ).getName( ); 251 if ( sWrappedException.equals( sException ) ) 252 { 253 sWrappedException = null; 254 } 255 256 String sMessage = null; 257 if ( th instanceof BirtException ) 258 { 259 sMessage = ( (BirtException) th ).getLocalizedMessage( ); 260 } 261 else 262 { 263 sMessage = ex.getMessage( ); 264 } 265 266 if ( sMessage == null ) 267 { 268 sMessage = "<null>"; } 270 StackTraceElement [] stea = ex.getStackTrace( ); 271 Point d = this.getSize( ); 272 273 Device dv = Display.getCurrent( ); 274 Font fo = new Font( dv, "Courier", SWT.BOLD, 16 ); g2d.setFont( fo ); 276 FontMetrics fm = g2d.getFontMetrics( ); 277 g2d.setBackground( dv.getSystemColor( SWT.COLOR_WHITE ) ); 278 g2d.fillRectangle( 20, 20, d.x - 40, d.y - 40 ); 279 g2d.setForeground( dv.getSystemColor( SWT.COLOR_BLACK ) ); 280 g2d.drawRectangle( 20, 20, d.x - 40, d.y - 40 ); 281 g2d.setClipping( 20, 20, d.x - 40, d.y - 40 ); 282 int x = 25, y = 20 + fm.getHeight( ); 283 g2d.drawString( "Exception:", x, y ); x += g2d.textExtent( "Exception:" ).x + 5; g2d.setForeground( dv.getSystemColor( SWT.COLOR_RED ) ); 286 g2d.drawString( sException, x, y ); 287 x = 25; 288 y += fm.getHeight( ); 289 if ( sWrappedException != null ) 290 { 291 g2d.setForeground( dv.getSystemColor( SWT.COLOR_BLACK ) ); 292 g2d.drawString( "Wrapped In:", x, y ); x += g2d.textExtent( "Wrapped In:" ).x + 5; g2d.setForeground( dv.getSystemColor( SWT.COLOR_RED ) ); 295 g2d.drawString( sWrappedException, x, y ); 296 x = 25; 297 y += fm.getHeight( ); 298 } 299 g2d.setForeground( dv.getSystemColor( SWT.COLOR_BLACK ) ); 300 y += 10; 301 g2d.drawString( "Message:", x, y ); x += g2d.textExtent( "Message:" ).x + 5; g2d.setForeground( dv.getSystemColor( SWT.COLOR_BLUE ) ); 304 g2d.drawString( sMessage, x, y ); 305 x = 25; 306 y += fm.getHeight( ); 307 g2d.setForeground( dv.getSystemColor( SWT.COLOR_BLACK ) ); 308 y += 10; 309 g2d.drawString( "Trace:", x, y ); x = 40; 311 y += fm.getHeight( ); 312 g2d.setForeground( dv.getSystemColor( SWT.COLOR_DARK_GREEN ) ); 313 for ( int i = 0; i < stea.length; i++ ) 314 { 315 g2d.drawString( stea[i].getClassName( ) + ":" + stea[i].getMethodName( ) 317 + "(...):" + stea[i].getLineNumber( ), x, y ); 319 x = 40; 320 y += fm.getHeight( ); 321 } 322 fo.dispose( ); 323 } 324 } 325 | Popular Tags |