KickJava   Java API By Example, From Geeks To Geeks.

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


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.awt.BorderLayout JavaDoc;
15 import java.awt.Color JavaDoc;
16 import java.awt.Container JavaDoc;
17 import java.awt.Dimension JavaDoc;
18 import java.awt.FlowLayout JavaDoc;
19 import java.awt.Font JavaDoc;
20 import java.awt.FontMetrics JavaDoc;
21 import java.awt.Graphics JavaDoc;
22 import java.awt.Graphics2D JavaDoc;
23 import java.awt.GridLayout JavaDoc;
24 import java.awt.Rectangle JavaDoc;
25 import java.awt.Toolkit JavaDoc;
26 import java.awt.event.ActionEvent JavaDoc;
27 import java.awt.event.ActionListener JavaDoc;
28 import java.awt.event.ComponentEvent JavaDoc;
29 import java.awt.event.ComponentListener JavaDoc;
30 import java.awt.event.WindowAdapter JavaDoc;
31 import java.awt.event.WindowEvent JavaDoc;
32 import java.awt.image.BufferedImage JavaDoc;
33 import java.util.HashMap JavaDoc;
34 import java.util.Map JavaDoc;
35
36 import javax.swing.JButton JavaDoc;
37 import javax.swing.JComboBox JavaDoc;
38 import javax.swing.JFrame JavaDoc;
39 import javax.swing.JLabel JavaDoc;
40 import javax.swing.JOptionPane JavaDoc;
41 import javax.swing.JPanel JavaDoc;
42
43 import org.eclipse.birt.chart.device.IDeviceRenderer;
44 import org.eclipse.birt.chart.device.ICallBackNotifier;
45 import org.eclipse.birt.chart.exception.ChartException;
46 import org.eclipse.birt.chart.factory.GeneratedChartState;
47 import org.eclipse.birt.chart.factory.Generator;
48 import org.eclipse.birt.chart.model.Chart;
49 import org.eclipse.birt.chart.model.attribute.Bounds;
50 import org.eclipse.birt.chart.model.attribute.CallBackValue;
51 import org.eclipse.birt.chart.model.attribute.impl.BoundsImpl;
52 import org.eclipse.birt.chart.util.PluginSettings;
53 import org.eclipse.birt.core.exception.BirtException;
54
55 /**
56  * The selector of interactivity charts in Swing JPanel.
57  *
58  * Note: Use an extra off-screen image buffer as the actual drawing canvas instead of
59  * the original chart rendering code in paint( ) method.
60  * Please see [bugzilla] 127615 for more details.
61  */

