Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / odk / examples / DevelopersGuide / Drawing / ControlAndSelectDemo.java
blobb2108db18deb4a2a42ad23ff5f7f0829e6b4f6b4
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
5 * the BSD license.
7 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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 // __________ Imports __________
38 import com.sun.star.uno.UnoRuntime;
39 import com.sun.star.lang.XComponent;
40 import com.sun.star.lang.XMultiServiceFactory;
42 import com.sun.star.awt.Point;
43 import com.sun.star.awt.Size;
44 import com.sun.star.awt.XControlModel;
46 import com.sun.star.beans.PropertyValue;
48 import com.sun.star.drawing.XShape;
49 import com.sun.star.drawing.XShapes;
50 import com.sun.star.drawing.XControlShape;
51 import com.sun.star.drawing.XDrawPage;
52 import com.sun.star.drawing.XDrawPages;
53 import com.sun.star.drawing.XDrawPagesSupplier;
55 import com.sun.star.frame.XModel;
56 import com.sun.star.frame.XController;
58 import com.sun.star.view.XSelectionSupplier;
61 // __________ Implementation __________
63 /** ControlAndSelectDemo
65 A (GroupBox) ControlShape will be created.
66 Finally the ControlShape will be inserted into a selection.
69 public class ControlAndSelectDemo
71 public static void main( String args[] )
73 XComponent xComponent = null;
74 try
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();
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 = Boolean.TRUE;
88 xComponent = Helper.createDocument( xOfficeContext,
89 "private:factory/sdraw", "_blank", 0, pPropValues );
91 XMultiServiceFactory xFactory =
92 UnoRuntime.queryInterface(
93 XMultiServiceFactory.class, xComponent );
95 XDrawPagesSupplier xDrawPagesSupplier =
96 UnoRuntime.queryInterface(
97 XDrawPagesSupplier.class, xComponent );
98 XDrawPages xDrawPages = xDrawPagesSupplier.getDrawPages();
99 XDrawPage xDrawPage = UnoRuntime.queryInterface(
100 XDrawPage.class, xDrawPages.getByIndex( 0 ));
101 XShapes xShapes = UnoRuntime.queryInterface(XShapes.class,
102 xDrawPage );
105 // create and insert the ControlShape
106 Object xObj = xFactory.createInstance(
107 "com.sun.star.drawing.ControlShape" );
108 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 );
113 // create and set the control
114 XControlModel xControlModel = UnoRuntime.queryInterface(
115 XControlModel.class,
116 xFactory.createInstance( "com.sun.star.form.component.GroupBox" ) );
117 XControlShape xControlShape = UnoRuntime.queryInterface(
118 XControlShape.class, xShape );
119 xControlShape.setControl( xControlModel );
122 // the following code will demonstrate how to
123 // make a selection that contains our new created ControlShape
124 XModel xModel = UnoRuntime.queryInterface( XModel.class,
125 xComponent );
126 XController xController = xModel.getCurrentController();
127 XSelectionSupplier xSelectionSupplier =UnoRuntime.queryInterface( XSelectionSupplier.class, xController );
128 // take care to use the global service factory only and not the one
129 // that is provided by the component if you create the ShapeColletion
130 XShapes xSelection = UnoRuntime.queryInterface( XShapes.class,
131 xOfficeContext.getServiceManager().createInstanceWithContext(
132 "com.sun.star.drawing.ShapeCollection", xOfficeContext ) );
133 xSelection.add( xShape );
134 xSelectionSupplier.select( xSelection );
136 catch( java.lang.Exception ex )
138 System.out.println( ex );
140 System.exit( 0 );
144 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */