KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > birt > chart > examples > api > interactivity > SwtInteractivityViewer


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.interactivity;
13
14 import java.util.HashMap JavaDoc;
15 import java.util.Map JavaDoc;
16
17 import org.eclipse.birt.chart.device.IDeviceRenderer;
18 import org.eclipse.birt.chart.device.ICallBackNotifier;
19 import org.eclipse.birt.chart.exception.ChartException;
20 import org.eclipse.birt.chart.factory.GeneratedChartState;
21 import org.eclipse.birt.chart.factory.Generator;
22 import org.eclipse.birt.chart.model.Chart;
23 import org.eclipse.birt.chart.model.attribute.Bounds;
24 import org.eclipse.birt.chart.model.attribute.CallBackValue;
25 import org.eclipse.birt.chart.model.attribute.impl.BoundsImpl;
26 import org.eclipse.birt.chart.util.PluginSettings;
27 import org.eclipse.swt.SWT;
28 import org.eclipse.swt.events.PaintEvent;
29 import org.eclipse.swt.events.PaintListener;
30 import org.eclipse.swt.events.SelectionEvent;
31 import org.eclipse.swt.events.SelectionListener;
32 import org.eclipse.swt.graphics.GC;
33 import org.eclipse.swt.graphics.Image;
34 import org.eclipse.swt.graphics.Rectangle;
35 import org.eclipse.swt.layout.GridData;
36 import org.eclipse.swt.layout.GridLayout;
37 import org.eclipse.swt.layout.RowLayout;
38 import org.eclipse.swt.widgets.Button;
39 import org.eclipse.swt.widgets.Combo;
40 import org.eclipse.swt.widgets.Composite;
41 import org.eclipse.swt.widgets.Display;
42 import org.eclipse.swt.widgets.Label;
43 import org.eclipse.swt.widgets.Shell;
44 import org.eclipse.swt.widgets.MessageBox;
45
46 /**
47  * The selector of charts in SWT.
48  *
49  */

