Branch libreoffice-5-0-4
[LibreOffice.git] / odk / examples / DevelopersGuide / Drawing / GluePointDemo.java
blob6f75c921473a6ae768da67bdc2c85058e961f9f5
1 /*************************************************************************
3 * The Contents of this file are made available subject to the terms of
4 * the BSD license.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * All rights reserved.
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.
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.
33 *************************************************************************/
35 // __________ Imports __________
37 import com.sun.star.uno.UnoRuntime;
38 import com.sun.star.lang.XComponent;
40 import com.sun.star.awt.Point;
41 import com.sun.star.awt.Size;
43 import com.sun.star.beans.PropertyValue;
44 import com.sun.star.beans.XPropertySet;
46 import com.sun.star.container.XIndexContainer;
47 import com.sun.star.container.XIdentifierContainer;
49 import com.sun.star.drawing.Alignment;
50 import com.sun.star.drawing.EscapeDirection;
51 import com.sun.star.drawing.GluePoint2;
52 import com.sun.star.drawing.XShape;
53 import com.sun.star.drawing.XShapes;
54 import com.sun.star.drawing.XDrawPage;
55 import com.sun.star.drawing.XGluePointsSupplier;
59 // __________ Implementation __________
61 // GluePointDemo
63 public class GluePointDemo
65 public static void main( String args[] )
67 XComponent xDrawDoc = null;
68 try
70 // get the remote office context of a running office (a new office
71 // instance is started if necessary)
72 com.sun.star.uno.XComponentContext xOfficeContext = Helper.connect();
74 // suppress Presentation Autopilot when opening the document
75 // properties are the same as described for
76 // com.sun.star.document.MediaDescriptor
77 PropertyValue[] pPropValues = new PropertyValue[ 1 ];
78 pPropValues[ 0 ] = new PropertyValue();
79 pPropValues[ 0 ].Name = "Silent";
80 pPropValues[ 0 ].Value = Boolean.TRUE;
82 xDrawDoc = Helper.createDocument( xOfficeContext,
83 "private:factory/sdraw", "_blank", 0, pPropValues );
86 XDrawPage xPage = PageHelper.getDrawPageByIndex( xDrawDoc, 0 );
87 XShapes xShapes = UnoRuntime.queryInterface( XShapes.class, xPage );
89 // create two rectangles
90 XShape xShape1 = ShapeHelper.createShape( xDrawDoc,
91 new Point( 15000, 1000 ), new Size( 5000, 5000 ),
92 "com.sun.star.drawing.RectangleShape" );
94 XShape xShape2 = ShapeHelper.createShape( xDrawDoc,
95 new Point( 2000, 15000 ), new Size( 5000, 5000 ),
96 "com.sun.star.drawing.EllipseShape" );
98 // and a connector
99 XShape xConnector = ShapeHelper.createShape( xDrawDoc,
100 new Point( 0, 0 ),
101 new Size( 0, 0 ),
102 "com.sun.star.drawing.ConnectorShape" );
104 xShapes.add( xShape1 );
105 xShapes.add( xShape2 );
106 xShapes.add( xConnector );
108 XPropertySet xConnectorPropSet = UnoRuntime.queryInterface( XPropertySet.class, xConnector );
110 // Index value of 0 : the shape is connected at the top
111 // Index value of 1 : the shape is connected at the left
112 // Index value of 2 : the shape is connected at the bottom
113 // Index value of 3 : the shape is connected at the right
115 int nStartIndex = 3;
116 int nEndIndex = 1;
118 // the "StartPosition" or "EndPosition" property needs not to be set
119 // if there is a shape to connect
120 xConnectorPropSet.setPropertyValue( "StartShape", xShape1 );
121 xConnectorPropSet.setPropertyValue( "StartGluePointIndex",
122 Integer.valueOf( nStartIndex ) );
124 xConnectorPropSet.setPropertyValue( "EndShape", xShape2 );
125 xConnectorPropSet.setPropertyValue( "EndGluePointIndex",
126 Integer.valueOf( nEndIndex ) );
128 XGluePointsSupplier xGluePointsSupplier;
129 XIndexContainer xIndexContainer;
130 XIdentifierContainer xIdentifierContainer;
132 GluePoint2 aGluePoint = new GluePoint2();
133 aGluePoint.IsRelative = false;
134 aGluePoint.PositionAlignment = Alignment.CENTER;
135 aGluePoint.Escape = EscapeDirection.SMART;
136 aGluePoint.IsUserDefined = true;
137 aGluePoint.Position.X = 0;
138 aGluePoint.Position.Y = 0;
140 // create and insert a glue point at shape1
141 xGluePointsSupplier = UnoRuntime.queryInterface( XGluePointsSupplier.class, xShape1 );
142 xIndexContainer = xGluePointsSupplier.getGluePoints();
143 xIdentifierContainer = UnoRuntime.queryInterface( XIdentifierContainer.class,
144 xIndexContainer );
145 int nIndexOfGluePoint1 = xIdentifierContainer.insert( aGluePoint );
147 // create and insert a glue point at shape2
148 xGluePointsSupplier = UnoRuntime.queryInterface( XGluePointsSupplier.class, xShape2 );
149 xIndexContainer = xGluePointsSupplier.getGluePoints();
150 xIdentifierContainer = UnoRuntime.queryInterface( XIdentifierContainer.class,
151 xIndexContainer );
152 int nIndexOfGluePoint2 = xIdentifierContainer.insert( aGluePoint );
154 // create and add a connector
155 XShape xConnector2 = ShapeHelper.createShape( xDrawDoc,
156 new Point( 0, 0 ),
157 new Size( 0, 0 ),
158 "com.sun.star.drawing.ConnectorShape" );
159 xShapes.add( xConnector2 );
161 XPropertySet xConnector2PropSet = UnoRuntime.queryInterface( XPropertySet.class, xConnector2 );
163 xConnector2PropSet.setPropertyValue( "StartShape", xShape1 );
164 xConnector2PropSet.setPropertyValue( "StartGluePointIndex",
165 Integer.valueOf( nIndexOfGluePoint1 ) );
167 xConnector2PropSet.setPropertyValue( "EndShape", xShape2 );
168 xConnector2PropSet.setPropertyValue( "EndGluePointIndex",
169 Integer.valueOf( nIndexOfGluePoint2 ) );
173 catch( Exception ex )
175 System.out.println( ex );
177 System.exit( 0 );