KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > birt > chart > examples > api > format > FormatChartsViewer


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.format;
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 FormatChartsViewer 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         FormatChartsViewer fcv = new FormatChartsViewer( );
79
80         JFrame JavaDoc jf = new JFrame JavaDoc( );
81         jf.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
82         jf.addComponentListener( fcv );
83
84         Container JavaDoc co = jf.getContentPane( );
85         co.setLayout( new BorderLayout JavaDoc( ) );
86         co.add( fcv, 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( fcv.getClass( ).getName( ) + " [device=" //$NON-NLS-1$
95
+ fcv.idr.getClass( ).getName( ) + "]" );//$NON-NLS-1$
96

97         ControlPanel cp = fcv.new ControlPanel( fcv );
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     FormatChartsViewer( )
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 = FormatCharts.createAxisFormatChart( );
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
385         private static final long serialVersionUID = 1L;
386
387         private JComboBox JavaDoc jcbModels = null;
388
389         private JButton JavaDoc jbUpdate = null;
390
391         private final FormatChartsViewer fcv;
392
393         ControlPanel( FormatChartsViewer fcv )
394         {
395             this.fcv = fcv;
396
397             setLayout( new GridLayout JavaDoc( 0, 1, 0, 0 ) );
398
399             JPanel JavaDoc jp = new JPanel JavaDoc( );
400             jp.setLayout( new FlowLayout JavaDoc( FlowLayout.LEFT, 3, 3 ) );
401
402             jp.add( new JLabel JavaDoc( "Choose:" ) );//$NON-NLS-1$
403
jcbModels = new JComboBox JavaDoc( );
404
405             jcbModels.addItem( "Axis Format" );//$NON-NLS-1$
406
jcbModels.addItem( "Colored By Category" );//$NON-NLS-1$
407
jcbModels.addItem( "Legend & Title Format" );//$NON-NLS-1$
408
jcbModels.addItem( "Percentage Values" );//$NON-NLS-1$
409
jcbModels.addItem( "Plot Format" );//$NON-NLS-1$
410
jcbModels.addItem( "Series Format" );//$NON-NLS-1$
411

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

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

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

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

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

477         public void actionPerformed( ActionEvent JavaDoc e )
478         {
479             int i = jcbModels.getSelectedIndex( );
480             cm = null;
481             switch ( i )
482             {
483                 case 0 :
484                     cm = FormatCharts.createAxisFormatChart( );
485                     break;
486                 case 1 :
487                     cm = FormatCharts.createColoredByCategoryChart( );
488                     break;
489                 case 2 :
490                     cm = FormatCharts.createLegendTitleChart( );
491                     break;
492                 case 3 :
493                     cm = FormatCharts.createPercentageValueChart( );
494                     break;
495                 case 4 :
496                     cm = FormatCharts.createPlotFormatChart( );
497                     break;
498                 case 5 :
499                     cm = FormatCharts.createSeriesFormatChart( );
500                     break;
501             }
502
503             bNeedsGeneration = true;
504             fcv.repaint( );
505         }
506     }
507 }
508
Popular Tags