File Control

From Apache OpenOffice Wiki
Jump to: navigation, search



The file control supports the service com.sun.star.awt.UnoControlFileControl and covers a lot of the functionality of an UnoControlEdit control and a command button that is built in the control. This is put into practice by a control supporting the service com.sun.star.awt.UnoControlEdit. Similar to a Text Field the content may be retrieved by a Text property. The value of Text denotes the path of the control. Clicking this button brings up a file dialog in which the user may select a file that is taken over by by the file control like a text field. The following example sets up a file control. It is initialized with the configured Workpath of the office installation that is converted to a system path before passed to the Text property of the control Path Settings.

  public XTextComponent insertFileControl(XTextListener _xTextListener, int _nPosX, int _nPosY, int _nWidth){
  XTextComponent xTextComponent = null;
  try{
      // create a unique name by means of an own implementation...
      String sName = createUniqueName(m_xDlgModelNameContainer, "FileControl");
 
      // retrieve the configured Work path...
      Object oPathSettings = m_xMCF.createInstanceWithContext("com.sun.star.util.PathSettings",m_xContext);
      XPropertySet xPropertySet = (XPropertySet) com.sun.star.uno.UnoRuntime.queryInterface(XPropertySet.class, oPathSettings);
      String sWorkUrl = (String) xPropertySet.getPropertyValue("Work");
 
      // convert the Url to a system path that is "human readable"...
      Object oFCProvider = m_xMCF.createInstanceWithContext("com.sun.star.ucb.FileContentProvider", m_xContext);
      XFileIdentifierConverter xFileIdentifierConverter = (XFileIdentifierConverter) UnoRuntime.queryInterface(XFileIdentifierConverter.class, oFCProvider);
      String sSystemWorkPath = xFileIdentifierConverter.getSystemPathFromFileURL(sWorkUrl);
 
      // create a controlmodel at the multiservicefactory of the dialog model... 
      Object oFCModel = m_xMSFDialogModel.createInstance("com.sun.star.awt.UnoControlFileControlModel");
 
      // Set the properties at the model - keep in mind to pass the property names in alphabetical order! 
      XMultiPropertySet xFCModelMPSet = (XMultiPropertySet) UnoRuntime.queryInterface(XMultiPropertySet.class, oFCModel);
      xFCModelMPSet.setPropertyValues(
      new String[] {"Height", "Name", "PositionX", "PositionY", "Text", "Width"},
      new Object[] { new Integer(12), sName, new Integer(_nPosX), new Integer(_nPosY), sSystemWorkPath, new Integer(_nWidth)});
 
      // The controlmodel is not really available until inserted to the Dialog container
      m_xDlgModelNameContainer.insertByName(sName, oFCModel);
      XPropertySet xFCModelPSet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, oFCModel);
 
      // add a textlistener that is notified on each change of the controlvalue... 
      XControl xFCControl = m_xDlgContainer.getControl(sName);
      xTextComponent = (XTextComponent) UnoRuntime.queryInterface(XTextComponent.class, xFCControl);
      XWindow xFCWindow = (XWindow) UnoRuntime.queryInterface(XWindow.class, xFCControl);
      xTextComponent.addTextListener(_xTextListener);
  }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);
  }
      return xTextComponent;
  }

The file control also allows the configuration of the file dialog. File dialogs implementing the service com.sun.star.ui.dialogs.FilePicker do not belong to the module com.sun.star.awt, but, as they are frequently used by extension developers, this topic shall also be covered in this chapter.

Currently the control does not yet offer the described complete functionality which is addressed by https://www.openoffice.org/issues/show_bug.cgi?id=71041.

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