/**
   * Action Listener - start dialog
   *
   * @param e Event
   */
  public void actionPerformed(ActionEvent e) {
    if (e.getActionCommand().equals(RecordInfo.CHANGE_LOG_COMMAND)) {
      RecordInfo.start(m_mField);
      return;
    }
    if (!m_button.isEnabled()) return;
    m_button.setEnabled(false);
    //
    Integer oldValue = (Integer) getValue();
    int S_ResourceAssignment_ID = oldValue == null ? 0 : oldValue.intValue();
    MResourceAssignment ma = new MResourceAssignment(Env.getCtx(), S_ResourceAssignment_ID, null);

    //	Start VAssignment Dialog
    if (S_ResourceAssignment_ID != 0) {
      VAssignmentDialog vad = new VAssignmentDialog(Env.getFrame(this), ma, true, true);
      ma = vad.getMResourceAssignment();
    }
    //	Start InfoSchedule directly
    else {
      InfoSchedule is = new InfoSchedule(Env.getFrame(this), ma, true);
      ma = is.getMResourceAssignment();
    }

    //	Set Value
    if (ma != null && ma.getS_ResourceAssignment_ID() != 0) {
      setValue(new Integer(ma.getS_ResourceAssignment_ID()));
      try {
        fireVetoableChange(this.getName(), new Object(), getValue());
      } catch (PropertyVetoException pve) {
        log.log(Level.SEVERE, "", pve);
      }
    }
    m_button.setEnabled(true);
    requestFocus();
  } //	actionPerformed
  public void onEvent(Event event) throws Exception {
    //
    if (Events.ON_CLICK.equalsIgnoreCase(event.getName())) {
      Integer oldValue = (Integer) getValue();
      int S_ResourceAssignment_ID = oldValue == null ? 0 : oldValue.intValue();
      MResourceAssignment ma = new MResourceAssignment(Env.getCtx(), S_ResourceAssignment_ID, null);

      //	Start VAssignment Dialog
      if (S_ResourceAssignment_ID != 0) {
        WAssignmentDialog vad = new WAssignmentDialog(ma, true, true);
        ma = vad.getMResourceAssignment();
      }
      //	Start InfoSchedule directly
      else {
        InfoSchedule is = new InfoSchedule(ma, true);
        ma = is.getMResourceAssignment();
      }

      //	Set Value
      if (ma != null && ma.getS_ResourceAssignment_ID() != 0) {
        setValue(new Integer(ma.getS_ResourceAssignment_ID()));
        ValueChangeEvent vce =
            new ValueChangeEvent(this, gridField.getColumnName(), oldValue, getValue());
        fireValueChange(vce);
      }
    }
  }
 /**
  * Check availability and insert record
  *
  * @return true if saved/updated
  */
 private boolean cmd_save() {
   log.config("");
   //	Set AssignDateTo
   Calendar date = new GregorianCalendar();
   getDateAndTimeFrom(date);
   Timestamp assignDateFrom = new Timestamp(date.getTimeInMillis());
   BigDecimal qty = fQty.getValue();
   ListItem listItem = fResource.getSelectedItem();
   KeyNamePair resource =
       listItem != null
           ? new KeyNamePair((Integer) listItem.getValue(), listItem.getLabel())
           : null;
   KeyNamePair uom = (KeyNamePair) m_lookup.get(resource);
   int minutes = MUOMConversion.convertToMinutes(Env.getCtx(), uom.getKey(), qty);
   Timestamp assignDateTo = TimeUtil.addMinutess(assignDateFrom, minutes);
   m_mAssignment.setAssignDateTo(assignDateTo);
   //
   //	m_mAssignment.dump();
   return m_mAssignment.save();
 } //	cmdSave
  /**
   * Assignment Dialog.
   *
   * <pre>
   * 		Creates a new assignment oor displays an assignment
   * 		Create new:	(ID == 0)
   * 			check availability & create assignment
   * 			(confirmed when order/incoice/timeExpense is processed)
   * 			alternatively let InfoResource do the assignment
   * 			return ID
   * 		Existing assignment: (ID != 0)
   * 			if confirmed - no change.
   * 			ability to delete or change assignment
   * 			return ID
   * 	</pre>
   *
   * @param mAssignment Assignment
   * @param allowZoom allow to zoom to schedule
   * @param allowDelete allow to delete recorde
   */
  public WAssignmentDialog(
      MResourceAssignment mAssignment, boolean allowZoom, boolean allowDelete) {
    super();
    this.setTitle(Msg.getMsg(Env.getCtx(), "VAssignmentDialog"));
    this.setAttribute("mode", "modal");
    this.setBorder("normal");

    log.config(mAssignment.toString());
    m_mAssignment = mAssignment;
    try {
      init();
      if (!allowZoom) confirmPanel.getButton("Zoom").setVisible(false);
      delete.setVisible(allowDelete);
    } catch (Exception e) {
      log.log(Level.SEVERE, "", e);
    }
    setDisplay(); //	from mAssignment
    //
    AEnv.showWindow(this);
  } //	VAssignmentDialog
  /** Initialize component & values from m_mAssignment */
  private void setDisplay() {
    m_setting = true;

    //	Set Resource
    int S_Resource_ID = m_mAssignment.getS_Resource_ID();

    for (ListItem item : fResource.getItems()) {
      if ((Integer) item.getValue() == S_Resource_ID) {
        fResource.setSelectedItem(item);
        break;
      }
    }

    ListItem listItem = fResource.getSelectedItem();
    KeyNamePair check = new KeyNamePair((Integer) listItem.getValue(), listItem.getLabel());
    if (check == null || check.getKey() != S_Resource_ID) {
      if (m_mAssignment.getS_ResourceAssignment_ID() == 0) // 	new record select first
      fResource.setSelectedItem(fResource.getSelectedItem()); // 	initiates UOM display
      else log.log(Level.SEVERE, "Resource not found ID=" + S_Resource_ID);
    }

    //	Set Date, Qty
    fDateFrom.setValue(m_mAssignment.getAssignDateFrom());
    fTimeFrom.setValue(m_mAssignment.getAssignDateFrom());
    fQty.setValue(m_mAssignment.getQty());

    //	Name, Description
    fName.setValue(m_mAssignment.getName());
    fDescription.setValue(m_mAssignment.getDescription());

    //	Set Editor to R/O if confirmed
    boolean readWrite = true;
    if (m_mAssignment.isConfirmed()) readWrite = false;
    confirmPanel.getButton("Cancel").setVisible(readWrite);
    fResource.setEnabled(readWrite);
    fDateFrom.setReadonly(!readWrite);
    fQty.setEnabled(readWrite);

    m_setting = false;
  } //	dynInit
  public void onEvent(Event e) throws Exception {
    if (m_setting) return;
    //	Update Assignment
    ListItem listItem = fResource.getSelectedItem();
    KeyNamePair resource =
        listItem != null
            ? new KeyNamePair((Integer) listItem.getValue(), listItem.getLabel())
            : null;
    if (resource != null) {
      int S_Resource_ID = resource.getKey();
      m_mAssignment.setS_Resource_ID(S_Resource_ID);
    }

    Calendar date = new GregorianCalendar();
    getDateAndTimeFrom(date);

    Timestamp assignDateFrom = new Timestamp(date.getTimeInMillis());
    if (assignDateFrom != null) m_mAssignment.setAssignDateFrom(assignDateFrom);
    if (fQty.getValue() != null) {
      BigDecimal qty = fQty.getValue();
      m_mAssignment.setQty(qty);
    }
    m_mAssignment.setName((String) fName.getValue());
    m_mAssignment.setDescription((String) fDescription.getValue());

    //	Resource - Look up UOM
    if (e.getTarget() == fResource) {
      Object o = m_lookup.get(resource);
      if (o == null) lUOM.setValue(" ? ");
      else lUOM.setValue(o.toString());
    }

    //	Zoom - InfoResource
    else if (e.getTarget().getId().equals("Zoom")) {
      InfoSchedule is = new InfoSchedule(m_mAssignment, true);
      if (is.getMResourceAssignment() != null) {
        m_mAssignment = is.getMResourceAssignment();
        //	setDisplay();
        detach();
      }
      is = null;
    }

    //	cancel - return
    else if (e.getTarget().getId().equals("Cancel")) {
      m_cancel = true;
      detach();
    }

    //	delete - delete and return
    else if (e.getTarget().getId().equals("Delete")) {
      if (m_mAssignment.delete(true)) {
        m_mAssignment = null;
        detach();
      } else FDialog.error(0, this, "ResourceAssignmentNotDeleted");
    }

    //	OK - Save
    else if (e.getTarget().getId().equals("Ok")) {
      if (cmd_save()) detach();
    }
  }