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 JavaViewer 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 Display display = Display.getDefault( ); 75 Shell shell = new Shell( display ); 76 shell.setSize( 600, 400 ); 77 shell.setLayout( new GridLayout( ) ); 78 79 JavaViewer jViewer = new JavaViewer( shell, SWT.NO_BACKGROUND ); 80 jViewer.setLayoutData( new GridData( GridData.FILL_BOTH ) ); 81 jViewer.addPaintListener( jViewer ); 82 83 Composite cBottom = new Composite( shell, SWT.NONE ); 84 cBottom.setLayoutData( new GridData( GridData.FILL_HORIZONTAL ) ); 85 cBottom.setLayout( new RowLayout( ) ); 86 87 description = new Label( shell, SWT.NONE ); 88 description.setLayoutData( new GridData( GridData.FILL_HORIZONTAL ) ); 89 description.setText( "beforeDrawAxisLabel( Axis, Label, IChartScriptContext )" + "\nbeforeDrawAxisTitle( Axis, Label, IChartScriptContext )" ); 92 Label la = new Label( cBottom, SWT.NONE ); 93 94 la.setText( "Choose: " ); cbType = new Combo( cBottom, SWT.DROP_DOWN | SWT.READ_ONLY ); 96 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 ); 104 105 btn = new Button( cBottom, SWT.NONE ); 106 btn.setText( "Update" ); btn.addSelectionListener( jViewer ); 108 109 shell.open( ); 110 while ( !shell.isDisposed( ) ) 111 { 112 if ( !display.readAndDispatch( ) ) 113 display.sleep( ); 114 } 115 display.dispose( ); 116 117 } 118 119 122 JavaViewer( Composite parent, int style ) 123 { 124 super( parent, style ); 125 final PluginSettings ps = PluginSettings.instance( ); 126 try 127 { 128 idr = ps.getDevice( "dv.SWT" ); } 130 catch ( ChartException pex ) 131 { 132 logger.log( pex ); 133 } 134 cm = ScriptCharts.createJChart_Axis( ); 135 } 136 137 142 public final void paintControl( PaintEvent e ) 143 { 144 Rectangle d = this.getClientArea( ); 145 Image imgChart = new Image( this.getDisplay( ), d ); 146 GC gcImage = new GC( imgChart ); 147 idr.setProperty( IDeviceRenderer.GRAPHICS_CONTEXT, gcImage ); 148 149 Bounds bo = BoundsImpl.create( 0, 0, d.width, d.height ); 150 bo.scale( 72d / idr.getDisplayServer( ).getDpiResolution( ) ); 151 152 Generator gr = Generator.instance( ); 153 if ( bNeedsGeneration ) 154 { 155 bNeedsGeneration = false; 156 try 157 { 158 gcs = gr.build( idr.getDisplayServer( ), 159 cm, 160 bo, 161 null, 162 null, 163 null ); 164 } 165 catch ( ChartException ce ) 166 { 167 ce.printStackTrace( ); 168 } 169 } 170 171 try 172 { 173 gr.render( idr, gcs ); 174 GC gc = e.gc; 175 gc.drawImage( imgChart, d.x, d.y ); 176 } 177 catch ( ChartException gex ) 178 { 179 showException( e.gc, gex ); 180 } 181 } 182 183 188 public void widgetSelected( SelectionEvent e ) 189 { 190 if ( e.widget.equals( btn ) ) 191 { 192 int iSelection = cbType.getSelectionIndex( ); 193 switch ( iSelection ) 194 { 195 case 0 : 196 cm = ScriptCharts.createJChart_Axis( ); 197 description.setText( "beforeDrawAxisLabel( Axis, Label, IChartScriptContext )" + "\nbeforeDrawAxisTitle( Axis, Label, IChartScriptContext )" ); break; 200 case 1 : 201 cm = ScriptCharts.createJChart_DataPoints( ); 202 description.setText( "beforeDrawDataPointLabel( DataPointHint, Label, IChartScriptContext )" ); break; 204 case 2 : 205 cm = ScriptCharts.createJChart_Marker( ); 206 description.setText( "beforeDrawMarkerLine( Axis, MarkerLine, IChartScriptContext )" + "\nbeforeDrawMarkerRange( Axis, MarkerRange, IChartScriptContext )" ); break; 209 case 3 : 210 cm = ScriptCharts.createJChart_Series( ); 211 description.setText( "beforeDrawSeries( Series, ISeriesRenderer, IChartScriptContext )" ); break; 213 case 4 : 214 cm = ScriptCharts.createJChart_SeriesTitle( ); 215 description.setText( "beforeDrawSeriesTitle( Series, Label, IChartScriptContext )" ); break; 217 case 5 : 218 cm = ScriptCharts.createJChart_Block( ); 219 description.setText( "beforeDrawBlock( Block, IChartScriptContext )" ); break; 221 case 6 : 222 cm = ScriptCharts.createJChart_Legend( ); 223 description.setText( "beforeDrawLegendEntry( Label, IChartScriptContext )" ); break; 225 } 226 bNeedsGeneration = true; 227 this.redraw( ); 228 } 229 } 230 231 236 public void widgetDefaultSelected( SelectionEvent e ) 237 { 238 } 240 241 private final void showException( GC g2d, Exception ex ) 242 { 243 String sWrappedException = ex.getClass( ).getName( ); 244 Throwable th = ex; 245 while ( ex.getCause( ) != null ) 246 { 247 ex = (Exception ) ex.getCause( ); 248 } 249 String sException = ex.getClass( ).getName( ); 250 if ( sWrappedException.equals( sException ) ) 251 { 252 sWrappedException = null; 253 } 254 255 String sMessage = null; 256 if ( th instanceof BirtException ) 257 { 258 sMessage = ( (BirtException) th ).getLocalizedMessage( ); 259 } 260 else 261 { 262 sMessage = ex.getMessage( ); 263 } 264 265 if ( sMessage == null ) 266 { 267 sMessage = "<null>"; } 269 StackTraceElement [] stea = ex.getStackTrace( ); 270 Point d = this.getSize( ); 271 272 Device dv = Display.getCurrent( ); 273 Font fo = new Font( dv, "Courier", SWT.BOLD, 16 ); g2d.setFont( fo ); 275 FontMetrics fm = g2d.getFontMetrics( ); 276 g2d.setBackground( dv.getSystemColor( SWT.COLOR_WHITE ) ); 277 g2d.fillRectangle( 20, 20, d.x - 40, d.y - 40 ); 278 g2d.setForeground( dv.getSystemColor( SWT.COLOR_BLACK ) ); 279 g2d.drawRectangle( 20, 20, d.x - 40, d.y - 40 ); 280 g2d.setClipping( 20, 20, d.x - 40, d.y - 40 ); 281 int x = 25, y = 20 + fm.getHeight( ); 282 g2d.drawString( "Exception:", x, y ); x += g2d.textExtent( "Exception:" ).x + 5; g2d.setForeground( dv.getSystemColor( SWT.COLOR_RED ) ); 285 g2d.drawString( sException, x, y ); 286 x = 25; 287 y += fm.getHeight( ); 288 if ( sWrappedException != null ) 289 { 290 g2d.setForeground( dv.getSystemColor( SWT.COLOR_BLACK ) ); 291 g2d.drawString( "Wrapped In:", x, y ); x += g2d.textExtent( "Wrapped In:" ).x + 5; g2d.setForeground( dv.getSystemColor( SWT.COLOR_RED ) ); 294 g2d.drawString( sWrappedException, x, y ); 295 x = 25; 296 y += fm.getHeight( ); 297 } 298 g2d.setForeground( dv.getSystemColor( SWT.COLOR_BLACK ) ); 299 y += 10; 300 g2d.drawString( "Message:", x, y ); x += g2d.textExtent( "Message:" ).x + 5; g2d.setForeground( dv.getSystemColor( SWT.COLOR_BLUE ) ); 303 g2d.drawString( sMessage, x, y ); 304 x = 25; 305 y += fm.getHeight( ); 306 g2d.setForeground( dv.getSystemColor( SWT.COLOR_BLACK ) ); 307 y += 10; 308 g2d.drawString( "Trace:", x, y ); x = 40; 310 y += fm.getHeight( ); 311 g2d.setForeground( dv.getSystemColor( SWT.COLOR_DARK_GREEN ) ); 312 for ( int i = 0; i < stea.length; i++ ) 313 { 314 g2d.drawString( stea[i].getClassName( ) + ":" + stea[i].getMethodName( ) 316 + "(...):" + stea[i].getLineNumber( ), x, y ); 318 x = 40; 319 y += fm.getHeight( ); 320 } 321 fo.dispose( ); 322 } 323 } 324 | Popular Tags |