xref: /AOO41X/main/odk/examples/DevelopersGuide/Spreadsheet/ViewSample.java (revision 34dd1e2512dbacb6a9a7e4c7f17b9296daa8eff3)
1*34dd1e25SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*34dd1e25SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*34dd1e25SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*34dd1e25SAndrew Rist  * distributed with this work for additional information
6*34dd1e25SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*34dd1e25SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*34dd1e25SAndrew Rist  * "License"); you may not use this file except in compliance
9*34dd1e25SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*34dd1e25SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*34dd1e25SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*34dd1e25SAndrew Rist  * software distributed under the License is distributed on an
15*34dd1e25SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*34dd1e25SAndrew Rist  * KIND, either express or implied.  See the License for the
17*34dd1e25SAndrew Rist  * specific language governing permissions and limitations
18*34dd1e25SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*34dd1e25SAndrew Rist  *************************************************************/
21*34dd1e25SAndrew Rist 
22*34dd1e25SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir // __________  implementation  ____________________________________
27cdf0e10cSrcweir 
28cdf0e10cSrcweir /** Create and modify a spreadsheet view.
29cdf0e10cSrcweir  */
30cdf0e10cSrcweir public class ViewSample extends SpreadsheetDocHelper
31cdf0e10cSrcweir {
32cdf0e10cSrcweir 
33cdf0e10cSrcweir // ________________________________________________________________
34cdf0e10cSrcweir 
main( String args[] )35cdf0e10cSrcweir     public static void main( String args[] )
36cdf0e10cSrcweir     {
37cdf0e10cSrcweir         try
38cdf0e10cSrcweir         {
39cdf0e10cSrcweir             ViewSample aSample = new ViewSample( args );
40cdf0e10cSrcweir             aSample.doSampleFunction();
41cdf0e10cSrcweir         }
42cdf0e10cSrcweir         catch (Exception ex)
43cdf0e10cSrcweir         {
44cdf0e10cSrcweir             System.out.println( "Sample caught exception! " + ex );
45cdf0e10cSrcweir             System.exit( 1 );
46cdf0e10cSrcweir         }
47cdf0e10cSrcweir         System.out.println( "\nSamples done." );
48cdf0e10cSrcweir         System.exit( 0 );
49cdf0e10cSrcweir     }
50cdf0e10cSrcweir 
51cdf0e10cSrcweir // ________________________________________________________________
52cdf0e10cSrcweir 
ViewSample( String[] args )53cdf0e10cSrcweir     public ViewSample( String[] args )
54cdf0e10cSrcweir     {
55cdf0e10cSrcweir         super( args );
56cdf0e10cSrcweir     }
57cdf0e10cSrcweir 
58cdf0e10cSrcweir // ________________________________________________________________
59cdf0e10cSrcweir 
60cdf0e10cSrcweir     /** This sample function performs all changes on the view. */
doSampleFunction()61cdf0e10cSrcweir     public void doSampleFunction() throws Exception
62cdf0e10cSrcweir     {
63cdf0e10cSrcweir         com.sun.star.sheet.XSpreadsheetDocument xDoc = getDocument();
64cdf0e10cSrcweir         com.sun.star.frame.XModel xModel = (com.sun.star.frame.XModel)
65cdf0e10cSrcweir             UnoRuntime.queryInterface( com.sun.star.frame.XModel.class, xDoc);
66cdf0e10cSrcweir         com.sun.star.frame.XController xController = xModel.getCurrentController();
67cdf0e10cSrcweir 
68cdf0e10cSrcweir         // --- Spreadsheet view ---
69cdf0e10cSrcweir         // freeze the first column and first two rows
70cdf0e10cSrcweir         com.sun.star.sheet.XViewFreezable xFreeze = (com.sun.star.sheet.XViewFreezable)
71cdf0e10cSrcweir             UnoRuntime.queryInterface( com.sun.star.sheet.XViewFreezable.class, xController );
72cdf0e10cSrcweir         if ( null != xFreeze )
73cdf0e10cSrcweir             System.out.println( "got xFreeze" );
74cdf0e10cSrcweir         xFreeze.freezeAtPosition( 1, 2 );
75cdf0e10cSrcweir 
76cdf0e10cSrcweir         // --- View pane ---
77cdf0e10cSrcweir         // get the cell range shown in the second pane and assign a cell background to them
78cdf0e10cSrcweir         com.sun.star.container.XIndexAccess xIndex = (com.sun.star.container.XIndexAccess)
79cdf0e10cSrcweir             UnoRuntime.queryInterface( com.sun.star.container.XIndexAccess.class, xController );
80cdf0e10cSrcweir         Object aPane = xIndex.getByIndex(1);
81cdf0e10cSrcweir         com.sun.star.sheet.XCellRangeReferrer xRefer = (com.sun.star.sheet.XCellRangeReferrer)
82cdf0e10cSrcweir             UnoRuntime.queryInterface( com.sun.star.sheet.XCellRangeReferrer.class, aPane );
83cdf0e10cSrcweir         com.sun.star.table.XCellRange xRange = xRefer.getReferredCells();
84cdf0e10cSrcweir         com.sun.star.beans.XPropertySet xRangeProp = (com.sun.star.beans.XPropertySet)
85cdf0e10cSrcweir             UnoRuntime.queryInterface( com.sun.star.beans.XPropertySet.class, xRange );
86cdf0e10cSrcweir         xRangeProp.setPropertyValue( "IsCellBackgroundTransparent", new Boolean( false ) );
87cdf0e10cSrcweir         xRangeProp.setPropertyValue( "CellBackColor", new Integer( 0xFFFFCC ) );
88cdf0e10cSrcweir 
89cdf0e10cSrcweir         // --- View settings ---
90cdf0e10cSrcweir         // change the view to display green grid lines
91cdf0e10cSrcweir         com.sun.star.beans.XPropertySet xProp = (com.sun.star.beans.XPropertySet)
92cdf0e10cSrcweir             UnoRuntime.queryInterface( com.sun.star.beans.XPropertySet.class, xController );
93cdf0e10cSrcweir         xProp.setPropertyValue( "ShowGrid", new Boolean(true) );
94cdf0e10cSrcweir         xProp.setPropertyValue( "GridColor", new Integer(0x00CC00) );
95cdf0e10cSrcweir 
96cdf0e10cSrcweir         // --- Range selection ---
97cdf0e10cSrcweir         // let the user select a range and use it as the view's selection
98cdf0e10cSrcweir         com.sun.star.sheet.XRangeSelection xRngSel = (com.sun.star.sheet.XRangeSelection)
99cdf0e10cSrcweir             UnoRuntime.queryInterface( com.sun.star.sheet.XRangeSelection.class, xController );
100cdf0e10cSrcweir         ExampleRangeListener aListener = new ExampleRangeListener();
101cdf0e10cSrcweir         xRngSel.addRangeSelectionListener( aListener );
102cdf0e10cSrcweir         com.sun.star.beans.PropertyValue[] aArguments = new com.sun.star.beans.PropertyValue[2];
103cdf0e10cSrcweir         aArguments[0] = new com.sun.star.beans.PropertyValue();
104cdf0e10cSrcweir         aArguments[0].Name   = "Title";
105cdf0e10cSrcweir         aArguments[0].Value  = "Please select a cell range (e.g. C4:E6)";
106cdf0e10cSrcweir         aArguments[1] = new com.sun.star.beans.PropertyValue();
107cdf0e10cSrcweir         aArguments[1].Name   = "CloseOnMouseRelease";
108cdf0e10cSrcweir         aArguments[1].Value  = new Boolean(true);
109cdf0e10cSrcweir         xRngSel.startRangeSelection( aArguments );
110cdf0e10cSrcweir         synchronized (aListener)
111cdf0e10cSrcweir         {
112cdf0e10cSrcweir             aListener.wait();       // wait until the selection is done
113cdf0e10cSrcweir         }
114cdf0e10cSrcweir         xRngSel.removeRangeSelectionListener( aListener );
115cdf0e10cSrcweir         if ( aListener.aResult != null && aListener.aResult.length() != 0 )
116cdf0e10cSrcweir         {
117cdf0e10cSrcweir             com.sun.star.view.XSelectionSupplier xSel = (com.sun.star.view.XSelectionSupplier)
118cdf0e10cSrcweir                 UnoRuntime.queryInterface( com.sun.star.view.XSelectionSupplier.class, xController );
119cdf0e10cSrcweir             com.sun.star.sheet.XSpreadsheetView xView = (com.sun.star.sheet.XSpreadsheetView)
120cdf0e10cSrcweir                 UnoRuntime.queryInterface( com.sun.star.sheet.XSpreadsheetView.class, xController );
121cdf0e10cSrcweir             com.sun.star.sheet.XSpreadsheet xSheet = xView.getActiveSheet();
122cdf0e10cSrcweir             com.sun.star.table.XCellRange xResultRange = xSheet.getCellRangeByName( aListener.aResult );
123cdf0e10cSrcweir             xSel.select( xResultRange );
124cdf0e10cSrcweir         }
125cdf0e10cSrcweir     }
126cdf0e10cSrcweir 
127cdf0e10cSrcweir // ________________________________________________________________
128cdf0e10cSrcweir 
129cdf0e10cSrcweir     //  listener to react on finished selection
130cdf0e10cSrcweir 
131cdf0e10cSrcweir     private class ExampleRangeListener implements com.sun.star.sheet.XRangeSelectionListener
132cdf0e10cSrcweir     {
133cdf0e10cSrcweir         public String aResult;
134cdf0e10cSrcweir 
done( com.sun.star.sheet.RangeSelectionEvent aEvent )135cdf0e10cSrcweir         public void done( com.sun.star.sheet.RangeSelectionEvent aEvent )
136cdf0e10cSrcweir         {
137cdf0e10cSrcweir             aResult = aEvent.RangeDescriptor;
138cdf0e10cSrcweir             synchronized (this)
139cdf0e10cSrcweir             {
140cdf0e10cSrcweir                 notify();
141cdf0e10cSrcweir             }
142cdf0e10cSrcweir         }
143cdf0e10cSrcweir 
aborted( com.sun.star.sheet.RangeSelectionEvent aEvent )144cdf0e10cSrcweir         public void aborted( com.sun.star.sheet.RangeSelectionEvent aEvent )
145cdf0e10cSrcweir         {
146cdf0e10cSrcweir             synchronized (this)
147cdf0e10cSrcweir             {
148cdf0e10cSrcweir                 notify();
149cdf0e10cSrcweir             }
150cdf0e10cSrcweir         }
151cdf0e10cSrcweir 
disposing( com.sun.star.lang.EventObject aObj )152cdf0e10cSrcweir         public void disposing( com.sun.star.lang.EventObject aObj )
153cdf0e10cSrcweir         {
154cdf0e10cSrcweir         }
155cdf0e10cSrcweir     }
156cdf0e10cSrcweir 
157cdf0e10cSrcweir // ________________________________________________________________
158cdf0e10cSrcweir 
159cdf0e10cSrcweir }
160