Control Models

From Apache OpenOffice Wiki
Jump to: navigation, search



As you know from Form Control Models, the base for all our control models is the com.sun.star.form.FormControlModel service. Let us look at the most relevant elements of the declaration of this service and what a component must do to support it:

com.sun.star.awt.UnoControlModel

This service specifies that a form control model complies to everything required for a control model by the UNO windowing toolkit as described in module com.sun.star.awt. This means support for the com.sun.star.awt.XControlModel interface, for property access and persistence.

com.sun.star.form.FormComponent

This service requires a form control model is part of a form component hierarchy. Refer to chapter A Hierarchy of Models.

com.sun.star.beans.XPropertyState

This optional interface allows the control model properties to have a default value. All known implementations of the FormControlModel service support this interface.

<idlml>com.sun.star.form.FormControlModel:ClassId</idlml>

This property determines the class of a control model you have , and it assumes a value from the com.sun.star.form.FormComponentType enumeration. The same is done using the com.sun.star.lang.XServiceInfo interface that is supported by every component, and as shown below it can be indispensable. Using the <idlml>com.sun.star.form.FormControlModel:ClassId</idlml> property is faster.
Documentation note.png Note that the com.sun.star.form.FormControlModel service does not state anything about data awareness. It describes the requirements for a control model which can be part of a form layer.

See chapter Data Awareness for additional information about the controls which are data aware.

The following example shows how to determine the type of a control model using the ClassId property introduced above:

  /** retrieves the type of a form component.
      <p>Speaking strictly, the function recognizes more than form components. Especially,
      it survives a null argument. which means it can be safely applied to the a top-level
      forms container; and it is able to classify grid columns (which are no form components)
      as well.</p>
   */
  static public String classifyFormComponentType(XPropertySet xComponent)
          throws com.sun.star.uno.Exception {
      String sType = "<unknown component>";
 
      XServiceInfo xSI = (XserviceInfo)UnoRuntime.queryInterface(XServiceInfo.class, xComponent);
 
      XPropertySetInfo xPSI = null;
      if (null != xComponent)
          xPSI = xComponent.getPropertySetInfo();
 
      if ( ( null != xPSI ) && xPSI.hasPropertyByName("ClassId")) {
          // get the ClassId property
          XPropertySet xCompProps = (XPropertySet)UnoRuntime.queryInterface(
              XPropertySet.class, xComponent);
 
          Short nClassId = (Short)xCompProps.getPropertyValue("ClassId");
          switch (nClassId.intValue())
          {
              case FormComponentType.COMMANDBUTTON: sType = "Command button"; break;
              case FormComponentType.RADIOBUTTON : sType = "Radio button"; break;
              case FormComponentType.IMAGEBUTTON : sType = "Image button"; break;
              case FormComponentType.CHECKBOX : sType = "Check Box"; break;
              case FormComponentType.LISTBOX : sType = "List Box"; break;
              case FormComponentType.COMBOBOX : sType = "Combo Box"; break;
              case FormComponentType.GROUPBOX : sType = "Group Box"; break;
              case FormComponentType.FIXEDTEXT : sType = "Fixed Text"; break;
              case FormComponentType.GRIDCONTROL : sType = "Grid Control"; break;
              case FormComponentType.FILECONTROL : sType = "File Control"; break;
              case FormComponentType.HIDDENCONTROL: sType = "Hidden Control"; break;
              case FormComponentType.IMAGECONTROL : sType = "Image Control"; break;
              case FormComponentType.DATEFIELD : sType = "Date Field"; break;
              case FormComponentType.TIMEFIELD : sType = "Time Field"; break;
              case FormComponentType.NUMERICFIELD : sType = "Numeric Field"; break;
              case FormComponentType.CURRENCYFIELD: sType = "Currency Field"; break;
              case FormComponentType.PATTERNFIELD : sType = "Pattern Field"; break;
 
              case FormComponentType.TEXTFIELD :
                  // there are two known services with this class id: the usual text field,
                  // and the formatted field
                  sType = "Text Field";
                  if (( null != xSI) && xSI.supportsService(
                      "com.sun.star.form.component.FormattedField")) {
                      sType = "Formatted Field";
                  }
                  break;
 
              default:
                  break;
          }
      }
      else {
          if ((null != xSI) && xSI.supportsService("com.sun.star.form.component.DataForm")) {
              sType = "Form";
          }
      }
 
      return sType;
  }

Note the special handling for the value <idlml>com.sun.star.form.FormComponentType:TEXTFIELD</idlml>. There are two different services where a component implementing them is required to act as text field, the com.sun.star.form.component.TextField and com.sun.star.form.component.FormattedField. Both services describe a text component, thus both have a class id of <idlml>com.sun.star.form.FormComponentType:TEXTFIELD</idlml>. To distinguish between them, ask the components for more details using the com.sun.star.lang.XServiceInfo interface.

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