xref: /AOO41X/main/odk/examples/DevelopersGuide/Drawing/Organigram.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  *  The Contents of this file are made available subject to the terms of
4*cdf0e10cSrcweir  *  the BSD license.
5*cdf0e10cSrcweir  *
6*cdf0e10cSrcweir  *  Copyright 2000, 2010 Oracle and/or its affiliates.
7*cdf0e10cSrcweir  *  All rights reserved.
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  *  Redistribution and use in source and binary forms, with or without
10*cdf0e10cSrcweir  *  modification, are permitted provided that the following conditions
11*cdf0e10cSrcweir  *  are met:
12*cdf0e10cSrcweir  *  1. Redistributions of source code must retain the above copyright
13*cdf0e10cSrcweir  *     notice, this list of conditions and the following disclaimer.
14*cdf0e10cSrcweir  *  2. Redistributions in binary form must reproduce the above copyright
15*cdf0e10cSrcweir  *     notice, this list of conditions and the following disclaimer in the
16*cdf0e10cSrcweir  *     documentation and/or other materials provided with the distribution.
17*cdf0e10cSrcweir  *  3. Neither the name of Sun Microsystems, Inc. nor the names of its
18*cdf0e10cSrcweir  *     contributors may be used to endorse or promote products derived
19*cdf0e10cSrcweir  *     from this software without specific prior written permission.
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22*cdf0e10cSrcweir  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23*cdf0e10cSrcweir  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24*cdf0e10cSrcweir  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25*cdf0e10cSrcweir  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26*cdf0e10cSrcweir  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27*cdf0e10cSrcweir  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28*cdf0e10cSrcweir  *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29*cdf0e10cSrcweir  *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30*cdf0e10cSrcweir  *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31*cdf0e10cSrcweir  *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*cdf0e10cSrcweir  *
33*cdf0e10cSrcweir  *************************************************************************/
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir import com.sun.star.beans.PropertyValue;
36*cdf0e10cSrcweir import com.sun.star.beans.XPropertySet;
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir import com.sun.star.bridge.XUnoUrlResolver;
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir import com.sun.star.frame.XComponentLoader;
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir import com.sun.star.lang.XComponent;
43*cdf0e10cSrcweir import com.sun.star.lang.XMultiComponentFactory;
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
46*cdf0e10cSrcweir import com.sun.star.uno.AnyConverter;
47*cdf0e10cSrcweir import com.sun.star.uno.XComponentContext;
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir /*
51*cdf0e10cSrcweir  * OpenQuery.java
52*cdf0e10cSrcweir  *
53*cdf0e10cSrcweir  * Created on 6. Juli 2002, 10:25
54*cdf0e10cSrcweir  */
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir /**
57*cdf0e10cSrcweir  *
58*cdf0e10cSrcweir  * @author  dschulten
59*cdf0e10cSrcweir  */
60*cdf0e10cSrcweir public class Organigram {
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir     private XComponentContext xRemoteContext = null;
63*cdf0e10cSrcweir     private XMultiComponentFactory xRemoteServiceManager = null;
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir     /** Creates a new instance of OpenQuery */
66*cdf0e10cSrcweir     public Organigram() {
67*cdf0e10cSrcweir     }
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir     /**
70*cdf0e10cSrcweir      * @param args the command line arguments
71*cdf0e10cSrcweir      */
72*cdf0e10cSrcweir     public static void main(String[] args) {
73*cdf0e10cSrcweir         Organigram organigram1 = new Organigram();
74*cdf0e10cSrcweir         try {
75*cdf0e10cSrcweir             organigram1.drawOrganigram();
76*cdf0e10cSrcweir         }
77*cdf0e10cSrcweir         catch (java.lang.Exception e){
78*cdf0e10cSrcweir             e.printStackTrace();
79*cdf0e10cSrcweir         }
80*cdf0e10cSrcweir         finally {
81*cdf0e10cSrcweir             System.exit(0);
82*cdf0e10cSrcweir         }
83*cdf0e10cSrcweir     }
84*cdf0e10cSrcweir     public void drawOrganigram() throws java.lang.Exception {
85*cdf0e10cSrcweir         // get the remote office component context
86*cdf0e10cSrcweir         xRemoteContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
87*cdf0e10cSrcweir         System.out.println("Connected to a running office ...");
88*cdf0e10cSrcweir         // get the remote service manager
89*cdf0e10cSrcweir         xRemoteServiceManager = xRemoteContext.getServiceManager();
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir         Object desktop = xRemoteServiceManager.createInstanceWithContext(
92*cdf0e10cSrcweir             "com.sun.star.frame.Desktop", xRemoteContext);
93*cdf0e10cSrcweir         XComponentLoader xComponentLoader = (XComponentLoader)
94*cdf0e10cSrcweir             UnoRuntime.queryInterface(XComponentLoader.class, desktop);
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir         PropertyValue[] loadProps = new PropertyValue[0];
97*cdf0e10cSrcweir         XComponent xDrawComponent = xComponentLoader.loadComponentFromURL(
98*cdf0e10cSrcweir             "private:factory/sdraw", "_blank", 0, loadProps);
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir         // get draw page by index
101*cdf0e10cSrcweir         com.sun.star.drawing.XDrawPagesSupplier xDrawPagesSupplier =
102*cdf0e10cSrcweir             (com.sun.star.drawing.XDrawPagesSupplier)UnoRuntime.queryInterface(
103*cdf0e10cSrcweir                 com.sun.star.drawing.XDrawPagesSupplier.class, xDrawComponent );
104*cdf0e10cSrcweir         com.sun.star.drawing.XDrawPages xDrawPages =
105*cdf0e10cSrcweir             xDrawPagesSupplier.getDrawPages();
106*cdf0e10cSrcweir         Object drawPage = xDrawPages.getByIndex(0);
107*cdf0e10cSrcweir         com.sun.star.drawing.XDrawPage xDrawPage = (com.sun.star.drawing.XDrawPage)
108*cdf0e10cSrcweir             UnoRuntime.queryInterface(com.sun.star.drawing.XDrawPage.class,
109*cdf0e10cSrcweir                                       drawPage);
110*cdf0e10cSrcweir 
111*cdf0e10cSrcweir         com.sun.star.lang.XMultiServiceFactory xDocumentFactory =
112*cdf0e10cSrcweir             (com.sun.star.lang.XMultiServiceFactory)UnoRuntime.queryInterface(
113*cdf0e10cSrcweir                 com.sun.star.lang.XMultiServiceFactory.class, xDrawComponent);
114*cdf0e10cSrcweir 
115*cdf0e10cSrcweir         com.sun.star.beans.XPropertySet xPageProps =
116*cdf0e10cSrcweir             (com.sun.star.beans.XPropertySet)UnoRuntime.queryInterface(
117*cdf0e10cSrcweir                 com.sun.star.beans.XPropertySet.class, xDrawPage);
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir         int pageWidth = AnyConverter.toInt(xPageProps.getPropertyValue("Width"));
120*cdf0e10cSrcweir         int pageHeight = AnyConverter.toInt(xPageProps.getPropertyValue("Height"));
121*cdf0e10cSrcweir         int pageBorderTop = AnyConverter.toInt(xPageProps.getPropertyValue("BorderTop"));
122*cdf0e10cSrcweir         int pageBorderLeft = AnyConverter.toInt(xPageProps.getPropertyValue("BorderLeft"));
123*cdf0e10cSrcweir         int pageBorderRight = AnyConverter.toInt(xPageProps.getPropertyValue("BorderRight"));
124*cdf0e10cSrcweir         int drawWidth = pageWidth - pageBorderLeft - pageBorderRight;
125*cdf0e10cSrcweir         int horCenter = pageBorderLeft + drawWidth / 2;
126*cdf0e10cSrcweir 
127*cdf0e10cSrcweir         String[][] orgUnits = new String[2][4];
128*cdf0e10cSrcweir         orgUnits[0][0] = "Management";
129*cdf0e10cSrcweir         orgUnits[1][0] = "Production";
130*cdf0e10cSrcweir         orgUnits[1][1] = "Purchasing";
131*cdf0e10cSrcweir         orgUnits[1][2] = "IT Services";
132*cdf0e10cSrcweir         orgUnits[1][3] = "Sales";
133*cdf0e10cSrcweir         int[] levelCount = {1, 4};
134*cdf0e10cSrcweir 
135*cdf0e10cSrcweir         int horSpace = 300;
136*cdf0e10cSrcweir         int verSpace = 3000;
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir         int shapeWidth = (drawWidth - (levelCount[1] - 1) * horSpace) / levelCount[1];
139*cdf0e10cSrcweir         int shapeHeight = pageHeight / 20;
140*cdf0e10cSrcweir         int shapeX = pageWidth / 2 - shapeWidth / 2;
141*cdf0e10cSrcweir         int shapeY = pageBorderTop;
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir         int levelY;
144*cdf0e10cSrcweir         int levelX;
145*cdf0e10cSrcweir 
146*cdf0e10cSrcweir         com.sun.star.drawing.XShape xStartShape = null;
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir         for (int level = 0; level <= 1; level++) {
149*cdf0e10cSrcweir             levelY = pageBorderTop + 2000 + level * (shapeHeight + verSpace);
150*cdf0e10cSrcweir             for (int i = levelCount[level] - 1; i > -1; i--) {
151*cdf0e10cSrcweir                 shapeX = horCenter - (levelCount[level] * shapeWidth +
152*cdf0e10cSrcweir                          (levelCount[level] - 1) * horSpace) / 2
153*cdf0e10cSrcweir                          + i * shapeWidth + i * horSpace;
154*cdf0e10cSrcweir                 Object shape = xDocumentFactory.createInstance("com.sun.star.drawing.RectangleShape");
155*cdf0e10cSrcweir                 com.sun.star.drawing.XShape xShape = (com.sun.star.drawing.XShape)
156*cdf0e10cSrcweir                     UnoRuntime.queryInterface(
157*cdf0e10cSrcweir                         com.sun.star.drawing.XShape.class, shape);
158*cdf0e10cSrcweir                 xShape.setPosition(new com.sun.star.awt.Point(shapeX, levelY));
159*cdf0e10cSrcweir                 xShape.setSize(new com.sun.star.awt.Size(shapeWidth, shapeHeight));
160*cdf0e10cSrcweir                 xDrawPage.add(xShape);
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir                 com.sun.star.text.XText xText = (com.sun.star.text.XText)
163*cdf0e10cSrcweir                     UnoRuntime.queryInterface(
164*cdf0e10cSrcweir                         com.sun.star.text.XText.class, xShape);
165*cdf0e10cSrcweir 
166*cdf0e10cSrcweir                 xText.setString(orgUnits[level][i]);
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir                 // memorize the root shape
169*cdf0e10cSrcweir                 if (level == 0 && i == 0)
170*cdf0e10cSrcweir                     xStartShape = xShape;
171*cdf0e10cSrcweir 
172*cdf0e10cSrcweir                 if (level == 1) {
173*cdf0e10cSrcweir                     Object connector = xDocumentFactory.createInstance("com.sun.star.drawing.ConnectorShape");
174*cdf0e10cSrcweir                     com.sun.star.beans.XPropertySet xConnectorProps =
175*cdf0e10cSrcweir                         (com.sun.star.beans.XPropertySet)UnoRuntime.queryInterface(
176*cdf0e10cSrcweir                             com.sun.star.beans.XPropertySet.class, connector);
177*cdf0e10cSrcweir                 com.sun.star.drawing.XShape xConnector =
178*cdf0e10cSrcweir                     (com.sun.star.drawing.XShape)UnoRuntime.queryInterface(
179*cdf0e10cSrcweir                         com.sun.star.drawing.XShape.class, connector);
180*cdf0e10cSrcweir                     xDrawPage.add(xConnector);
181*cdf0e10cSrcweir                     xConnectorProps.setPropertyValue("StartShape", xStartShape);
182*cdf0e10cSrcweir                     xConnectorProps.setPropertyValue("EndShape", xShape);
183*cdf0e10cSrcweir                     xConnectorProps.setPropertyValue("StartGluePointIndex",
184*cdf0e10cSrcweir                                       new Integer(2)); // 2 = bottom glue point
185*cdf0e10cSrcweir                     xConnectorProps.setPropertyValue("EndGluePointIndex",
186*cdf0e10cSrcweir                                       new Integer(0));   // 0 = top glue point
187*cdf0e10cSrcweir                 }
188*cdf0e10cSrcweir             }
189*cdf0e10cSrcweir         }
190*cdf0e10cSrcweir     }
191*cdf0e10cSrcweir }
192