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