private void commitStepLabels() {
    if (!typeEl.isOneSelected()) return;

    SliderType selectedType = SliderType.valueOf(typeEl.getSelectedKey());
    if (selectedType == SliderType.discrete || selectedType == SliderType.discrete_slider) {
      if (rubric.getStepLabels() == null) {
        rubric.setStepLabels(new ArrayList<>());
      }

      int steps = Integer.parseInt(stepsEl.getSelectedKey());
      for (int i = 0; i < stepLabels.size() && i < steps; i++) {
        StepLabelColumn stepLabel = stepLabels.get(i);
        if (i < rubric.getStepLabels().size()) {
          rubric.getStepLabels().get(i).setLabel(stepLabel.getStepLabelEl().getValue());
        } else {
          StepLabel label = new StepLabel();
          label.setId(UUID.randomUUID().toString());
          label.setLabel(stepLabel.getStepLabelEl().getValue());
          rubric.getStepLabels().add(label);
        }
      }

      if (rubric.getStepLabels().size() > steps) {
        List<StepLabel> labels = new ArrayList<>(rubric.getStepLabels().subList(0, steps));
        rubric.setStepLabels(labels);
      }
    } else {
      rubric.getStepLabels().clear();
    }
  }
  @Override
  protected void formOK(UserRequest ureq) {
    commitFields();
    commitStepLabels();

    String selectedType = typeEl.getSelectedKey();
    SliderType type = SliderType.valueOf(selectedType);
    rubric.setSliderType(type);
    if (type == SliderType.continuous) {
      rubric.setStart(1);
      rubric.setEnd(100);
      rubric.setSteps(100);
    } else {
      int steps = Integer.parseInt(stepsEl.getSelectedKey());
      rubric.setStart(1);
      rubric.setEnd(steps);
      rubric.setSteps(steps);
    }

    for (Iterator<Slider> sliderIt = rubric.getSliders().iterator(); sliderIt.hasNext(); ) {
      Slider slider = sliderIt.next();
      if (!StringHelper.containsNonWhitespace(slider.getStartLabel())
          && !StringHelper.containsNonWhitespace(slider.getEndLabel())) {
        sliderIt.remove();
      }
    }

    rubricCtrl.updateForm();

    fireEvent(ureq, new ChangePartEvent(rubric));
    fireEvent(ureq, new ClosePartEvent(rubric));
  }
 private void doAddSlider() {
   Slider slider = new Slider();
   slider.setId(UUID.randomUUID().toString());
   rubric.getSliders().add(slider);
   SliderRow row = forgeSliderRow(slider);
   sliders.add(row);
   flc.setDirty(true);
 }
  private void updateSteps() {
    List<StepLabelColumn> stepLabelColumns = new ArrayList<>();
    if (stepsEl.isVisible()
        && stepsEl.isOneSelected()
        && (typeEl.isSelected(0) || typeEl.isSelected(1))) {
      int steps = Integer.parseInt(stepsEl.getSelectedKey());
      for (int i = 0; i < steps; i++) {
        Integer step = new Integer(i);
        StepLabelColumn col = stepToColumns.get(step);
        if (col == null) {
          String label = "";
          if (rubric.getStepLabels() != null && i < rubric.getStepLabels().size()) {
            label = rubric.getStepLabels().get(i).getLabel();
          }

          TextElement textEl =
              uifactory.addTextElement(
                  "steplabel_" + count.incrementAndGet(),
                  "steplabel_" + count.incrementAndGet(),
                  null,
                  256,
                  label,
                  flc);
          textEl.setDomReplacementWrapperRequired(false);
          textEl.setDisplaySize(4);
          col = new StepLabelColumn(i, textEl);
        }

        stepLabelColumns.add(col);
      }

      int stepInPercent =
          Math.round(90.0f / steps); // 90 is empirically choose to not make a second line
      flc.contextPut("stepInPercent", stepInPercent);
    }
    stepLabels = stepLabelColumns;
    flc.contextPut("stepLabels", stepLabelColumns);
  }
  @Override
  protected void initForm(FormItemContainer formLayout, Controller listener, UserRequest ureq) {
    rubricCtrl = new RubricController(ureq, getWindowControl(), rubric, mainForm);
    listenTo(rubricCtrl);
    formLayout.add("rubric", rubricCtrl.getInitialFormItem());

    settingsLayout = FormLayoutContainer.createDefaultFormLayout("settings", getTranslator());
    settingsLayout.setRootForm(mainForm);
    formLayout.add("settings", settingsLayout);

    String[] sliderTypeValues =
        new String[] {
          translate("slider.discrete"),
          translate("slider.discrete.slider"),
          translate("slider.continuous")
        };
    typeEl =
        uifactory.addDropdownSingleselect(
            "slider.type." + count.incrementAndGet(),
            "slider.type",
            settingsLayout,
            sliderTypeKeys,
            sliderTypeValues,
            null);
    typeEl.addActionListener(FormEvent.ONCHANGE);
    typeEl.setEnabled(!restrictedEdit);
    boolean typeSelected = false;
    if (rubric != null && rubric.getSliderType() != null) {
      for (String sliderTypeKey : sliderTypeKeys) {
        if (sliderTypeKey.equals(rubric.getSliderType().name())) {
          typeEl.select(sliderTypeKey, true);
          typeSelected = true;
        }
      }
    }
    if (!typeSelected) {
      typeEl.select(sliderTypeKeys[0], true);
    }

    stepsEl =
        uifactory.addDropdownSingleselect(
            "slider.steps." + count.incrementAndGet(),
            "slider.steps",
            settingsLayout,
            sliderStepKeys,
            sliderStepKeys,
            null);
    stepsEl.addActionListener(FormEvent.ONCHANGE);
    stepsEl.setEnabled(!restrictedEdit);
    boolean stepSelected = false;
    if (rubric != null && rubric.getSteps() > 0) {
      String steps = Integer.toString(rubric.getSteps());
      for (String sliderStepKey : sliderStepKeys) {
        if (sliderStepKey.equals(steps)) {
          stepsEl.select(sliderStepKey, true);
          stepSelected = true;
        }
      }
    }
    if (!stepSelected) {
      stepsEl.select(sliderStepKeys[4], true);
    }
    updateTypeSettings();
    updateSteps();

    for (Slider slider : rubric.getSliders()) {
      SliderRow row = forgeSliderRow(slider);
      sliders.add(row);
    }

    if (formLayout instanceof FormLayoutContainer) {
      FormLayoutContainer layoutCont = (FormLayoutContainer) formLayout;
      layoutCont.contextPut("sliders", sliders);
    }

    long postfix = CodeHelper.getRAMUniqueID();
    saveButton = uifactory.addFormLink("save_" + postfix, "save", null, formLayout, Link.BUTTON);
    if (!restrictedEdit) {
      addSliderButton =
          uifactory.addFormLink(
              "add.slider." + postfix, "add.slider", null, formLayout, Link.BUTTON);
      addSliderButton.setIconLeftCSS("o_icon o_icon-lg o_icon_add");
    }
    if (formLayout instanceof FormLayoutContainer) {
      FormLayoutContainer layoutCont = (FormLayoutContainer) formLayout;
      layoutCont.contextPut("postfix", Long.toString(postfix));
    }
  }
 private void doRemoveSlider(SliderRow row) {
   updateSteps();
   sliders.remove(row);
   rubric.getSliders().remove(row.getSlider());
   flc.setDirty(true);
 }