Image Control

From Apache OpenOffice Wiki
< Documentation‎ | DevGuide
Revision as of 08:42, 25 October 2009 by BMarcelly (Talk | contribs)

Jump to: navigation, search



If you want to display an image without the command button functionality, the image control com.sun.star.awt.UnoControlImageControl and its model com.sun.star.awt.UnoControlImageControlModel 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 OpenOffice.org 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){
  try{
      // 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
      java.io.File oFile = new java.io.File(_sImageSystemPath);
      Object oFCProvider = _xMCF.createInstanceWithContext("com.sun.star.ucb.FileContentProvider", 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("com.sun.star.awt.UnoControlImageControlModel");
      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
      xICModelMPSet.setPropertyValues(
      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 (com.sun.star.uno.Exception ex){
      /* perform individual exception handling here.
       * Possible exception types are:
       * com.sun.star.lang.IllegalArgumentException,
       * com.sun.star.lang.WrappedTargetException,
       * com.sun.star.container.ElementExistException,
       * com.sun.star.beans.PropertyVetoException,
       * com.sun.star.beans.UnknownPropertyException,
       * com.sun.star.uno.Exception
       */
      ex.printStackTrace(System.out);
  }}
 
  // 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;
  try{
      // create a GraphicProvider at the global service manager...
      Object oGraphicProvider = m_xMCF.createInstanceWithContext("com.sun.star.graphic.GraphicProvider", 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 (com.sun.star.uno.Exception 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. Apache OpenOffice version 2.3 provides 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">
      <value>%origin%/images</value>
  </prop>

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/com.sun.star.util.theMacroExpander");
              XMacroExpander xMacroExpander = (XMacroExpander) UnoRuntime.queryInterface(XMacroExpander.class, oMacroExpander);
              sImageUrl = xMacroExpander.expandMacros(sImageUrl);
              sImageUrl = sImageUrl.substring(new String("vnd.sun.star.expand:").length(), sImageUrl.length());
              sImageUrl = sImageUrl.trim();
              sImageUrl += "/" + _sImageName;
          }
      } 
  } catch (Exception ex) {
      /* perform individual exception handling here.
       * Possible exception types are:
       * com.sun.star.lang.IllegalArgumentException,
       * com.sun.star.lang.WrappedTargetException,
       */
      ex.printStackTrace(System.out); 
  }
      return sImageUrl;
  }
 
  /**
   * @param _sKeyName
   * @return
   */
  public XNameAccess getRegistryKeyContent(String _sKeyName){
  try {
      Object oConfigProvider;
      PropertyValue[] aNodePath = new PropertyValue[1];
      oConfigProvider = m_xMCF.createInstanceWithContext("com.sun.star.configuration.ConfigurationProvider", 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("com.sun.star.configuration.ConfigurationAccess", aNodePath);
      XNameAccess xNameAccess = (XNameAccess) UnoRuntime.queryInterface(XNameAccess.class, oNode);
      return xNameAccess;
  } catch (Exception exception) {
      exception.printStackTrace(System.out);
      return null;
  }}

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

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