Zh/Documentation/DevGuide/Drawings/Example: Creating a Simple Organizational Chart
From Apache OpenOffice Wiki
< Zh | Documentation
下面这个例子创建了一个简单的两层组织机构图,它由5个矩形框图形和4个连接器组成,4个连接器将第2层的矩形框和第1层的矩形框连接起来.
例子中的函数方法getRemoteServiceManager()被用来连接office服务.我们先讨论这个方法.首先是加载空的绘图文挡,并且检索第1个绘图页查找它的页面规格.然后,准备机构数据和计算图形尺寸.整个组织结构图的图形对象在 一个for循环中被加载,再为第2层的所有图形增加连接器.
public void drawOrganigram() throws java.lang.Exception { xRemoteServiceManager = this.getRemoteServiceManager( "uno:socket,host=localhost,port=2083;urp;StarOffice.ServiceManager"); Object desktop = xRemoteServiceManager.createInstanceWithContext( "com.sun.star.frame.Desktop", xRemoteContext); XComponentLoader xComponentLoader = (XComponentLoader) UnoRuntime.queryInterface( XComponentLoader.class, desktop); PropertyValue[] loadProps = new PropertyValue[0]; XComponent xDrawComponent = xComponentLoader.loadComponentFromURL( "private:factory/sdraw", "_blank", 0, loadProps); // 通过索引号获得绘图页对象 com.sun.star.drawing.XDrawPagesSupplier xDrawPagesSupplier = (com.sun.star.drawing.XDrawPagesSupplier) UnoRuntime.queryInterface( com.sun.star.drawing.XDrawPagesSupplier.class, xDrawComponent); com.sun.star.drawing.XDrawPages xDrawPages = xDrawPagesSupplier.getDrawPages(); Object drawPage = xDrawPages.getByIndex(0); // 得到绘图页对象的页面的绘图接口 com.sun.star.drawing.XDrawPage xDrawPage = (com.sun.star.drawing.XDrawPage) UnoRuntime.queryInterface( com.sun.star.drawing.XDrawPage.class, drawPage); // 通过页面的绘图接口得到页面的属性 com.sun.star.beans.XPropertySet xPageProps = (com.sun.star.beans.XPropertySet) UnoRuntime.queryInterface( com.sun.star.beans.XPropertySet.class, xDrawPage); int pageWidth = AnyConverter.toInt(xPageProps.getPropertyValue("Width")); int pageHeight = AnyConverter.toInt(xPageProps.getPropertyValue("Height")); int pageBorderTop = AnyConverter.toInt(xPageProps.getPropertyValue("BorderTop")); int pageBorderLeft = AnyConverter.toInt(xPageProps.getPropertyValue("BorderLeft")); int pageBorderRight = AnyConverter.toInt(xPageProps.getPropertyValue("BorderRight")); int drawWidth = pageWidth - pageBorderLeft - pageBorderRight; int horCenter = pageBorderLeft + drawWidth / 2; // 组织机构的内容,用一个2维数组存放.维度1表示层,维度2表示层中的第几个元素. String[][] orgUnits = new String[2][4]; orgUnits[0][0] = "Management"; // level 0 orgUnits[1][0] = "Production"; // level 1 orgUnits[1][1] = "Purchasing"; // level 1 orgUnits[1][2] = "IT Services"; // level 1 orgUnits[1][3] = "Sales"; // level 1 int[] levelCount = {1, 4}; // 每层的元素个数 // 计算图形的大小和位置 int horSpace = 300; int verSpace = 3000; int shapeWidth = (drawWidth - (levelCount[1] - 1) * horSpace) / levelCount[1]; // 图形的宽 int shapeHeight = pageHeight / 20; // 图形的高 int shapeX = pageWidth / 2 - shapeWidth / 2; int levelY = 0; // 用于存放根图形的对象 com.sun.star.drawing.XShape xStartShape = null; // 取得文档工厂 com.sun.star.lang.XMultiServiceFactory xDocumentFactory = (com.sun.star.lang.XMultiServiceFactory) UnoRuntime.queryInterface( com.sun.star.lang.XMultiServiceFactory.class, xDrawComponent); // 绘制图形和连接器 for (int level = 0; level <= 1; level++) { // 计算图形的Y坐标 levelY = pageBorderTop + 2000 + level * (shapeHeight + verSpace); for (int i = levelCount[level] - 1; i > -1; i--) { // 计算图形的X坐标 shapeX = horCenter - (levelCount[level] * shapeWidth + (levelCount[level] - 1) * horSpace) / 2 + i * shapeWidth + i * horSpace; // 创建图形对象 Object shape = xDocumentFactory.createInstance("com.sun.star.drawing.RectangleShape"); // 获得图形对象的绘图接口 com.sun.star.drawing.XShape xShape = (com.sun.star.drawing.XShape) UnoRuntime.queryInterface( com.sun.star.drawing.XShape.class, shape); // 指定图形的位置 xShape.setPosition(new com.sun.star.awt.Point(shapeX, levelY)); // 指定图形的大小 xShape.setSize(new com.sun.star.awt.Size(shapeWidth, shapeHeight)); // 将图形加入到绘图页 xDrawPage.add(xShape); // 绘制文本 com.sun.star.text.XText xText = (com.sun.star.text.XText) UnoRuntime.queryInterface( com.sun.star.text.XText.class, xShape); xText.setString(orgUnits[level][i]); // 记录根图形,用于绘制连接器 if (level == 0 && i == 0) { xStartShape = xShape; } // 绘制连接器 if (level == 1) { Object connector = xDocumentFactory.createInstance( "com.sun.star.drawing.ConnectorShape"); com.sun.star.drawing.XShape xConnector = (com.sun.star.drawing.XShape) UnoRuntime.queryInterface( com.sun.star.drawing.XShape.class, connector); xDrawPage.add(xConnector); com.sun.star.beans.XPropertySet xConnectorProps = (com.sun.star.beans.XPropertySet) UnoRuntime.queryInterface( com.sun.star.beans.XPropertySet.class, connector); xConnectorProps.setPropertyValue("StartShape", xStartShape); xConnectorProps.setPropertyValue("EndShape", xShape); // glue point positions: 0=top 1=left 2=bottom 3=right xConnectorProps.setPropertyValue("StartGluePointIndex", new Integer(2)); xConnectorProps.setPropertyValue("EndGluePointIndex", new Integer(0)); } } } }