/**
  * Editor Listener
  *
  * @param evt Event
  * @exception ValueChangeEvent if the recipient wishes to roll back.
  */
 public void valueChange(ValueChangeEvent evt) {
   if (evt.getSource() instanceof WEditor) {
     GridField changedField = ((WEditor) evt.getSource()).getGridField();
     if (changedField != null) {
       processDependencies(changedField);
       // future processCallout (changedField);
     }
   }
   String columnName = "";
   if (evt.getSource() instanceof WEditor) {
     WEditor wEditor = (WEditor) evt.getSource();
     columnName = wEditor.getGridField().getVO().Help;
   }
   processNewValue(evt.getNewValue(), columnName);
 } // valueChange
  /**
   * Get dynamic WHERE part of SQL To be overwritten by concrete classes
   *
   * @return WHERE clause
   */
  protected String getSQLWhere() {
    StringBuilder sql = new StringBuilder();

    Integer S_ResourceType_ID = (Integer) fieldResourceType.getValue();

    if (S_ResourceType_ID != null)
      sql.append(" AND rt.S_ResourceType_ID=").append(S_ResourceType_ID.intValue());

    Integer S_Resource_ID = (Integer) fieldResource.getValue();

    if (S_Resource_ID != null) sql.append(" AND r.S_Resource_ID=").append(S_Resource_ID.intValue());

    Date f = fieldFrom.getValue();
    Timestamp ts = f != null ? new Timestamp(f.getTime()) : null;

    if (ts != null) sql.append(" AND TRUNC(ra.AssignDateFrom)>=").append(DB.TO_DATE(ts, false));

    Date t = fieldTo.getValue();
    ts = t != null ? new Timestamp(t.getTime()) : null;

    if (ts != null) sql.append(" AND TRUNC(ra.AssignDateTo)<=").append(DB.TO_DATE(ts, false));

    return sql.toString();
  } // getSQLWhere
  /** @param e */
  public void valueChange(ValueChangeEvent e) {
    if (gridTab.isProcessed()) //  only active records
    {
      Object source = e.getSource();
      if (source instanceof WEditor) {
        // Elaine 2009/05/06
        WEditor editor = (WEditor) source;
        GridField gridField = editor.getGridField();

        if (gridField != null) {
          if (!gridField.isEditable(true)) {
            logger.config("(" + gridTab.toString() + ") " + e.getPropertyName());
            return;
          }
        } else if (!editor.isReadWrite()) {
          logger.config("(" + gridTab.toString() + ") " + e.getPropertyName());
          return;
        }
      } else {
        logger.config("(" + gridTab.toString() + ") " + e.getPropertyName());
        return;
      }
    } //  processed
    logger.config(
        "("
            + gridTab.toString()
            + ") "
            + e.getPropertyName()
            + "="
            + e.getNewValue()
            + " ("
            + e.getOldValue()
            + ") "
            + (e.getOldValue() == null ? "" : e.getOldValue().getClass().getName()));

    //  Get Row/Col Info
    GridTable mTable = gridTab.getTableModel();
    int row = gridTab.getCurrentRow();
    int col = mTable.findColumn(e.getPropertyName());
    //
    if (e.getNewValue() == null
        && e.getOldValue() != null
        && e.getOldValue().toString().length() > 0) //  some editors return "" instead of null
      //        	  this is the original code from GridController, don't know what it does there but
      // it breaks ignore button for web ui
      //            mTable.setChanged (true);
      mTable.setValueAt(e.getNewValue(), row, col);
    else {

      Object newValue = e.getNewValue();
      Integer newValues[] = null;

      if (newValue instanceof Integer[]) {
        newValues = ((Integer[]) newValue);
        newValue = newValues[0];

        if (newValues.length > 1) {
          Integer valuesCopy[] = new Integer[newValues.length - 1];
          System.arraycopy(newValues, 1, valuesCopy, 0, valuesCopy.length);
          newValues = valuesCopy;
        } else {
          newValues = null;
        }
      } else if (newValue instanceof Object[]) {
        logger.severe("Multiple values can only be processed for IDs (Integer)");
        throw new IllegalArgumentException(
            "Multiple Selection values not available for this field. " + e.getPropertyName());
      }

      mTable.setValueAt(newValue, row, col);
      //  Force Callout
      if (e.getPropertyName().equals("S_ResourceAssignment_ID")) {
        GridField mField = gridTab.getField(col);
        if (mField != null && mField.getCallout().length() > 0) {
          gridTab.processFieldChange(mField); //  Dependencies & Callout
        }
      }

      if (newValues != null && newValues.length > 0) {
        // Save data, since record need to be used for generating clones.
        if (!gridTab.dataSave(false)) {
          throw new AdempiereException("SaveError");
        }

        // Retrieve the current record ID
        int recordId = gridTab.getKeyID(gridTab.getCurrentRow());

        Trx trx = Trx.get(Trx.createTrxName(), true);
        trx.start();
        try {
          saveMultipleRecords(
              Env.getCtx(),
              gridTab.getTableName(),
              e.getPropertyName(),
              recordId,
              newValues,
              trx.getTrxName());
          trx.commit();
          gridTab.dataRefreshAll();
        } catch (Exception ex) {
          trx.rollback();
          logger.severe(ex.getMessage());
          throw new AdempiereException("SaveError");
        } finally {
          trx.close();
        }
      }
    }
  } // ValueChange
  public void addField(MBrowseField field, Row row, String name, String title) {
    GridFieldVO voBase =
        GridFieldVO.createStdField(field.getCtx(), p_WindowNo, 0, 0, 0, false, false, false);

    String uniqueName = field.getAD_View_Column().getColumnSQL();

    voBase.isProcess = true;
    voBase.IsDisplayed = true;
    voBase.IsReadOnly = false;
    voBase.IsUpdateable = true;
    voBase.WindowNo = p_WindowNo;

    voBase.AD_Column_ID = field.getAD_View_Column().getAD_Column_ID();
    voBase.AD_Table_ID = field.getAD_View_Column().getAD_Column().getAD_Table_ID();
    voBase.ColumnName = field.getAD_View_Column().getAD_Column().getColumnName();
    voBase.displayType = field.getAD_Reference_ID();
    voBase.AD_Reference_Value_ID = field.getAD_Reference_Value_ID();
    voBase.IsMandatory = field.isMandatory();
    voBase.IsAlwaysUpdateable = false;
    voBase.IsKey = field.isKey();

    voBase.DefaultValue = field.getDefaultValue();
    voBase.DefaultValue2 = field.getDefaultValue2();
    voBase.InfoFactoryClass = field.getInfoFactoryClass();
    voBase.FieldLength = field.getFieldLength();
    voBase.ReadOnlyLogic = field.getReadOnlyLogic();
    voBase.DisplayLogic = field.getDisplayLogic();
    voBase.VFormat = field.getVFormat();
    voBase.ValueMin = field.getValueMin();
    voBase.ValueMax = field.getValueMax();
    voBase.ValidationCode = field.getAD_Val_Rule().getCode();

    voBase.isRange = field.isRange();
    voBase.Description = field.getDescription();
    voBase.Help = uniqueName;
    voBase.Header = title;

    GridField gField = new GridField(GridFieldVO.createParameter(voBase));
    GridField gField2 = null;

    WEditor editor = WebEditorFactory.getEditor(gField, false);
    editor.setReadWrite(true);
    editor.addValueChangeListener(this);
    editor.dynamicDisplay();
    gField.addPropertyChangeListener(editor);

    Object defaultObject = null;
    Object defaultObject2 = null;

    //  Set Default
    if (field.getAD_View_Column().getAD_Column_ID() > 0) {
      defaultObject = gField.getDefault();
      if (defaultObject != null && field.getDefaultValue() != null) {
        gField.setValue(defaultObject, true);
        editor.setValue(defaultObject);
      }
    }

    gField.lookupLoadComplete();
    m_mFields.add(gField);
    m_wEditors.add(editor);

    if (DisplayType.YesNo != field.getAD_Reference_ID()) {
      Div div = new Div();
      div.setAlign("right");
      org.adempiere.webui.component.Label label = editor.getLabel();
      label.setValue(title);
      div.appendChild(label);
      if (label.getDecorator() != null) div.appendChild(label.getDecorator());
      row.appendChild(div);
    }
    row.appendChild(editor.getComponent());
    setParameter(name, editor);

    if (field.isRange()) {
      title = Msg.getMsg(Env.getCtx(), "To");
      name = name + "_To";
      voBase.Header = title;
      voBase.DefaultValue = field.getDefaultValue2();
      gField2 = new GridField(GridFieldVO.createParameter(voBase));
      gField2.lookupLoadComplete();

      WEditor editor2 = WebEditorFactory.getEditor(gField2, false);
      editor2.setReadWrite(true);
      editor2.addValueChangeListener(this);
      editor2.dynamicDisplay();
      //  Set Default
      if (field.getAD_View_Column().getAD_Column_ID() > 0) {
        defaultObject2 = gField2.getDefault();
        if (defaultObject2 != null && field.getDefaultValue2() != null) {
          gField2.setValue(defaultObject2, true);
          editor2.setValue(defaultObject2);
        }
      }
      m_mFields2.add(gField2);
      m_wEditors2.add(editor2);

      Div div = new Div();
      div.setAlign("right");
      org.adempiere.webui.component.Label label = editor2.getLabel();
      label.setValue(title);
      div.appendChild(label);
      if (label.getDecorator() != null) div.appendChild(label.getDecorator());
      row.appendChild(div);

      row.appendChild(editor2.getComponent());
      setParameter(name, editor2);
    } else {
      m_mFields2.add(null);
      m_wEditors2.add(null);
    }

    if (gField != null) processNewValue(defaultObject, gField.getVO().Help);
    if (gField2 != null) processNewValue(defaultObject2, gField2.getVO().Help);
  }
 /** Dynamic Display. */
 public void dynamicDisplay() {
   for (int i = 0; i < m_wEditors.size(); i++) {
     WEditor editor = m_wEditors.get(i);
     GridField mField = editor.getGridField();
     if (mField.isDisplayed(true)) {
       if (!editor.isVisible()) {
         editor.setVisible(true);
         if (mField.getVO().isRange) {
           // m_separators.get(i).setVisible(true);
           m_wEditors2.get(i).setVisible(true);
         }
       }
       boolean rw = mField.isEditablePara(true); // r/w - check if
       // field is Editable
       editor.setReadWrite(rw);
       editor.dynamicDisplay();
       if (mField.getVO().isRange) {
         WEditor editorRange = m_wEditors2.get(i);
         if (editorRange != null) {
           editorRange.setReadWrite(rw);
           editorRange.dynamicDisplay();
         }
       }
     } else if (editor.isVisible()) {
       editor.setVisible(false);
       if (mField.getVO().isRange) {
         // m_separators.get(i).setVisible(false);
         m_wEditors2.get(i).setVisible(false);
       }
     }
   }
 }
 /**
  * get Parameter Value
  *
  * @param id
  * @return Object Value
  */
 public Object getParamenterValue(Object key) {
   WEditor editor = (WEditor) m_search.get(key);
   if (editor != null) return editor.getValue();
   else return null;
 }
  /**
   * Static Setup - add fields to parameterPanel.
   *
   * <pre>
   * 		ResourceType	Resource	DateTimeFrom	DateTimeTo	New
   *  </pre>
   */
  private void statInit() {
    fieldFrom.setWidth("180px");
    fieldTo.setWidth("180px");

    bNew.addEventListener(Events.ON_CLICK, this);

    Grid grid = GridFactory.newGridLayout();

    Rows rows = new Rows();
    grid.appendChild(rows);

    Row row = new Row();
    rows.appendChild(row);
    row.appendChild(fieldResourceType.getLabel().rightAlign());
    row.appendChild(fieldResource.getLabel().rightAlign());
    row.appendChild(labelFrom.rightAlign());
    row.appendChild(labelTo.rightAlign());
    row.appendChild(new Label());

    row = new Row();
    rows.appendChild(row);
    row.appendChild(fieldResourceType.getComponent());
    row.appendChild(fieldResource.getComponent());
    Div div = new Div();
    div.setStyle("text-align: right;");
    div.appendChild(fieldFrom);
    row.appendChild(div);
    div = new Div();
    div.setStyle("text-align: right;");
    div.appendChild(fieldTo);
    row.appendChild(div);
    row.appendChild(bNew);

    layout = new Borderlayout();
    layout.setWidth("100%");
    layout.setHeight("100%");
    if (!isLookup()) {
      layout.setStyle("position: absolute");
    }
    this.appendChild(layout);

    North north = new North();
    layout.appendChild(north);
    north.appendChild(grid);

    Center center = new Center();
    layout.appendChild(center);
    div = new Div();
    div.appendChild(contentPanel);
    if (isLookup()) contentPanel.setWidth("99%");
    else contentPanel.setStyle("width: 99%; margin: 0px auto;");
    contentPanel.setVflex(true);
    div.setStyle("width :100%; height: 100%");
    center.appendChild(div);
    div.setVflex("1");
    div.setHflex("1");

    South south = new South();
    layout.appendChild(south);
    southBody = new Vbox();
    southBody.setWidth("100%");
    south.appendChild(southBody);
    southBody.appendChild(confirmPanel);
    southBody.appendChild(new Separator());
    southBody.appendChild(statusBar);
  }