62 public final class SwingInteractivityViewer extends JPanel JavaDoc implements
63         ICallBackNotifier,
64         ComponentListener JavaDoc
65 {
66
67     private static final long serialVersionUID = 1L;
68     
69     private boolean bNeedsGeneration = true;
70
71     private GeneratedChartState gcs = null;
72
73     private Chart cm = null;
74
75     private IDeviceRenderer idr = null;
76     
77     private BufferedImage JavaDoc bi = null;
78
79     private Map JavaDoc contextMap;
80     
81     /**
82      * Contructs the layout with a container for displaying chart and a control
83      * panel for selecting interactivity.
84      *
85      * @param args
86      */

87     public static void main( String JavaDoc[] args )
88     {
89         final SwingInteractivityViewer siv = new SwingInteractivityViewer( );
90
91         JFrame JavaDoc jf = new JFrame JavaDoc( );
92         jf.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
93         jf.addComponentListener( siv );
94
95         Container JavaDoc co = jf.getContentPane( );
96         co.setLayout( new BorderLayout JavaDoc( ) );
97         co.add( siv, BorderLayout.CENTER );
98
99         Dimension JavaDoc dScreen = Toolkit.getDefaultToolkit( ).getScreenSize( );
100         Dimension JavaDoc dApp = new Dimension JavaDoc( 600, 400 );
101         jf.setSize( dApp );
102         jf.setLocation( ( dScreen.width - dApp.width ) / 2,
103                 ( dScreen.height - dApp.height ) / 2 );
104
105         jf.setTitle( siv.getClass( ).getName( ) + " [device=" //$NON-NLS-1$
106
+ siv.idr.getClass( ).getName( ) + "]" );//$NON-NLS-1$
107

108         ControlPanel cp = siv.new ControlPanel( siv );
109         co.add( cp, BorderLayout.SOUTH );
110         
111         siv.idr.setProperty( IDeviceRenderer.UPDATE_NOTIFIER, siv );
112
113         jf.addWindowListener( new WindowAdapter JavaDoc( ) {
114
115             public void windowClosing( WindowEvent JavaDoc e )
116             {
117                 siv.idr.dispose( );
118             }
119
120         } );
121
122         jf.setVisible( true );
123     }
124
125     /**
126      * Get the connection with SWING device to render the graphics.
127      */

128     SwingInteractivityViewer( )
129     {
130         contextMap = new HashMap JavaDoc();
131         
132         final PluginSettings ps = PluginSettings.instance( );
133         try
134         {
135             idr = ps.getDevice( "dv.SWING" );//$NON-NLS-1$
136
}
137         catch ( ChartException ex )
138         {
139             ex.printStackTrace( );
140         }
141         cm = InteractivityCharts.createHSChart( );
142     }
143
144     /*
145      * (non-Javadoc)
146      *
147      * @see org.eclipse.birt.chart.device.swing.IUpdateNotifier#update()
148      */

149     public void regenerateChart( )
150     {
151         bNeedsGeneration = true;
152         updateBuffer( );
153         repaint( );
154     }
155
156     /*
157      * (non-Javadoc)
158      *
159      * @see org.eclipse.birt.chart.device.swing.IUpdateNotifier#update()
160      */

161     public void repaintChart( )
162     {
163         repaint( );
164     }
165
166     /*
167      * (non-Javadoc)
168      *
169      * @see org.eclipse.birt.chart.device.swing.IUpdateNotifier#peerInstance()
170      */

171     public Object JavaDoc peerInstance( )
172     {
173         return this;
174     }
175
176     /*
177      * (non-Javadoc)
178      *
179      * @see org.eclipse.birt.chart.device.swing.IUpdateNotifier#getDesignTimeModel()
180      */

181     public Chart getDesignTimeModel( )
182     {
183         return cm;
184     }
185
186     /*
187      * (non-Javadoc)
188      *
189      * @see org.eclipse.birt.chart.device.swing.IUpdateNotifier#getRunTimeModel()
190      */

191     public Chart getRunTimeModel( )
192     {
193         return gcs.getChartModel( );
194     }
195     
196     /*
197      * (non-Javadoc)
198      *
199      * @see org.eclipse.birt.chart.device.IUpdateNotifier#getContext(java.lang.Object)
200      */

201     public Object JavaDoc getContext( Object JavaDoc key )
202     {
203         return contextMap.get( key );
204     }
205
206     /*
207      * (non-Javadoc)
208      *
209      * @see org.eclipse.birt.chart.device.IUpdateNotifier#putContext(java.lang.Object,
210      * java.lang.Object)
211      */

212     public Object JavaDoc putContext( Object JavaDoc key, Object JavaDoc value )
213     {
214         return contextMap.put( key, value );
215     }
216
217     /*
218      * (non-Javadoc)
219      *
220      * @see org.eclipse.birt.chart.device.IUpdateNotifier#removeContext(java.lang.Object)
221      */

222     public Object JavaDoc removeContext( Object JavaDoc key )
223     {
224         return contextMap.remove( key );
225     }
226
227     public void updateBuffer( )
228     {
229         Dimension JavaDoc d = getSize( );
230
231         if ( bi == null
232                 || bi.getWidth( ) != d.width
233                 || bi.getHeight( ) != d.height )
234         {
235             bi = new BufferedImage JavaDoc( d.width,
236                     d.height,
237                     BufferedImage.TYPE_INT_ARGB );
238         }
239
240         Graphics2D JavaDoc g2d = (Graphics2D JavaDoc) bi.getGraphics( );
241
242         idr.setProperty( IDeviceRenderer.GRAPHICS_CONTEXT, g2d );
243         Bounds bo = BoundsImpl.create( 0, 0, d.width, d.height );
244         bo.scale( 72d / idr.getDisplayServer( ).getDpiResolution( ) ); // BOUNDS
245
// MUST
246
// BE
247
// SPECIFIED
248
// IN
249
// POINTS
250

251         Generator gr = Generator.instance( );
252         if ( bNeedsGeneration )
253         {
254             bNeedsGeneration = false;
255             try
256             {
257                 gcs = gr.build( idr.getDisplayServer( ),
258                         cm,
259                         bo,
260                         null,
261                         null,
262                         null );
263             }
264             catch ( ChartException ex )
265             {
266                 showException( g2d, ex );
267             }
268         }
269
270         try
271         {
272             gr.render( idr, gcs );
273         }
274         catch ( ChartException rex )
275         {
276             showException( g2d, rex );
277         }
278         finally
279         {
280             g2d.dispose( );
281         }
282
283     }
284     
285     /*
286      * (non-Javadoc)
287      *
288      * @see javax.swing.JComponent#paint(java.awt.Graphics)
289      */

290     public void paint( Graphics JavaDoc g )
291     {
292         super.paint( g );
293
294         if ( bi == null )
295         {
296             updateBuffer( );
297         }
298         
299         g.drawImage( bi, 0, 0, this );
300     }
301
302     /**
303      * Presents the Exceptions if the chart cannot be displayed properly.
304      *
305      * @param g2d
306      * @param ex
307      */

308     private final void showException( Graphics2D JavaDoc g2d, Exception JavaDoc ex )
309     {
310         String JavaDoc sWrappedException = ex.getClass( ).getName( );
311         Throwable JavaDoc th = ex;
312         while ( ex.getCause( ) != null )
313         {
314             ex = (Exception JavaDoc) ex.getCause( );
315         }
316         String JavaDoc sException = ex.getClass( ).getName( );
317         if ( sWrappedException.equals( sException ) )
318         {
319             sWrappedException = null;
320         }
321
322         String JavaDoc sMessage = null;
323         if ( th instanceof BirtException )
324         {
325             sMessage = ( (BirtException) th ).getLocalizedMessage( );
326         }
327         else
328         {
329             sMessage = ex.getMessage( );
330         }
331
332         if ( sMessage == null )
333         {
334             sMessage = "<null>";//$NON-NLS-1$
335
}
336
337         StackTraceElement JavaDoc[] stea = ex.getStackTrace( );
338         Dimension JavaDoc d = getSize( );
339
340         Font JavaDoc fo = new Font JavaDoc( "Monospaced", Font.BOLD, 14 );//$NON-NLS-1$
341
g2d.setFont( fo );
342         FontMetrics JavaDoc fm = g2d.getFontMetrics( );
343         g2d.setColor( Color.WHITE );
344         g2d.fillRect( 20, 20, d.width - 40, d.height - 40 );
345         g2d.setColor( Color.BLACK );
346         g2d.drawRect( 20, 20, d.width - 40, d.height - 40 );
347         g2d.setClip( 20, 20, d.width - 40, d.height - 40 );
348         int x = 25, y = 20 + fm.getHeight( );
349         g2d.drawString( "Exception:", x, y );//$NON-NLS-1$
350
x += fm.stringWidth( "Exception:" ) + 5;//$NON-NLS-1$
351
g2d.setColor( Color.RED );
352         g2d.drawString( sException, x, y );
353         x = 25;
354         y += fm.getHeight( );
355         if ( sWrappedException != null )
356         {
357             g2d.setColor( Color.BLACK );
358             g2d.drawString( "Wrapped In:", x, y );//$NON-NLS-1$
359
x += fm.stringWidth( "Wrapped In:" ) + 5;//$NON-NLS-1$
360
g2d.setColor( Color.RED );
361             g2d.drawString( sWrappedException, x, y );
362             x = 25;
363             y += fm.getHeight( );
364         }
365         g2d.setColor( Color.BLACK );
366         y += 10;
367         g2d.drawString( "Message:", x, y );//$NON-NLS-1$
368
x += fm.stringWidth( "Message:" ) + 5;//$NON-NLS-1$
369
g2d.setColor( Color.BLUE );
370         g2d.drawString( sMessage, x, y );
371         x = 25;
372         y += fm.getHeight( );
373         g2d.setColor( Color.BLACK );
374         y += 10;
375         g2d.drawString( "Trace:", x, y );//$NON-NLS-1$
376
x = 40;
377         y += fm.getHeight( );
378         g2d.setColor( Color.GREEN.darker( ) );
379         for ( int i = 0; i < stea.length; i++ )
380         {
381             g2d.drawString( stea[i].getClassName( ) + ":"//$NON-NLS-1$
382
+ stea[i].getMethodName( ) + "(...):"//$NON-NLS-1$
383
+ stea[i].getLineNumber( ), x, y );
384             x = 40;
385             y += fm.getHeight( );
386         }
387     }
388
389     /*
390      * (non-Javadoc)
391      *
392      * @see java.awt.event.ComponentListener#componentHidden(java.awt.event.ComponentEvent)
393      */

394     public void componentHidden( ComponentEvent JavaDoc e )
395     {
396         // TODO Auto-generated method stub
397

398     }
399
400     /*
401      * (non-Javadoc)
402      *
403      * @see java.awt.event.ComponentListener#componentMoved(java.awt.event.ComponentEvent)
404      */

405     public void componentMoved( ComponentEvent JavaDoc e )
406     {
407         // TODO Auto-generated method stub
408

409     }
410
411     /*
412      * (non-Javadoc)
413      *
414      * @see java.awt.event.ComponentListener#componentResized(java.awt.event.ComponentEvent)
415      */

416     public void componentResized( ComponentEvent JavaDoc e )
417     {
418         bNeedsGeneration = true;
419     }
420
421     /*
422      * (non-Javadoc)
423      *
424      * @see java.awt.event.ComponentListener#componentShown(java.awt.event.ComponentEvent)
425      */

426     public void componentShown( ComponentEvent JavaDoc e )
427     {
428         // TODO Auto-generated method stub
429

430     }
431
432     /**
433      * An inner class Control Panel, which provides the interactive interface
434      * with the user.
435      */

436     private final class ControlPanel extends JPanel JavaDoc implements ActionListener JavaDoc
437     {
438
439         private static final long serialVersionUID = 1L;
440         
441         private JComboBox JavaDoc jcbModels = null;
442
443         private JButton JavaDoc jbUpdate = null;
444
445         private final SwingInteractivityViewer siv;
446
447         ControlPanel( SwingInteractivityViewer siv )
448         {
449             this.siv = siv;
450
451             setLayout( new GridLayout JavaDoc( 0, 1, 0, 0 ) );
452
453             JPanel JavaDoc jp = new JPanel JavaDoc( );
454             jp.setLayout( new FlowLayout JavaDoc( FlowLayout.LEFT, 3, 3 ) );
455
456             jp.add( new JLabel JavaDoc( "Choose:" ) );//$NON-NLS-1$
457
jcbModels = new JComboBox JavaDoc( );
458
459             jcbModels.addItem( "Highlight Series" );//$NON-NLS-1$
460
jcbModels.addItem( "Show Tooltip" );//$NON-NLS-1$
461
jcbModels.addItem( "Toggle Visibility" );//$NON-NLS-1$
462
jcbModels.addItem( "URL Redirect" );//$NON-NLS-1$
463
jcbModels.addItem( "Call Back" );//$NON-NLS-1$
464

465             jcbModels.setSelectedIndex( 0 );
466             jp.add( jcbModels );
467
468             jbUpdate = new JButton JavaDoc( "Update" );//$NON-NLS-1$
469
jbUpdate.addActionListener( this );
470             jp.add( jbUpdate );
471
472             add( jp );
473         }
474
475         /*
476          * (non-Javadoc)
477          *
478          * @see java.awt.event.ComponentListener#componentHidden(java.awt.event.ComponentEvent)
479          */

480         public void componentHidden( ComponentEvent JavaDoc cev )
481         {
482             setVisible( false );
483         }
484
485         /*
486          * (non-Javadoc)
487          *
488          * @see java.awt.event.ComponentListener#componentMoved(java.awt.event.ComponentEvent)
489          */

490         public void componentMoved( ComponentEvent JavaDoc cev )
491         {
492             JFrame JavaDoc jf = (JFrame JavaDoc) cev.getComponent( );
493             Rectangle JavaDoc r = jf.getBounds( );
494             setLocation( r.x, r.y + r.height );
495             setSize( r.width, 50 );
496         }
497
498         /*
499          * (non-Javadoc)
500          *
501          * @see java.awt.event.ComponentListener#componentResized(java.awt.event.ComponentEvent)
502          */

503         public void componentResized( ComponentEvent JavaDoc cev )
504         {
505             JFrame JavaDoc jf = (JFrame JavaDoc) cev.getComponent( );
506             Rectangle JavaDoc r = jf.getBounds( );
507             setLocation( r.x, r.y + r.height );
508             setSize( r.width, 50 );
509         }
510
511         /*
512          * (non-Javadoc)
513          *
514          * @see java.awt.event.ComponentListener#componentShown(java.awt.event.ComponentEvent)
515          */

516         public void componentShown( ComponentEvent JavaDoc cev )
517         {
518             JFrame JavaDoc jf = (JFrame JavaDoc) cev.getComponent( );
519             Rectangle JavaDoc r = jf.getBounds( );
520             setLocation( r.x, r.y + r.height );
521             setSize( r.width, 50 );
522             setVisible( true );
523         }
524
525         /*
526          * (non-Javadoc)
527          *
528          * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
529          */

530         public void actionPerformed( ActionEvent JavaDoc e )
531         {
532             int i = jcbModels.getSelectedIndex( );
533             cm = null;
534             switch ( i )
535             {
536                 case 0 :
537                     cm = InteractivityCharts.createHSChart( );
538                     break;
539                 case 1 :
540                     cm = InteractivityCharts.createSTChart( );
541                     break;
542                 case 2 :
543                     cm = InteractivityCharts.createTVChart( );
544                     break;
545                 case 3 :
546                     cm = InteractivityCharts.createURChart( );
547                     break;
548                 case 4 :
549                     cm = InteractivityCharts.createCBChart( );
550                     break;
551             }
552
553             bNeedsGeneration = true;
554             siv.updateBuffer( );
555             siv.repaint( );
556         }
557     }
558     
559     public void callback( Object JavaDoc event, Object JavaDoc source, CallBackValue value )
560     {
561         JOptionPane.showMessageDialog( SwingInteractivityViewer.this,
562                 value.getIdentifier( ) );
563     }
564 }
Popular Tags