1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // __________ Imports __________
25 
26 // base classes
27 import com.sun.star.uno.XInterface;
28 import com.sun.star.uno.UnoRuntime;
29 import com.sun.star.lang.*;
30 
31 // property access
32 import com.sun.star.beans.*;
33 
34 // application specific classes
35 import com.sun.star.chart.*;
36 import com.sun.star.drawing.*;
37 
38 import com.sun.star.table.CellRangeAddress;
39 import com.sun.star.table.XCellRange;
40 import com.sun.star.sheet.XSpreadsheetDocument;
41 
42 import com.sun.star.frame.XModel;
43 import com.sun.star.frame.XController;
44 
45 import com.sun.star.util.XNumberFormatsSupplier;
46 import com.sun.star.util.XNumberFormats;
47 
48 // base graphics things
49 import com.sun.star.awt.Point;
50 import com.sun.star.awt.Size;
51 import com.sun.star.awt.Rectangle;
52 import com.sun.star.awt.FontWeight;
53 import com.sun.star.awt.FontRelief;
54 
55 // Exceptions
56 import com.sun.star.uno.Exception;
57 import com.sun.star.uno.RuntimeException;
58 import com.sun.star.beans.UnknownPropertyException;
59 import com.sun.star.lang.IndexOutOfBoundsException;
60 import com.sun.star.util.MalformedNumberFormatException;
61 
62 
63 // __________ Implementation __________
64 
65 /** Create a spreadsheet add some data.
66     Create a presentation and add a chart.
67     Connect the chart to a calc range via a listener
68     @author Björn Milcke
69  */
70 public class ListenAtCalcRangeInDraw implements XChartDataChangeEventListener
71 {
main( String args[] )72     public static void main( String args[] )
73     {
74         ListenAtCalcRangeInDraw aMySelf = new ListenAtCalcRangeInDraw( args );
75 
76         aMySelf.run();
77     }
78 
ListenAtCalcRangeInDraw( String args[] )79     public ListenAtCalcRangeInDraw( String args[] )
80     {
81         Helper aHelper = new Helper( args );
82 
83         maSheetDoc = aHelper.createSpreadsheetDocument();
84         maDrawDoc  = aHelper.createDrawingDocument();
85         CalcHelper aCalcHelper   = new CalcHelper(  maSheetDoc );
86         ChartHelper aChartHelper = new ChartHelper( maDrawDoc );
87 
88         XCellRange aRange = aCalcHelper.insertFormulaRange( 3, 30 );
89 
90         // the unit for measures is 1/100th of a millimeter
91         // position at (1cm, 1cm)
92         Point aPos    = new Point( 1000, 1000 );
93 
94         // size of the chart is 15cm x 9.271cm
95         Size  aExtent = new Size( 15000, 9271 );
96 
97         // insert a new chart into the "Chart" sheet of the
98         // spreadsheet document
99         maChartDocument = aChartHelper.insertOLEChartInDraw(
100             "ChartWithCalcData",
101             aPos,
102             aExtent,
103             "com.sun.star.chart.XYDiagram" );
104 
105         // attach the data coming from the cell range to the chart
106         maChartData = (XChartData) UnoRuntime.queryInterface( XChartData.class, aRange );
107         maChartDocument.attachData( maChartData );
108     }
109 
110     // ____________________
111 
run()112     public void run()
113     {
114         try
115         {
116             ((XPropertySet) UnoRuntime.queryInterface(
117                 XPropertySet.class, maChartDocument )).setPropertyValue(
118                 "HasSubTitle", new Boolean( true ));
119 
120             // start listening for death of spreadsheet
121             ((XComponent) UnoRuntime.queryInterface(
122                 XComponent.class, maSheetDoc )).addEventListener( this );
123 
124             // start listening for death of chart
125             ((XComponent) UnoRuntime.queryInterface(
126                 XComponent.class, maChartDocument )).addEventListener( this );
127 
128             //start listening for change of data
129             maChartData.addChartDataChangeEventListener( this );
130         }
131         catch( Exception ex )
132         {
133             System.out.println( "Oops: " + ex );
134         }
135 
136         // call listener
137         ChartDataChangeEvent aEvent = new ChartDataChangeEvent();
138         aEvent.Type = ChartDataChangeType.ALL;
139         chartDataChanged( aEvent );
140     }
141 
142     // ____________________
143 
144     // XEventListener (base of XChartDataChangeEventListener)
disposing( EventObject aSourceObj )145     public void disposing( EventObject aSourceObj )
146     {
147         if( UnoRuntime.queryInterface( XChartDocument.class, aSourceObj.Source ) != null )
148             System.out.println( "Disconnecting Listener because Chart was shut down" );
149 
150         if( UnoRuntime.queryInterface( XSpreadsheetDocument.class, aSourceObj.Source ) != null )
151             System.out.println( "Disconnecting Listener because Spreadsheet was shut down" );
152 
153         // remove data change listener
154         maChartData.removeChartDataChangeEventListener( this );
155 
156         // remove dispose listeners
157         ((XComponent) UnoRuntime.queryInterface(
158             XComponent.class, maSheetDoc )).removeEventListener( this );
159         ((XComponent) UnoRuntime.queryInterface(
160             XComponent.class, maChartDocument )).removeEventListener( this );
161 
162         System.exit( 0 );
163     }
164 
165     // ____________________
166 
167     // XChartDataChangeEventListener
chartDataChanged( ChartDataChangeEvent aEvent )168 	public void chartDataChanged( ChartDataChangeEvent aEvent )
169     {
170         // update subtitle
171         String aTitle = new String( "Last Update: " + new java.util.Date( System.currentTimeMillis() ));
172 
173         try
174         {
175             XPropertySet aDocProp = (XPropertySet) UnoRuntime.queryInterface(
176                 XPropertySet.class, maChartDocument );
177             aDocProp.setPropertyValue( "HasMainTitle", new Boolean( true ));
178 
179             ((XPropertySet) UnoRuntime.queryInterface(
180                 XPropertySet.class, maChartDocument.getSubTitle())).setPropertyValue(
181                 "String", aTitle );
182 
183             maChartDocument.attachData( maChartData );
184         }
185         catch( Exception ex )
186         {
187             System.out.println( "Oops: " + ex );
188         }
189 
190         System.out.println( "Data has changed" );
191     }
192 
193 
194     // __________ private __________
195 
196     private XSpreadsheetDocument      maSheetDoc;
197     private XModel                    maDrawDoc;
198     private XChartDocument            maChartDocument;
199     private XChartData                maChartData;
200 }
201