1 /*************************************************************************
3 * $RCSfile: ControlAndSelectDemo.java,v $
7 * last change: $Author: rt $ $Date: 2005-01-31 16:21:17 $
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 // __________ Imports __________
43 import com
.sun
.star
.uno
.UnoRuntime
;
44 import com
.sun
.star
.lang
.XComponent
;
45 import com
.sun
.star
.lang
.XMultiServiceFactory
;
47 import com
.sun
.star
.awt
.Point
;
48 import com
.sun
.star
.awt
.Size
;
49 import com
.sun
.star
.awt
.XControlModel
;
51 import com
.sun
.star
.beans
.PropertyValue
;
53 import com
.sun
.star
.drawing
.XShape
;
54 import com
.sun
.star
.drawing
.XShapes
;
55 import com
.sun
.star
.drawing
.XControlShape
;
56 import com
.sun
.star
.drawing
.XDrawPage
;
57 import com
.sun
.star
.drawing
.XDrawPages
;
58 import com
.sun
.star
.drawing
.XDrawPagesSupplier
;
60 import com
.sun
.star
.frame
.XModel
;
61 import com
.sun
.star
.frame
.XController
;
63 import com
.sun
.star
.view
.XSelectionSupplier
;
66 // __________ Implementation __________
68 /** ControlAndSelectDemo
71 A (GroupBox) ControlShape will be created.
72 Finally the ControlShape will be inserted into a selection.
75 public class ControlAndSelectDemo
77 public static void main( String args
[] )
79 XComponent xComponent
= null;
82 // get the remote office context of a running office (a new office
83 // instance is started if necessary)
84 com
.sun
.star
.uno
.XComponentContext xOfficeContext
= Helper
.connect();
86 // suppress Presentation Autopilot when opening the document
87 // properties are the same as described for
88 // com.sun.star.document.MediaDescriptor
89 PropertyValue
[] pPropValues
= new PropertyValue
[ 1 ];
90 pPropValues
[ 0 ] = new PropertyValue();
91 pPropValues
[ 0 ].Name
= "Silent";
92 pPropValues
[ 0 ].Value
= new Boolean( true );
94 xComponent
= Helper
.createDocument( xOfficeContext
,
95 "private:factory/sdraw", "_blank", 0, pPropValues
);
97 XMultiServiceFactory xFactory
=
98 (XMultiServiceFactory
)UnoRuntime
.queryInterface(
99 XMultiServiceFactory
.class, xComponent
);
101 XDrawPagesSupplier xDrawPagesSupplier
=
102 (XDrawPagesSupplier
)UnoRuntime
.queryInterface(
103 XDrawPagesSupplier
.class, xComponent
);
104 XDrawPages xDrawPages
= xDrawPagesSupplier
.getDrawPages();
105 XDrawPage xDrawPage
= (XDrawPage
)UnoRuntime
.queryInterface(
106 XDrawPage
.class, xDrawPages
.getByIndex( 0 ));
107 XShapes xShapes
= (XShapes
)UnoRuntime
.queryInterface(XShapes
.class,
111 // create and insert the ControlShape
112 Object xObj
= xFactory
.createInstance(
113 "com.sun.star.drawing.ControlShape" );
114 XShape xShape
= (XShape
)UnoRuntime
.queryInterface( XShape
.class, xObj
);
115 xShape
.setPosition( new Point( 1000, 1000 ) );
116 xShape
.setSize( new Size( 2000, 2000 ) );
117 xShapes
.add( xShape
);
119 // create and set the control
120 XControlModel xControlModel
= (XControlModel
)UnoRuntime
.queryInterface(
122 xFactory
.createInstance( "com.sun.star.form.component.GroupBox" ) );
123 XControlShape xControlShape
= (XControlShape
)UnoRuntime
.queryInterface(
124 XControlShape
.class, xShape
);
125 xControlShape
.setControl( xControlModel
);
128 // the following code will demonstrate how to
129 // make a selection that contains our new created ControlShape
130 XModel xModel
= (XModel
)UnoRuntime
.queryInterface( XModel
.class,
132 XController xController
= xModel
.getCurrentController();
133 XSelectionSupplier xSelectionSupplier
=(XSelectionSupplier
)
134 UnoRuntime
.queryInterface( XSelectionSupplier
.class, xController
);
135 // take care to use the global service factory only and not the one
136 // that is provided by the component if you create the ShapeColletion
137 XShapes xSelection
= (XShapes
)UnoRuntime
.queryInterface( XShapes
.class,
138 xOfficeContext
.getServiceManager().createInstanceWithContext(
139 "com.sun.star.drawing.ShapeCollection", xOfficeContext
) );
140 xSelection
.add( xShape
);
141 xSelectionSupplier
.select( xSelection
);
143 catch( java
.lang
.Exception ex
)
145 System
.out
.println( ex
);