Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

74
LINES

< > BotCompany Repo | #1035747 // JTargetAndActualSlider

JavaX fragment (include) [tags: use-pretranspiled]

Transpiled version (12320L) is out of date.

sclass JTargetAndActualSlider extends MetaWithChangeListeners is Swingable {
  new TargetAndActual<Double> data;
  settableWithVar bool showSlider = true;
  settableWithVar bool sliderEnabled = true;
  settableWithVar double min;
  settableWithVar double max = 100;
  settableWithVar int decimalsToShow = 1;
  settableWithVar int widthOfNumbers = 50;
  settableWithVar S unit;
  settableWithVar S description;
  settableWithVar int preferredBarWidth = 50;
  
  // set to false to use ints
  settable bool scaling = true;

  transient JColorBar bar;
  transient JSlider slider;
  transient JLabel lblActual, lblTarget;
  
  *() {}
  *(TargetAndActual<Double> *data) {}
  
  double value() { ret unnull(data.value()); }
  double target() { ret unnull(data.target()); }

  cachedVisualize {
    bar = swing(-> new JColorBar().max(max));
    jPreferWidth(bar, preferredBarWidth);
    if (showSlider) {
      slider = scaling
        ? jLiveValueSlider_double_bothWays(min, max, data.varTarget())
        : jLiveValueSlider_double_noScaling(iround(min), iround(max), data.varTarget());
      jPreferWidth(slider, preferredBarWidth);
    }
    lblActual = jlabel();
    lblTarget = jlabel();
    data.varActual().onChangeAndNow(-> {
      bar.setValue(value());
      setText(lblActual, renderValue(value()));
    });
    data.varTarget().onChangeAndNow(-> {
      setText(lblTarget, renderValue(target()));
    });
      
    varSliderEnabled().onChangeAndNow(-> setEnabled(slider, sliderEnabled));
    
    ret vstack/*WithSpacing*/(
      slider == null ?: westCenterAndEastWithMargin(
        jlabel(spaceCombine("Target", description)), slider,
        jMinWidth(widthOfNumbers, lblTarget)),
      westCenterAndEastWithMargin(
        jlabel(spaceCombine("Actual", description)), bar,
        jMinWidth(widthOfNumbers, lblActual)));
  }
  
  swappable S renderValue(double value) {
    ret spaceCombine(!scaling ? n2(iround(value)) : formatDoubleX(value, decimalsToShow), unit);
  }
  
  JSlider slider() {
    visualize();
    ret slider;
  }
  
  selfType target(double target) {
    data.target(target);
    this;
  }
  
  selfType value(double value) {
    data.value(value);
    this;
  }
}

Author comment

Began life as a copy of #1035746

download  show line numbers  debug dex  old transpilations   

Travelled to 2 computer(s): elmgxqgtpvxh, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1035747
Snippet name: JTargetAndActualSlider
Eternal ID of this version: #1035747/29
Text MD5: 22c06f226810d4b9c563952d5feab939
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-07-29 22:43:56
Source code size: 2267 bytes / 74 lines
Pitched / IR pitched: No / No
Views / Downloads: 190 / 382
Version history: 28 change(s)
Referenced in: [show references]