/** Start dialog */
  private void cmd_dialog() {
    //
    Integer oldValue = (Integer) getValue();
    int oldValueInt = oldValue == null ? 0 : oldValue.intValue();
    int M_AttributeSetInstance_ID = oldValueInt;
    int M_Product_ID = Env.getContextAsInt(Env.getCtx(), m_WindowNo, "M_Product_ID");
    int M_ProductBOM_ID = Env.getContextAsInt(Env.getCtx(), m_WindowNo, "M_ProductBOM_ID");

    log.config(
        "M_Product_ID="
            + M_Product_ID
            + "/"
            + M_ProductBOM_ID
            + ",M_AttributeSetInstance_ID="
            + M_AttributeSetInstance_ID
            + ", AD_Column_ID="
            + gridField.getAD_Column_ID());

    //	M_Product.M_AttributeSetInstance_ID = 8418
    boolean productWindow = (gridField.getAD_Column_ID() == 8418); // 	HARDCODED

    //	Exclude ability to enter ASI
    boolean exclude = true;

    if (M_Product_ID != 0) {
      MProduct product = MProduct.get(Env.getCtx(), M_Product_ID);
      int M_AttributeSet_ID = Services.get(IProductBL.class).getM_AttributeSet_ID(product);
      if (M_AttributeSet_ID != 0) {
        final IAttributeExcludeBL excludeBL = Services.get(IAttributeExcludeBL.class);
        final I_M_AttributeSet attributeSet =
            InterfaceWrapperHelper.create(
                Env.getCtx(), M_AttributeSet_ID, I_M_AttributeSet.class, ITrx.TRXNAME_None);
        final I_M_AttributeSetExclude asExclude =
            excludeBL.getAttributeSetExclude(
                attributeSet, gridField.getAD_Column_ID(), Env.isSOTrx(Env.getCtx(), m_WindowNo));
        if ((null == asExclude) || (!excludeBL.isFullExclude(asExclude))) {
          exclude = false;
        }
      }
    }

    boolean changed = false;
    if (M_ProductBOM_ID != 0) // 	Use BOM Component
    M_Product_ID = M_ProductBOM_ID;
    //
    if (!productWindow && (M_Product_ID == 0 || exclude)) {
      changed = true;
      getComponent().setText(null);
      M_AttributeSetInstance_ID = 0;
    } else {
      WPAttributeDialog vad =
          new WPAttributeDialog(
              M_AttributeSetInstance_ID,
              M_Product_ID,
              m_C_BPartner_ID,
              productWindow,
              gridField.getAD_Column_ID(),
              m_WindowNo);
      if (vad.isChanged()) {
        getComponent().setText(vad.getM_AttributeSetInstanceName());
        M_AttributeSetInstance_ID = vad.getM_AttributeSetInstance_ID();
        if (m_GridTab != null && !productWindow && vad.getM_Locator_ID() > 0)
          m_GridTab.setValue("M_Locator_ID", vad.getM_Locator_ID());
        changed = true;
      }
    }
    /**
     * Selection { // Get Model MAttributeSetInstance masi = MAttributeSetInstance.get(Env.getCtx(),
     * M_AttributeSetInstance_ID, M_Product_ID); if (masi == null) { log.log(Level.SEVERE, "No Model
     * for M_AttributeSetInstance_ID=" + M_AttributeSetInstance_ID + ", M_Product_ID=" +
     * M_Product_ID); } else { Env.setContext(Env.getCtx(), m_WindowNo, "M_AttributeSet_ID",
     * masi.getM_AttributeSet_ID()); // Get Attribute Set MAttributeSet as =
     * masi.getMAttributeSet(); // Product has no Attribute Set if (as == null)
     * ADialog.error(m_WindowNo, this, "PAttributeNoAttributeSet"); // Product has no Instance
     * Attributes else if (!as.isInstanceAttribute()) ADialog.error(m_WindowNo, this,
     * "PAttributeNoInstanceAttribute"); else { int M_Warehouse_ID = Env.getContextAsInt (Env.getCtx
     * (), m_WindowNo, "M_Warehouse_ID"); int M_Locator_ID = Env.getContextAsInt (Env.getCtx (),
     * m_WindowNo, "M_Locator_ID"); String title = ""; PAttributeInstance pai = new
     * PAttributeInstance ( Env.getFrame(this), title, M_Warehouse_ID, M_Locator_ID, M_Product_ID,
     * m_C_BPartner_ID); if (pai.getM_AttributeSetInstance_ID() != -1) {
     * m_text.setText(pai.getM_AttributeSetInstanceName()); M_AttributeSetInstance_ID =
     * pai.getM_AttributeSetInstance_ID(); changed = true; } } } }
     */

    //	Set Value
    if (changed) {
      log.finest("Changed M_AttributeSetInstance_ID=" + M_AttributeSetInstance_ID);
      m_value = new Object(); // 	force re-query display
      if (M_AttributeSetInstance_ID == 0) setValue(null);
      else setValue(new Integer(M_AttributeSetInstance_ID));

      ValueChangeEvent vce =
          new ValueChangeEvent(this, gridField.getColumnName(), new Object(), getValue());
      fireValueChange(vce);
      if (M_AttributeSetInstance_ID == oldValueInt && m_GridTab != null && gridField != null) {
        //  force Change - user does not realize that embedded object is already saved.
        m_GridTab.processFieldChange(gridField);
      }
    } //	change
  } //  cmd_file
  /** Initialize all panel fields and editors based on {@link #asiTemplate}. */
  private final void initAttributes() {
    final Properties ctx = getCtx();
    final boolean isProductWindow = isProductWindow();
    final boolean isProcessParameter = isProcessParameter();
    final boolean isPureProductASI = isPureProductASI();
    final boolean allowSelectExistingASI = isAllowSelectExistingASI();
    final MAttributeSet as = asiTemplate.getMAttributeSet();
    Check.assumeNotNull(as, "attribute set not null");
    final boolean isASITemplateNew = asiTemplate.getM_AttributeSetInstance_ID() <= 0;

    //
    // Show Select existing ASI (if allowed)
    if (allowSelectExistingASI) {
      // New/Edit - Selection
      if (isASITemplateNew) // new ASI
      cbNewEdit.setText(msgBL.getMsg(ctx, "NewRecord"));
      else cbNewEdit.setText(msgBL.getMsg(ctx, "EditRecord"));
      cbNewEdit.addActionListener(this);
      centerPanel.add(cbNewEdit, new ALayoutConstraint(m_row++, 0));
      bSelectExistingASI.setText(msgBL.getMsg(ctx, "SelectExisting"));
      bSelectExistingASI.addActionListener(this);
      centerPanel.add(bSelectExistingASI, null);
    }

    //
    // Fetch M_Attributes
    final List<MAttribute> attributes;
    if (isProductWindow) {
      attributes = Arrays.asList(as.getMAttributes(false)); // non-instance attributes
    } else if (isPureProductASI) {
      // Regular product's attribute set instance attributes
      attributes = Arrays.asList(as.getMAttributes(true)); // all instance attributes
    } else if (isProcessParameter) {
      final IQueryBuilder<MAttribute> attributesQueryBuilder =
          queryBL
              .createQueryBuilder(MAttribute.class)
              .setContext(ctx, ITrx.TRXNAME_None)
              .addOnlyActiveRecordsFilter()
              .addOnlyContextClient();
      attributesQueryBuilder
          .orderBy()
          .addColumn(I_M_Attribute.COLUMNNAME_Name)
          .addColumn(I_M_Attribute.COLUMNNAME_M_Attribute_ID);
      attributes = attributesQueryBuilder.create().list(MAttribute.class);
    } else {
      attributes = Collections.emptyList();
    }

    //
    // Create attributes UI editors
    for (final MAttribute attribute : attributes) {
      if (!attributeExcludeBL.isExcludedAttribute(
          attribute, as, m_AD_Column_ID, attributeContext.isSOTrx())) {
        addAttributeLine(attribute);
      }
    }

    //
    // Lot
    if (isPureProductASI && as.isLot()) {
      CLabel label = new CLabel(msgBL.translate(ctx, "Lot"));
      label.setLabelFor(fieldLotString);
      centerPanel.add(label, new ALayoutConstraint(m_row++, 0));
      centerPanel.add(fieldLotString, null);
      fieldLotString.setText(asiTemplate.getLot());
      // M_Lot_ID
      // int AD_Column_ID = 9771; // M_AttributeSetInstance.M_Lot_ID
      // fieldLot = new VLookup ("M_Lot_ID", false,false, true,
      // MLookupFactory.get(getCtx(), m_WindowNo, 0, AD_Column_ID, DisplayType.TableDir));
      final String sql =
          "SELECT M_Lot_ID, Name "
              + "FROM M_Lot l "
              + "WHERE EXISTS (SELECT M_Product_ID FROM M_Product p "
              + "WHERE p.M_AttributeSet_ID="
              + asiTemplate.getM_AttributeSet_ID()
              + " AND p.M_Product_ID=l.M_Product_ID)";
      fieldLot = new CComboBox<>(DB.getKeyNamePairs(sql, true));
      label = new CLabel(msgBL.translate(ctx, "M_Lot_ID"));
      label.setLabelFor(fieldLot);
      centerPanel.add(label, new ALayoutConstraint(m_row++, 0));
      centerPanel.add(fieldLot, null);
      if (asiTemplate.getM_Lot_ID() > 0) {
        for (int i = 1; i < fieldLot.getItemCount(); i++) {
          KeyNamePair pp = fieldLot.getItemAt(i);
          if (pp.getKey() == asiTemplate.getM_Lot_ID()) {
            fieldLot.setSelectedIndex(i);
            fieldLotString.setEditable(false);
            break;
          }
        }
      }
      fieldLot.addActionListener(this);
      // New Lot Button
      if (asiTemplate.getMAttributeSet().getM_LotCtl_ID() > 0) {
        if (Env.getUserRolePermissions().isTableAccess(MLot.Table_ID, false)
            && Env.getUserRolePermissions().isTableAccess(MLotCtl.Table_ID, false)
            && !asiTemplate.isExcludeLot(m_AD_Column_ID, attributeContext.isSOTrx())) {
          centerPanel.add(bLot, null);
          bLot.addActionListener(this);
        }
      }
      // Popup
      fieldLot.addMouseListener(new VPAttributeDialog_mouseAdapter(this)); // popup
      mZoom = new CMenuItem(msgBL.getMsg(ctx, "Zoom"), Images.getImageIcon2("Zoom16"));
      mZoom.addActionListener(this);
      popupMenu.add(mZoom);
    } // Lot

    //
    // SerNo
    if (isPureProductASI && as.isSerNo()) {
      CLabel label = new CLabel(msgBL.translate(ctx, "SerNo"));
      label.setLabelFor(fieldSerNo);
      fieldSerNo.setText(asiTemplate.getSerNo());
      centerPanel.add(label, new ALayoutConstraint(m_row++, 0));
      centerPanel.add(fieldSerNo, null);
      // New SerNo Button
      if (asiTemplate.getMAttributeSet().getM_SerNoCtl_ID() != 0) {
        if (Env.getUserRolePermissions().isTableAccess(MSerNoCtl.Table_ID, false)
            && !asiTemplate.isExcludeSerNo(m_AD_Column_ID, attributeContext.isSOTrx())) {
          centerPanel.add(bSerNo, null);
          bSerNo.addActionListener(this);
        }
      }
    } // SerNo

    //
    // GuaranteeDate.
    // We are displaying it if we deal with a pure product ASI (i.e. user is not editing the ASI
    // from product window),
    // and if:
    // * the attribute set requires a GuaranteeDate
    // * or if the ASI has a GuaranteeDate already set
    if (isPureProductASI && (as.isGuaranteeDate() || asiTemplate.getGuaranteeDate() != null)) {
      CLabel label = new CLabel(msgBL.translate(ctx, "GuaranteeDate"));
      label.setLabelFor(fieldGuaranteeDate);
      if (isASITemplateNew) {
        Date guaranteeDate = asiTemplate.getGuaranteeDate();
        if (guaranteeDate == null) {
          guaranteeDate =
              attributesBL.calculateBestBeforeDate(
                  ctx,
                  m_M_Product_ID, // product
                  attributeContext.getC_BPartner_ID(), // vendor bpartner
                  Env.getDate(ctx) // dateReceipt
                  );
        }
        fieldGuaranteeDate.setValue(guaranteeDate);
      } else {
        fieldGuaranteeDate.setValue(asiTemplate.getGuaranteeDate());
      }
      centerPanel.add(label, new ALayoutConstraint(m_row++, 0));
      centerPanel.add(fieldGuaranteeDate, null);
      fieldGuaranteeDateDisplayed = true;
    } // GuaranteeDate

    // Make sure we have at least something to edit or something to select,
    // else there is no point in showing empty this window.
    if (m_row == 0) {
      throw new AdempiereException("@PAttributeNoInfo@");
    }

    //
    // New/Edit Window
    if (allowSelectExistingASI) {
      cbNewEdit.setSelected(isASITemplateNew);
      cmd_newEdit();
    }

    //
    // Attrribute Set Instance Description
    {
      final CLabel labelDescription = new CLabel(msgBL.translate(ctx, "Description"));
      labelDescription.setLabelFor(fieldDescription);
      fieldDescription.setText(asiTemplate.getDescription());
      fieldDescription.setEditable(false);
      centerPanel.add(labelDescription, new ALayoutConstraint(m_row++, 0));
      centerPanel.add(fieldDescription, null);
    }

    // Window usually to wide (??)
    {
      final Dimension dd = centerPanel.getPreferredSize();
      dd.width = Math.min(500, dd.width);
      centerPanel.setPreferredSize(dd);
    }
  } // initAttribute