public void setGenerateButtonState(
      final ViewDefinitionState state,
      final Locale locale,
      final String plugin,
      final String entityName) {
    WindowComponent window = (WindowComponent) state.getComponentByReference("window");
    FormComponent form = (FormComponent) state.getComponentByReference("form");
    RibbonActionItem generateButton =
        window.getRibbon().getGroupByName("generate").getItemByName("generate");
    RibbonActionItem deleteButton =
        window.getRibbon().getGroupByName("actions").getItemByName("delete");

    if (form.getEntityId() == null) {
      generateButton.setMessage("recordNotCreated");
      generateButton.setEnabled(false);
      deleteButton.setMessage(null);
      deleteButton.setEnabled(false);
    } else {

      Entity simpleMaterialBalanceEntity =
          dataDefinitionService.get(plugin, entityName).get(form.getEntityId());

      if (simpleMaterialBalanceEntity.getField(L_GENERATED) == null) {
        simpleMaterialBalanceEntity.setField(L_GENERATED, "0");
      }

      if ("1".equals(simpleMaterialBalanceEntity.getField(L_GENERATED))) {
        generateButton.setMessage("orders.ribbon.message.recordAlreadyGenerated");
        generateButton.setEnabled(false);
        deleteButton.setMessage("orders.ribbon.message.recordAlreadyGenerated");
        deleteButton.setEnabled(false);
      } else {
        generateButton.setMessage(null);
        generateButton.setEnabled(true);
        deleteButton.setMessage(null);
        deleteButton.setEnabled(true);
      }
    }
    generateButton.requestUpdate(true);
    deleteButton.requestUpdate(true);
    window.requestRibbonRender();
  }
  public void disableCreateButton(final ViewDefinitionState view) {
    WindowComponent window = (WindowComponent) view.getComponentByReference(L_WINDOW);

    RibbonGroup operationalTasks = window.getRibbon().getGroupByName(L_OPERATIONAL_TASKS);
    RibbonActionItem createOperationalTasks =
        operationalTasks.getItemByName(L_CREATE_OPERATIONAL_TASKS);

    if (isGenerated(view) && orderHasTechnologyAndCorrectState(view)) {
      createOperationalTasks.setEnabled(true);
    } else {
      createOperationalTasks.setEnabled(false);
    }

    createOperationalTasks.requestUpdate(true);
  }
  private void enableAddButton(ViewDefinitionState view) {
    FormComponent form = (FormComponent) view.getComponentByReference(L_FORM);
    Entity multiAssignment = form.getPersistedEntityWithIncludedFormValues();

    GridComponent workersComponent = (GridComponent) view.getComponentByReference("workers");
    WindowComponent window = (WindowComponent) view.getComponentByReference("window");
    RibbonGroup add = (RibbonGroup) window.getRibbon().getGroupByName("add");
    RibbonActionItem addMany = (RibbonActionItem) add.getItemByName("addManyWorkers");
    if (workersComponent.getEntities().isEmpty() || !multiAssignment.isValid()) {
      addMany.setEnabled(false);
      addMany.requestUpdate(true);
    } else {
      addMany.setEnabled(true);
      addMany.requestUpdate(true);
    }
  }
  public void disableCalendarButtonWhenRealizationTimeNotGenerated(final ViewDefinitionState view) {
    FormComponent form = (FormComponent) view.getComponentByReference("form");
    if (form.getEntity() == null) {
      return;
    }

    WindowComponent window = (WindowComponent) view.getComponentByReference("window");
    RibbonActionItem showOnCalendarButton =
        window
            .getRibbon()
            .getGroupByName(ProductionSchedulingConstants.VIEW_RIBBON_ACTION_ITEM_GROUP)
            .getItemByName(ProductionSchedulingConstants.VIEW_RIBBON_ACTION_ITEM_NAME);
    if (isRealizationTimeGenerated(view)) {
      showOnCalendarButton.setEnabled(true);
    } else {
      showOnCalendarButton.setEnabled(false);
    }
    showOnCalendarButton.requestUpdate(true);
  }
  public void setGridGenerateButtonState(
      final ViewDefinitionState state,
      final Locale locale,
      final String plugin,
      final String entityName) {
    WindowComponent window = (WindowComponent) state.getComponentByReference("window");
    GridComponent grid = (GridComponent) state.getComponentByReference("grid");
    RibbonActionItem deleteButton =
        window.getRibbon().getGroupByName("actions").getItemByName("delete");

    if (grid.getSelectedEntitiesIds() == null || grid.getSelectedEntitiesIds().size() == 0) {
      deleteButton.setMessage(null);
      deleteButton.setEnabled(false);
    } else {
      boolean canDelete = true;
      for (Long entityId : grid.getSelectedEntitiesIds()) {
        Entity simpleMaterialBalanceEntity =
            dataDefinitionService.get(plugin, entityName).get(entityId);

        if ((Boolean) simpleMaterialBalanceEntity.getField(L_GENERATED)) {
          canDelete = false;
          break;
        }
      }
      if (canDelete) {
        deleteButton.setMessage(null);
        deleteButton.setEnabled(true);
      } else {
        deleteButton.setMessage("orders.ribbon.message.selectedRecordAlreadyGenerated");
        deleteButton.setEnabled(false);
      }
    }

    deleteButton.requestUpdate(true);
    window.requestRibbonRender();
  }