KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > birt > chart > examples > api > viewer > CurveFittingViewer


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.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     /**
67      * execute application
68      *
69      * @param args
70      */

71     public static void main( String JavaDoc[] 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: " );//$NON-NLS-1$
90
cbType = new Combo( cBottom, SWT.DROP_DOWN | SWT.READ_ONLY );
91         cbType.add( "Bar Chart" );//$NON-NLS-1$
92
cbType.add( "Line Chart" );//$NON-NLS-1$
93
cbType.add( "Stock Chart" );//$NON-NLS-1$
94
cbType.add( "Area Chart" );//$NON-NLS-1$
95
cbType.select( 0 );
96
97         btn = new Button( cBottom, SWT.NONE );
98         btn.setText( "Update" );//$NON-NLS-1$
99
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     /**
111      * Constructor
112      */

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" );//$NON-NLS-1$
120
}
121         catch ( ChartException pex )
122         {
123             logger.log( pex );
124         }
125         cm = PrimitiveCharts.createCFBarChart( );
126     }
127
128     /*
129      * (non-Javadoc)
130      *
131      * @see org.eclipse.swt.events.PaintListener#paintControl(org.eclipse.swt.events.PaintEvent)
132      */

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     /*
175      * (non-Javadoc)
176      *
177      * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
178      */

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     /*
205      * (non-Javadoc)
206      *
207      * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
208      */

209     public void widgetDefaultSelected( SelectionEvent e )
210     {
211         // TODO Auto-generated method stub
212

213     }
214
215     private final void showException( GC g2d, Exception JavaDoc ex )
216     {
217         String JavaDoc sWrappedException = ex.getClass( ).getName( );
218         Throwable JavaDoc th = ex;
219         while ( ex.getCause( ) != null )
220         {
221             ex = (Exception JavaDoc) ex.getCause( );
222         }
223         String JavaDoc sException = ex.getClass( ).getName( );
224         if ( sWrappedException.equals( sException ) )
225         {
226             sWrappedException = null;
227         }
228
229         String JavaDoc 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>";//$NON-NLS-1$
242
}
243         StackTraceElement JavaDoc[] stea = ex.getStackTrace( );
244         Point d = this.getSize( );
245
246         Device dv = Display.getCurrent( );
247         Font fo = new Font( dv, "Courier", SWT.BOLD, 16 );//$NON-NLS-1$
248
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 );//$NON-NLS-1$
257
x += g2d.textExtent( "Exception:" ).x + 5;//$NON-NLS-1$
258
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 );//$NON-NLS-1$
266
x += g2d.textExtent( "Wrapped In:" ).x + 5;//$NON-NLS-1$
267
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 );//$NON-NLS-1$
275
x += g2d.textExtent( "Message:" ).x + 5;//$NON-NLS-1$
276
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 );//$NON-NLS-1$
283
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( ) + ":"//$NON-NLS-1$
289
+ stea[i].getMethodName( )
290                     + "(...):"//$NON-NLS-1$
291
+ stea[i].getLineNumber( ), x, y );
292             x = 40;
293             y += fm.getHeight( );
294         }
295         fo.dispose( );
296     }
297 }
298
Popular Tags