Formatted Field

From Apache OpenOffice Wiki
Jump to: navigation, search



The formatted field control com.sun.star.awt.UnoControlFormattedField specifies a format that is used for formatting the entered and displayed data.

Properties of com.sun.star.awt.UnoControlFormattedFieldModel
TreatAsNumber boolean. If the TreatAsNumber property is set to true, the text of the control is interpreted as a number.
FormatsSupplier The FormatsSupplier property returns a com.sun.star.util.XNumberFormatsSupplier. that offers access to the number format of the control. Initially, when no number formats supplier is assigned, a default number formatter is set. Further information about the management of number formats can be found in Number Formats.
FormatKey long. The unique key that represents the number format of the control may be set an queried by the property FormatKey.
EffectiveDefault any. Specifies the default value of the control. Depending on the value of TreatAsNumber, this may be a string or a numeric (double) value.
EffectiveMax double. Specifies the maximum value that the user may enter.
EffectiveMin double. Specifies the minimum value that the user may enter.
EffectiveValue any. Specifies the current value of the control. In dependence on the value of TreatAsNumber this may be a string or a numeric (double) value.

As any kind of number format at the model of the formatted field may be set, this control can be universally used instead of the date field, time field, numeric field or currency field controls that are designed for special purposes as described in the following sections.

The following example demonstrates the creation of a formatted field. One critical point is to assign the NumberFormatsSupplier to the property FormatsSupplier. In the example, this is created directly at the global ServiceManager (m_xMCF). It is also possible to assign an existing NumberFormatsSupplier, like a spreadsheet document or a text document.

  public XPropertySet insertFormattedField(XSpinListener _xSpinListener, int _nPosX, int _nPosY, int _nWidth){
  XPropertySet xFFModelPSet = null;
  try{
      // create a unique name by means of an own implementation...
      String sName = createUniqueName(m_xDlgModelNameContainer, "FormattedField");
 
      // create a controlmodel at the multiservicefactory of the dialog model... 
      Object oFFModel = m_xMSFDialogModel.createInstance("com.sun.star.awt.UnoControlFormattedFieldModel");
      XMultiPropertySet xFFModelMPSet = (XMultiPropertySet) UnoRuntime.queryInterface(XMultiPropertySet.class, oFFModel);
      // Set the properties at the model - keep in mind to pass the property names in alphabetical order! 
      xFFModelMPSet.setPropertyValues(
          new String[] {"EffectiveValue", "Height", "Name", "PositionX", "PositionY", "StrictFormat", "Spin", "Width"},
          new Object[] { new Double(12348), new Integer(12), sName, new Integer(_nPosX), new Integer(_nPosY), Boolean.TRUE, Boolean.TRUE, new Integer(_nWidth)});
 
      xFFModelPSet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, oFFModel);
      // to define a numberformat you always need a locale...
      com.sun.star.lang.Locale aLocale = new com.sun.star.lang.Locale();
      aLocale.Country = "US";
      aLocale.Language = "en";
      // this Format is only compliant to the english locale! 
      String sFormatString = "NNNNMMMM DD, YYYY";
 
      // a NumberFormatsSupplier has to be created first "in the open countryside"...
      Object oNumberFormatsSupplier = m_xMCF.createInstanceWithContext("com.sun.star.util.NumberFormatsSupplier", m_xContext);
      XNumberFormatsSupplier xNumberFormatsSupplier = (XNumberFormatsSupplier) UnoRuntime.queryInterface(XNumberFormatsSupplier.class, oNumberFormatsSupplier);
      XNumberFormats xNumberFormats = xNumberFormatsSupplier.getNumberFormats();
      // is the numberformat already defined?
      int nFormatKey = xNumberFormats.queryKey(sFormatString, aLocale, true);
      if (nFormatKey == -1){
          // if not then add it to the NumberFormatsSupplier
          nFormatKey = xNumberFormats.addNew(sFormatString, aLocale);
      }
 
      // The following property may also be set with XMultiPropertySet but we
      // use the XPropertySet interface merely for reasons of demonstration 
      xFFModelPSet.setPropertyValue("FormatsSupplier", xNumberFormatsSupplier);
      xFFModelPSet.setPropertyValue("FormatKey", new Integer(nFormatKey));
 
      // The controlmodel is not really available until inserted to the Dialog container
      m_xDlgModelNameContainer.insertByName(sName, oFFModel); 
 
      // finally we add a Spin-Listener to the control
      XControl xFFControl = m_xDlgContainer.getControl(sName);
      // add a SpinListener that is notified on each change of the controlvalue... 
      XSpinField xSpinField = (XSpinField) UnoRuntime.queryInterface(XSpinField.class, xFFControl);
      xSpinField.addSpinListener(_xSpinListener);
 
  }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 xFFModelPSet;
  }

The attached spin listener in this code example must implement com.sun.star.awt.XSpinListener which, among other things, includes up()

  public void up(SpinEvent spinEvent) {
  try{
      // get the control that has fired the event, 
      XControl xControl = (XControl) UnoRuntime.queryInterface(XControl.class, spinEvent.Source);
      XControlModel xControlModel = xControl.getModel();
      XPropertySet xPSet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xControlModel);
      String sName = (String) xPSet.getPropertyValue("Name");
      // just in case the listener has been added to several controls,
      // we make sure we refer to the right one
      if (sName.equals("FormattedField1")){
          double fvalue = AnyConverter.toDouble(xPSet.getPropertyValue("EffectiveValue"));
          System.out.println("Controlvalue: " + fvalue);
          // insert your code here to validate the value of the control...
      } 
  }catch (com.sun.star.uno.Exception ex){
      /* perform individual exception handling here.
       * Possible exception types are:
       * com.sun.star.lang.WrappedTargetException,
       * com.sun.star.beans.UnknownPropertyException,
       * com.sun.star.uno.Exception
       */
      ex.printStackTrace(System.out);
  }}
 
  public void down(SpinEvent spinEvent) {
  }
 
  public void last(SpinEvent spinEvent) {
  }
 
  public void first(SpinEvent spinEvent) {
  }
 
  public void disposing(EventObject rEventObject){ 
  }
Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages