1 /************************************************************************* 2 * 3 * The Contents of this file are made available subject to the terms of 4 * the BSD license. 5 * 6 * Copyright 2000, 2010 Oracle and/or its affiliates. 7 * All rights reserved. 8 * 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. 20 * 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. 32 * 33 *************************************************************************/ 34 35 import com.sun.star.awt.XMessageBox; 36 import com.sun.star.awt.XWindowPeer; 37 import com.sun.star.frame.XDesktop; 38 import com.sun.star.uno.UnoRuntime; 39 import com.sun.star.lang.*; 40 import com.sun.star.frame.XModel; 41 import com.sun.star.frame.XController; 42 43 // application specific classes 44 import com.sun.star.chart.*; 45 import com.sun.star.uno.XComponentContext; 46 import com.sun.star.uno.XInterface; 47 48 import com.sun.star.view.XSelectionChangeListener; 49 import com.sun.star.view.XSelectionSupplier; 50 51 import com.sun.star.table.CellRangeAddress; 52 import com.sun.star.table.XCellRange; 53 import com.sun.star.sheet.XCellRangeAddressable; 54 55 import com.sun.star.awt.Point; 56 import com.sun.star.awt.Rectangle; 57 import com.sun.star.awt.Size; 58 import com.sun.star.awt.XMessageBoxFactory; 59 import com.sun.star.awt.XWindow; 60 61 // __________ Implementation __________ 62 63 /** Create a spreadsheet add some data. 64 * Create a presentation and add a chart. 65 * Connect the chart to a calc range via a listener 66 * 67 * Note: This example does not work in StarOffice 6.0. It will be available 68 * in the StarOffice Accessibility release. 69 * 70 * @author Björn Milcke 71 */ 72 public class SelectionChangeListener implements XSelectionChangeListener { 73 public static void main( String args[] ) { 74 SelectionChangeListener aMySelf = new SelectionChangeListener( args ); 75 76 aMySelf.run(); 77 } 78 79 public SelectionChangeListener( String args[] ) { 80 Helper aHelper = new Helper( args ); 81 82 maContext = aHelper.getComponentContext(); 83 84 CalcHelper aCalcHelper = new CalcHelper( aHelper.createSpreadsheetDocument() ); 85 86 // insert a cell range with 4 columns and 12 rows filled with random numbers 87 XCellRange aRange = aCalcHelper.insertRandomRange( 4, 12 ); 88 CellRangeAddress aRangeAddress = ((XCellRangeAddressable) UnoRuntime.queryInterface( 89 XCellRangeAddressable.class, aRange)).getRangeAddress(); 90 91 // change view to sheet containing the chart 92 aCalcHelper.raiseChartSheet(); 93 94 // the unit for measures is 1/100th of a millimeter 95 // position at (1cm, 1cm) 96 Point aPos = new Point( 1000, 1000 ); 97 98 // size of the chart is 15cm x 9.271cm 99 Size aExtent = new Size( 15000, 9271 ); 100 101 // insert a new chart into the "Chart" sheet of the 102 // spreadsheet document 103 maChartDocument = aCalcHelper.insertChart( 104 "SampleChart", 105 aRangeAddress, 106 aPos, 107 aExtent, 108 "com.sun.star.chart.XYDiagram" ); 109 } 110 111 // ____________________ 112 113 public void run() { 114 boolean bTrying = true; 115 116 while( bTrying ) { 117 // start listening for selection changes 118 XSelectionSupplier aSelSupp = (XSelectionSupplier) UnoRuntime.queryInterface( 119 XSelectionSupplier.class, 120 (((XModel) UnoRuntime.queryInterface( 121 XModel.class, maChartDocument )).getCurrentController()) ); 122 if( aSelSupp != null ) { 123 aSelSupp.addSelectionChangeListener( this ); 124 System.out.println( "Successfully attached as selection change listener" ); 125 bTrying = false; 126 } 127 128 // start listening for death of Controller 129 XComponent aComp = (XComponent) UnoRuntime.queryInterface( XComponent.class, aSelSupp ); 130 if( aComp != null ) { 131 aComp.addEventListener( this ); 132 System.out.println( "Successfully attached as dispose listener" ); 133 } 134 135 try { 136 Thread.currentThread().sleep( 500 ); 137 } catch( InterruptedException ex ) { 138 } 139 } 140 } 141 142 // ____________________ 143 144 // XEventListener (base of XSelectionChangeListener) 145 public void disposing( EventObject aSourceObj ) { 146 System.out.println( "disposing called. detaching as listener" ); 147 148 // stop listening for selection changes 149 XSelectionSupplier aCtrl = (XSelectionSupplier) UnoRuntime.queryInterface( 150 XSelectionSupplier.class, aSourceObj ); 151 if( aCtrl != null ) 152 aCtrl.removeSelectionChangeListener( this ); 153 154 // remove as dispose listener 155 XComponent aComp = (XComponent) UnoRuntime.queryInterface( XComponent.class, aSourceObj ); 156 if( aComp != null ) 157 aComp.removeEventListener( this ); 158 159 // bail out 160 System.exit( 0 ); 161 } 162 163 // ____________________ 164 165 // XSelectionChangeListener 166 public void selectionChanged( EventObject aEvent ) { 167 XController aCtrl = (XController) UnoRuntime.queryInterface( XController.class, aEvent.Source ); 168 if( aCtrl != null ) { 169 XMultiComponentFactory mMCF = maContext.getServiceManager(); 170 171 MyMessageBox aMsgBox = new MyMessageBox(mMCF); 172 173 aMsgBox.start(); 174 175 System.out.println("Listener finished"); 176 } 177 } 178 179 // __________ private __________ 180 181 private class MyMessageBox extends Thread{ 182 private XMultiComponentFactory mMCF; 183 184 public MyMessageBox(XMultiComponentFactory xMCF){ 185 mMCF = xMCF; 186 } 187 188 public void run() { 189 XDesktop aDesktop = null; 190 XInterface aToolKit = null; 191 try { 192 Thread.sleep(1000); 193 } catch (InterruptedException ex) { 194 ex.printStackTrace(); 195 } 196 try { 197 Object oDesktop = mMCF.createInstanceWithContext("com.sun.star.frame.Desktop", maContext); 198 Object oToolKit = mMCF.createInstanceWithContext("com.sun.star.awt.Toolkit", maContext); 199 200 aDesktop = (XDesktop) UnoRuntime.queryInterface(XDesktop.class, oDesktop); 201 aToolKit = (XInterface) UnoRuntime.queryInterface(XInterface.class, oToolKit); 202 } catch (Exception ex) { 203 ex.printStackTrace(); 204 } 205 206 XWindow xWin = aDesktop.getCurrentFrame().getContainerWindow(); 207 XWindowPeer aWinPeer = (XWindowPeer) UnoRuntime.queryInterface(XWindowPeer.class, xWin); 208 209 Rectangle aRect = new Rectangle(); 210 int button = com.sun.star.awt.MessageBoxButtons.BUTTONS_OK; 211 XMessageBoxFactory aMBF = (XMessageBoxFactory) UnoRuntime.queryInterface(XMessageBoxFactory.class, aToolKit); 212 XMessageBox xMB = aMBF.createMessageBox(aWinPeer, aRect, "infobox" , button, "Event-Notify", "Listener was called, selcetion has changed"); 213 xMB.execute(); 214 } 215 } 216 217 private XChartDocument maChartDocument; 218 private XComponentContext maContext; 219 } 220