KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

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

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

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

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

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