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