KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > birt > chart > examples > api > data > DataChartsViewer


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.data;
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.util.HashMap JavaDoc;
31 import java.util.Map JavaDoc;
32
33 import javax.swing.JButton JavaDoc;
34 import javax.swing.JComboBox JavaDoc;
35 import javax.swing.JFrame JavaDoc;
36 import javax.swing.JLabel JavaDoc;
37 import javax.swing.JPanel JavaDoc;
38 import org.eclipse.birt.chart.device.IDeviceRenderer;
39 import org.eclipse.birt.chart.device.IUpdateNotifier;
40 import org.eclipse.birt.chart.exception.ChartException;
41 import org.eclipse.birt.chart.factory.GeneratedChartState;
42 import org.eclipse.birt.chart.factory.Generator;
43 import org.eclipse.birt.chart.model.Chart;
44 import org.eclipse.birt.chart.model.attribute.Bounds;
45 import org.eclipse.birt.chart.model.attribute.impl.BoundsImpl;
46 import org.eclipse.birt.chart.util.PluginSettings;
47 import org.eclipse.birt.core.exception.BirtException;
48
49 /**
50  * The selector of interactivity charts in Swing JPanel.
51  *
52  */

53 public final class DataChartsViewer extends JPanel JavaDoc implements
54         IUpdateNotifier,
55         ComponentListener JavaDoc
56 {
57
58     private static final long serialVersionUID = 1L;
59
60     private boolean bNeedsGeneration = true;
61
62     private GeneratedChartState gcs = null;
63
64     private Chart cm = null;
65
66     private IDeviceRenderer idr = null;
67     
68     private Map JavaDoc contextMap;
69
70     /**
71      * Contructs the layout with a container for displaying chart and a control
72      * panel for selecting interactivity.
73      *
74      * @param args
75      */

76     public static void main( String JavaDoc[] args )
77     {
78         DataChartsViewer dcv = new DataChartsViewer( );
79
80         JFrame JavaDoc jf = new JFrame JavaDoc( );
81         jf.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
82         jf.addComponentListener( dcv );
83
84         Container JavaDoc co = jf.getContentPane( );
85         co.setLayout( new BorderLayout JavaDoc( ) );
86         co.add( dcv, BorderLayout.CENTER );
87
88         Dimension JavaDoc dScreen = Toolkit.getDefaultToolkit( ).getScreenSize( );
89         Dimension JavaDoc dApp = new Dimension JavaDoc( 600, 400 );
90         jf.setSize( dApp );
91         jf.setLocation( ( dScreen.width - dApp.width ) / 2,
92                 ( dScreen.height - dApp.height ) / 2 );
93
94         jf.setTitle( dcv.getClass( ).getName( ) + " [device=" //$NON-NLS-1$
95
+ dcv.idr.getClass( ).getName( ) + "]" );//$NON-NLS-1$
96

97         ControlPanel cp = dcv.new ControlPanel( dcv );
98         co.add( cp, BorderLayout.SOUTH );
99
100         jf.setVisible( true );
101     }
102
103     /**
104      * Get the connection with SWING device to render the graphics.
105      */

106     DataChartsViewer( )
107     {
108         contextMap = new HashMap JavaDoc();
109         
110         final PluginSettings ps = PluginSettings.instance( );
111         try
112         {
113             idr = ps.getDevice( "dv.SWING" );//$NON-NLS-1$
114
}
115         catch ( ChartException ex )
116         {
117             ex.printStackTrace( );
118         }
119         cm = DataCharts.createMinSliceChart( );
120     }
121
122     /*
123      * (non-Javadoc)
124      *
125      * @see org.eclipse.birt.chart.device.swing.IUpdateNotifier#update()
126      */

127     public void regenerateChart( )
128     {
129         bNeedsGeneration = true;
130         repaint( );
131     }
132
133     /*
134      * (non-Javadoc)
135      *
136      * @see org.eclipse.birt.chart.device.swing.IUpdateNotifier#update()
137      */

138     public void repaintChart( )
139     {
140         repaint( );
141     }
142
143     /*
144      * (non-Javadoc)
145      *
146      * @see org.eclipse.birt.chart.device.swing.IUpdateNotifier#peerInstance()
147      */

148     public Object JavaDoc peerInstance( )
149     {
150         return this;
151     }
152
153     /*
154      * (non-Javadoc)
155      *
156      * @see org.eclipse.birt.chart.device.swing.IUpdateNotifier#getDesignTimeModel()
157      */

158     public Chart getDesignTimeModel( )
159     {
160         return cm;
161     }
162
163     /*
164      * (non-Javadoc)
165      *
166      * @see org.eclipse.birt.chart.device.swing.IUpdateNotifier#getRunTimeModel()
167      */

168     public Chart getRunTimeModel( )
169     {
170         return gcs.getChartModel( );
171     }
172     
173     /*
174      * (non-Javadoc)
175      *
176      * @see org.eclipse.birt.chart.device.IUpdateNotifier#getContext(java.lang.Object)
177      */

178     public Object JavaDoc getContext( Object JavaDoc key )
179     {
180         return contextMap.get( key );
181     }
182
183     /*
184      * (non-Javadoc)
185      *
186      * @see org.eclipse.birt.chart.device.IUpdateNotifier#putContext(java.lang.Object,
187      * java.lang.Object)
188      */

189     public Object JavaDoc putContext( Object JavaDoc key, Object JavaDoc value )
190     {
191         return contextMap.put( key, value );
192     }
193
194     /*
195      * (non-Javadoc)
196      *
197      * @see org.eclipse.birt.chart.device.IUpdateNotifier#removeContext(java.lang.Object)
198      */

199     public Object JavaDoc removeContext( Object JavaDoc key )
200     {
201         return contextMap.remove( key );
202     }
203
204     public void paint( Graphics JavaDoc g )
205     {
206         super.paint( g );
207
208         Graphics2D JavaDoc g2d = (Graphics2D JavaDoc) g;
209         idr.setProperty( IDeviceRenderer.GRAPHICS_CONTEXT, g2d );
210         idr.setProperty( IDeviceRenderer.UPDATE_NOTIFIER, this );
211
212         Dimension JavaDoc d = getSize( );
213         Bounds bo = BoundsImpl.create( 0, 0, d.width, d.height );
214         bo.scale( 72d / idr.getDisplayServer( ).getDpiResolution( ) );
215
216         Generator gr = Generator.instance( );
217
218         // When the update button has been pushed, build a chart offscreen.
219
if ( bNeedsGeneration )
220         {
221             bNeedsGeneration = false;
222             try
223             {
224                 gcs = gr.build( idr.getDisplayServer( ),
225                         cm,
226                         bo,
227                         null,
228                         null,
229                         null );
230             }
231             catch ( ChartException ex )
232             {
233                 showException( g2d, ex );
234             }
235         }
236
237         // Draw the previous built chart on screen.
238
try
239         {
240             gr.render( idr, gcs );
241         }
242         catch ( ChartException ex )
243         {
244             showException( g2d, ex );
245         }
246     }
247
248     /**
249      * Presents the Exceptions if the chart cannot be displayed properly.
250      *
251      * @param g2d
252      * @param ex
253      */

254     private final void showException( Graphics2D JavaDoc g2d, Exception JavaDoc ex )
255     {
256         String JavaDoc sWrappedException = ex.getClass( ).getName( );
257         Throwable JavaDoc th = ex;
258         while ( ex.getCause( ) != null )
259         {
260             ex = (Exception JavaDoc) ex.getCause( );
261         }
262         String JavaDoc sException = ex.getClass( ).getName( );
263         if ( sWrappedException.equals( sException ) )
264         {
265             sWrappedException = null;
266         }
267
268         String JavaDoc sMessage = null;
269         if ( th instanceof BirtException )
270         {
271             sMessage = ( (BirtException) th ).getLocalizedMessage( );
272         }
273         else
274         {
275             sMessage = ex.getMessage( );
276         }
277
278         if ( sMessage == null )
279         {
280             sMessage = "<null>";//$NON-NLS-1$
281
}
282
283         StackTraceElement JavaDoc[] stea = ex.getStackTrace( );
284         Dimension JavaDoc d = getSize( );
285
286         Font JavaDoc fo = new Font JavaDoc( "Monospaced", Font.BOLD, 14 );//$NON-NLS-1$
287
g2d.setFont( fo );
288         FontMetrics JavaDoc fm = g2d.getFontMetrics( );
289         g2d.setColor( Color.WHITE );
290         g2d.fillRect( 20, 20, d.width - 40, d.height - 40 );
291         g2d.setColor( Color.BLACK );
292         g2d.drawRect( 20, 20, d.width - 40, d.height - 40 );
293         g2d.setClip( 20, 20, d.width - 40, d.height - 40 );
294         int x = 25, y = 20 + fm.getHeight( );
295         g2d.drawString( "Exception:", x, y );//$NON-NLS-1$
296
x += fm.stringWidth( "Exception:" ) + 5;//$NON-NLS-1$
297
g2d.setColor( Color.RED );
298         g2d.drawString( sException, x, y );
299         x = 25;
300         y += fm.getHeight( );
301         if ( sWrappedException != null )
302         {
303             g2d.setColor( Color.BLACK );
304             g2d.drawString( "Wrapped In:", x, y );//$NON-NLS-1$
305
x += fm.stringWidth( "Wrapped In:" ) + 5;//$NON-NLS-1$
306
g2d.setColor( Color.RED );
307             g2d.drawString( sWrappedException, x, y );
308             x = 25;
309             y += fm.getHeight( );
310         }
311         g2d.setColor( Color.BLACK );
312         y += 10;
313         g2d.drawString( "Message:", x, y );//$NON-NLS-1$
314
x += fm.stringWidth( "Message:" ) + 5;//$NON-NLS-1$
315
g2d.setColor( Color.BLUE );
316         g2d.drawString( sMessage, x, y );
317         x = 25;
318         y += fm.getHeight( );
319         g2d.setColor( Color.BLACK );
320         y += 10;
321         g2d.drawString( "Trace:", x, y );//$NON-NLS-1$
322
x = 40;
323         y += fm.getHeight( );
324         g2d.setColor( Color.GREEN.darker( ) );
325         for ( int i = 0; i < stea.length; i++ )
326         {
327             g2d.drawString( stea[i].getClassName( ) + ":"//$NON-NLS-1$
328
+ stea[i].getMethodName( ) + "(...):"//$NON-NLS-1$
329
+ stea[i].getLineNumber( ), x, y );
330             x = 40;
331             y += fm.getHeight( );
332         }
333     }
334
335     /*
336      * (non-Javadoc)
337      *
338      * @see java.awt.event.ComponentListener#componentHidden(java.awt.event.ComponentEvent)
339      */

340     public void componentHidden( ComponentEvent JavaDoc e )
341     {
342         // TODO Auto-generated method stub
343

344     }
345
346     /*
347      * (non-Javadoc)
348      *
349      * @see java.awt.event.ComponentListener#componentMoved(java.awt.event.ComponentEvent)
350      */

351     public void componentMoved( ComponentEvent JavaDoc e )
352     {
353         // TODO Auto-generated method stub
354

355     }
356
357     /*
358      * (non-Javadoc)
359      *
360      * @see java.awt.event.ComponentListener#componentResized(java.awt.event.ComponentEvent)
361      */

362     public void componentResized( ComponentEvent JavaDoc e )
363     {
364         bNeedsGeneration = true;
365     }
366
367     /*
368      * (non-Javadoc)
369      *
370      * @see java.awt.event.ComponentListener#componentShown(java.awt.event.ComponentEvent)
371      */

372     public void componentShown( ComponentEvent JavaDoc e )
373     {
374         // TODO Auto-generated method stub
375

376     }
377
378     /**
379      * An inner class Control Panel, which provides the interactive interface
380      * with the user.
381      */

382     private final class ControlPanel extends JPanel JavaDoc implements ActionListener JavaDoc
383     {
384         private static final long serialVersionUID = 1L;
385
386         private JComboBox JavaDoc jcbModels = null;
387
388         private JButton JavaDoc jbUpdate = null;
389
390         private final DataChartsViewer dcv;
391
392         ControlPanel( DataChartsViewer dcv )
393         {
394             this.dcv = dcv;
395
396             setLayout( new GridLayout JavaDoc( 0, 1, 0, 0 ) );
397
398             JPanel JavaDoc jp = new JPanel JavaDoc( );
399             jp.setLayout( new FlowLayout JavaDoc( FlowLayout.LEFT, 3, 3 ) );
400
401             jp.add( new JLabel JavaDoc( "Choose:" ) );//$NON-NLS-1$
402
jcbModels = new JComboBox JavaDoc( );
403
404             jcbModels.addItem( "Min Slice" );//$NON-NLS-1$
405
jcbModels.addItem( "Multiple Y Axis" );//$NON-NLS-1$
406
jcbModels.addItem( "Multiple Y Series" );//$NON-NLS-1$
407

408             jcbModels.setSelectedIndex( 0 );
409             jp.add( jcbModels );
410
411             jbUpdate = new JButton JavaDoc( "Update" );//$NON-NLS-1$
412
jbUpdate.addActionListener( this );
413             jp.add( jbUpdate );
414
415             add( jp );
416         }
417
418         /*
419          * (non-Javadoc)
420          *
421          * @see java.awt.event.ComponentListener#componentHidden(java.awt.event.ComponentEvent)
422          */

423         public void componentHidden( ComponentEvent JavaDoc cev )
424         {
425             setVisible( false );
426         }
427
428         /*
429          * (non-Javadoc)
430          *
431          * @see java.awt.event.ComponentListener#componentMoved(java.awt.event.ComponentEvent)
432          */

433         public void componentMoved( ComponentEvent JavaDoc cev )
434         {
435             JFrame JavaDoc jf = (JFrame JavaDoc) cev.getComponent( );
436             Rectangle JavaDoc r = jf.getBounds( );
437             setLocation( r.x, r.y + r.height );
438             setSize( r.width, 50 );
439         }
440
441         /*
442          * (non-Javadoc)
443          *
444          * @see java.awt.event.ComponentListener#componentResized(java.awt.event.ComponentEvent)
445          */

446         public void componentResized( ComponentEvent JavaDoc cev )
447         {
448             JFrame JavaDoc jf = (JFrame JavaDoc) cev.getComponent( );
449             Rectangle JavaDoc r = jf.getBounds( );
450             setLocation( r.x, r.y + r.height );
451             setSize( r.width, 50 );
452         }
453
454         /*
455          * (non-Javadoc)
456          *
457          * @see java.awt.event.ComponentListener#componentShown(java.awt.event.ComponentEvent)
458          */

459         public void componentShown( ComponentEvent JavaDoc cev )
460         {
461             JFrame JavaDoc jf = (JFrame JavaDoc) cev.getComponent( );
462             Rectangle JavaDoc r = jf.getBounds( );
463             setLocation( r.x, r.y + r.height );
464             setSize( r.width, 50 );
465             setVisible( true );
466         }
467
468         /*
469          * (non-Javadoc)
470          *
471          * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
472          */

473         public void actionPerformed( ActionEvent JavaDoc e )
474         {
475             int i = jcbModels.getSelectedIndex( );
476             cm = null;
477             switch ( i )
478             {
479                 case 0 :
480                     cm = DataCharts.createMinSliceChart( );
481                     break;
482                 case 1 :
483                     cm = DataCharts.createMulitYSeriesChart( );
484                     break;
485                 case 2 :
486                     cm = DataCharts.createMultiYAxisChart( );
487                     break;
488             }
489
490             bNeedsGeneration = true;
491             dcv.repaint( );
492         }
493     }
494 }
495
Popular Tags