50 public final class SwtInteractivityViewer extends Composite implements
51         PaintListener,
52         ICallBackNotifier,
53         SelectionListener
54 {
55     private IDeviceRenderer idr = null;
56
57     private Chart cm = null;
58
59     private static Combo cbType = null;
60
61     private static Button btn = null;
62
63     private GeneratedChartState gcs = null;
64
65     private boolean bNeedsGeneration = true;
66     
67     private Map JavaDoc contextMap;
68
69     /**
70      * main() method for constructing the layout.
71      *
72      * @param args
73      */

74     public static void main( String JavaDoc[] args )
75     {
76         Display display = Display.getDefault( );
77         Shell shell = new Shell( display );
78         shell.setSize( 600, 400 );
79         shell.setLayout( new GridLayout( ) );
80
81         SwtInteractivityViewer siv = new SwtInteractivityViewer( shell,
82                 SWT.NO_BACKGROUND );
83         siv.setLayoutData( new GridData( GridData.FILL_BOTH ) );
84         siv.addPaintListener( siv );
85
86         Composite cBottom = new Composite( shell, SWT.NONE );
87         cBottom.setLayoutData( new GridData( GridData.FILL_HORIZONTAL ) );
88         cBottom.setLayout( new RowLayout( ) );
89
90         Label la = new Label( cBottom, SWT.NONE );
91
92         la.setText( "Choose: " );//$NON-NLS-1$
93
cbType = new Combo( cBottom, SWT.DROP_DOWN | SWT.READ_ONLY );
94         cbType.add( "Highlight Series" );//$NON-NLS-1$
95
cbType.add( "Show Tooltip" );//$NON-NLS-1$
96
cbType.add( "Toggle Visibility" );//$NON-NLS-1$
97
cbType.add( "URL Redirect" );//$NON-NLS-1$
98
cbType.add( "Call Back" );//$NON-NLS-1$
99
cbType.select( 0 );
100
101         btn = new Button( cBottom, SWT.NONE );
102         btn.setText( "Update" );//$NON-NLS-1$
103
btn.addSelectionListener( siv );
104
105         shell.open( );
106         while ( !shell.isDisposed( ) )
107         {
108             if ( !display.readAndDispatch( ) )
109                 display.sleep( );
110         }
111         display.dispose( );
112     }
113
114     /**
115      * Get the connection with SWT device to render the graphics.
116      */

117     SwtInteractivityViewer( Composite parent, int style )
118     {
119         super( parent, style );
120         
121         contextMap = new HashMap JavaDoc();
122         
123         final PluginSettings ps = PluginSettings.instance( );
124         try
125         {
126             idr = ps.getDevice( "dv.SWT" );//$NON-NLS-1$
127
idr.setProperty( IDeviceRenderer.UPDATE_NOTIFIER, this );
128         }
129         catch ( ChartException ex )
130         {
131             ex.printStackTrace( );
132         }
133         cm = InteractivityCharts.createHSChart( );
134     }
135
136     /*
137      * (non-Javadoc)
138      *
139      * @see org.eclipse.swt.events.PaintListener#paintControl(org.eclipse.swt.events.PaintEvent)
140      */

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

187     public void widgetSelected( SelectionEvent e )
188     {
189         if ( e.widget.equals( btn ) )
190         {
191             int iSelection = cbType.getSelectionIndex( );
192             switch ( iSelection )
193             {
194                 case 0 :
195                     cm = InteractivityCharts.createHSChart( );
196                     break;
197                 case 1 :
198                     cm = InteractivityCharts.createSTChart( );
199                     break;
200                 case 2 :
201                     cm = InteractivityCharts.createTVChart( );
202                     break;
203                 case 3 :
204                     cm = InteractivityCharts.createURChart( );
205                     break;
206                 case 4 :
207                     cm = InteractivityCharts.createCBChart( );
208                     break;
209             }
210             bNeedsGeneration = true;
211             this.redraw( );
212         }
213     }
214
215     /*
216      * (non-Javadoc)
217      *
218      * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
219      */

220     public void widgetDefaultSelected( SelectionEvent e )
221     {
222         // TODO Auto-generated method stub
223
}
224
225     /*
226      * (non-Javadoc)
227      *
228      * @see org.eclipse.birt.chart.device.swing.IUpdateNotifier#getDesignTimeModel()
229      */

230     public Chart getDesignTimeModel( )
231     {
232         return cm;
233     }
234
235     /*
236      * (non-Javadoc)
237      *
238      * @see org.eclipse.birt.chart.device.swing.IUpdateNotifier#getRunTimeModel()
239      */

240     public Chart getRunTimeModel( )
241     {
242         return gcs.getChartModel( );
243     }
244
245     public Object JavaDoc peerInstance( )
246     {
247         return this;
248     }
249
250     public void regenerateChart( )
251     {
252         bNeedsGeneration = true;
253         redraw( );
254     }
255
256     public void repaintChart( )
257     {
258         redraw( );
259     }
260     
261     /*
262      * (non-Javadoc)
263      *
264      * @see org.eclipse.birt.chart.device.IUpdateNotifier#getContext(java.lang.Object)
265      */

266     public Object JavaDoc getContext( Object JavaDoc key )
267     {
268         return contextMap.get( key );
269     }
270
271     /*
272      * (non-Javadoc)
273      *
274      * @see org.eclipse.birt.chart.device.IUpdateNotifier#putContext(java.lang.Object,
275      * java.lang.Object)
276      */

277     public Object JavaDoc putContext( Object JavaDoc key, Object JavaDoc value )
278     {
279         return contextMap.put( key, value );
280     }
281     
282     /*
283      * (non-Javadoc)
284      *
285      * @see org.eclipse.birt.chart.device.IUpdateNotifier#removeContext(java.lang.Object)
286      */

287     public Object JavaDoc removeContext( Object JavaDoc key )
288     {
289         return contextMap.remove( key );
290     }
291     
292     public void callback( Object JavaDoc event, Object JavaDoc source, CallBackValue value )
293     {
294         MessageBox mb = new MessageBox ( this.getShell( ) );
295         mb.setText( value.getIdentifier( ) );
296         mb.open( );
297     }
298
299 }
300
Popular Tags