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
7 * Copyright 2000, 2010 Oracle and/or its affiliates.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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 import com
.sun
.star
.awt
.XMessageBox
;
37 import com
.sun
.star
.awt
.XWindowPeer
;
38 import com
.sun
.star
.frame
.XDesktop
;
39 import com
.sun
.star
.uno
.UnoRuntime
;
40 import com
.sun
.star
.lang
.*;
41 import com
.sun
.star
.frame
.XModel
;
42 import com
.sun
.star
.frame
.XController
;
44 // application specific classes
45 import com
.sun
.star
.chart
.*;
46 import com
.sun
.star
.uno
.XComponentContext
;
47 import com
.sun
.star
.uno
.XInterface
;
49 import com
.sun
.star
.view
.XSelectionChangeListener
;
50 import com
.sun
.star
.view
.XSelectionSupplier
;
52 import com
.sun
.star
.table
.CellRangeAddress
;
53 import com
.sun
.star
.table
.XCellRange
;
54 import com
.sun
.star
.sheet
.XCellRangeAddressable
;
56 import com
.sun
.star
.awt
.Point
;
57 import com
.sun
.star
.awt
.Size
;
58 import com
.sun
.star
.awt
.XMessageBoxFactory
;
59 import com
.sun
.star
.awt
.MessageBoxType
;
60 import com
.sun
.star
.awt
.XWindow
;
62 // __________ Implementation __________
64 /** Create a spreadsheet add some data.
65 * Create a presentation and add a chart.
66 * Connect the chart to a calc range via a listener
68 * Note: This example does not work in StarOffice 6.0. It will be available
69 * in the StarOffice Accessibility release.
71 public class SelectionChangeListener
implements XSelectionChangeListener
{
72 public static void main( String args
[] ) {
73 SelectionChangeListener aMySelf
= new SelectionChangeListener( args
);
78 public SelectionChangeListener( String args
[] ) {
79 Helper aHelper
= new Helper( args
);
81 maContext
= aHelper
.getComponentContext();
83 CalcHelper aCalcHelper
= new CalcHelper( aHelper
.createSpreadsheetDocument() );
85 // insert a cell range with 4 columns and 12 rows filled with random numbers
86 XCellRange aRange
= aCalcHelper
.insertRandomRange( 4, 12 );
87 CellRangeAddress aRangeAddress
= UnoRuntime
.queryInterface(
88 XCellRangeAddressable
.class, aRange
).getRangeAddress();
90 // change view to sheet containing the chart
91 aCalcHelper
.raiseChartSheet();
93 // the unit for measures is 1/100th of a millimeter
94 // position at (1cm, 1cm)
95 Point aPos
= new Point( 1000, 1000 );
97 // size of the chart is 15cm x 9.271cm
98 Size aExtent
= new Size( 15000, 9271 );
100 // insert a new chart into the "Chart" sheet of the
101 // spreadsheet document
102 maChartDocument
= aCalcHelper
.insertChart(
107 "com.sun.star.chart.XYDiagram" );
113 boolean bTrying
= true;
116 // start listening for selection changes
117 XSelectionSupplier aSelSupp
= UnoRuntime
.queryInterface(
118 XSelectionSupplier
.class,
119 (UnoRuntime
.queryInterface(
120 XModel
.class, maChartDocument
).getCurrentController()) );
121 if( aSelSupp
!= null ) {
122 aSelSupp
.addSelectionChangeListener( this );
123 System
.out
.println( "Successfully attached as selection change listener" );
127 // start listening for death of Controller
128 XComponent aComp
= UnoRuntime
.queryInterface( XComponent
.class, aSelSupp
);
129 if( aComp
!= null ) {
130 aComp
.addEventListener( this );
131 System
.out
.println( "Successfully attached as dispose listener" );
136 } catch( InterruptedException ex
) {
143 // XEventListener (base of XSelectionChangeListener)
144 public void disposing( EventObject aSourceObj
) {
145 System
.out
.println( "disposing called. detaching as listener" );
147 // stop listening for selection changes
148 XSelectionSupplier aCtrl
= UnoRuntime
.queryInterface(
149 XSelectionSupplier
.class, aSourceObj
);
151 aCtrl
.removeSelectionChangeListener( this );
153 // remove as dispose listener
154 XComponent aComp
= UnoRuntime
.queryInterface( XComponent
.class, aSourceObj
);
156 aComp
.removeEventListener( this );
164 // XSelectionChangeListener
165 public void selectionChanged( EventObject aEvent
) {
166 XController aCtrl
= UnoRuntime
.queryInterface( XController
.class, aEvent
.Source
);
167 if( aCtrl
!= null ) {
168 XMultiComponentFactory mMCF
= maContext
.getServiceManager();
170 MyMessageBox aMsgBox
= new MyMessageBox(mMCF
);
174 System
.out
.println("Listener finished");
178 // __________ private __________
180 private class MyMessageBox
extends Thread
{
181 private final XMultiComponentFactory mMCF
;
183 public MyMessageBox(XMultiComponentFactory xMCF
){
189 XDesktop aDesktop
= null;
190 XInterface aToolKit
= null;
193 } catch (InterruptedException ex
) {
194 ex
.printStackTrace();
197 Object oDesktop
= mMCF
.createInstanceWithContext("com.sun.star.frame.Desktop", maContext
);
198 Object oToolKit
= mMCF
.createInstanceWithContext("com.sun.star.awt.Toolkit", maContext
);
200 aDesktop
= UnoRuntime
.queryInterface(XDesktop
.class, oDesktop
);
201 aToolKit
= UnoRuntime
.queryInterface(XInterface
.class, oToolKit
);
202 } catch (Exception ex
) {
203 ex
.printStackTrace();
206 XWindow xWin
= aDesktop
.getCurrentFrame().getContainerWindow();
207 XWindowPeer aWinPeer
= UnoRuntime
.queryInterface(XWindowPeer
.class, xWin
);
209 int button
= com
.sun
.star
.awt
.MessageBoxButtons
.BUTTONS_OK
;
210 XMessageBoxFactory aMBF
= UnoRuntime
.queryInterface(XMessageBoxFactory
.class, aToolKit
);
211 XMessageBox xMB
= aMBF
.createMessageBox(aWinPeer
, MessageBoxType
.INFOBOX
, button
, "Event-Notify", "Listener was called, selection has changed");
216 private final XChartDocument maChartDocument
;
217 private final XComponentContext maContext
;
220 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */