Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / odk / examples / DevelopersGuide / Charts / ListenAtCalcRangeInDraw.java
blob76808b4ae971b7f393ca34af5a865f290576eafb
1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * The Contents of this file are made available subject to the terms of
5 * the BSD license.
7 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
29 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
31 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
32 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *************************************************************************/
36 // __________ Imports __________
38 import com.sun.star.uno.UnoRuntime;
39 import com.sun.star.lang.*;
41 // property access
42 import com.sun.star.beans.*;
44 // application specific classes
45 import com.sun.star.chart.*;
46 import com.sun.star.table.XCellRange;
47 import com.sun.star.sheet.XSpreadsheetDocument;
49 import com.sun.star.frame.XModel;
50 // base graphics things
51 import com.sun.star.awt.Point;
52 import com.sun.star.awt.Size;
53 // Exceptions
54 import com.sun.star.uno.Exception;
57 // __________ Implementation __________
59 /** Create a spreadsheet add some data.
60 Create a presentation and add a chart.
61 Connect the chart to a calc range via a listener
63 public class ListenAtCalcRangeInDraw implements XChartDataChangeEventListener
65 public static void main( String args[] )
67 ListenAtCalcRangeInDraw aMySelf = new ListenAtCalcRangeInDraw( args );
69 aMySelf.run();
72 public ListenAtCalcRangeInDraw( String args[] )
74 Helper aHelper = new Helper( args );
76 maSheetDoc = aHelper.createSpreadsheetDocument();
77 XModel aDrawDoc = aHelper.createDrawingDocument();
78 CalcHelper aCalcHelper = new CalcHelper( maSheetDoc );
79 ChartHelper aChartHelper = new ChartHelper( aDrawDoc );
81 XCellRange aRange = aCalcHelper.insertFormulaRange( 3, 30 );
83 // the unit for measures is 1/100th of a millimeter
84 // position at (1cm, 1cm)
85 Point aPos = new Point( 1000, 1000 );
87 // size of the chart is 15cm x 9.271cm
88 Size aExtent = new Size( 15000, 9271 );
90 // insert a new chart into the "Chart" sheet of the
91 // spreadsheet document
92 maChartDocument = aChartHelper.insertOLEChartInDraw(
93 aPos,
94 aExtent,
95 "com.sun.star.chart.XYDiagram" );
97 // attach the data coming from the cell range to the chart
98 maChartData = UnoRuntime.queryInterface( XChartData.class, aRange );
99 maChartDocument.attachData( maChartData );
104 public void run()
108 UnoRuntime.queryInterface(
109 XPropertySet.class, maChartDocument ).setPropertyValue(
110 "HasSubTitle", Boolean.TRUE);
112 // start listening for death of spreadsheet
113 UnoRuntime.queryInterface(
114 XComponent.class, maSheetDoc ).addEventListener( this );
116 // start listening for death of chart
117 UnoRuntime.queryInterface(
118 XComponent.class, maChartDocument ).addEventListener( this );
120 //start listening for change of data
121 maChartData.addChartDataChangeEventListener( this );
123 catch( Exception ex )
125 System.out.println( "Oops: " + ex );
128 // call listener
129 ChartDataChangeEvent aEvent = new ChartDataChangeEvent();
130 aEvent.Type = ChartDataChangeType.ALL;
131 chartDataChanged( aEvent );
136 // XEventListener (base of XChartDataChangeEventListener)
137 public void disposing( EventObject aSourceObj )
139 if( UnoRuntime.queryInterface( XChartDocument.class, aSourceObj.Source ) != null )
140 System.out.println( "Disconnecting Listener because Chart was shut down" );
142 if( UnoRuntime.queryInterface( XSpreadsheetDocument.class, aSourceObj.Source ) != null )
143 System.out.println( "Disconnecting Listener because Spreadsheet was shut down" );
145 // remove data change listener
146 maChartData.removeChartDataChangeEventListener( this );
148 // remove dispose listeners
149 UnoRuntime.queryInterface(
150 XComponent.class, maSheetDoc ).removeEventListener( this );
151 UnoRuntime.queryInterface(
152 XComponent.class, maChartDocument ).removeEventListener( this );
154 System.exit( 0 );
159 // XChartDataChangeEventListener
160 public void chartDataChanged( ChartDataChangeEvent aEvent )
162 // update subtitle
163 String aTitle = "Last Update: " + new java.util.Date();
167 XPropertySet aDocProp = UnoRuntime.queryInterface(
168 XPropertySet.class, maChartDocument );
169 aDocProp.setPropertyValue( "HasMainTitle", Boolean.TRUE);
171 UnoRuntime.queryInterface(
172 XPropertySet.class, maChartDocument.getSubTitle()).setPropertyValue(
173 "String", aTitle );
175 maChartDocument.attachData( maChartData );
177 catch( Exception ex )
179 System.out.println( "Oops: " + ex );
182 System.out.println( "Data has changed" );
186 // __________ private __________
188 private final XSpreadsheetDocument maSheetDoc;
189 private final XChartDocument maChartDocument;
190 private final XChartData maChartData;
193 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */