KickJava   Java API By Example, From Geeks To Geeks.

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


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 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.JCheckBox JavaDoc;
35 import javax.swing.JComboBox JavaDoc;
36 import javax.swing.JFrame JavaDoc;
37 import javax.swing.JLabel JavaDoc;
38 import javax.swing.JPanel JavaDoc;
39
40 import org.eclipse.birt.chart.device.IDeviceRenderer;
41 import org.eclipse.birt.chart.device.IUpdateNotifier;
42 import org.eclipse.birt.chart.exception.ChartException;
43 import org.eclipse.birt.chart.factory.GeneratedChartState;
44 import org.eclipse.birt.chart.factory.Generator;
45 import org.eclipse.birt.chart.model.Chart;
46 import org.eclipse.birt.chart.model.ChartWithAxes;
47 import org.eclipse.birt.chart.model.ChartWithoutAxes;
48 import org.eclipse.birt.chart.model.attribute.AxisType;
49 import org.eclipse.birt.chart.model.attribute.Bounds;
50 import org.eclipse.birt.chart.model.attribute.ChartDimension;
51 import org.eclipse.birt.chart.model.attribute.impl.BoundsImpl;
52 import org.eclipse.birt.chart.model.attribute.impl.JavaNumberFormatSpecifierImpl;
53 import org.eclipse.birt.chart.model.component.Axis;
54 import org.eclipse.birt.chart.util.PluginSettings;
55 import org.eclipse.birt.core.exception.BirtException;
56
57 /**
58  * The selector of charts in Swing JPanel.
59  *
60  */

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

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

106         ControlPanel cp = scv.new ControlPanel( scv );
107         co.add( cp, BorderLayout.SOUTH );
108
109         jf.setVisible( true );
110     }
111
112     /**
113      * Get the connection with SWING device to render the graphics.
114      */

115     SwingChartViewerSelector( )
116     {
117         contextMap = new HashMap JavaDoc( );
118
119         final PluginSettings ps = PluginSettings.instance( );
120         try
121         {
122             idr = ps.getDevice( "dv.SWING" );//$NON-NLS-1$
123
}
124         catch ( ChartException ex )
125         {
126             ex.printStackTrace( );
127         }
128         // The default chart displayed in the container is the simple bar chart.
129
cm = PrimitiveCharts.createBarChart( );
130     }
131
132     /*
133      * (non-Javadoc)
134      *
135      * @see org.eclipse.birt.chart.device.swing.IUpdateNotifier#update()
136      */

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

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

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

168     public Chart getDesignTimeModel( )
169     {
170         return cm;
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     /*
205      * (non-Javadoc)
206      *
207      * @see org.eclipse.birt.chart.device.swing.IUpdateNotifier#getRunTimeModel()
208      */

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

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

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

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

362     public void componentMoved( ComponentEvent JavaDoc e )
363     {
364         // TODO Auto-generated method stub
365

366     }
367
368     /*
369      * (non-Javadoc)
370      *
371      * @see java.awt.event.ComponentListener#componentResized(java.awt.event.ComponentEvent)
372      */

373     public void componentResized( ComponentEvent JavaDoc e )
374     {
375         bNeedsGeneration = true;
376     }
377
378     /*
379      * (non-Javadoc)
380      *
381      * @see java.awt.event.ComponentListener#componentShown(java.awt.event.ComponentEvent)
382      */

383     public void componentShown( ComponentEvent JavaDoc e )
384     {
385         // TODO Auto-generated method stub
386

387     }
388
389     /**
390      * An inner class Control Panel, which provides the interactive interface
391      * with the user.
392      */

393     private final class ControlPanel extends JPanel JavaDoc implements ActionListener JavaDoc
394     {
395
396         private static final long serialVersionUID = 1L;
397         
398         private JComboBox JavaDoc jcbModels = null;
399
400         private JButton JavaDoc jbUpdate = null;
401
402         private JComboBox JavaDoc jcbDimensions = null;
403
404         private JCheckBox JavaDoc jcbTransposed = null;
405
406         private JCheckBox JavaDoc jcbPercent = null;
407
408         private JCheckBox JavaDoc jcbLogarithmic = null;
409
410         private final SwingChartViewerSelector scv;
411
412         ControlPanel( SwingChartViewerSelector scv )
413         {
414             this.scv = scv;
415
416             setLayout( new GridLayout JavaDoc( 0, 1, 0, 0 ) );
417
418             JPanel JavaDoc jp1 = new JPanel JavaDoc( );
419             jp1.setLayout( new FlowLayout JavaDoc( FlowLayout.LEFT, 5, 5 ) );
420
421             jp1.add( new JLabel JavaDoc( "Choose:" ) );//$NON-NLS-1$
422
jcbModels = new JComboBox JavaDoc( );
423
424             jcbModels.addItem( "Bar Chart" );//$NON-NLS-1$
425
jcbModels.addItem( "Bar Chart(2 Series)" );//$NON-NLS-1$
426
jcbModels.addItem( "Pie Chart" );//$NON-NLS-1$
427
jcbModels.addItem( "Pie Chart(4 Series)" );//$NON-NLS-1$
428
jcbModels.addItem( "Line Chart" );//$NON-NLS-1$
429
jcbModels.addItem( "Bar/Line Stacked Chart" );//$NON-NLS-1$
430
jcbModels.addItem( "Scatter Chart" );//$NON-NLS-1$
431
jcbModels.addItem( "Stock Chart" );//$NON-NLS-1$
432
jcbModels.addItem( "Area Chart" );//$NON-NLS-1$
433
jcbModels.addItem( "Open Chart File" ); //$NON-NLS-1$
434

435             jcbModels.setSelectedIndex( 0 );
436             jp1.add( jcbModels );
437
438             jcbDimensions = new JComboBox JavaDoc( );
439             jcbDimensions.addItem( "2D" );//$NON-NLS-1$
440
jcbDimensions.addItem( "2D with Depth" );//$NON-NLS-1$
441
jp1.add( jcbDimensions );
442
443             jcbTransposed = new JCheckBox JavaDoc( "Transposed", false );//$NON-NLS-1$
444
jp1.add( jcbTransposed );
445
446             jcbPercent = new JCheckBox JavaDoc( "Percent", false );//$NON-NLS-1$
447
jp1.add( jcbPercent );
448
449             jcbLogarithmic = new JCheckBox JavaDoc( "Logarithmic", false );//$NON-NLS-1$
450
jp1.add( jcbLogarithmic );
451
452             jbUpdate = new JButton JavaDoc( "Update" );//$NON-NLS-1$
453
jbUpdate.addActionListener( this );
454             jp1.add( jbUpdate );
455
456             add( jp1 );
457         }
458
459         /*
460          * (non-Javadoc)
461          *
462          * @see java.awt.event.ComponentListener#componentHidden(java.awt.event.ComponentEvent)
463          */

464         public void componentHidden( ComponentEvent JavaDoc cev )
465         {
466             setVisible( false );
467         }
468
469         /*
470          * (non-Javadoc)
471          *
472          * @see java.awt.event.ComponentListener#componentMoved(java.awt.event.ComponentEvent)
473          */

474         public void componentMoved( ComponentEvent JavaDoc cev )
475         {
476             JFrame JavaDoc jf = (JFrame JavaDoc) cev.getComponent( );
477             Rectangle JavaDoc r = jf.getBounds( );
478             setLocation( r.x, r.y + r.height );
479             setSize( r.width, 50 );
480         }
481
482         /*
483          * (non-Javadoc)
484          *
485          * @see java.awt.event.ComponentListener#componentResized(java.awt.event.ComponentEvent)
486          */

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

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

514         public void actionPerformed( ActionEvent JavaDoc e )
515         {
516             int i = jcbModels.getSelectedIndex( );
517             cm = null;
518             switch ( i )
519             {
520                 case 0 :
521                     cm = PrimitiveCharts.createBarChart( );
522                     break;
523                 case 1 :
524                     cm = PrimitiveCharts.createMultiBarChart( );
525                     break;
526                 case 2 :
527                     cm = PrimitiveCharts.createPieChart( );
528                     break;
529                 case 3 :
530                     cm = PrimitiveCharts.createMultiPieChart( );
531                     break;
532                 case 4 :
533                     cm = PrimitiveCharts.createLineChart( );
534                     break;
535                 case 5 :
536                     cm = PrimitiveCharts.createStackedChart( );
537                     break;
538                 case 6 :
539                     cm = PrimitiveCharts.createScatterChart( );
540                     break;
541                 case 7 :
542                     cm = PrimitiveCharts.createStockChart( );
543                     break;
544                 case 8 :
545                     cm = PrimitiveCharts.createAreaChart( );
546                     break;
547                 case 9 :
548                     cm = PrimitiveCharts.openChart( );
549                     break;
550             }
551
552             if ( cm instanceof ChartWithAxes )
553             {
554                 jcbTransposed.setEnabled( true );
555                 jcbLogarithmic.setEnabled( true );
556                 jcbPercent.setEnabled( true );
557
558                 ChartWithAxes cwa = ( (ChartWithAxes) cm );
559                 cwa.setTransposed( jcbTransposed.isSelected( ) );
560                 Axis ax = cwa.getPrimaryOrthogonalAxis( cwa.getPrimaryBaseAxes( )[0] );
561
562                 if ( jcbLogarithmic.isSelected( ) )
563                 {
564                     if ( ax.getType( ) == AxisType.LINEAR_LITERAL )
565                     {
566                         ax.setType( AxisType.LOGARITHMIC_LITERAL );
567                     }
568                 }
569                 else
570                 {
571                     if ( ax.getType( ) == AxisType.LOGARITHMIC_LITERAL )
572                     {
573                         ax.setType( AxisType.LINEAR_LITERAL );
574                     }
575                 }
576
577                 if ( jcbPercent.isSelected( ) == true )
578                 {
579                     ax.setFormatSpecifier( JavaNumberFormatSpecifierImpl.create( "0'%'" ) );//$NON-NLS-1$
580
}
581                 else
582                 {
583                     ax.setFormatSpecifier( null );
584                 }
585             }
586             else if ( cm instanceof ChartWithoutAxes )
587             {
588                 jcbTransposed.setEnabled( false );
589                 jcbLogarithmic.setEnabled( false );
590                 jcbPercent.setEnabled( false );
591             }
592
593             if ( jcbModels.getSelectedIndex( ) == 7 )
594             {
595                 cm.setDimension( ChartDimension.TWO_DIMENSIONAL_LITERAL );
596             }
597             else
598             {
599                 switch ( jcbDimensions.getSelectedIndex( ) )
600                 {
601                     case 0 :
602                         cm.setDimension( ChartDimension.TWO_DIMENSIONAL_LITERAL );
603                         break;
604                     case 1 :
605                         cm.setDimension( ChartDimension.TWO_DIMENSIONAL_WITH_DEPTH_LITERAL );
606                         break;
607                 }
608             }
609
610             bNeedsGeneration = true;
611             scv.repaint( );
612         }
613     }
614 }
Popular Tags