134dd1e25SAndrew Rist /************************************************************** 234dd1e25SAndrew Rist * 334dd1e25SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 434dd1e25SAndrew Rist * or more contributor license agreements. See the NOTICE file 534dd1e25SAndrew Rist * distributed with this work for additional information 634dd1e25SAndrew Rist * regarding copyright ownership. The ASF licenses this file 734dd1e25SAndrew Rist * to you under the Apache License, Version 2.0 (the 834dd1e25SAndrew Rist * "License"); you may not use this file except in compliance 934dd1e25SAndrew Rist * with the License. You may obtain a copy of the License at 1034dd1e25SAndrew Rist * 1134dd1e25SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 1234dd1e25SAndrew Rist * 1334dd1e25SAndrew Rist * Unless required by applicable law or agreed to in writing, 1434dd1e25SAndrew Rist * software distributed under the License is distributed on an 1534dd1e25SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 1634dd1e25SAndrew Rist * KIND, either express or implied. See the License for the 1734dd1e25SAndrew Rist * specific language governing permissions and limitations 1834dd1e25SAndrew Rist * under the License. 1934dd1e25SAndrew Rist * 2034dd1e25SAndrew Rist *************************************************************/ 2134dd1e25SAndrew Rist 2234dd1e25SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir import com.sun.star.awt.XMessageBox; 25cdf0e10cSrcweir import com.sun.star.awt.XWindowPeer; 26cdf0e10cSrcweir import com.sun.star.frame.XDesktop; 27cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 28cdf0e10cSrcweir import com.sun.star.lang.*; 29cdf0e10cSrcweir import com.sun.star.frame.XModel; 30cdf0e10cSrcweir import com.sun.star.frame.XController; 31cdf0e10cSrcweir 32cdf0e10cSrcweir // application specific classes 33cdf0e10cSrcweir import com.sun.star.chart.*; 34cdf0e10cSrcweir import com.sun.star.uno.XComponentContext; 35cdf0e10cSrcweir import com.sun.star.uno.XInterface; 36cdf0e10cSrcweir 37cdf0e10cSrcweir import com.sun.star.view.XSelectionChangeListener; 38cdf0e10cSrcweir import com.sun.star.view.XSelectionSupplier; 39cdf0e10cSrcweir 40cdf0e10cSrcweir import com.sun.star.table.CellRangeAddress; 41cdf0e10cSrcweir import com.sun.star.table.XCellRange; 42cdf0e10cSrcweir import com.sun.star.sheet.XCellRangeAddressable; 43cdf0e10cSrcweir 44cdf0e10cSrcweir import com.sun.star.awt.Point; 45cdf0e10cSrcweir import com.sun.star.awt.Rectangle; 46cdf0e10cSrcweir import com.sun.star.awt.Size; 47cdf0e10cSrcweir import com.sun.star.awt.XMessageBoxFactory; 48*61161268SAriel Constenla-Haile import com.sun.star.awt.MessageBoxType; 49cdf0e10cSrcweir import com.sun.star.awt.XWindow; 50cdf0e10cSrcweir 51cdf0e10cSrcweir // __________ Implementation __________ 52cdf0e10cSrcweir 53cdf0e10cSrcweir /** Create a spreadsheet add some data. 54cdf0e10cSrcweir * Create a presentation and add a chart. 55cdf0e10cSrcweir * Connect the chart to a calc range via a listener 56cdf0e10cSrcweir * 57cdf0e10cSrcweir * Note: This example does not work in StarOffice 6.0. It will be available 58cdf0e10cSrcweir * in the StarOffice Accessibility release. 59cdf0e10cSrcweir * 60cdf0e10cSrcweir * @author Björn Milcke 61cdf0e10cSrcweir */ 62cdf0e10cSrcweir public class SelectionChangeListener implements XSelectionChangeListener { main( String args[] )63cdf0e10cSrcweir public static void main( String args[] ) { 64cdf0e10cSrcweir SelectionChangeListener aMySelf = new SelectionChangeListener( args ); 65cdf0e10cSrcweir 66cdf0e10cSrcweir aMySelf.run(); 67cdf0e10cSrcweir } 68cdf0e10cSrcweir SelectionChangeListener( String args[] )69cdf0e10cSrcweir public SelectionChangeListener( String args[] ) { 70cdf0e10cSrcweir Helper aHelper = new Helper( args ); 71cdf0e10cSrcweir 72cdf0e10cSrcweir maContext = aHelper.getComponentContext(); 73cdf0e10cSrcweir 74cdf0e10cSrcweir CalcHelper aCalcHelper = new CalcHelper( aHelper.createSpreadsheetDocument() ); 75cdf0e10cSrcweir 76cdf0e10cSrcweir // insert a cell range with 4 columns and 12 rows filled with random numbers 77cdf0e10cSrcweir XCellRange aRange = aCalcHelper.insertRandomRange( 4, 12 ); 78cdf0e10cSrcweir CellRangeAddress aRangeAddress = ((XCellRangeAddressable) UnoRuntime.queryInterface( 79cdf0e10cSrcweir XCellRangeAddressable.class, aRange)).getRangeAddress(); 80cdf0e10cSrcweir 81cdf0e10cSrcweir // change view to sheet containing the chart 82cdf0e10cSrcweir aCalcHelper.raiseChartSheet(); 83cdf0e10cSrcweir 84cdf0e10cSrcweir // the unit for measures is 1/100th of a millimeter 85cdf0e10cSrcweir // position at (1cm, 1cm) 86cdf0e10cSrcweir Point aPos = new Point( 1000, 1000 ); 87cdf0e10cSrcweir 88cdf0e10cSrcweir // size of the chart is 15cm x 9.271cm 89cdf0e10cSrcweir Size aExtent = new Size( 15000, 9271 ); 90cdf0e10cSrcweir 91cdf0e10cSrcweir // insert a new chart into the "Chart" sheet of the 92cdf0e10cSrcweir // spreadsheet document 93cdf0e10cSrcweir maChartDocument = aCalcHelper.insertChart( 94cdf0e10cSrcweir "SampleChart", 95cdf0e10cSrcweir aRangeAddress, 96cdf0e10cSrcweir aPos, 97cdf0e10cSrcweir aExtent, 98cdf0e10cSrcweir "com.sun.star.chart.XYDiagram" ); 99cdf0e10cSrcweir } 100cdf0e10cSrcweir 101cdf0e10cSrcweir // ____________________ 102cdf0e10cSrcweir run()103cdf0e10cSrcweir public void run() { 104cdf0e10cSrcweir boolean bTrying = true; 105cdf0e10cSrcweir 106cdf0e10cSrcweir while( bTrying ) { 107cdf0e10cSrcweir // start listening for selection changes 108cdf0e10cSrcweir XSelectionSupplier aSelSupp = (XSelectionSupplier) UnoRuntime.queryInterface( 109cdf0e10cSrcweir XSelectionSupplier.class, 110cdf0e10cSrcweir (((XModel) UnoRuntime.queryInterface( 111cdf0e10cSrcweir XModel.class, maChartDocument )).getCurrentController()) ); 112cdf0e10cSrcweir if( aSelSupp != null ) { 113cdf0e10cSrcweir aSelSupp.addSelectionChangeListener( this ); 114cdf0e10cSrcweir System.out.println( "Successfully attached as selection change listener" ); 115cdf0e10cSrcweir bTrying = false; 116cdf0e10cSrcweir } 117cdf0e10cSrcweir 118cdf0e10cSrcweir // start listening for death of Controller 119cdf0e10cSrcweir XComponent aComp = (XComponent) UnoRuntime.queryInterface( XComponent.class, aSelSupp ); 120cdf0e10cSrcweir if( aComp != null ) { 121cdf0e10cSrcweir aComp.addEventListener( this ); 122cdf0e10cSrcweir System.out.println( "Successfully attached as dispose listener" ); 123cdf0e10cSrcweir } 124cdf0e10cSrcweir 125cdf0e10cSrcweir try { 126cdf0e10cSrcweir Thread.currentThread().sleep( 500 ); 127cdf0e10cSrcweir } catch( InterruptedException ex ) { 128cdf0e10cSrcweir } 129cdf0e10cSrcweir } 130cdf0e10cSrcweir } 131cdf0e10cSrcweir 132cdf0e10cSrcweir // ____________________ 133cdf0e10cSrcweir 134cdf0e10cSrcweir // XEventListener (base of XSelectionChangeListener) disposing( EventObject aSourceObj )135cdf0e10cSrcweir public void disposing( EventObject aSourceObj ) { 136cdf0e10cSrcweir System.out.println( "disposing called. detaching as listener" ); 137cdf0e10cSrcweir 138cdf0e10cSrcweir // stop listening for selection changes 139cdf0e10cSrcweir XSelectionSupplier aCtrl = (XSelectionSupplier) UnoRuntime.queryInterface( 140cdf0e10cSrcweir XSelectionSupplier.class, aSourceObj ); 141cdf0e10cSrcweir if( aCtrl != null ) 142cdf0e10cSrcweir aCtrl.removeSelectionChangeListener( this ); 143cdf0e10cSrcweir 144cdf0e10cSrcweir // remove as dispose listener 145cdf0e10cSrcweir XComponent aComp = (XComponent) UnoRuntime.queryInterface( XComponent.class, aSourceObj ); 146cdf0e10cSrcweir if( aComp != null ) 147cdf0e10cSrcweir aComp.removeEventListener( this ); 148cdf0e10cSrcweir 149cdf0e10cSrcweir // bail out 150cdf0e10cSrcweir System.exit( 0 ); 151cdf0e10cSrcweir } 152cdf0e10cSrcweir 153cdf0e10cSrcweir // ____________________ 154cdf0e10cSrcweir 155cdf0e10cSrcweir // XSelectionChangeListener selectionChanged( EventObject aEvent )156cdf0e10cSrcweir public void selectionChanged( EventObject aEvent ) { 157cdf0e10cSrcweir XController aCtrl = (XController) UnoRuntime.queryInterface( XController.class, aEvent.Source ); 158cdf0e10cSrcweir if( aCtrl != null ) { 159cdf0e10cSrcweir XMultiComponentFactory mMCF = maContext.getServiceManager(); 160cdf0e10cSrcweir 161cdf0e10cSrcweir MyMessageBox aMsgBox = new MyMessageBox(mMCF); 162cdf0e10cSrcweir 163cdf0e10cSrcweir aMsgBox.start(); 164cdf0e10cSrcweir 165cdf0e10cSrcweir System.out.println("Listener finished"); 166cdf0e10cSrcweir } 167cdf0e10cSrcweir } 168cdf0e10cSrcweir 169cdf0e10cSrcweir // __________ private __________ 170cdf0e10cSrcweir 171cdf0e10cSrcweir private class MyMessageBox extends Thread{ 172cdf0e10cSrcweir private XMultiComponentFactory mMCF; 173cdf0e10cSrcweir MyMessageBox(XMultiComponentFactory xMCF)174cdf0e10cSrcweir public MyMessageBox(XMultiComponentFactory xMCF){ 175cdf0e10cSrcweir mMCF = xMCF; 176cdf0e10cSrcweir } 177cdf0e10cSrcweir run()178cdf0e10cSrcweir public void run() { 179cdf0e10cSrcweir XDesktop aDesktop = null; 180cdf0e10cSrcweir XInterface aToolKit = null; 181cdf0e10cSrcweir try { 182cdf0e10cSrcweir Thread.sleep(1000); 183cdf0e10cSrcweir } catch (InterruptedException ex) { 184cdf0e10cSrcweir ex.printStackTrace(); 185cdf0e10cSrcweir } 186cdf0e10cSrcweir try { 187cdf0e10cSrcweir Object oDesktop = mMCF.createInstanceWithContext("com.sun.star.frame.Desktop", maContext); 188cdf0e10cSrcweir Object oToolKit = mMCF.createInstanceWithContext("com.sun.star.awt.Toolkit", maContext); 189cdf0e10cSrcweir 190cdf0e10cSrcweir aDesktop = (XDesktop) UnoRuntime.queryInterface(XDesktop.class, oDesktop); 191cdf0e10cSrcweir aToolKit = (XInterface) UnoRuntime.queryInterface(XInterface.class, oToolKit); 192cdf0e10cSrcweir } catch (Exception ex) { 193cdf0e10cSrcweir ex.printStackTrace(); 194cdf0e10cSrcweir } 195cdf0e10cSrcweir 196cdf0e10cSrcweir XWindow xWin = aDesktop.getCurrentFrame().getContainerWindow(); 197cdf0e10cSrcweir XWindowPeer aWinPeer = (XWindowPeer) UnoRuntime.queryInterface(XWindowPeer.class, xWin); 198*61161268SAriel Constenla-Haile 199cdf0e10cSrcweir int button = com.sun.star.awt.MessageBoxButtons.BUTTONS_OK; 200cdf0e10cSrcweir XMessageBoxFactory aMBF = (XMessageBoxFactory) UnoRuntime.queryInterface(XMessageBoxFactory.class, aToolKit); 201*61161268SAriel Constenla-Haile XMessageBox xMB = aMBF.createMessageBox(aWinPeer, MessageBoxType.INFOBOX, button, "Event-Notify", "Listener was called, selcetion has changed"); 202cdf0e10cSrcweir xMB.execute(); 203cdf0e10cSrcweir } 204cdf0e10cSrcweir } 205cdf0e10cSrcweir 206cdf0e10cSrcweir private XChartDocument maChartDocument; 207cdf0e10cSrcweir private XComponentContext maContext; 208cdf0e10cSrcweir } 209