Branch libreoffice-5-0-4
[LibreOffice.git] / odk / examples / DevelopersGuide / Charts / ListenAtCalcRangeInDraw.java
blob36e7b3e4ec9216efe6f5a1faf445382ba6af17db
1 /*************************************************************************
3 * The Contents of this file are made available subject to the terms of
4 * the BSD license.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 *************************************************************************/
35 // __________ Imports __________
37 import com.sun.star.uno.UnoRuntime;
38 import com.sun.star.lang.*;
40 // property access
41 import com.sun.star.beans.*;
43 // application specific classes
44 import com.sun.star.chart.*;
45 import com.sun.star.table.XCellRange;
46 import com.sun.star.sheet.XSpreadsheetDocument;
48 import com.sun.star.frame.XModel;
49 // base graphics things
50 import com.sun.star.awt.Point;
51 import com.sun.star.awt.Size;
52 // Exceptions
53 import com.sun.star.uno.Exception;
56 // __________ Implementation __________
58 /** Create a spreadsheet add some data.
59 Create a presentation and add a chart.
60 Connect the chart to a calc range via a listener
62 public class ListenAtCalcRangeInDraw implements XChartDataChangeEventListener
64 public static void main( String args[] )
66 ListenAtCalcRangeInDraw aMySelf = new ListenAtCalcRangeInDraw( args );
68 aMySelf.run();
71 public ListenAtCalcRangeInDraw( String args[] )
73 Helper aHelper = new Helper( args );
75 maSheetDoc = aHelper.createSpreadsheetDocument();
76 XModel aDrawDoc = aHelper.createDrawingDocument();
77 CalcHelper aCalcHelper = new CalcHelper( maSheetDoc );
78 ChartHelper aChartHelper = new ChartHelper( aDrawDoc );
80 XCellRange aRange = aCalcHelper.insertFormulaRange( 3, 30 );
82 // the unit for measures is 1/100th of a millimeter
83 // position at (1cm, 1cm)
84 Point aPos = new Point( 1000, 1000 );
86 // size of the chart is 15cm x 9.271cm
87 Size aExtent = new Size( 15000, 9271 );
89 // insert a new chart into the "Chart" sheet of the
90 // spreadsheet document
91 maChartDocument = aChartHelper.insertOLEChartInDraw(
92 aPos,
93 aExtent,
94 "com.sun.star.chart.XYDiagram" );
96 // attach the data coming from the cell range to the chart
97 maChartData = UnoRuntime.queryInterface( XChartData.class, aRange );
98 maChartDocument.attachData( maChartData );
103 public void run()
107 UnoRuntime.queryInterface(
108 XPropertySet.class, maChartDocument ).setPropertyValue(
109 "HasSubTitle", Boolean.TRUE);
111 // start listening for death of spreadsheet
112 UnoRuntime.queryInterface(
113 XComponent.class, maSheetDoc ).addEventListener( this );
115 // start listening for death of chart
116 UnoRuntime.queryInterface(
117 XComponent.class, maChartDocument ).addEventListener( this );
119 //start listening for change of data
120 maChartData.addChartDataChangeEventListener( this );
122 catch( Exception ex )
124 System.out.println( "Oops: " + ex );
127 // call listener
128 ChartDataChangeEvent aEvent = new ChartDataChangeEvent();
129 aEvent.Type = ChartDataChangeType.ALL;
130 chartDataChanged( aEvent );
135 // XEventListener (base of XChartDataChangeEventListener)
136 public void disposing( EventObject aSourceObj )
138 if( UnoRuntime.queryInterface( XChartDocument.class, aSourceObj.Source ) != null )
139 System.out.println( "Disconnecting Listener because Chart was shut down" );
141 if( UnoRuntime.queryInterface( XSpreadsheetDocument.class, aSourceObj.Source ) != null )
142 System.out.println( "Disconnecting Listener because Spreadsheet was shut down" );
144 // remove data change listener
145 maChartData.removeChartDataChangeEventListener( this );
147 // remove dispose listeners
148 UnoRuntime.queryInterface(
149 XComponent.class, maSheetDoc ).removeEventListener( this );
150 UnoRuntime.queryInterface(
151 XComponent.class, maChartDocument ).removeEventListener( this );
153 System.exit( 0 );
158 // XChartDataChangeEventListener
159 public void chartDataChanged( ChartDataChangeEvent aEvent )
161 // update subtitle
162 String aTitle = "Last Update: " + new java.util.Date();
166 XPropertySet aDocProp = UnoRuntime.queryInterface(
167 XPropertySet.class, maChartDocument );
168 aDocProp.setPropertyValue( "HasMainTitle", Boolean.TRUE);
170 UnoRuntime.queryInterface(
171 XPropertySet.class, maChartDocument.getSubTitle()).setPropertyValue(
172 "String", aTitle );
174 maChartDocument.attachData( maChartData );
176 catch( Exception ex )
178 System.out.println( "Oops: " + ex );
181 System.out.println( "Data has changed" );
185 // __________ private __________
187 private final XSpreadsheetDocument maSheetDoc;
188 private final XChartDocument maChartDocument;
189 private final XChartData maChartData;