Example #1
0
  /* ------------------------------------------------------------------ */
  public void propertyChange(PropertyChangeEvent aEvent) throws com.sun.star.uno.RuntimeException {
    try {
      // did it come from a radio button or checkbox?
      if (aEvent.PropertyName.equals("State")) { // yep
        Short aNewState = (Short) aEvent.NewValue;

        XPropertySet xModel = UNO.queryPropertySet(aEvent.Source);
        String sName = (String) xModel.getPropertyValue("Name");

        Short aClassId = (Short) xModel.getPropertyValue("ClassId");
        if (FormComponentType.RADIOBUTTON == aClassId.shortValue()) {
          String sRefValue = (String) xModel.getPropertyValue("RefValue");

          short nNewValue = ((Short) aEvent.NewValue).shortValue();
          if (sName.equals("KeyGen")) {
            // it's one of the options for key generation
            if (sRefValue.equals("none")) { // no automatic generation at all
              m_aSalesmanKeyGenerator.stopGenerator();
              m_aSalesKeyGenerator.stopGenerator();
            } else {
              boolean bGenerateOnReset = true;
              if (sRefValue.equals("update")) { // generate on update
                bGenerateOnReset = (0 == nNewValue);
              } else if (sRefValue.equals("reset")) { // generate on reset
                bGenerateOnReset = (0 != nNewValue);
              }
              m_aSalesmanKeyGenerator.activateKeyGenerator(bGenerateOnReset);
              m_aSalesKeyGenerator.activateKeyGenerator(bGenerateOnReset);
            }
          }
        } else if (FormComponentType.CHECKBOX == aClassId.shortValue()) {
          boolean bEnabled = (0 != aNewState.shortValue());
          if (sName.equals("defaultdate")) {
            m_bDefaultSalesDate = bEnabled;
          } else if (sName.equals("protectkeys")) {
            m_bProtectKeyFields = bEnabled;
            m_aSalesmenLocker.enableLock(m_bProtectKeyFields);
            m_aSalesLocker.enableLock(m_bProtectKeyFields);
          } else if (sName.equals("emptysales")) {
            m_bAllowEmptySales = bEnabled;
            m_aSalesNameValidator.enableColumnWatch(m_bAllowEmptySales);
          }
        }
      }
    } catch (com.sun.star.uno.Exception e) {
      System.out.println(e);
      e.printStackTrace();
    }
  }
