快捷搜索:   nginx

OpenOffice.org Developer's Gui

Programming With UNO UNO(['ju:nou])是Universal Network Objects的简写,is the base component technology for OpenOffice.org。因此,与COM技术一样,是跨平台跨语言的一种技术,主要的开发语言有C++、Java和OpenOffice.org Basic,同时,借助微软的COM技术,其他语言也可以使用。另外,there is also a language binding for Python available、.Net和诸多的脚本语言如JavaScript,Beanshell 和Jython等。UNO is used to access OpenOffice.org。   Fields of Application for UNO 可以使用C++, Java 和COM/DCOM连接本地或远方的OpenOffice。C++和Java桌面程序、Java servlets, JSP, 脚本语言和诸如Delphi, VB等语言都可以使用OpenOffice文档。It is possible to develop UNO Components in C++ or Java that can be instantiated by the office process and add new capabilities to OpenOffice.org. 比如说给OpenOffice增加一个菜单项。   Getting Started 条件:JDK、OpenOffice以及OpenOffice SDK。 建议使用NetBeans,因为它与OpenOffice整合的最好,毕竟属于同一个公司。也可以使用Eclipse,但是NetBeans的OpenOffice插件可以很方便的开发OpenOffice应用,并且无需编写一对的环境变量。可以使用带JDK的NetBeans,这样,无需下载JDK。 建议先装OpenOffice和SDK,然后再装NetBeans及OpenOffice插件,这样在安装OpenOffice插件时可以自动获取OpenOffice及SDK所在目录。也先装NetBeans和插件,再安装OpenOffice和SDK,只需在“工具”-->“选项”-->“其他”-->“OpenOffice.org插件设置”中指定相应路径即可。 Configuration OpenOffice启动时会自动查找 Java运行时环境JRE,如果有多个JRE版本(尤其是Linux会自带JRE),可以通过“工具”-->“选项”对话框左侧节点“OpenOffice.org”-->“Java” 。在右侧的对话框中选择其中一个版本。 Using the Java UNO runtime and API in your IDE 如果安装了NetBeans和OOo插件,NetBeans依赖的JRE则被自动设置。 其他IDE的设置参考Prepare the Eclipse IDE Add the API Reference to your IDE  如果安装了NetBeans和OOo插件,IDL参考手册将被自动安装。在NetBeans中查找某个IDL的帮助,选中IDL并按下ALT + F1即可。 First Contact The FirstUnoContact example that connects to the office with java: package de.ooo.devguide.example1;
public class FirstUnoContact {
 public static void main(String[] args) {
  try {
     // get the remote office component context
     com.sun.star.uno.XComponentContext xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
    System.out.println("Connected to a running office ...");
    com.sun.star.lang.XMultiComponentFactory xMCF = xContext.getServiceManager();
    String available = (xMCF != null ? "available" : "not available");
     System.out.println("remote ServiceManager is " + available);
  }
  catch(java.lang.Exception e){
     e.printStackTrace();
  }
  finally {
     System.exit(0);
  }
  }
}UNO编程中有个重要的概念“服务管理器”,服务管理器可用来创建服务。服务则是一个个UNO对象,用来完成特定的任务,服务通常拥有一个组件上下文:创建该服务的服务管理器以及该服务会用到的其他数据。如: com.sun.star.frame.Desktop 加载文档(Docoment),获取当前文档以及访问所有打开的文档 com.sun.star.configuration.ConfigurationProvider 访问Openoffice的配置,比如“工具”-->“选项”对话框中的配置 com.sun.star.sdb.DatabaseContext holds databases registered with OpenOffice.org com.sun.star.system.SystemShellExecute executes system commands or documents registered for an application on the current platform。 com.sun.star.text.GlobalSettings manages global view and print settings for text documents 例子中的类FirstUnoContact是OpenOffice的客户程序,OpenOffice是服务器,服务器拥有自己的服务管理器和组件上下文,该组件上下文可被客户程序访问。客户程序初始化并从OpenOffice获取组件上下文。客户端初始化会创建一个本地的服务管理器,建立一个连接到正在运行的OpenOffice的管道并返回远程的服务管理器。方法com.sun.star.comp.helper.Bootstrap.bootstrap()初始化UNO并返回远程的服务管理器。更多信息请见UNO Concepts。初始化后,可以使用getServiceManager从返回的组件上下文获取远程服务管理器(from OpenOffice)。该远程的服务管理器可以用来实现OpenOffice的所有功能。 A remote connection can fail under certain conditions。比如说客户端程序运行了一段时间后,其初始化时获取的远程服务管理器可能不能使用。连接断开时会抛出 异常DisposedException。A more sophisticated way to handle lost connections is to register a listener at the underlying bridge object.详见UNO Interprocess ConnectionsWorking with Objects Objects, Interfaces, and Services An object in our context is a software artifact that has methods you can call and attributes you can get or set.对象所提供的方法和属性则有其提供的一系列接口决定。通常,对象由服务管理器创建,如例子FirstLoadComponent中,远方的服务管理器创建了远方的Desktop对象(OOo中处理应用程序窗口和加载文档的对象)。 Object desktop = xRemoteServiceManager.createInstanceWithContext(
                   "com.sun.star.frame.Desktop", xRemoteContext); An interface specifies a set of attributes and methods that together define one single aspect of an object. 接口可以(多重)继承。严格地来说,接口的属性不是必须的,但是,接口属性又有其存在的理由:1.getting and setting a value seems to be widespread enough to warrant extra support.2.接口设计者可以表达同一对象的不同特性间的细微差别。属性则用于这些不是对象基本的特性。 Attributes can be used for those features that are not considered integral or structural parts of an object, while explicit methods are reserved to access the core features. (与Objects can hand out other objects. There are two cases:一个意思)。因为历史原因,一个对象通常支持一系列相互独立的接口用来与对象不同特性通信,而借助多重继承,现在的对象只需一个接口继承自多个相互独立的接口,这些接口则构成了对象的不同方面。 因为历史原因,“服务”这一术语也有两种不同的含义(OpenOffice.org 2.0前后)。Formally, new-style services are called “single-interface–based services.”在新的“服务”中,包含属性的对象只需支持接口com.sun.star.beans.XPropertySetcom.sun.star.beans.XMultiPropertySet,在使用时,只需调用getPropertyValue()/setPropertyValue()函数。而在旧的"服务"中,需要提供多个Get/Set函数,如getPrinter()等。 Using Services 概念“服务”和“接口”有如下两个好处: 1.Interfaces and services separate specification from implementation 2.Service names allow to create instances by specification name, not by class names. 在Openoffice中,全局的服务管理器(对象工厂)用来创建一个个对象而无需定义其内部的实现。这是因为工厂通过服务名字来决定哪个实现被返回。而在Java或C++中,对象需用new来创建,这样You cannot later on exchange it by another class without editing the code。而借助服务则可以做到。你只需使用服务中定义好的接口,而无需关心你获取的是哪种实现。 Interfaces 细粒度的接口可以很好的重用。比如说很多对象都支持Text:Document的内容、文本框、页眉页脚、页脚注释等。这些对象都支持同一个接口。这样,程序可以通过使用getText来获取这些对象中的文本。在新的“服务”中,所有的方法都是通过一个继承了多个接口的接口来访问。 Document objects represent the files that are opened with OpenOffice.org. They are created by the Desktop object, which has a loadComponentFromURL() method for this purpose. Objects can hand out other objects. There are two cases: 1.对象A是对象B的一部分,如Calc文档的Sheet(getSheets)、Writer文档的Text(getText)。 2.对象A是对象B的一个属性,如spreadsheets的页面样式有如下两个属性:"RightPageHeaderContent" and "LeftPageHeaderContent".通常使用getPropertyValue获取属性对象。 Objects can be elements in a set of similar objects,访问数组中的对象有四种方法:参数为名称、序号或enumeration的访问方法;像使用数组一样使用对象集合,如a[2]。

 

顶(2)
踩(0)

您可能还会对下面的文章感兴趣:

最新评论