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