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