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