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.uno.UnoRuntime;
25 
26 // __________  implementation  ____________________________________
27 
28 /** Create and modify a spreadsheet view.
29  */
30 public class ViewSample extends SpreadsheetDocHelper
31 {
32 
33 // ________________________________________________________________
34 
main( String args[] )35     public static void main( String args[] )
36     {
37         try
38         {
39             ViewSample aSample = new ViewSample( args );
40             aSample.doSampleFunction();
41         }
42         catch (Exception ex)
43         {
44             System.out.println( "Sample caught exception! " + ex );
45             System.exit( 1 );
46         }
47         System.out.println( "\nSamples done." );
48         System.exit( 0 );
49     }
50 
51 // ________________________________________________________________
52 
ViewSample( String[] args )53     public ViewSample( String[] args )
54     {
55         super( args );
56     }
57 
58 // ________________________________________________________________
59 
60     /** This sample function performs all changes on the view. */
doSampleFunction()61     public void doSampleFunction() throws Exception
62     {
63         com.sun.star.sheet.XSpreadsheetDocument xDoc = getDocument();
64         com.sun.star.frame.XModel xModel = (com.sun.star.frame.XModel)
65             UnoRuntime.queryInterface( com.sun.star.frame.XModel.class, xDoc);
66         com.sun.star.frame.XController xController = xModel.getCurrentController();
67 
68         // --- Spreadsheet view ---
69         // freeze the first column and first two rows
70         com.sun.star.sheet.XViewFreezable xFreeze = (com.sun.star.sheet.XViewFreezable)
71             UnoRuntime.queryInterface( com.sun.star.sheet.XViewFreezable.class, xController );
72         if ( null != xFreeze )
73             System.out.println( "got xFreeze" );
74         xFreeze.freezeAtPosition( 1, 2 );
75 
76         // --- View pane ---
77         // get the cell range shown in the second pane and assign a cell background to them
78         com.sun.star.container.XIndexAccess xIndex = (com.sun.star.container.XIndexAccess)
79             UnoRuntime.queryInterface( com.sun.star.container.XIndexAccess.class, xController );
80         Object aPane = xIndex.getByIndex(1);
81         com.sun.star.sheet.XCellRangeReferrer xRefer = (com.sun.star.sheet.XCellRangeReferrer)
82             UnoRuntime.queryInterface( com.sun.star.sheet.XCellRangeReferrer.class, aPane );
83         com.sun.star.table.XCellRange xRange = xRefer.getReferredCells();
84         com.sun.star.beans.XPropertySet xRangeProp = (com.sun.star.beans.XPropertySet)
85             UnoRuntime.queryInterface( com.sun.star.beans.XPropertySet.class, xRange );
86         xRangeProp.setPropertyValue( "IsCellBackgroundTransparent", new Boolean( false ) );
87         xRangeProp.setPropertyValue( "CellBackColor", new Integer( 0xFFFFCC ) );
88 
89         // --- View settings ---
90         // change the view to display green grid lines
91         com.sun.star.beans.XPropertySet xProp = (com.sun.star.beans.XPropertySet)
92             UnoRuntime.queryInterface( com.sun.star.beans.XPropertySet.class, xController );
93         xProp.setPropertyValue( "ShowGrid", new Boolean(true) );
94         xProp.setPropertyValue( "GridColor", new Integer(0x00CC00) );
95 
96         // --- Range selection ---
97         // let the user select a range and use it as the view's selection
98         com.sun.star.sheet.XRangeSelection xRngSel = (com.sun.star.sheet.XRangeSelection)
99             UnoRuntime.queryInterface( com.sun.star.sheet.XRangeSelection.class, xController );
100         ExampleRangeListener aListener = new ExampleRangeListener();
101         xRngSel.addRangeSelectionListener( aListener );
102         com.sun.star.beans.PropertyValue[] aArguments = new com.sun.star.beans.PropertyValue[2];
103         aArguments[0] = new com.sun.star.beans.PropertyValue();
104         aArguments[0].Name   = "Title";
105         aArguments[0].Value  = "Please select a cell range (e.g. C4:E6)";
106         aArguments[1] = new com.sun.star.beans.PropertyValue();
107         aArguments[1].Name   = "CloseOnMouseRelease";
108         aArguments[1].Value  = new Boolean(true);
109         xRngSel.startRangeSelection( aArguments );
110         synchronized (aListener)
111         {
112             aListener.wait();       // wait until the selection is done
113         }
114         xRngSel.removeRangeSelectionListener( aListener );
115         if ( aListener.aResult != null && aListener.aResult.length() != 0 )
116         {
117             com.sun.star.view.XSelectionSupplier xSel = (com.sun.star.view.XSelectionSupplier)
118                 UnoRuntime.queryInterface( com.sun.star.view.XSelectionSupplier.class, xController );
119             com.sun.star.sheet.XSpreadsheetView xView = (com.sun.star.sheet.XSpreadsheetView)
120                 UnoRuntime.queryInterface( com.sun.star.sheet.XSpreadsheetView.class, xController );
121             com.sun.star.sheet.XSpreadsheet xSheet = xView.getActiveSheet();
122             com.sun.star.table.XCellRange xResultRange = xSheet.getCellRangeByName( aListener.aResult );
123             xSel.select( xResultRange );
124         }
125     }
126 
127 // ________________________________________________________________
128 
129     //  listener to react on finished selection
130 
131     private class ExampleRangeListener implements com.sun.star.sheet.XRangeSelectionListener
132     {
133         public String aResult;
134 
done( com.sun.star.sheet.RangeSelectionEvent aEvent )135         public void done( com.sun.star.sheet.RangeSelectionEvent aEvent )
136         {
137             aResult = aEvent.RangeDescriptor;
138             synchronized (this)
139             {
140                 notify();
141             }
142         }
143 
aborted( com.sun.star.sheet.RangeSelectionEvent aEvent )144         public void aborted( com.sun.star.sheet.RangeSelectionEvent aEvent )
145         {
146             synchronized (this)
147             {
148                 notify();
149             }
150         }
151 
disposing( com.sun.star.lang.EventObject aObj )152         public void disposing( com.sun.star.lang.EventObject aObj )
153         {
154         }
155     }
156 
157 // ________________________________________________________________
158 
159 }
160