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