1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * The Contents of this file are made available subject to the terms of
7 * Copyright 2000, 2010 Oracle and/or its affiliates.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
29 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
31 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
32 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *************************************************************************/
36 import com
.sun
.star
.uno
.UnoRuntime
;
38 // __________ implementation ____________________________________
40 /** Create and modify a spreadsheet view.
42 public class ViewSample
extends SpreadsheetDocHelper
47 public static void main( String args
[] )
51 ViewSample aSample
= new ViewSample( args
);
52 aSample
.doSampleFunction();
56 System
.out
.println( "Sample caught exception! " + ex
);
59 System
.out
.println( "\nSamples done." );
65 public ViewSample( String
[] args
)
72 /** This sample function performs all changes on the view. */
73 public void doSampleFunction() throws Exception
75 com
.sun
.star
.sheet
.XSpreadsheetDocument xDoc
= getDocument();
76 com
.sun
.star
.frame
.XModel xModel
= UnoRuntime
.queryInterface( com
.sun
.star
.frame
.XModel
.class, xDoc
);
77 com
.sun
.star
.frame
.XController xController
= xModel
.getCurrentController();
79 // --- Spreadsheet view ---
80 // freeze the first column and first two rows
81 com
.sun
.star
.sheet
.XViewFreezable xFreeze
= UnoRuntime
.queryInterface( com
.sun
.star
.sheet
.XViewFreezable
.class, xController
);
82 if ( null != xFreeze
)
83 System
.out
.println( "got xFreeze" );
84 xFreeze
.freezeAtPosition( 1, 2 );
87 // get the cell range shown in the second pane and assign a cell background to them
88 com
.sun
.star
.container
.XIndexAccess xIndex
= UnoRuntime
.queryInterface( com
.sun
.star
.container
.XIndexAccess
.class, xController
);
89 Object aPane
= xIndex
.getByIndex(1);
90 com
.sun
.star
.sheet
.XCellRangeReferrer xRefer
= UnoRuntime
.queryInterface( com
.sun
.star
.sheet
.XCellRangeReferrer
.class, aPane
);
91 com
.sun
.star
.table
.XCellRange xRange
= xRefer
.getReferredCells();
92 com
.sun
.star
.beans
.XPropertySet xRangeProp
= UnoRuntime
.queryInterface( com
.sun
.star
.beans
.XPropertySet
.class, xRange
);
93 xRangeProp
.setPropertyValue( "IsCellBackgroundTransparent", Boolean
.FALSE
);
94 xRangeProp
.setPropertyValue( "CellBackColor", Integer
.valueOf( 0xFFFFCC ) );
96 // --- View settings ---
97 // change the view to display green grid lines
98 com
.sun
.star
.beans
.XPropertySet xProp
= UnoRuntime
.queryInterface( com
.sun
.star
.beans
.XPropertySet
.class, xController
);
99 xProp
.setPropertyValue( "ShowGrid", Boolean
.TRUE
);
100 xProp
.setPropertyValue( "GridColor", Integer
.valueOf(0x00CC00) );
102 // --- Range selection ---
103 // let the user select a range and use it as the view's selection
104 com
.sun
.star
.sheet
.XRangeSelection xRngSel
= UnoRuntime
.queryInterface( com
.sun
.star
.sheet
.XRangeSelection
.class, xController
);
105 ExampleRangeListener aListener
= new ExampleRangeListener();
106 xRngSel
.addRangeSelectionListener( aListener
);
107 com
.sun
.star
.beans
.PropertyValue
[] aArguments
= new com
.sun
.star
.beans
.PropertyValue
[2];
108 aArguments
[0] = new com
.sun
.star
.beans
.PropertyValue();
109 aArguments
[0].Name
= "Title";
110 aArguments
[0].Value
= "Please select a cell range (e.g. C4:E6)";
111 aArguments
[1] = new com
.sun
.star
.beans
.PropertyValue();
112 aArguments
[1].Name
= "CloseOnMouseRelease";
113 aArguments
[1].Value
= Boolean
.TRUE
;
114 xRngSel
.startRangeSelection( aArguments
);
115 synchronized (aListener
)
117 aListener
.wait(); // wait until the selection is done
119 xRngSel
.removeRangeSelectionListener( aListener
);
120 if ( aListener
.aResult
!= null && aListener
.aResult
.length() != 0 )
122 com
.sun
.star
.view
.XSelectionSupplier xSel
= UnoRuntime
.queryInterface( com
.sun
.star
.view
.XSelectionSupplier
.class, xController
);
123 com
.sun
.star
.sheet
.XSpreadsheetView xView
= UnoRuntime
.queryInterface( com
.sun
.star
.sheet
.XSpreadsheetView
.class, xController
);
124 com
.sun
.star
.sheet
.XSpreadsheet xSheet
= xView
.getActiveSheet();
125 com
.sun
.star
.table
.XCellRange xResultRange
= xSheet
.getCellRangeByName( aListener
.aResult
);
126 xSel
.select( xResultRange
);
132 // listener to react on finished selection
134 private class ExampleRangeListener
implements com
.sun
.star
.sheet
.XRangeSelectionListener
136 public String aResult
;
138 public void done( com
.sun
.star
.sheet
.RangeSelectionEvent aEvent
)
140 aResult
= aEvent
.RangeDescriptor
;
147 public void aborted( com
.sun
.star
.sheet
.RangeSelectionEvent aEvent
)
155 public void disposing( com
.sun
.star
.lang
.EventObject aObj
)
164 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */