Update ooo320-m1
[ooovba.git] / odk / examples / DevelopersGuide / Charts / SelectionChangeListener.java
blobd62097dee1743207f1c6329c9b0ec15d111278ae
1 /*************************************************************************
3 * $RCSfile: SelectionChangeListener.java,v $
5 * $Revision: 1.4 $
7 * last change: $Author: rt $ $Date: 2007-11-06 15:07:48 $
9 * The Contents of this file are made available subject to the terms of
10 * the BSD license.
12 * Copyright (c) 2003 by Sun Microsystems, Inc.
13 * All rights reserved.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *************************************************************************/
41 import com.sun.star.awt.XMessageBox;
42 import com.sun.star.awt.XWindowPeer;
43 import com.sun.star.frame.XDesktop;
44 import com.sun.star.uno.UnoRuntime;
45 import com.sun.star.lang.*;
46 import com.sun.star.frame.XModel;
47 import com.sun.star.frame.XController;
49 // application specific classes
50 import com.sun.star.chart.*;
51 import com.sun.star.uno.XComponentContext;
52 import com.sun.star.uno.XInterface;
54 import com.sun.star.view.XSelectionChangeListener;
55 import com.sun.star.view.XSelectionSupplier;
57 import com.sun.star.table.CellRangeAddress;
58 import com.sun.star.table.XCellRange;
59 import com.sun.star.sheet.XCellRangeAddressable;
61 import com.sun.star.awt.Point;
62 import com.sun.star.awt.Rectangle;
63 import com.sun.star.awt.Size;
64 import com.sun.star.awt.XMessageBoxFactory;
65 import com.sun.star.awt.XWindow;
67 // __________ Implementation __________
69 /** Create a spreadsheet add some data.
70 * Create a presentation and add a chart.
71 * Connect the chart to a calc range via a listener
73 * Note: This example does not work in StarOffice 6.0. It will be available
74 * in the StarOffice Accessibility release.
76 * @author Björn Milcke
78 public class SelectionChangeListener implements XSelectionChangeListener {
79 public static void main( String args[] ) {
80 SelectionChangeListener aMySelf = new SelectionChangeListener( args );
82 aMySelf.run();
85 public SelectionChangeListener( String args[] ) {
86 Helper aHelper = new Helper( args );
88 maContext = aHelper.getComponentContext();
90 CalcHelper aCalcHelper = new CalcHelper( aHelper.createSpreadsheetDocument() );
92 // insert a cell range with 4 columns and 12 rows filled with random numbers
93 XCellRange aRange = aCalcHelper.insertRandomRange( 4, 12 );
94 CellRangeAddress aRangeAddress = ((XCellRangeAddressable) UnoRuntime.queryInterface(
95 XCellRangeAddressable.class, aRange)).getRangeAddress();
97 // change view to sheet containing the chart
98 aCalcHelper.raiseChartSheet();
100 // the unit for measures is 1/100th of a millimeter
101 // position at (1cm, 1cm)
102 Point aPos = new Point( 1000, 1000 );
104 // size of the chart is 15cm x 9.271cm
105 Size aExtent = new Size( 15000, 9271 );
107 // insert a new chart into the "Chart" sheet of the
108 // spreadsheet document
109 maChartDocument = aCalcHelper.insertChart(
110 "SampleChart",
111 aRangeAddress,
112 aPos,
113 aExtent,
114 "com.sun.star.chart.XYDiagram" );
117 // ____________________
119 public void run() {
120 boolean bTrying = true;
122 while( bTrying ) {
123 // start listening for selection changes
124 XSelectionSupplier aSelSupp = (XSelectionSupplier) UnoRuntime.queryInterface(
125 XSelectionSupplier.class,
126 (((XModel) UnoRuntime.queryInterface(
127 XModel.class, maChartDocument )).getCurrentController()) );
128 if( aSelSupp != null ) {
129 aSelSupp.addSelectionChangeListener( this );
130 System.out.println( "Successfully attached as selection change listener" );
131 bTrying = false;
134 // start listening for death of Controller
135 XComponent aComp = (XComponent) UnoRuntime.queryInterface( XComponent.class, aSelSupp );
136 if( aComp != null ) {
137 aComp.addEventListener( this );
138 System.out.println( "Successfully attached as dispose listener" );
141 try {
142 Thread.currentThread().sleep( 500 );
143 } catch( InterruptedException ex ) {
148 // ____________________
150 // XEventListener (base of XSelectionChangeListener)
151 public void disposing( EventObject aSourceObj ) {
152 System.out.println( "disposing called. detaching as listener" );
154 // stop listening for selection changes
155 XSelectionSupplier aCtrl = (XSelectionSupplier) UnoRuntime.queryInterface(
156 XSelectionSupplier.class, aSourceObj );
157 if( aCtrl != null )
158 aCtrl.removeSelectionChangeListener( this );
160 // remove as dispose listener
161 XComponent aComp = (XComponent) UnoRuntime.queryInterface( XComponent.class, aSourceObj );
162 if( aComp != null )
163 aComp.removeEventListener( this );
165 // bail out
166 System.exit( 0 );
169 // ____________________
171 // XSelectionChangeListener
172 public void selectionChanged( EventObject aEvent ) {
173 XController aCtrl = (XController) UnoRuntime.queryInterface( XController.class, aEvent.Source );
174 if( aCtrl != null ) {
175 XMultiComponentFactory mMCF = maContext.getServiceManager();
177 MyMessageBox aMsgBox = new MyMessageBox(mMCF);
179 aMsgBox.start();
181 System.out.println("Listener finished");
185 // __________ private __________
187 private class MyMessageBox extends Thread{
188 private XMultiComponentFactory mMCF;
190 public MyMessageBox(XMultiComponentFactory xMCF){
191 mMCF = xMCF;
194 public void run() {
195 XDesktop aDesktop = null;
196 XInterface aToolKit = null;
197 try {
198 Thread.sleep(1000);
199 } catch (InterruptedException ex) {
200 ex.printStackTrace();
202 try {
203 Object oDesktop = mMCF.createInstanceWithContext("com.sun.star.frame.Desktop", maContext);
204 Object oToolKit = mMCF.createInstanceWithContext("com.sun.star.awt.Toolkit", maContext);
206 aDesktop = (XDesktop) UnoRuntime.queryInterface(XDesktop.class, oDesktop);
207 aToolKit = (XInterface) UnoRuntime.queryInterface(XInterface.class, oToolKit);
208 } catch (Exception ex) {
209 ex.printStackTrace();
212 XWindow xWin = aDesktop.getCurrentFrame().getContainerWindow();
213 XWindowPeer aWinPeer = (XWindowPeer) UnoRuntime.queryInterface(XWindowPeer.class, xWin);
215 Rectangle aRect = new Rectangle();
216 int button = com.sun.star.awt.MessageBoxButtons.BUTTONS_OK;
217 XMessageBoxFactory aMBF = (XMessageBoxFactory) UnoRuntime.queryInterface(XMessageBoxFactory.class, aToolKit);
218 XMessageBox xMB = aMBF.createMessageBox(aWinPeer, aRect, "infobox" , button, "Event-Notify", "Listener was called, selcetion has changed");
219 xMB.execute();
223 private XChartDocument maChartDocument;
224 private XComponentContext maContext;