1*34dd1e25SAndrew Rist /**************************************************************
2*34dd1e25SAndrew Rist  *
3*34dd1e25SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*34dd1e25SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*34dd1e25SAndrew Rist  * distributed with this work for additional information
6*34dd1e25SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*34dd1e25SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*34dd1e25SAndrew Rist  * "License"); you may not use this file except in compliance
9*34dd1e25SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*34dd1e25SAndrew Rist  *
11*34dd1e25SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*34dd1e25SAndrew Rist  *
13*34dd1e25SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*34dd1e25SAndrew Rist  * software distributed under the License is distributed on an
15*34dd1e25SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*34dd1e25SAndrew Rist  * KIND, either express or implied.  See the License for the
17*34dd1e25SAndrew Rist  * specific language governing permissions and limitations
18*34dd1e25SAndrew Rist  * under the License.
19*34dd1e25SAndrew Rist  *
20*34dd1e25SAndrew Rist  *************************************************************/
21*34dd1e25SAndrew Rist 
22*34dd1e25SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // __________ Imports __________
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
27cdf0e10cSrcweir import com.sun.star.lang.XComponent;
28cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
29cdf0e10cSrcweir 
30cdf0e10cSrcweir import com.sun.star.awt.Point;
31cdf0e10cSrcweir import com.sun.star.awt.Size;
32cdf0e10cSrcweir import com.sun.star.awt.XControlModel;
33cdf0e10cSrcweir 
34cdf0e10cSrcweir import com.sun.star.beans.PropertyValue;
35cdf0e10cSrcweir 
36cdf0e10cSrcweir import com.sun.star.drawing.XShape;
37cdf0e10cSrcweir import com.sun.star.drawing.XShapes;
38cdf0e10cSrcweir import com.sun.star.drawing.XControlShape;
39cdf0e10cSrcweir import com.sun.star.drawing.XDrawPage;
40cdf0e10cSrcweir import com.sun.star.drawing.XDrawPages;
41cdf0e10cSrcweir import com.sun.star.drawing.XDrawPagesSupplier;
42cdf0e10cSrcweir 
43cdf0e10cSrcweir import com.sun.star.frame.XModel;
44cdf0e10cSrcweir import com.sun.star.frame.XController;
45cdf0e10cSrcweir 
46cdf0e10cSrcweir import com.sun.star.view.XSelectionSupplier;
47cdf0e10cSrcweir 
48cdf0e10cSrcweir 
49cdf0e10cSrcweir // __________ Implementation __________
50cdf0e10cSrcweir 
51cdf0e10cSrcweir /** ControlAndSelectDemo
52cdf0e10cSrcweir     @author Sven Jacobi
53cdf0e10cSrcweir 
54cdf0e10cSrcweir    A (GroupBox) ControlShape will be created.
55cdf0e10cSrcweir    Finally the ControlShape will be inserted into a selection.
56cdf0e10cSrcweir */
57cdf0e10cSrcweir 
58cdf0e10cSrcweir public class ControlAndSelectDemo
59cdf0e10cSrcweir {
main( String args[] )60cdf0e10cSrcweir     public static void main( String args[] )
61cdf0e10cSrcweir     {
62cdf0e10cSrcweir 		XComponent xComponent = null;
63cdf0e10cSrcweir 		try
64cdf0e10cSrcweir 		{
65cdf0e10cSrcweir             // get the remote office context of a running office (a new office
66cdf0e10cSrcweir             // instance is started if necessary)
67cdf0e10cSrcweir 			com.sun.star.uno.XComponentContext xOfficeContext = Helper.connect();
68cdf0e10cSrcweir 
69cdf0e10cSrcweir 			// suppress Presentation Autopilot when opening the document
70cdf0e10cSrcweir 			// properties are the same as described for
71cdf0e10cSrcweir 			// com.sun.star.document.MediaDescriptor
72cdf0e10cSrcweir 			PropertyValue[] pPropValues = new PropertyValue[ 1 ];
73cdf0e10cSrcweir 			pPropValues[ 0 ] = new PropertyValue();
74cdf0e10cSrcweir 			pPropValues[ 0 ].Name = "Silent";
75cdf0e10cSrcweir 			pPropValues[ 0 ].Value = new Boolean( true );
76cdf0e10cSrcweir 
77cdf0e10cSrcweir 			xComponent = Helper.createDocument( xOfficeContext,
78cdf0e10cSrcweir 				"private:factory/sdraw", "_blank", 0, pPropValues );
79cdf0e10cSrcweir 
80cdf0e10cSrcweir 			XMultiServiceFactory xFactory =
81cdf0e10cSrcweir 				(XMultiServiceFactory )UnoRuntime.queryInterface(
82cdf0e10cSrcweir 					XMultiServiceFactory.class, xComponent );
83cdf0e10cSrcweir 
84cdf0e10cSrcweir 			XDrawPagesSupplier xDrawPagesSupplier =
85cdf0e10cSrcweir 				(XDrawPagesSupplier)UnoRuntime.queryInterface(
86cdf0e10cSrcweir 					XDrawPagesSupplier.class, xComponent );
87cdf0e10cSrcweir 			XDrawPages xDrawPages = xDrawPagesSupplier.getDrawPages();
88cdf0e10cSrcweir 			XDrawPage xDrawPage = (XDrawPage)UnoRuntime.queryInterface(
89cdf0e10cSrcweir                 XDrawPage.class, xDrawPages.getByIndex( 0 ));
90cdf0e10cSrcweir 			XShapes xShapes = (XShapes)UnoRuntime.queryInterface(XShapes.class,
91cdf0e10cSrcweir                                                                  xDrawPage );
92cdf0e10cSrcweir 
93cdf0e10cSrcweir 
94cdf0e10cSrcweir 			// create and insert the ControlShape
95cdf0e10cSrcweir 			Object xObj = xFactory.createInstance(
96cdf0e10cSrcweir                 "com.sun.star.drawing.ControlShape" );
97cdf0e10cSrcweir 			XShape xShape = (XShape)UnoRuntime.queryInterface( XShape.class, xObj );
98cdf0e10cSrcweir 			xShape.setPosition( new Point( 1000, 1000 ) );
99cdf0e10cSrcweir 			xShape.setSize( new Size( 2000, 2000 ) );
100cdf0e10cSrcweir 			xShapes.add( xShape );
101cdf0e10cSrcweir 
102cdf0e10cSrcweir 			// create and set the control
103cdf0e10cSrcweir 			XControlModel xControlModel = (XControlModel)UnoRuntime.queryInterface(
104cdf0e10cSrcweir                 XControlModel.class,
105cdf0e10cSrcweir 				xFactory.createInstance( "com.sun.star.form.component.GroupBox" ) );
106cdf0e10cSrcweir 			XControlShape xControlShape = (XControlShape)UnoRuntime.queryInterface(
107cdf0e10cSrcweir                 XControlShape.class, xShape );
108cdf0e10cSrcweir 			xControlShape.setControl( xControlModel );
109cdf0e10cSrcweir 
110cdf0e10cSrcweir 
111cdf0e10cSrcweir 			// the following code will demonstrate how to
112cdf0e10cSrcweir 			// make a selection that contains our new created ControlShape
113cdf0e10cSrcweir 			XModel xModel = (XModel)UnoRuntime.queryInterface( XModel.class,
114cdf0e10cSrcweir                                                                xComponent );
115cdf0e10cSrcweir 			XController xController = xModel.getCurrentController();
116cdf0e10cSrcweir 			XSelectionSupplier xSelectionSupplier =(XSelectionSupplier)
117cdf0e10cSrcweir 				UnoRuntime.queryInterface( XSelectionSupplier.class, xController );
118cdf0e10cSrcweir 			// take care to use the global service factory only and not the one
119cdf0e10cSrcweir 			// that is provided by the component if you create the ShapeColletion
120cdf0e10cSrcweir 			XShapes xSelection = (XShapes)UnoRuntime.queryInterface( XShapes.class,
121cdf0e10cSrcweir 				xOfficeContext.getServiceManager().createInstanceWithContext(
122cdf0e10cSrcweir                     "com.sun.star.drawing.ShapeCollection", xOfficeContext ) );
123cdf0e10cSrcweir 			xSelection.add( xShape );
124cdf0e10cSrcweir 			xSelectionSupplier.select( xSelection );
125cdf0e10cSrcweir 		}
126cdf0e10cSrcweir 		catch( java.lang.Exception ex )
127cdf0e10cSrcweir 		{
128cdf0e10cSrcweir 			System.out.println( ex );
129cdf0e10cSrcweir 		}
130cdf0e10cSrcweir 		System.exit( 0 );
131cdf0e10cSrcweir     }
132cdf0e10cSrcweir }
133