KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > birt > chart > examples > api > script > JavaScriptViewer


1 /***********************************************************************
2  * Copyright (c) 2004 Actuate Corporation.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * Actuate Corporation - initial API and implementation
10  ***********************************************************************/

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     /**
68      * Execute application
69      *
70      * @param args
71      */

72     public static void main( String JavaDoc[] 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 )" //$NON-NLS-1$
92
+ "\nbeforeDrawAxisTitle( Axis, Label, IChartScriptContext )" ); //$NON-NLS-1$
93

94         Label la = new Label( cBottom, SWT.NONE );
95
96         la.setText( "Choose: " );//$NON-NLS-1$
97
cbType = new Combo( cBottom, SWT.DROP_DOWN | SWT.READ_ONLY );
98         cbType.add( "Axis" );//$NON-NLS-1$
99
cbType.add( "DataPoints" );//$NON-NLS-1$
100
cbType.add( "Marker" );//$NON-NLS-1$
101
cbType.add( "Series" );//$NON-NLS-1$
102
cbType.add( "Series Title" ); //$NON-NLS-1$
103
cbType.add( "Block" );//$NON-NLS-1$
104
cbType.add( "Legend" ); //$NON-NLS-1$
105
cbType.select( 0 );
106
107         btn = new Button( cBottom, SWT.NONE );
108         btn.setText( "Update" );//$NON-NLS-1$
109
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     /**
121      * Constructor
122      */

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" );//$NON-NLS-1$
130
}
131         catch ( ChartException pex )
132         {
133             logger.log( pex );
134         }
135         cm = ScriptCharts.createChart_Axis( );
136     }
137
138     /*
139      * (non-Javadoc)
140      *
141      * @see org.eclipse.swt.events.PaintListener#paintControl(org.eclipse.swt.events.PaintEvent)
142      */

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     /*
185      * (non-Javadoc)
186      *
187      * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
188      */

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 )" //$NON-NLS-1$
199
+ "\nbeforeDrawAxisTitle( Axis, Label, IChartScriptContext )" ); //$NON-NLS-1$
200
break;
201                 case 1 :
202                     cm = ScriptCharts.createChart_DataPoints( );
203                     description.setText( "beforeDrawDataPointLabel( DataPointHint, Label, IChartScriptContext )" ); //$NON-NLS-1$
204
break;
205                 case 2 :
206                     cm = ScriptCharts.createChart_Marker( );
207                     description.setText( "beforeDrawMarkerLine( Axis, MarkerLine, IChartScriptContext )" //$NON-NLS-1$
208
+ "\nbeforeDrawMarkerRange( Axis, MarkerRange, IChartScriptContext )" ); //$NON-NLS-1$
209
break;
210                 case 3 :
211                     cm = ScriptCharts.createChart_Series( );
212                     description.setText( "beforeDrawSeries( Series, ISeriesRenderer, IChartScriptContext )" ); //$NON-NLS-1$
213
break;
214                 case 4 :
215                     cm = ScriptCharts.createChart_SeriesTitle( );
216                     description.setText( "beforeDrawSeriesTitle( Series, Label, IChartScriptContext )" ); //$NON-NLS-1$
217
break;
218                 case 5 :
219                     cm = ScriptCharts.createChart_Block( );
220                     description.setText( "beforeDrawBlock( Block, IChartScriptContext )" ); //$NON-NLS-1$
221
break;
222                 case 6 :
223                     cm = ScriptCharts.createChart_Legend( );
224                     description.setText( "beforeDrawLegendEntry( Label, IChartScriptContext )" ); //$NON-NLS-1$
225
break;
226             }
227             bNeedsGeneration = true;
228             this.redraw( );
229         }
230     }
231
232     /*
233      * (non-Javadoc)
234      *
235      * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
236      */

237     public void widgetDefaultSelected( SelectionEvent e )
238     {
239         // TODO Auto-generated method stub
240
}
241
242     private final void showException( GC g2d, Exception JavaDoc ex )
243     {
244         String JavaDoc sWrappedException = ex.getClass( ).getName( );
245         Throwable JavaDoc th = ex;
246         while ( ex.getCause( ) != null )
247         {
248             ex = (Exception JavaDoc) ex.getCause( );
249         }
250         String JavaDoc sException = ex.getClass( ).getName( );
251         if ( sWrappedException.equals( sException ) )
252         {
253             sWrappedException = null;
254         }
255
256         String JavaDoc 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>";//$NON-NLS-1$
269
}
270         StackTraceElement JavaDoc[] stea = ex.getStackTrace( );
271         Point d = this.getSize( );
272
273         Device dv = Display.getCurrent( );
274         Font fo = new Font( dv, "Courier", SWT.BOLD, 16 );//$NON-NLS-1$
275
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 );//$NON-NLS-1$
284
x += g2d.textExtent( "Exception:" ).x + 5;//$NON-NLS-1$
285
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 );//$NON-NLS-1$
293
x += g2d.textExtent( "Wrapped In:" ).x + 5;//$NON-NLS-1$
294
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 );//$NON-NLS-1$
302
x += g2d.textExtent( "Message:" ).x + 5;//$NON-NLS-1$
303
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 );//$NON-NLS-1$
310
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( ) + ":"//$NON-NLS-1$
316
+ stea[i].getMethodName( )
317                     + "(...):"//$NON-NLS-1$
318
+ stea[i].getLineNumber( ), x, y );
319             x = 40;
320             y += fm.getHeight( );
321         }
322         fo.dispose( );
323     }
324 }
325
Popular Tags