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 CurveFittingViewer 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 CurveFittingViewer cfViewer = new CurveFittingViewer( shell, 79 SWT.NO_BACKGROUND ); 80 cfViewer.setLayoutData( new GridData( GridData.FILL_BOTH ) ); 81 cfViewer.addPaintListener( cfViewer ); 82 83 Composite cBottom = new Composite( shell, SWT.NONE ); 84 cBottom.setLayoutData( new GridData( GridData.FILL_HORIZONTAL ) ); 85 cBottom.setLayout( new RowLayout( ) ); 86 87 Label la = new Label( cBottom, SWT.NONE ); 88 89 la.setText( "Choose: " ); cbType = new Combo( cBottom, SWT.DROP_DOWN | SWT.READ_ONLY ); 91 cbType.add( "Bar Chart" ); cbType.add( "Line Chart" ); cbType.add( "Stock Chart" ); cbType.add( "Area Chart" ); cbType.select( 0 ); 96 97 btn = new Button( cBottom, SWT.NONE ); 98 btn.setText( "Update" ); btn.addSelectionListener( cfViewer ); 100 101 shell.open( ); 102 while ( !shell.isDisposed( ) ) 103 { 104 if ( !display.readAndDispatch( ) ) 105 display.sleep( ); 106 } 107 display.dispose( ); 108 } 109 110 113 CurveFittingViewer( Composite parent, int style ) 114 { 115 super( parent, style ); 116 final PluginSettings ps = PluginSettings.instance( ); 117 try 118 { 119 idr = ps.getDevice( "dv.SWT" ); } 121 catch ( ChartException pex ) 122 { 123 logger.log( pex ); 124 } 125 cm = PrimitiveCharts.createCFBarChart( ); 126 } 127 128 133 public final void paintControl( PaintEvent e ) 134 { 135 Rectangle d = this.getClientArea( ); 136 Image imgChart = new Image( this.getDisplay( ), d ); 137 GC gcImage = new GC( imgChart ); 138 idr.setProperty( IDeviceRenderer.GRAPHICS_CONTEXT, gcImage ); 139 140 Bounds bo = BoundsImpl.create( 0, 0, d.width, d.height ); 141 bo.scale( 72d / idr.getDisplayServer( ).getDpiResolution( ) ); 142 143 Generator gr = Generator.instance( ); 144 if ( bNeedsGeneration ) 145 { 146 bNeedsGeneration = false; 147 try 148 { 149 gcs = gr.build( idr.getDisplayServer( ), 150 cm, 151 bo, 152 null, 153 null, 154 null ); 155 } 156 catch ( ChartException ce ) 157 { 158 ce.printStackTrace( ); 159 } 160 } 161 162 try 163 { 164 gr.render( idr, gcs ); 165 GC gc = e.gc; 166 gc.drawImage( imgChart, d.x, d.y ); 167 } 168 catch ( ChartException gex ) 169 { 170 showException( e.gc, gex ); 171 } 172 } 173 174 179 public void widgetSelected( SelectionEvent e ) 180 { 181 if ( e.widget.equals( btn ) ) 182 { 183 int iSelection = cbType.getSelectionIndex( ); 184 switch ( iSelection ) 185 { 186 case 0 : 187 cm = PrimitiveCharts.createCFBarChart( ); 188 break; 189 case 1 : 190 cm = PrimitiveCharts.createCFLineChart( ); 191 break; 192 case 2 : 193 cm = PrimitiveCharts.createCFStockChart( ); 194 break; 195 case 3 : 196 cm = PrimitiveCharts.createCFAreaChart( ); 197 break; 198 } 199 bNeedsGeneration = true; 200 this.redraw( ); 201 } 202 } 203 204 209 public void widgetDefaultSelected( SelectionEvent e ) 210 { 211 213 } 214 215 private final void showException( GC g2d, Exception ex ) 216 { 217 String sWrappedException = ex.getClass( ).getName( ); 218 Throwable th = ex; 219 while ( ex.getCause( ) != null ) 220 { 221 ex = (Exception ) ex.getCause( ); 222 } 223 String sException = ex.getClass( ).getName( ); 224 if ( sWrappedException.equals( sException ) ) 225 { 226 sWrappedException = null; 227 } 228 229 String sMessage = null; 230 if ( th instanceof BirtException ) 231 { 232 sMessage = ( (BirtException) th ).getLocalizedMessage( ); 233 } 234 else 235 { 236 sMessage = ex.getMessage( ); 237 } 238 239 if ( sMessage == null ) 240 { 241 sMessage = "<null>"; } 243 StackTraceElement [] stea = ex.getStackTrace( ); 244 Point d = this.getSize( ); 245 246 Device dv = Display.getCurrent( ); 247 Font fo = new Font( dv, "Courier", SWT.BOLD, 16 ); g2d.setFont( fo ); 249 FontMetrics fm = g2d.getFontMetrics( ); 250 g2d.setBackground( dv.getSystemColor( SWT.COLOR_WHITE ) ); 251 g2d.fillRectangle( 20, 20, d.x - 40, d.y - 40 ); 252 g2d.setForeground( dv.getSystemColor( SWT.COLOR_BLACK ) ); 253 g2d.drawRectangle( 20, 20, d.x - 40, d.y - 40 ); 254 g2d.setClipping( 20, 20, d.x - 40, d.y - 40 ); 255 int x = 25, y = 20 + fm.getHeight( ); 256 g2d.drawString( "Exception:", x, y ); x += g2d.textExtent( "Exception:" ).x + 5; g2d.setForeground( dv.getSystemColor( SWT.COLOR_RED ) ); 259 g2d.drawString( sException, x, y ); 260 x = 25; 261 y += fm.getHeight( ); 262 if ( sWrappedException != null ) 263 { 264 g2d.setForeground( dv.getSystemColor( SWT.COLOR_BLACK ) ); 265 g2d.drawString( "Wrapped In:", x, y ); x += g2d.textExtent( "Wrapped In:" ).x + 5; g2d.setForeground( dv.getSystemColor( SWT.COLOR_RED ) ); 268 g2d.drawString( sWrappedException, x, y ); 269 x = 25; 270 y += fm.getHeight( ); 271 } 272 g2d.setForeground( dv.getSystemColor( SWT.COLOR_BLACK ) ); 273 y += 10; 274 g2d.drawString( "Message:", x, y ); x += g2d.textExtent( "Message:" ).x + 5; g2d.setForeground( dv.getSystemColor( SWT.COLOR_BLUE ) ); 277 g2d.drawString( sMessage, x, y ); 278 x = 25; 279 y += fm.getHeight( ); 280 g2d.setForeground( dv.getSystemColor( SWT.COLOR_BLACK ) ); 281 y += 10; 282 g2d.drawString( "Trace:", x, y ); x = 40; 284 y += fm.getHeight( ); 285 g2d.setForeground( dv.getSystemColor( SWT.COLOR_DARK_GREEN ) ); 286 for ( int i = 0; i < stea.length; i++ ) 287 { 288 g2d.drawString( stea[i].getClassName( ) + ":" + stea[i].getMethodName( ) 290 + "(...):" + stea[i].getLineNumber( ), x, y ); 292 x = 40; 293 y += fm.getHeight( ); 294 } 295 fo.dispose( ); 296 } 297 } 298 | Popular Tags |