public int insertAndValidateForm() {
    boolean kodeValid = true;
    boolean inputValid = true;
    String theId = (String) view.getFieldId().getConvertedValue();

    if (theId.trim().equalsIgnoreCase("New")) {
      kodeValid = false;
      Notification.show("ID tidak boleh New!!", Notification.TYPE_TRAY_NOTIFICATION);
      view.focustIdOrDesc();
      return 1;
    }

    //		//VALIDASI KODE TIDAK DIPERLUKAN LAGI
    //		if (model.getfAreaJpaService().findAll(theId).size()>0){
    //			kodeValid = false;
    //			Notification.show("Kode Sudah Terpakai!!.");
    //			view.focustIdOrDesc();
    //			return 1;
    //		}

    // 2. Additional Validasi input di handle oleh form Validator
    // 3. Aksi Save
    if (kodeValid && inputValid) {
      try {
        // 3.1 commit form >> MASUKKAN KE DATABASE
        model.getBinderHeader().commit(); // COMMIT TETAP PAKE NEW TAPI CLIENT SIDE

        model.getsMerkJpaService().createObject(model.getNewItemHeader());

        // 3.2 Masukkan nilai form yang valid(commit) ke dalam tabel
        model.getBeanItemContainerHeader().addItem(model.getNewItemHeader());
        view.getTable().refreshRowCache();

        // 3.3. atur kondisi status form
        view.setDisplay();
        model.setOperationStatus(EnumOperationStatus.OPEN.getStrCode());
        view.setFormButtonAndTextState();
        // 3.4. Refresh lagi container >> SUDAH TIDAK DIPERLUKAN LAGI OLEH KARENA OPERSI //3.2

        Notification.show("Penambahan Sukses!!!", Notification.TYPE_TRAY_NOTIFICATION);
        try {
          view.getTable().focus();
        } catch (Exception ex) {
        }

      } catch (Exception ex) {
        Notification.show("Terjadi Kesalahan Insert data!!!", Notification.TYPE_TRAY_NOTIFICATION);
        view.focustIdOrDesc();
        ex.printStackTrace();
        return 1;
      }
    } else {
      return 2;
    }

    return 0;
  }
  @Override
  public void buttonClick(ClickEvent event) {
    // TODO Auto-generated method stub
    // Antisipasi
    if (model.getOperationStatus() == null)
      model.setOperationStatus(EnumOperationStatus.OPEN.getStrCode());
    if (model.getOperationStatus().equals(""))
      model.setOperationStatus(EnumOperationStatus.OPEN.getStrCode());

    if (event.getButton() == view.getBtnNewForm()) {
      addItem();
      view.showWindowForm();
    } else if (event.getButton() == view.getBtnEditForm()) {
      view.showWindowForm();
    } else if (event.getButton() == view.getBtnDeleteForm()) {
      deleteForm();
    } else if (event.getButton() == view.getBtnSaveForm()) {
      ConfirmDialog commitDialog =
          ConfirmDialog.show(
              view.getUI(),
              "Konfirmasi Simpan",
              "Simpan Data?",
              "Save",
              "No",
              new ConfirmDialog.Listener() {
                public void onClose(ConfirmDialog dialog) {
                  if (dialog.isConfirmed()) {
                    // Confirmed to continue
                    try {
                      if (model
                          .getOperationStatus()
                          .equals(EnumOperationStatus.ADDING.getStrCode())) {
                        if (insertAndValidateForm() == 0) {
                          view.getTable().focus();
                          view.closeWindowForm();
                        } else {
                          view.focustIdOrDesc();
                        }
                      } else if (model
                          .getOperationStatus()
                          .equals(EnumOperationStatus.EDITING.getStrCode())) {
                        if (updateAndValidateForm() == 0) {
                          view.getTable().focus();
                          view.closeWindowForm();
                        } else {
                          view.focustIdOrDesc();
                        }
                      }

                    } catch (Exception ex) {
                      Notification.show("Terjadi Kesalahan Simpan");
                    }

                  } else {
                    view.focustIdOrDesc();
                  }
                }
              });
      commitDialog.setStyleName("dialog");
      commitDialog.getOkButton().setStyleName("small");
      commitDialog.getCancelButton().setStyleName("small");
      // Jangan lupa
      commitDialog.focus();

    } else if (event.getButton() == view.getBtnCancelForm()) {
      if (model.getOperationStatus().equals(EnumOperationStatus.ADDING.getStrCode())) {
        discardForm();
      } else if (model.getOperationStatus().equals(EnumOperationStatus.ADDING.getStrCode())
          || model.getOperationStatus().equals(EnumOperationStatus.EDITING.getStrCode())) {
        if (view.getFormLayout().isVisible()) {
          discardForm();
        }
      } else {
        view.getTable().focus();
      }

      //			discardForm(operationStatus);
    } else if (event.getButton() == view.getBtnSearch()) {
      searchForm();
    } else if (event.getButton() == view.getBtnPrint()) {
      printForm();
    } else if (event.getButton() == view.getBtnHelp()) {
      helpForm();
    } else if (event.getButton() == view.getBtnUtility()) {
      view.showWindowUtility();
    }
    // Tidak semua akan di refresh container nya >> Jadi refresh container tidak bisa di taruh
    // disini

  }
  public void setFormButtonAndTextState() {

    // KODE REFNO SELALU READ ONLY KARENA OTOMATIS
    if (model.getOperationStatus().equals(EnumOperationStatus.OPEN.getStrCode())) {
      tabSheet.setSelectedTab(panelUtamaDetil);
      tabSheet.getTab(panelUtamaList).setEnabled(true);

      btnNewForm.setEnabled(true);
      btnEditForm.setEnabled(true);
      btnDeleteForm.setEnabled(true);
      btnSearch.setEnabled(true);
      btnPosting.setEnabled(true);

      btnAddItem.setEnabled(false);
      btnEditItem.setEnabled(false);
      btnRemoveItem.setEnabled(false);

      btnSaveForm.setEnabled(false);
      btnCancelForm.setEnabled(false);

      tableDetil.setReadOnly(true);

      fieldNomor.setReadOnly(true);

      fieldAmount.setReadOnly(true);
      fieldAmountafterppn.setReadOnly(true);
      fieldDisc1.setReadOnly(true);
      fieldDisc1rp.setReadOnly(true);
      fieldDisc1rpafterppn.setReadOnly(true);
      fieldDisc2.setReadOnly(true);
      fieldDisc2rp.setReadOnly(true);
      fieldDisc2rpafterppn.setReadOnly(true);
      fieldDisc.setReadOnly(true);
      fieldDiscrp.setReadOnly(true);
      fieldDiscrpafterppn.setReadOnly(true);

      fieldPpnpercent.setReadOnly(true);
      fieldPpnrp.setReadOnly(true);
      fieldAmountafterdisc.setReadOnly(true);
      fieldAmountafterdiscafterppn.setReadOnly(true);

    } else if (model.getOperationStatus().equals(EnumOperationStatus.ADDING.getStrCode())) {
      tabSheet.setSelectedTab(panelUtamaDetil);
      tabSheet.getTab(panelUtamaList).setEnabled(false);
      btnNewForm.setEnabled(false);
      btnEditForm.setEnabled(false);
      btnDeleteForm.setEnabled(false);
      btnSearch.setEnabled(false);
      btnPosting.setEnabled(false);

      btnAddItem.setEnabled(true);
      btnEditItem.setEnabled(true);
      btnRemoveItem.setEnabled(true);

      btnSaveForm.setEnabled(true);
      btnCancelForm.setEnabled(true);

      tableDetil.setReadOnly(false);

      fieldNomor.setReadOnly(true);

      fieldAmount.setReadOnly(false);
      fieldAmountafterppn.setReadOnly(false);
      fieldDisc1.setReadOnly(false);
      fieldDisc1rp.setReadOnly(false);
      fieldDisc1rpafterppn.setReadOnly(false);
      fieldDisc2.setReadOnly(false);
      fieldDisc2rp.setReadOnly(false);
      fieldDisc2rpafterppn.setReadOnly(false);
      fieldDisc.setReadOnly(false);
      fieldDiscrp.setReadOnly(false);
      fieldDiscrpafterppn.setReadOnly(false);

      fieldPpnpercent.setReadOnly(false);
      fieldPpnrp.setReadOnly(false);
      fieldAmountafterdisc.setReadOnly(false);
      fieldAmountafterdiscafterppn.setReadOnly(false);

    } else if (model.getOperationStatus().equals(EnumOperationStatus.EDITING.getStrCode())) {
      tabSheet.setSelectedTab(panelUtamaDetil);
      tabSheet.getTab(panelUtamaList).setEnabled(false);

      btnNewForm.setEnabled(false);
      btnEditForm.setEnabled(false);
      btnDeleteForm.setEnabled(false);
      btnSearch.setEnabled(false);
      btnPosting.setEnabled(false);

      btnAddItem.setEnabled(true);
      btnEditItem.setEnabled(true);
      btnRemoveItem.setEnabled(true);

      btnSaveForm.setEnabled(true);
      btnCancelForm.setEnabled(true);

      tableDetil.setReadOnly(false);

      fieldNomor.setReadOnly(false);

      fieldAmount.setReadOnly(false);
      fieldAmountafterppn.setReadOnly(false);
      fieldDisc1.setReadOnly(false);
      fieldDisc1rp.setReadOnly(false);
      fieldDisc1rpafterppn.setReadOnly(false);
      fieldDisc2.setReadOnly(false);
      fieldDisc2rp.setReadOnly(false);
      fieldDisc2rpafterppn.setReadOnly(false);
      fieldDisc.setReadOnly(false);
      fieldDiscrp.setReadOnly(false);
      fieldDiscrpafterppn.setReadOnly(false);

      fieldPpnpercent.setReadOnly(false);
      fieldPpnrp.setReadOnly(false);
      fieldAmountafterdisc.setReadOnly(false);
      fieldAmountafterdiscafterppn.setReadOnly(false);
    }

    if (checkPosting.getValue() == true) {
      btnPosting.setEnabled(false);
      btnPostingBatal.setEnabled(true);
    } else {
      btnPosting.setEnabled(true);
      btnPostingBatal.setEnabled(false);
    }

    checkEndofday.setReadOnly(true);
    checkPosting.setReadOnly(true);
  }