KickJava   Java API By Example, From Geeks To Geeks.

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


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 DialChartViewer 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
74         Display display = Display.getDefault( );
75         Shell shell = new Shell( display );
76         shell.setSize( 600, 400 );
77         shell.setLayout( new GridLayout( ) );
78
79         DialChartViewer dcViewer = new DialChartViewer( shell,
80                 SWT.NO_BACKGROUND );
81         dcViewer.setLayoutData( new GridData( GridData.FILL_BOTH ) );
82         dcViewer.addPaintListener( dcViewer );
83
84         Composite cBottom = new Composite( shell, SWT.NONE );
85         cBottom.setLayoutData( new GridData( GridData.FILL_HORIZONTAL ) );
86         cBottom.setLayout( new RowLayout( ) );
87
88         Label la = new Label( cBottom, SWT.NONE );
89
90         la.setText( "Choose: " );//$NON-NLS-1$
91
cbType = new Combo( cBottom, SWT.DROP_DOWN | SWT.READ_ONLY );
92         cbType.add( "Single Dial, Multi Regions" );//$NON-NLS-1$
93
cbType.add( "Multi Dials, Multi Regions" );//$NON-NLS-1$
94
cbType.add( "Single Dial, Single Region" );//$NON-NLS-1$
95
cbType.add( "Multi Dials, Single Region" );//$NON-NLS-1$
96
cbType.select( 0 );
97
98         btn = new Button( cBottom, SWT.NONE );
99         btn.setText( "Update" );//$NON-NLS-1$
100
btn.addSelectionListener( dcViewer );
101
102         shell.open( );
103         while ( !shell.isDisposed( ) )
104         {
105             if ( !display.readAndDispatch( ) )
106                 display.sleep( );
107         }
108         display.dispose( );
109     }
110
111     /**
112      * Constructor
113      */

114     DialChartViewer( Composite parent, int style )
115     {
116         super( parent, style );
117         final PluginSettings ps = PluginSettings.instance( );
118         try
119         {
120             idr = ps.getDevice( "dv.SWT" );//$NON-NLS-1$
121
}
122         catch ( ChartException pex )
123         {
124             logger.log( pex );
125         }
126         cm = PrimitiveCharts.createSDialMRegionChart( );
127     }
128
129     /*
130      * (non-Javadoc)
131      *
132      * @see org.eclipse.swt.events.PaintListener#paintControl(org.eclipse.swt.events.PaintEvent)
133      */

134     public final void paintControl( PaintEvent e )
135     {
136         Rectangle d = this.getClientArea( );
137         Image imgChart = new Image( this.getDisplay( ), d );
138         GC gcImage = new GC( imgChart );
139         idr.setProperty( IDeviceRenderer.GRAPHICS_CONTEXT, gcImage );
140
141         Bounds bo = BoundsImpl.create( 0, 0, d.width, d.height );
142         bo.scale( 72d / idr.getDisplayServer( ).getDpiResolution( ) );
143
144         Generator gr = Generator.instance( );
145         if ( bNeedsGeneration )
146         {
147             bNeedsGeneration = false;
148             try
149             {
150                 gcs = gr.build( idr.getDisplayServer( ),
151                         cm,
152                         bo,
153                         null,
154                         null,
155                         null );
156             }
157             catch ( ChartException ce )
158             {
159                 ce.printStackTrace( );
160             }
161         }
162
163         try
164         {
165             gr.render( idr, gcs );
166             GC gc = e.gc;
167             gc.drawImage( imgChart, d.x, d.y );
168         }
169         catch ( ChartException gex )
170         {
171             showException( e.gc, gex );
172         }
173     }
174
175     /*
176      * (non-Javadoc)
177      *
178      * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
179      */

180     public void widgetSelected( SelectionEvent e )
181     {
182         if ( e.widget.equals( btn ) )
183         {
184             int iSelection = cbType.getSelectionIndex( );
185             switch ( iSelection )
186             {
187                 case 0 :
188                     cm = PrimitiveCharts.createSDialMRegionChart( );
189                     break;
190                 case 1 :
191                     cm = PrimitiveCharts.createMDialMRegionChart( );
192                     break;
193                 case 2 :
194                     cm = PrimitiveCharts.createSDialSRegionChart( );
195                     break;
196                 case 3 :
197                     cm = PrimitiveCharts.createMDialSRegionChart( );
198                     break;
199             }
200             bNeedsGeneration = true;
201             this.redraw( );
202         }
203     }
204
205     /*
206      * (non-Javadoc)
207      *
208      * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
209      */

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

