1 11 12 package org.eclipse.birt.chart.examples.api.interactivity; 13 14 import java.util.HashMap ; 15 import java.util.Map ; 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 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 contextMap; 68 69 74 public static void main( String [] 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: " ); cbType = new Combo( cBottom, SWT.DROP_DOWN | SWT.READ_ONLY ); 94 cbType.add( "Highlight Series" ); cbType.add( "Show Tooltip" ); cbType.add( "Toggle Visibility" ); cbType.add( "URL Redirect" ); cbType.add( "Call Back" ); cbType.select( 0 ); 100 101 btn = new Button( cBottom, SWT.NONE ); 102 btn.setText( "Update" ); 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 117 SwtInteractivityViewer( Composite parent, int style ) 118 { 119 super( parent, style ); 120 121 contextMap = new HashMap (); 122 123 final PluginSettings ps = PluginSettings.instance( ); 124 try 125 { 126 idr = ps.getDevice( "dv.SWT" ); idr.setProperty( IDeviceRenderer.UPDATE_NOTIFIER, this ); 128 } 129 catch ( ChartException ex ) 130 { 131 ex.printStackTrace( ); 132 } 133 cm = InteractivityCharts.createHSChart( ); 134 } 135 136 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 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 220 public void widgetDefaultSelected( SelectionEvent e ) 221 { 222 } 224 225 230 public Chart getDesignTimeModel( ) 231 { 232 return cm; 233 } 234 235 240 public Chart getRunTimeModel( ) 241 { 242 return gcs.getChartModel( ); 243 } 244 245 public Object 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 266 public Object getContext( Object key ) 267 { 268 return contextMap.get( key ); 269 } 270 271 277 public Object putContext( Object key, Object value ) 278 { 279 return contextMap.put( key, value ); 280 } 281 282 287 public Object removeContext( Object key ) 288 { 289 return contextMap.remove( key ); 290 } 291 292 public void callback( Object event, Object 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 |