<commit_before>package uk.ac.surrey.soc.cress.extrawidgets.slider

import java.awt.BorderLayout.CENTER
import java.awt.BorderLayout.EAST
import java.awt.BorderLayout.NORTH

import org.nlogo.window.GUIWorkspace

import javax.swing.BorderFactory.createEmptyBorder
import javax.swing.JLabel
import javax.swing.JSlider
import uk.ac.surrey.soc.cress.extrawidgets.api.IntegerPropertyDef
import uk.ac.surrey.soc.cress.extrawidgets.api.LabeledPanelWidget
import uk.ac.surrey.soc.cress.extrawidgets.api.StateUpdater
import uk.ac.surrey.soc.cress.extrawidgets.api.WidgetKey
import uk.ac.surrey.soc.cress.extrawidgets.api.swing.enrichSlider

class Slider(
  val key: WidgetKey,
  val stateUpdater: StateUpdater,
  ws: GUIWorkspace)
  extends LabeledPanelWidget {

  override def borderPadding = createEmptyBorder(0, 4, 0, 4)
  override def labelPosition = CENTER
  val slider = new JSlider()
  val valueLabel = new JLabel(slider.getValue.toString)
  add(slider, NORTH)
  add(valueLabel, EAST)

  val xwMinimum = new IntegerPropertyDef(this,
    slider.setMinimum(_),
    slider.getMinimum)

  val xwMaximum = new IntegerPropertyDef(this,
    slider.setMaximum(_),
    slider.getMaximum)

  val xwValue = new IntegerPropertyDef(this,
    slider.setValue(_),
    slider.getValue)

  slider.onStateChange { _ ⇒
    valueLabel.setText(xwValue.toString)
    xwValue.updateInState()
  }

}
<commit_msg>support floating points in sliders, close #83<commit_after>package uk.ac.surrey.soc.cress.extrawidgets.slider

import java.awt.BorderLayout.CENTER
import java.awt.BorderLayout.EAST
import java.awt.BorderLayout.NORTH

import org.nlogo.api.Dump
import org.nlogo.window.GUIWorkspace
import org.nlogo.window.SliderData

import javax.swing.BorderFactory.createEmptyBorder
import javax.swing.JLabel
import javax.swing.JSlider
import uk.ac.surrey.soc.cress.extrawidgets.api.DoublePropertyDef
import uk.ac.surrey.soc.cress.extrawidgets.api.LabeledPanelWidget
import uk.ac.surrey.soc.cress.extrawidgets.api.StateUpdater
import uk.ac.surrey.soc.cress.extrawidgets.api.StringPropertyDef
import uk.ac.surrey.soc.cress.extrawidgets.api.WidgetKey
import uk.ac.surrey.soc.cress.extrawidgets.api.swing.enrichSlider

class Slider(
  val key: WidgetKey,
  val stateUpdater: StateUpdater,
  ws: GUIWorkspace)
  extends LabeledPanelWidget {

  val sliderData = new SliderData() {
    def update(v: Double) = valueSetter(coerceValue(v))
  }

  val slider = new JSlider() {
    minorTickSpacing = 1
    snapToTicks = true
    updateFromData()
    def updateFromData(): Unit = {
      val nbUnits = (sliderData.maximum - sliderData.minimum) / sliderData.increment
      setMaximum(nbUnits.intValue)
      setValue((nbUnits / sliderData.increment).intValue)
    }
  }
  add(slider, NORTH)

  override def borderPadding = createEmptyBorder(0, 4, 0, 4)
  override def labelPosition = CENTER

  val valueLabel = new JLabel() {
    def update(): Unit = setText(valueString(sliderData.value))
    update()
  }
  add(valueLabel, EAST)

  private var units = ""
  val xwUnits = new StringPropertyDef(this,
    u ⇒ { units = u; valueLabel.update() },
    () ⇒ units)

  val xwMinimum = new DoublePropertyDef(this,
    min ⇒ { sliderData.minimum = min; slider.updateFromData() },
    () ⇒ sliderData.minimum)

  val xwMaximum = new DoublePropertyDef(this,
    max ⇒ { sliderData.maximum = max; slider.updateFromData() },
    () ⇒ sliderData.maximum)

  val xwValue = new DoublePropertyDef(this,
    v ⇒ if (sliderData.update(v)) slider.updateFromData(),
    () ⇒ sliderData.value)

  val xwIncrement = new DoublePropertyDef(this,
    inc ⇒ { sliderData.increment = inc; slider.updateFromData() },
    () ⇒ sliderData.increment)

  slider.onStateChange { _ ⇒
    sliderData.update(slider.getValue * sliderData.increment)
    valueLabel.update()
    xwValue.updateInState()
  }

  /** copied from org.nlogo.window.AbstractSliderWidget */
  def valueString(num: Double): String = {
    var numString = Dump.number(num)
    var place = numString.indexOf('.')
    val p = sliderData.precision
    if (p > 0 && place == -1) {
      numString += "."
      place = numString.length - 1
    }
    if (place != -1 && numString.indexOf('E') == -1) {
      val padding = p - (numString.length - place - 1)
      numString = numString + ("0" * padding)
    }
    if (units == "") numString else numString + " " + units
  }
}

{'fraction_non_alphanumeric': 0.11205316223648029, 'fraction_numerical': 0.004124656278643446, 'mean_word_length': 3.9266365688487586, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1224784', 'n_tokens_mistral': 1525, 'n_tokens_neox': 1482, 'n_words': 295}