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 import com.sun.star.beans.PropertyValue;
36 import com.sun.star.beans.XPropertySet;
37 
38 import com.sun.star.bridge.XUnoUrlResolver;
39 
40 import com.sun.star.frame.XComponentLoader;
41 
42 import com.sun.star.lang.XComponent;
43 import com.sun.star.lang.XMultiComponentFactory;
44 
45 import com.sun.star.uno.UnoRuntime;
46 import com.sun.star.uno.AnyConverter;
47 import com.sun.star.uno.XComponentContext;
48 
49 
50 /*
51  * OpenQuery.java
52  *
53  * Created on 6. Juli 2002, 10:25
54  */
55 
56 /**
57  *
58  * @author  dschulten
59  */
60 public class Organigram {
61 
62     private XComponentContext xRemoteContext = null;
63     private XMultiComponentFactory xRemoteServiceManager = null;
64 
65     /** Creates a new instance of OpenQuery */
66     public Organigram() {
67     }
68 
69     /**
70      * @param args the command line arguments
71      */
72     public static void main(String[] args) {
73         Organigram organigram1 = new Organigram();
74         try {
75             organigram1.drawOrganigram();
76         }
77         catch (java.lang.Exception e){
78             e.printStackTrace();
79         }
80         finally {
81             System.exit(0);
82         }
83     }
84     public void drawOrganigram() throws java.lang.Exception {
85         // get the remote office component context
86         xRemoteContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
87         System.out.println("Connected to a running office ...");
88         // get the remote service manager
89         xRemoteServiceManager = xRemoteContext.getServiceManager();
90 
91         Object desktop = xRemoteServiceManager.createInstanceWithContext(
92             "com.sun.star.frame.Desktop", xRemoteContext);
93         XComponentLoader xComponentLoader = (XComponentLoader)
94             UnoRuntime.queryInterface(XComponentLoader.class, desktop);
95 
96         PropertyValue[] loadProps = new PropertyValue[0];
97         XComponent xDrawComponent = xComponentLoader.loadComponentFromURL(
98             "private:factory/sdraw", "_blank", 0, loadProps);
99 
100         // get draw page by index
101         com.sun.star.drawing.XDrawPagesSupplier xDrawPagesSupplier =
102             (com.sun.star.drawing.XDrawPagesSupplier)UnoRuntime.queryInterface(
103                 com.sun.star.drawing.XDrawPagesSupplier.class, xDrawComponent );
104         com.sun.star.drawing.XDrawPages xDrawPages =
105             xDrawPagesSupplier.getDrawPages();
106         Object drawPage = xDrawPages.getByIndex(0);
107         com.sun.star.drawing.XDrawPage xDrawPage = (com.sun.star.drawing.XDrawPage)
108             UnoRuntime.queryInterface(com.sun.star.drawing.XDrawPage.class,
109                                       drawPage);
110 
111         com.sun.star.lang.XMultiServiceFactory xDocumentFactory =
112             (com.sun.star.lang.XMultiServiceFactory)UnoRuntime.queryInterface(
113                 com.sun.star.lang.XMultiServiceFactory.class, xDrawComponent);
114 
115         com.sun.star.beans.XPropertySet xPageProps =
116             (com.sun.star.beans.XPropertySet)UnoRuntime.queryInterface(
117                 com.sun.star.beans.XPropertySet.class, xDrawPage);
118 
119         int pageWidth = AnyConverter.toInt(xPageProps.getPropertyValue("Width"));
120         int pageHeight = AnyConverter.toInt(xPageProps.getPropertyValue("Height"));
121         int pageBorderTop = AnyConverter.toInt(xPageProps.getPropertyValue("BorderTop"));
122         int pageBorderLeft = AnyConverter.toInt(xPageProps.getPropertyValue("BorderLeft"));
123         int pageBorderRight = AnyConverter.toInt(xPageProps.getPropertyValue("BorderRight"));
124         int drawWidth = pageWidth - pageBorderLeft - pageBorderRight;
125         int horCenter = pageBorderLeft + drawWidth / 2;
126 
127         String[][] orgUnits = new String[2][4];
128         orgUnits[0][0] = "Management";
129         orgUnits[1][0] = "Production";
130         orgUnits[1][1] = "Purchasing";
131         orgUnits[1][2] = "IT Services";
132         orgUnits[1][3] = "Sales";
133         int[] levelCount = {1, 4};
134 
135         int horSpace = 300;
136         int verSpace = 3000;
137 
138         int shapeWidth = (drawWidth - (levelCount[1] - 1) * horSpace) / levelCount[1];
139         int shapeHeight = pageHeight / 20;
140         int shapeX = pageWidth / 2 - shapeWidth / 2;
141         int shapeY = pageBorderTop;
142 
143         int levelY;
144         int levelX;
145 
146         com.sun.star.drawing.XShape xStartShape = null;
147 
148         for (int level = 0; level <= 1; level++) {
149             levelY = pageBorderTop + 2000 + level * (shapeHeight + verSpace);
150             for (int i = levelCount[level] - 1; i > -1; i--) {
151                 shapeX = horCenter - (levelCount[level] * shapeWidth +
152                          (levelCount[level] - 1) * horSpace) / 2
153                          + i * shapeWidth + i * horSpace;
154                 Object shape = xDocumentFactory.createInstance("com.sun.star.drawing.RectangleShape");
155                 com.sun.star.drawing.XShape xShape = (com.sun.star.drawing.XShape)
156                     UnoRuntime.queryInterface(
157                         com.sun.star.drawing.XShape.class, shape);
158                 xShape.setPosition(new com.sun.star.awt.Point(shapeX, levelY));
159                 xShape.setSize(new com.sun.star.awt.Size(shapeWidth, shapeHeight));
160                 xDrawPage.add(xShape);
161 
162                 com.sun.star.text.XText xText = (com.sun.star.text.XText)
163                     UnoRuntime.queryInterface(
164                         com.sun.star.text.XText.class, xShape);
165 
166                 xText.setString(orgUnits[level][i]);
167 
168                 // memorize the root shape
169                 if (level == 0 && i == 0)
170                     xStartShape = xShape;
171 
172                 if (level == 1) {
173                     Object connector = xDocumentFactory.createInstance("com.sun.star.drawing.ConnectorShape");
174                     com.sun.star.beans.XPropertySet xConnectorProps =
175                         (com.sun.star.beans.XPropertySet)UnoRuntime.queryInterface(
176                             com.sun.star.beans.XPropertySet.class, connector);
177                 com.sun.star.drawing.XShape xConnector =
178                     (com.sun.star.drawing.XShape)UnoRuntime.queryInterface(
179                         com.sun.star.drawing.XShape.class, connector);
180                     xDrawPage.add(xConnector);
181                     xConnectorProps.setPropertyValue("StartShape", xStartShape);
182                     xConnectorProps.setPropertyValue("EndShape", xShape);
183                     xConnectorProps.setPropertyValue("StartGluePointIndex",
184                                       new Integer(2)); // 2 = bottom glue point
185                     xConnectorProps.setPropertyValue("EndGluePointIndex",
186                                       new Integer(0));   // 0 = top glue point
187                 }
188             }
189         }
190     }
191 }
192