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