summaryrefslogtreecommitdiff
path: root/odk/examples/CLI/CSharp/Spreadsheet/ViewSample.cs
blob: b2795e02f4d7ef6c3c84bcbfec2c36885b5d86e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

using System;
using System.Threading;

// __________  implementation  ____________________________________

/** Create and modify a spreadsheet view.
 */
public class ViewSample : SpreadsheetDocHelper
{

    public static void Main( String [] args )
    {
        try
        {
            using ( ViewSample aSample = new ViewSample( args ) )
            {
                aSample.doSampleFunction();
            }
            Console.WriteLine( "\nSamples done." );
        }
        catch (Exception ex)
        {
            Console.WriteLine( "Sample caught exception! " + ex );
        }
    }

// ________________________________________________________________

    public ViewSample( String[] args )
        : base( args )
    {
    }
    
// ________________________________________________________________

    /** This sample function performs all changes on the view. */
    public void doSampleFunction()
    {
        unoidl.com.sun.star.sheet.XSpreadsheetDocument xDoc = getDocument();
        unoidl.com.sun.star.frame.XModel xModel =
            (unoidl.com.sun.star.frame.XModel) xDoc;
        unoidl.com.sun.star.frame.XController xController =
            xModel.getCurrentController();
 
        // --- Spreadsheet view ---
        // freeze the first column and first two rows
        unoidl.com.sun.star.sheet.XViewFreezable xFreeze =
            (unoidl.com.sun.star.sheet.XViewFreezable) xController;
        if ( null != xFreeze )
            Console.WriteLine( "got xFreeze" );
        xFreeze.freezeAtPosition( 1, 2 );

        // --- View pane ---
        // get the cell range shown in the second pane and assign
        // a cell background to them
        unoidl.com.sun.star.container.XIndexAccess xIndex =
            (unoidl.com.sun.star.container.XIndexAccess) xController;
        uno.Any aPane = xIndex.getByIndex(1);
        unoidl.com.sun.star.sheet.XCellRangeReferrer xRefer =
            (unoidl.com.sun.star.sheet.XCellRangeReferrer) aPane.Value;
        unoidl.com.sun.star.table.XCellRange xRange = xRefer.getReferredCells();
        unoidl.com.sun.star.beans.XPropertySet xRangeProp =
            (unoidl.com.sun.star.beans.XPropertySet) xRange;
        xRangeProp.setPropertyValue(
            "IsCellBackgroundTransparent", new uno.Any( false ) );
        xRangeProp.setPropertyValue(
            "CellBackColor", new uno.Any( (Int32) 0xFFFFCC ) );
 
        // --- View settings ---
        // change the view to display green grid lines
        unoidl.com.sun.star.beans.XPropertySet xProp =
            (unoidl.com.sun.star.beans.XPropertySet) xController;
        xProp.setPropertyValue(
            "ShowGrid", new uno.Any( true ) );
        xProp.setPropertyValue(
            "GridColor", new uno.Any( (Int32) 0x00CC00 ) );

        // --- Range selection ---
        // let the user select a range and use it as the view's selection
        unoidl.com.sun.star.sheet.XRangeSelection xRngSel =
            (unoidl.com.sun.star.sheet.XRangeSelection) xController;
        ExampleRangeListener aListener = new ExampleRangeListener();
        xRngSel.addRangeSelectionListener( aListener );
        unoidl.com.sun.star.beans.PropertyValue[] aArguments =
            new unoidl.com.sun.star.beans.PropertyValue[2];
        aArguments[0] = new unoidl.com.sun.star.beans.PropertyValue();
        aArguments[0].Name   = "Title";
        aArguments[0].Value  = new uno.Any( "Please select a range" );
        aArguments[1] = new unoidl.com.sun.star.beans.PropertyValue();
        aArguments[1].Name   = "CloseOnMouseRelease";
        aArguments[1].Value  = new uno.Any( true );
        xRngSel.startRangeSelection( aArguments );
        Monitor.Enter( aListener );
        try
        {
            Monitor.Wait( aListener );       // wait until the selection is done
        }
        finally
        {
            Monitor.Exit( aListener );
        }
        xRngSel.removeRangeSelectionListener( aListener );
        if ( aListener.aResult != null && aListener.aResult.Length != 0 )
        {
            unoidl.com.sun.star.view.XSelectionSupplier xSel =
                (unoidl.com.sun.star.view.XSelectionSupplier) xController;
            unoidl.com.sun.star.sheet.XSpreadsheetView xView =
                (unoidl.com.sun.star.sheet.XSpreadsheetView) xController;
            unoidl.com.sun.star.sheet.XSpreadsheet xSheet =
                xView.getActiveSheet();
            unoidl.com.sun.star.table.XCellRange xResultRange =
                xSheet.getCellRangeByName( aListener.aResult );
            xSel.select(
                new uno.Any(
                    typeof (unoidl.com.sun.star.table.XCellRange),
                    xResultRange ) );
        }
    }

// ________________________________________________________________

    //  listener to react on finished selection

    private class ExampleRangeListener
        : unoidl.com.sun.star.sheet.XRangeSelectionListener
    {
        public String aResult;

        public void done( unoidl.com.sun.star.sheet.RangeSelectionEvent aEvent )
        {
            aResult = aEvent.RangeDescriptor;
            Monitor.Enter( this );
            try
            {
                Monitor.Pulse( this );
            }
            finally
            {
                Monitor.Exit( this );
            }
        }

        public void aborted(
            unoidl.com.sun.star.sheet.RangeSelectionEvent aEvent )
        {
            Monitor.Enter( this );
            try
            {
                Monitor.Pulse( this );
            }
            finally
            {
                Monitor.Exit( this );
            }
        }

        public void disposing( unoidl.com.sun.star.lang.EventObject aObj )
        {
        }
    }

// ________________________________________________________________

}