214     }
215
216     private final void showException( GC g2d, Exception JavaDoc ex )
217     {
218         String JavaDoc sWrappedException = ex.getClass( ).getName( );
219         Throwable JavaDoc th = ex;
220         while ( ex.getCause( ) != null )
221         {
222             ex = (Exception JavaDoc) ex.getCause( );
223         }
224         String JavaDoc sException = ex.getClass( ).getName( );
225         if ( sWrappedException.equals( sException ) )
226         {
227             sWrappedException = null;
228         }
229
230         String JavaDoc sMessage = null;
231         if ( th instanceof BirtException )
232         {
233             sMessage = ( (BirtException) th ).getLocalizedMessage( );
234         }
235         else
236         {
237             sMessage = ex.getMessage( );
238         }
239
240         if ( sMessage == null )
241         {
242             sMessage = "<null>";//$NON-NLS-1$
243
}
244         StackTraceElement JavaDoc[] stea = ex.getStackTrace( );
245         Point d = this.getSize( );
246
247         Device dv = Display.getCurrent( );
248         Font fo = new Font( dv, "Courier", SWT.BOLD, 16 );//$NON-NLS-1$
249
g2d.setFont( fo );
250         FontMetrics fm = g2d.getFontMetrics( );
251         g2d.setBackground( dv.getSystemColor( SWT.COLOR_WHITE ) );
252         g2d.fillRectangle( 20, 20, d.x - 40, d.y - 40 );
253         g2d.setForeground( dv.getSystemColor( SWT.COLOR_BLACK ) );
254         g2d.drawRectangle( 20, 20, d.x - 40, d.y - 40 );
255         g2d.setClipping( 20, 20, d.x - 40, d.y - 40 );
256         int x = 25, y = 20 + fm.getHeight( );
257         g2d.drawString( "Exception:", x, y );//$NON-NLS-1$
258
x += g2d.textExtent( "Exception:" ).x + 5;//$NON-NLS-1$
259
g2d.setForeground( dv.getSystemColor( SWT.COLOR_RED ) );
260         g2d.drawString( sException, x, y );
261         x = 25;
262         y += fm.getHeight( );
263         if ( sWrappedException != null )
264         {
265             g2d.setForeground( dv.getSystemColor( SWT.COLOR_BLACK ) );
266             g2d.drawString( "Wrapped In:", x, y );//$NON-NLS-1$
267
x += g2d.textExtent( "Wrapped In:" ).x + 5;//$NON-NLS-1$
268
g2d.setForeground( dv.getSystemColor( SWT.COLOR_RED ) );
269             g2d.drawString( sWrappedException, x, y );
270             x = 25;
271             y += fm.getHeight( );
272         }
273         g2d.setForeground( dv.getSystemColor( SWT.COLOR_BLACK ) );
274         y += 10;
275         g2d.drawString( "Message:", x, y );//$NON-NLS-1$
276
x += g2d.textExtent( "Message:" ).x + 5;//$NON-NLS-1$
277
g2d.setForeground( dv.getSystemColor( SWT.COLOR_BLUE ) );
278         g2d.drawString( sMessage, x, y );
279         x = 25;
280         y += fm.getHeight( );
281         g2d.setForeground( dv.getSystemColor( SWT.COLOR_BLACK ) );
282         y += 10;
283         g2d.drawString( "Trace:", x, y );//$NON-NLS-1$
284
x = 40;
285         y += fm.getHeight( );
286         g2d.setForeground( dv.getSystemColor( SWT.COLOR_DARK_GREEN ) );
287         for ( int i = 0; i < stea.length; i++ )
288         {
289             g2d.drawString( stea[i].getClassName( ) + ":"//$NON-NLS-1$
290
+ stea[i].getMethodName( )
291                     + "(...):"//$NON-NLS-1$
292
+ stea[i].getLineNumber( ), x, y );
293             x = 40;
294             y += fm.getHeight( );
295         }
296         fo.dispose( );
297     }
298 }
299
Popular Tags