Image Control

From Apache OpenOffice Wiki
Jump to: navigation, search

If you want to display an image without the command button functionality, the image control and its model is the control of choice. The location of the graphic for the command button is set by the ImageURL property. Usually, the size of the image does not match the size of the control, therefore the image control automatically scales the image to the size of the control by setting the ScaleImage property to true.

One problem with URLs in Apache OpenOffice is that the developer, in certain contexts, may only know the system dependent path to his or her image file. A system path is not accepted by ImageURL. The following example shows how you can convert this path to a URL that can then be passed to the property ImageURL.

  public void insertImageControl(XMultiComponentFactory _xMCF, String _sImageSystemPath, int _nPosX, int _nPosY, int _nHeight, int _nWidth){
      // create a unique name by means of an own implementation...
      String sName = createUniqueName(m_xDlgModelNameContainer, "ImageControl");
      // convert the system path to the image to a FileUrl oFile = new;
      Object oFCProvider = _xMCF.createInstanceWithContext("", this.m_xContext);
      XFileIdentifierConverter xFileIdentifierConverter = (XFileIdentifierConverter) UnoRuntime.queryInterface(XFileIdentifierConverter.class, oFCProvider);
      String sImageUrl = xFileIdentifierConverter.getFileURLFromSystemPath(_sImageSystemPath, oFile.getAbsolutePath());
      XGraphic xGraphic = getGraphic(sImageUrl);
      // create a controlmodel at the multiservicefactory of the dialog model... 
      Object oICModel = m_xMSFDialogModel.createInstance("");
      XMultiPropertySet xICModelMPSet = (XMultiPropertySet) UnoRuntime.queryInterface(XMultiPropertySet.class, oICModel);
      // Set the properties at the model - keep in mind to pass the property names in alphabetical order!
      // The image is not scaled
      new String[] {"Border", "Graphic", "Height", "Name", "PositionX", "PositionY", "ScaleImage", "Width"},
      new Object[] { new Short((short) 1), xGraphic, new Integer(_nHeight), sName, new Integer(_nPosX), new Integer(_nPosY), Boolean.FALSE, new Integer(_nWidth)});
      // The controlmodel is not really available until inserted to the Dialog container
      m_xDlgModelNameContainer.insertByName(sName, oICModel); 
  }catch ( ex){
      /* perform individual exception handling here.
       * Possible exception types are:
  // creates a UNO graphic object that can be used to be assigned 
  // to the property "Graphic" of a controlmodel
  public XGraphic getGraphic(String _sImageUrl){
  XGraphic xGraphic = null;
      // create a GraphicProvider at the global service manager...
      Object oGraphicProvider = m_xMCF.createInstanceWithContext("", m_xContext);
      XGraphicProvider xGraphicProvider = (XGraphicProvider) UnoRuntime.queryInterface(XGraphicProvider.class, oGraphicProvider);
      // create the graphic object
      PropertyValue[] aPropertyValues = new PropertyValue[1];
      PropertyValue aPropertyValue = new PropertyValue();
      aPropertyValue.Name = "URL";
      aPropertyValue.Value = _sImageUrl;
      aPropertyValues[0] = aPropertyValue;
      xGraphic = xGraphicProvider.queryGraphic(aPropertyValues);
      return xGraphic;
  }catch ( ex){
      throw new java.lang.RuntimeException("cannot happen...");

Extension developers will be confronted with the problem that the graphic to be displayed by the image is located within the extension file. version 2.3 provided a simple method to query the path of the extension, see Location of Installed Extensions.

For previous versions of Apache OpenOffice a manual workaround can be used, see description hereunder.

The path to the images of the extension should be set in a configuration file of the component For example:

  <prop oor:name="Images" oor:type="xs:string">

The variable %origin% will be automatically assigned the value of the URL of the component file when this entry is queried during runtime:

   * @param _sRegistryPath the path a registryNode
   * @param _sImageName the name of the image
  public String getImageUrl(String _sRegistryPath, String _sImageName){
  String sImageUrl = "";
  try {
      // retrive the configuration node of the extension
      XNameAccess xNameAccess = getRegistryKeyContent(_sRegistryPath);
      if (xNameAccess != null){
          if (xNameAccess.hasByName(_sImageName)){
              // get the Image Url and process the Url by the macroexpander...
              sImageUrl = (String) xNameAccess.getByName(_sImageName);
              Object oMacroExpander = this.m_xContext.getValueByName("/singletons/");
              XMacroExpander xMacroExpander = (XMacroExpander) UnoRuntime.queryInterface(XMacroExpander.class, oMacroExpander);
              sImageUrl = xMacroExpander.expandMacros(sImageUrl);
              sImageUrl = sImageUrl.substring(new String("").length(), sImageUrl.length());
              sImageUrl = sImageUrl.trim();
              sImageUrl += "/" + _sImageName;
  } catch (Exception ex) {
      /* perform individual exception handling here.
       * Possible exception types are:
      return sImageUrl;
   * @param _sKeyName
   * @return
  public XNameAccess getRegistryKeyContent(String _sKeyName){
  try {
      Object oConfigProvider;
      PropertyValue[] aNodePath = new PropertyValue[1];
      oConfigProvider = m_xMCF.createInstanceWithContext("", this.m_xContext);
      aNodePath[0] = new PropertyValue();
      aNodePath[0].Name = "nodepath";
      aNodePath[0].Value = _sKeyName;
      XMultiServiceFactory xMSFConfig = (XMultiServiceFactory) UnoRuntime.queryInterface(XMultiServiceFactory.class, oConfigProvider);
      Object oNode = xMSFConfig.createInstanceWithArguments("", aNodePath);
      XNameAccess xNameAccess = (XNameAccess) UnoRuntime.queryInterface(XNameAccess.class, oNode);
      return xNameAccess;
  } catch (Exception exception) {
      return null;

For further information about the development of extensions and configuration file handling, please see Integrating Components into Apache OpenOffice).

Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages