content: /*
   * The MIT License (MIT)
   *
   * Copyright (c) 2011-2021 Broad Institute, Aiden Lab, Rice University, Baylor College of Medicine
   *
   * Permission is hereby granted, free of charge, to any person obtaining a copy
   * of this software and associated documentation files (the "Software"), to deal
   * in the Software without restriction, including without limitation the rights
   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
   * copies of the Software, and to permit persons to whom the Software is
   * furnished to do so, subject to the following conditions:
   *
   * The above copyright notice and this permission notice shall be included in
   * all copies or substantial portions of the Software.
   *
   *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   *  FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
   *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
   *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
   *  THE SOFTWARE.
   */
  
  package juicebox.mapcolorui;
  
  import javax.swing.*;
  
  /**
   * An extension of JSlider to select a range of values using two thumb controls.
   * The thumb controls are used to select the lower and upper value of a range
   * with predetermined minimum and maximum values.
   * <p/>
   * <p>Note that RangeSlider makes use of the default BoundedRangeModel, which
   * supports an inner range defined by a value and an extent.  The upper value
   * returned by RangeSlider is simply the lower value plus the extent.</p>
   *
   * @author Ernest Yu, Jim Robinson, Muhammad S Shamim
   */
  class RangeSlider extends JSlider {
  
      private static final long serialVersionUID = 9000031;
      private RangeSliderUI rangeSliderUI;
  
      /**
       * Constructs a RangeSlider with default minimum and maximum values of 0
       * and 100.
       */
      public RangeSlider() {
          initSlider();
      }
  
      /**
       * Initializes the slider by setting default properties.
       */
      private void initSlider() {
          setOrientation(HORIZONTAL);
      }
  
      /**
       * Overrides the superclass method to install the UI delegate to draw two
       * thumbs.
       */
      @Override
      public void updateUI() {
          rangeSliderUI = new RangeSliderUI(this);
          setUI(rangeSliderUI);
          // Update UI for slider labels.  This must be called after updating the
          // UI of the slider.  Refer to JSlider.updateUI().
          updateLabelUIs();
      }
  
  
      public int getLowerValue() {
          return getValue();
      }
  
      public void setLowerValue(int value) {
  
          setValue(value);
      }
  
      /**
       * Sets the lower value in the range.
       */
      @Override
      public void setValue(int value) {
          int oldValue = getValue();
          if (oldValue == value) {
              return;
          }
  
          // Compute new value and extent to maintain upper value.
          int oldExtent = getExtent();
          int newValue = Math.min(Math.max(getMinimum(), value), oldValue + oldExtent);
          int newExtent = oldExtent + oldValue - newValue;
  
          // Set new value and extent, and fire a single change event.
          getModel().setRangeProperties(newValue, newExtent, getMinimum(),
                  getMaximum(), getValueIsAdjusting());
      }
  
      /**
       * Returns the upper value in the range.
       */
      public int getUpperValue() {
          return getValue() + getExtent();
      }
  
      /**
       * Sets the upper value in the range.
       */
      public void setUpperValue(int value) {
          // Compute new extent.
          int lowerValue = getValue();
          int newExtent = Math.min(Math.max(0, value - lowerValue), getMaximum() - lowerValue);
  
          // Set extent to set upper value.
          setExtent(newExtent);
      }
  
      public void setDisplayToOE(boolean colorIsOE) {
          rangeSliderUI.setDisplayToOE(colorIsOE);
      }
  
      public void setDisplayToBlank(boolean colorIsBlank) {
          rangeSliderUI.setDisplayToBlank(colorIsBlank);
      }
  }