1 /*************************************************************************
2  *
3  *  The Contents of this file are made available subject to the terms of
4  *  the BSD license.
5  *
6  *  Copyright 2000, 2010 Oracle and/or its affiliates.
7  *  All rights reserved.
8  *
9  *  Redistribution and use in source and binary forms, with or without
10  *  modification, are permitted provided that the following conditions
11  *  are met:
12  *  1. Redistributions of source code must retain the above copyright
13  *     notice, this list of conditions and the following disclaimer.
14  *  2. Redistributions in binary form must reproduce the above copyright
15  *     notice, this list of conditions and the following disclaimer in the
16  *     documentation and/or other materials provided with the distribution.
17  *  3. Neither the name of Sun Microsystems, Inc. nor the names of its
18  *     contributors may be used to endorse or promote products derived
19  *     from this software without specific prior written permission.
20  *
21  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28  *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29  *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30  *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31  *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  *************************************************************************/
34 
35 import com.sun.star.uno.UnoRuntime;
36 
37 // __________  implementation  ____________________________________
38 
39 /** Create and modify a spreadsheet view.
40  */
41 public class ViewSample extends SpreadsheetDocHelper
42 {
43 
44 // ________________________________________________________________
45 
46     public static void main( String args[] )
47     {
48         try
49         {
50             ViewSample aSample = new ViewSample( args );
51             aSample.doSampleFunction();
52         }
53         catch (Exception ex)
54         {
55             System.out.println( "Sample caught exception! " + ex );
56             System.exit( 1 );
57         }
58         System.out.println( "\nSamples done." );
59         System.exit( 0 );
60     }
61 
62 // ________________________________________________________________
63 
64     public ViewSample( String[] args )
65     {
66         super( args );
67     }
68 
69 // ________________________________________________________________
70 
71     /** This sample function performs all changes on the view. */
72     public void doSampleFunction() throws Exception
73     {
74         com.sun.star.sheet.XSpreadsheetDocument xDoc = getDocument();
75         com.sun.star.frame.XModel xModel = (com.sun.star.frame.XModel)
76             UnoRuntime.queryInterface( com.sun.star.frame.XModel.class, xDoc);
77         com.sun.star.frame.XController xController = xModel.getCurrentController();
78 
79         // --- Spreadsheet view ---
80         // freeze the first column and first two rows
81         com.sun.star.sheet.XViewFreezable xFreeze = (com.sun.star.sheet.XViewFreezable)
82             UnoRuntime.queryInterface( com.sun.star.sheet.XViewFreezable.class, xController );
83         if ( null != xFreeze )
84             System.out.println( "got xFreeze" );
85         xFreeze.freezeAtPosition( 1, 2 );
86 
87         // --- View pane ---
88         // get the cell range shown in the second pane and assign a cell background to them
89         com.sun.star.container.XIndexAccess xIndex = (com.sun.star.container.XIndexAccess)
90             UnoRuntime.queryInterface( com.sun.star.container.XIndexAccess.class, xController );
91         Object aPane = xIndex.getByIndex(1);
92         com.sun.star.sheet.XCellRangeReferrer xRefer = (com.sun.star.sheet.XCellRangeReferrer)
93             UnoRuntime.queryInterface( com.sun.star.sheet.XCellRangeReferrer.class, aPane );
94         com.sun.star.table.XCellRange xRange = xRefer.getReferredCells();
95         com.sun.star.beans.XPropertySet xRangeProp = (com.sun.star.beans.XPropertySet)
96             UnoRuntime.queryInterface( com.sun.star.beans.XPropertySet.class, xRange );
97         xRangeProp.setPropertyValue( "IsCellBackgroundTransparent", new Boolean( false ) );
98         xRangeProp.setPropertyValue( "CellBackColor", new Integer( 0xFFFFCC ) );
99 
100         // --- View settings ---
101         // change the view to display green grid lines
102         com.sun.star.beans.XPropertySet xProp = (com.sun.star.beans.XPropertySet)
103             UnoRuntime.queryInterface( com.sun.star.beans.XPropertySet.class, xController );
104         xProp.setPropertyValue( "ShowGrid", new Boolean(true) );
105         xProp.setPropertyValue( "GridColor", new Integer(0x00CC00) );
106 
107         // --- Range selection ---
108         // let the user select a range and use it as the view's selection
109         com.sun.star.sheet.XRangeSelection xRngSel = (com.sun.star.sheet.XRangeSelection)
110             UnoRuntime.queryInterface( com.sun.star.sheet.XRangeSelection.class, xController );
111         ExampleRangeListener aListener = new ExampleRangeListener();
112         xRngSel.addRangeSelectionListener( aListener );
113         com.sun.star.beans.PropertyValue[] aArguments = new com.sun.star.beans.PropertyValue[2];
114         aArguments[0] = new com.sun.star.beans.PropertyValue();
115         aArguments[0].Name   = "Title";
116         aArguments[0].Value  = "Please select a cell range (e.g. C4:E6)";
117         aArguments[1] = new com.sun.star.beans.PropertyValue();
118         aArguments[1].Name   = "CloseOnMouseRelease";
119         aArguments[1].Value  = new Boolean(true);
120         xRngSel.startRangeSelection( aArguments );
121         synchronized (aListener)
122         {
123             aListener.wait();       // wait until the selection is done
124         }
125         xRngSel.removeRangeSelectionListener( aListener );
126         if ( aListener.aResult != null && aListener.aResult.length() != 0 )
127         {
128             com.sun.star.view.XSelectionSupplier xSel = (com.sun.star.view.XSelectionSupplier)
129                 UnoRuntime.queryInterface( com.sun.star.view.XSelectionSupplier.class, xController );
130             com.sun.star.sheet.XSpreadsheetView xView = (com.sun.star.sheet.XSpreadsheetView)
131                 UnoRuntime.queryInterface( com.sun.star.sheet.XSpreadsheetView.class, xController );
132             com.sun.star.sheet.XSpreadsheet xSheet = xView.getActiveSheet();
133             com.sun.star.table.XCellRange xResultRange = xSheet.getCellRangeByName( aListener.aResult );
134             xSel.select( xResultRange );
135         }
136     }
137 
138 // ________________________________________________________________
139 
140     //  listener to react on finished selection
141 
142     private class ExampleRangeListener implements com.sun.star.sheet.XRangeSelectionListener
143     {
144         public String aResult;
145 
146         public void done( com.sun.star.sheet.RangeSelectionEvent aEvent )
147         {
148             aResult = aEvent.RangeDescriptor;
149             synchronized (this)
150             {
151                 notify();
152             }
153         }
154 
155         public void aborted( com.sun.star.sheet.RangeSelectionEvent aEvent )
156         {
157             synchronized (this)
158             {
159                 notify();
160             }
161         }
162 
163         public void disposing( com.sun.star.lang.EventObject aObj )
164         {
165         }
166     }
167 
168 // ________________________________________________________________
169 
170 }
171