Example #2
0
  /** creates our sample document */
  @Override
  protected void prepareDocument() throws com.sun.star.uno.Exception, java.lang.Exception {
    super.prepareDocument();

    m_database = new HsqlDatabase(m_xCtx);

    // ensure that we have the tables needed for our example
    ensureTables();

    /* create some shapes */
    XPropertySet xSNRField = m_formLayer.insertControlLine("NumericField", "SNR", "", 3);
    m_formLayer.insertControlLine("TextField", "FIRSTNAME", "", 11);
    m_formLayer.insertControlLine("TextField", "LASTNAME", "", 19);
    m_formLayer.insertControlLine("TextField", "STREET", "", 27);
    m_formLayer.insertControlLine("TextField", "STATE", "", 35);
    XPropertySet xZipField = m_formLayer.insertControlLine("NumericField", "ZIP", "", 43);
    m_formLayer.insertControlLine("FormattedField", "BIRTHDATE", "", 51);

    // for the salesman number / zip code, we don't want to have decimal places:
    xSNRField.setPropertyValue("DecimalAccuracy", Short.valueOf((short) 0));
    xZipField.setPropertyValue("DecimalAccuracy", Short.valueOf((short) 0));

    /**
     * need the form the control models belong to for this, we simply obtain the parent for any of
     * the control models we have
     *
     * <p>Note that this involves knowledge about the implementation: If a control shape is inserted
     * into a document, where the control model does not belong to the form component hierarchy,
     * yet, it is automatically inserted into the first form, which is created if necessary.
     */
    m_xMasterForm = FLTools.getParent(xZipField);

    // set the data source signature at the form
    m_xMasterForm.setPropertyValue("DataSourceName", m_database.getDocumentURL());
    m_xMasterForm.setPropertyValue("CommandType", Integer.valueOf(CommandType.TABLE));
    m_xMasterForm.setPropertyValue("Command", "SALESMEN");

    // insert the buttons
    // create our button operator, if necessary
    m_aOperator = new ButtonOperator(m_xCtx, m_document, m_xMasterForm);

    createButton(2, 63, 8, "first", "<<", FormFeature.MoveToFirst);
    createButton(12, 63, 8, "prev", "<", FormFeature.MoveToPrevious);
    createButton(22, 63, 8, "next", ">", FormFeature.MoveToNext);
    createButton(32, 63, 8, "last", ">>", FormFeature.MoveToLast);
    createButton(42, 63, 8, "new", ">*", FormFeature.MoveToInsertRow);
    createButton(58, 63, 13, "reload", "reload", FormFeature.ReloadForm);

    // create a sub form for the sales

    // for this, first create a sub form and bind it to the SALES table
    XIndexContainer xSalesForm = m_document.createSubForm(m_xMasterForm, "Sales");
    XPropertySet xSalesFormProps = UNO.queryPropertySet(xSalesForm);

    xSalesFormProps.setPropertyValue("DataSourceName", m_database.getDocumentURL());
    xSalesFormProps.setPropertyValue("CommandType", Integer.valueOf(CommandType.COMMAND));

    String sCommand = "SELECT * FROM ";
    sCommand += s_tableNameSales;
    sCommand += " WHERE " + s_tableNameSales + ".SNR = :salesmen";
    xSalesFormProps.setPropertyValue("Command", sCommand);

    // the master-details connection
    String[] aMasterFields = new String[] {"SNR"}; // the field in the master form
    String[] aDetailFields = new String[] {"salesmen"}; // the name in the detail form
    xSalesFormProps.setPropertyValue("MasterFields", aMasterFields);
    xSalesFormProps.setPropertyValue("DetailFields", aDetailFields);

    // the create thr grid model
    XPropertySet xSalesGridModel =
        m_formLayer.createControlAndShape("GridControl", 2, 80, 162, 40, xSalesForm);
    xSalesGridModel.setPropertyValue("Name", "SalesTable");
    XPropertySet xKeyColumn = createGridColumn(xSalesGridModel, "NumericField", "SALENR", 12);
    XPropertySet xCustomerColumn = createGridColumn(xSalesGridModel, "ListBox", "COS_NR", 40);
    XPropertySet xSalesNameColumn = createGridColumn(xSalesGridModel, "TextField", "NAME", 25);
    createGridColumn(xSalesGridModel, "DateField", "SALEDATE", 24);
    createGridColumn(xSalesGridModel, "CurrencyField", "PRICE", 16);

    // please note that a better solution for the SALEDATE field would have been to use
    // a FormattedField. But we want to demonstrate some effects with DateFields here ...

    m_aSalesNameValidator = new GridFieldValidator(m_xCtx, xSalesNameColumn);
    m_aSalesNameValidator.enableColumnWatch(m_bAllowEmptySales);

    xKeyColumn.setPropertyValue("DecimalAccuracy", Short.valueOf((short) 0));

    // init the list box which is for choosing the customer a sale belongs to
    xCustomerColumn.setPropertyValue("BoundColumn", Short.valueOf((short) 1));
    xCustomerColumn.setPropertyValue("Label", "Customer");
    xCustomerColumn.setPropertyValue("ListSourceType", ListSourceType.SQL);

    String sListSource = "SELECT LASTNAME, COS_NR FROM ";
    sListSource += s_tableNameCustomers;
    String[] aListSource = new String[] {sListSource};
    xCustomerColumn.setPropertyValue("ListSource", aListSource);

    // We want to demonstrate how to reset fields to NULL, we do this with the SALEDATE field
    // above. For this, we add as reset listener to the form
    XReset xFormReset = UNO.queryReset(xSalesForm);
    xFormReset.addResetListener(this);

    // the option for filtering the sales form
    XIndexContainer xSalesFilterForm = m_document.createSiblingForm(xSalesForm, "SalesFilter");
    XPropertySet xSFFProps = UNO.queryPropertySet(xSalesFilterForm);
    XPropertySet xLabel =
        m_formLayer.createControlAndShape("FixedText", 2, 125, 35, 6, xSalesFilterForm);
    xLabel.setPropertyValue("Label", "show only sales since");
    xLabel.setPropertyValue("Name", "FilterLabel");

    XPropertySet xFilterSelection =
        m_formLayer.createControlAndShape("ListBox", 40, 125, 59, 6, xSalesFilterForm);
    xFilterSelection.setPropertyValue("Name", "FilterList");
    xFilterSelection.setPropertyValue("LabelControl", xLabel);
    XPropertySet xManualFilter =
        m_formLayer.createControlAndShape("DateField", 104, 125, 30, 6, xSalesFilterForm);
    xManualFilter.setPropertyValue("Name", "ManualFilter");
    XPropertySet xApplyFilter =
        m_formLayer.createControlAndShape("CommandButton", 139, 125, 25, 6, xSalesFilterForm);
    xApplyFilter.setPropertyValue("Name", "ApplyFilter");
    xApplyFilter.setPropertyValue("DefaultButton", Boolean.TRUE);
    new SalesFilter(m_document, xSalesFormProps, xFilterSelection, xManualFilter, xApplyFilter);

    // the options section
    // for this, we need a form which is a sibling of our master form (don't want to interfere
    // the controls which represent options only with the controls which are used for data access)

    XIndexContainer xOptionsForm = m_document.createSiblingForm(m_xMasterForm, "Options");

    xLabel = m_formLayer.createControlAndShape("GroupBox", 98, 0, 66, 62, xOptionsForm);
    xLabel.setPropertyValue("Name", "Options");
    xLabel.setPropertyValue("Label", "Options");

    // radio buttons which controls how we generate unique keys
    xLabel = m_formLayer.createControlAndShape("GroupBox", 103, 5, 56, 25, xOptionsForm);
    xLabel.setPropertyValue("Label", "key generation");
    xLabel.setPropertyValue("Name", "KeyGeneration");
    XPropertySet xKeyGen =
        m_formLayer.createControlAndShape("RadioButton", 106, 11, 50, 6, xOptionsForm);
    xKeyGen.setPropertyValue("Name", "KeyGen");
    xKeyGen.setPropertyValue("Label", "no automatic generation");
    xKeyGen.setPropertyValue("RefValue", "none");
    xKeyGen.addPropertyChangeListener("State", this);

    xKeyGen = m_formLayer.createControlAndShape("RadioButton", 106, 17, 50, 6, xOptionsForm);
    xKeyGen.setPropertyValue("Name", "KeyGen");
    xKeyGen.setPropertyValue("Label", "before inserting a record");
    xKeyGen.setPropertyValue("RefValue", "update");
    xKeyGen.addPropertyChangeListener("State", this);

    xKeyGen = m_formLayer.createControlAndShape("RadioButton", 106, 23, 50, 6, xOptionsForm);
    xKeyGen.setPropertyValue("Name", "KeyGen");
    xKeyGen.setPropertyValue("Label", "when moving to a new record");
    xKeyGen.setPropertyValue("RefValue", "reset");
    xKeyGen.addPropertyChangeListener("State", this);

    // initialize listeners
    // master form - key generation
    m_aSalesmanKeyGenerator = new KeyGenerator(m_xMasterForm, "SNR", m_xCtx);
    m_aSalesmanKeyGenerator.activateKeyGenerator(true);
    // master form - control locking
    m_aSalesmenLocker = new ControlLock(m_xMasterForm, "SNR");
    m_aSalesmenLocker.enableLock(m_bProtectKeyFields);

    // details form - key generation
    m_aSalesKeyGenerator = new KeyGenerator(xSalesFormProps, "SALENR", m_xCtx);
    m_aSalesKeyGenerator.activateKeyGenerator(true);

    // details form - control locking
    m_aSalesLocker = new ControlLock(xSalesFormProps, "SALENR");
    m_aSalesLocker.enableLock(m_bProtectKeyFields);

    // initially, we want to generate keys when moving to a new record
    xKeyGen.setPropertyValue("DefaultState", Short.valueOf((short) 1));

    // second options block
    xLabel = m_formLayer.createControlAndShape("GroupBox", 103, 33, 56, 25, xOptionsForm);
    xLabel.setPropertyValue("Name", "Misc");
    xLabel.setPropertyValue("Label", "Miscellaneous");

    XPropertySet xCheck =
        m_formLayer.createControlAndShape("CheckBox", 106, 39, 60, 6, xOptionsForm);
    xCheck.setPropertyValue("Name", "defaultdate");
    xCheck.setPropertyValue("Label", "default sales date to \"today\"");
    xCheck.setPropertyValue(
        "HelpText",
        "When checked, newly entered sales records are pre-filled with today's date, else left empty.");
    xCheck.addPropertyChangeListener("State", this);

    xCheck = m_formLayer.createControlAndShape("CheckBox", 106, 45, 60, 6, xOptionsForm);
    xCheck.setPropertyValue("Name", "protectkeys");
    xCheck.setPropertyValue("Label", "protect key fields from editing");
    xCheck.setPropertyValue(
        "HelpText",
        "When checked, you cannot modify the values in the table's key fields (SNR and SALENR)");
    xCheck.addPropertyChangeListener("State", this);

    xCheck = m_formLayer.createControlAndShape("CheckBox", 106, 51, 60, 6, xOptionsForm);
    xCheck.setPropertyValue("Name", "emptysales");
    xCheck.setPropertyValue("Label", "check for empty sales names");
    xCheck.setPropertyValue(
        "HelpText",
        "When checked, you cannot enter empty values into the NAME column of the 'Sales' table.");
    xCheck.addPropertyChangeListener("State", this);

    // dump the form component tree
    enumFormComponents();
  }