xref: /AOO41X/main/odk/examples/DevelopersGuide/Spreadsheet/ViewSample.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  *  The Contents of this file are made available subject to the terms of
4*cdf0e10cSrcweir  *  the BSD license.
5*cdf0e10cSrcweir  *
6*cdf0e10cSrcweir  *  Copyright 2000, 2010 Oracle and/or its affiliates.
7*cdf0e10cSrcweir  *  All rights reserved.
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  *  Redistribution and use in source and binary forms, with or without
10*cdf0e10cSrcweir  *  modification, are permitted provided that the following conditions
11*cdf0e10cSrcweir  *  are met:
12*cdf0e10cSrcweir  *  1. Redistributions of source code must retain the above copyright
13*cdf0e10cSrcweir  *     notice, this list of conditions and the following disclaimer.
14*cdf0e10cSrcweir  *  2. Redistributions in binary form must reproduce the above copyright
15*cdf0e10cSrcweir  *     notice, this list of conditions and the following disclaimer in the
16*cdf0e10cSrcweir  *     documentation and/or other materials provided with the distribution.
17*cdf0e10cSrcweir  *  3. Neither the name of Sun Microsystems, Inc. nor the names of its
18*cdf0e10cSrcweir  *     contributors may be used to endorse or promote products derived
19*cdf0e10cSrcweir  *     from this software without specific prior written permission.
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22*cdf0e10cSrcweir  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23*cdf0e10cSrcweir  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24*cdf0e10cSrcweir  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25*cdf0e10cSrcweir  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26*cdf0e10cSrcweir  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27*cdf0e10cSrcweir  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28*cdf0e10cSrcweir  *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29*cdf0e10cSrcweir  *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30*cdf0e10cSrcweir  *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31*cdf0e10cSrcweir  *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*cdf0e10cSrcweir  *
33*cdf0e10cSrcweir  *************************************************************************/
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir // __________  implementation  ____________________________________
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir /** Create and modify a spreadsheet view.
40*cdf0e10cSrcweir  */
41*cdf0e10cSrcweir public class ViewSample extends SpreadsheetDocHelper
42*cdf0e10cSrcweir {
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir // ________________________________________________________________
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir     public static void main( String args[] )
47*cdf0e10cSrcweir     {
48*cdf0e10cSrcweir         try
49*cdf0e10cSrcweir         {
50*cdf0e10cSrcweir             ViewSample aSample = new ViewSample( args );
51*cdf0e10cSrcweir             aSample.doSampleFunction();
52*cdf0e10cSrcweir         }
53*cdf0e10cSrcweir         catch (Exception ex)
54*cdf0e10cSrcweir         {
55*cdf0e10cSrcweir             System.out.println( "Sample caught exception! " + ex );
56*cdf0e10cSrcweir             System.exit( 1 );
57*cdf0e10cSrcweir         }
58*cdf0e10cSrcweir         System.out.println( "\nSamples done." );
59*cdf0e10cSrcweir         System.exit( 0 );
60*cdf0e10cSrcweir     }
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir // ________________________________________________________________
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir     public ViewSample( String[] args )
65*cdf0e10cSrcweir     {
66*cdf0e10cSrcweir         super( args );
67*cdf0e10cSrcweir     }
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir // ________________________________________________________________
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir     /** This sample function performs all changes on the view. */
72*cdf0e10cSrcweir     public void doSampleFunction() throws Exception
73*cdf0e10cSrcweir     {
74*cdf0e10cSrcweir         com.sun.star.sheet.XSpreadsheetDocument xDoc = getDocument();
75*cdf0e10cSrcweir         com.sun.star.frame.XModel xModel = (com.sun.star.frame.XModel)
76*cdf0e10cSrcweir             UnoRuntime.queryInterface( com.sun.star.frame.XModel.class, xDoc);
77*cdf0e10cSrcweir         com.sun.star.frame.XController xController = xModel.getCurrentController();
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir         // --- Spreadsheet view ---
80*cdf0e10cSrcweir         // freeze the first column and first two rows
81*cdf0e10cSrcweir         com.sun.star.sheet.XViewFreezable xFreeze = (com.sun.star.sheet.XViewFreezable)
82*cdf0e10cSrcweir             UnoRuntime.queryInterface( com.sun.star.sheet.XViewFreezable.class, xController );
83*cdf0e10cSrcweir         if ( null != xFreeze )
84*cdf0e10cSrcweir             System.out.println( "got xFreeze" );
85*cdf0e10cSrcweir         xFreeze.freezeAtPosition( 1, 2 );
86*cdf0e10cSrcweir 
87*cdf0e10cSrcweir         // --- View pane ---
88*cdf0e10cSrcweir         // get the cell range shown in the second pane and assign a cell background to them
89*cdf0e10cSrcweir         com.sun.star.container.XIndexAccess xIndex = (com.sun.star.container.XIndexAccess)
90*cdf0e10cSrcweir             UnoRuntime.queryInterface( com.sun.star.container.XIndexAccess.class, xController );
91*cdf0e10cSrcweir         Object aPane = xIndex.getByIndex(1);
92*cdf0e10cSrcweir         com.sun.star.sheet.XCellRangeReferrer xRefer = (com.sun.star.sheet.XCellRangeReferrer)
93*cdf0e10cSrcweir             UnoRuntime.queryInterface( com.sun.star.sheet.XCellRangeReferrer.class, aPane );
94*cdf0e10cSrcweir         com.sun.star.table.XCellRange xRange = xRefer.getReferredCells();
95*cdf0e10cSrcweir         com.sun.star.beans.XPropertySet xRangeProp = (com.sun.star.beans.XPropertySet)
96*cdf0e10cSrcweir             UnoRuntime.queryInterface( com.sun.star.beans.XPropertySet.class, xRange );
97*cdf0e10cSrcweir         xRangeProp.setPropertyValue( "IsCellBackgroundTransparent", new Boolean( false ) );
98*cdf0e10cSrcweir         xRangeProp.setPropertyValue( "CellBackColor", new Integer( 0xFFFFCC ) );
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir         // --- View settings ---
101*cdf0e10cSrcweir         // change the view to display green grid lines
102*cdf0e10cSrcweir         com.sun.star.beans.XPropertySet xProp = (com.sun.star.beans.XPropertySet)
103*cdf0e10cSrcweir             UnoRuntime.queryInterface( com.sun.star.beans.XPropertySet.class, xController );
104*cdf0e10cSrcweir         xProp.setPropertyValue( "ShowGrid", new Boolean(true) );
105*cdf0e10cSrcweir         xProp.setPropertyValue( "GridColor", new Integer(0x00CC00) );
106*cdf0e10cSrcweir 
107*cdf0e10cSrcweir         // --- Range selection ---
108*cdf0e10cSrcweir         // let the user select a range and use it as the view's selection
109*cdf0e10cSrcweir         com.sun.star.sheet.XRangeSelection xRngSel = (com.sun.star.sheet.XRangeSelection)
110*cdf0e10cSrcweir             UnoRuntime.queryInterface( com.sun.star.sheet.XRangeSelection.class, xController );
111*cdf0e10cSrcweir         ExampleRangeListener aListener = new ExampleRangeListener();
112*cdf0e10cSrcweir         xRngSel.addRangeSelectionListener( aListener );
113*cdf0e10cSrcweir         com.sun.star.beans.PropertyValue[] aArguments = new com.sun.star.beans.PropertyValue[2];
114*cdf0e10cSrcweir         aArguments[0] = new com.sun.star.beans.PropertyValue();
115*cdf0e10cSrcweir         aArguments[0].Name   = "Title";
116*cdf0e10cSrcweir         aArguments[0].Value  = "Please select a cell range (e.g. C4:E6)";
117*cdf0e10cSrcweir         aArguments[1] = new com.sun.star.beans.PropertyValue();
118*cdf0e10cSrcweir         aArguments[1].Name   = "CloseOnMouseRelease";
119*cdf0e10cSrcweir         aArguments[1].Value  = new Boolean(true);
120*cdf0e10cSrcweir         xRngSel.startRangeSelection( aArguments );
121*cdf0e10cSrcweir         synchronized (aListener)
122*cdf0e10cSrcweir         {
123*cdf0e10cSrcweir             aListener.wait();       // wait until the selection is done
124*cdf0e10cSrcweir         }
125*cdf0e10cSrcweir         xRngSel.removeRangeSelectionListener( aListener );
126*cdf0e10cSrcweir         if ( aListener.aResult != null && aListener.aResult.length() != 0 )
127*cdf0e10cSrcweir         {
128*cdf0e10cSrcweir             com.sun.star.view.XSelectionSupplier xSel = (com.sun.star.view.XSelectionSupplier)
129*cdf0e10cSrcweir                 UnoRuntime.queryInterface( com.sun.star.view.XSelectionSupplier.class, xController );
130*cdf0e10cSrcweir             com.sun.star.sheet.XSpreadsheetView xView = (com.sun.star.sheet.XSpreadsheetView)
131*cdf0e10cSrcweir                 UnoRuntime.queryInterface( com.sun.star.sheet.XSpreadsheetView.class, xController );
132*cdf0e10cSrcweir             com.sun.star.sheet.XSpreadsheet xSheet = xView.getActiveSheet();
133*cdf0e10cSrcweir             com.sun.star.table.XCellRange xResultRange = xSheet.getCellRangeByName( aListener.aResult );
134*cdf0e10cSrcweir             xSel.select( xResultRange );
135*cdf0e10cSrcweir         }
136*cdf0e10cSrcweir     }
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir // ________________________________________________________________
139*cdf0e10cSrcweir 
140*cdf0e10cSrcweir     //  listener to react on finished selection
141*cdf0e10cSrcweir 
142*cdf0e10cSrcweir     private class ExampleRangeListener implements com.sun.star.sheet.XRangeSelectionListener
143*cdf0e10cSrcweir     {
144*cdf0e10cSrcweir         public String aResult;
145*cdf0e10cSrcweir 
146*cdf0e10cSrcweir         public void done( com.sun.star.sheet.RangeSelectionEvent aEvent )
147*cdf0e10cSrcweir         {
148*cdf0e10cSrcweir             aResult = aEvent.RangeDescriptor;
149*cdf0e10cSrcweir             synchronized (this)
150*cdf0e10cSrcweir             {
151*cdf0e10cSrcweir                 notify();
152*cdf0e10cSrcweir             }
153*cdf0e10cSrcweir         }
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir         public void aborted( com.sun.star.sheet.RangeSelectionEvent aEvent )
156*cdf0e10cSrcweir         {
157*cdf0e10cSrcweir             synchronized (this)
158*cdf0e10cSrcweir             {
159*cdf0e10cSrcweir                 notify();
160*cdf0e10cSrcweir             }
161*cdf0e10cSrcweir         }
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir         public void disposing( com.sun.star.lang.EventObject aObj )
164*cdf0e10cSrcweir         {
165*cdf0e10cSrcweir         }
166*cdf0e10cSrcweir     }
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir // ________________________________________________________________
169*cdf0e10cSrcweir 
170*cdf0e10cSrcweir }
171