Example #1
0
 private static Integer getMaxSliderValue(JSlider slider) {
   Dictionary dictionary = slider.getLabelTable();
   if (dictionary != null) {
     Enumeration keys = dictionary.keys();
     int max = slider.getMinimum() - 1;
     while (keys.hasMoreElements()) {
       max = Math.max(max, ((Integer) keys.nextElement()).intValue());
     }
     if (max == slider.getMinimum() - 1) {
       return null;
     }
     return new Integer(max);
   }
   return null;
 }
Example #2
0
  private static int getSliderBaseline(JSlider slider, int height) {
    // We don't handle GTK as too much is hidden to be able to calculate it
    if (slider.getPaintLabels() && !isGTK()) {
      boolean isAqua = isAqua();
      FontMetrics metrics = slider.getFontMetrics(slider.getFont());
      Insets insets = slider.getInsets();
      Insets focusInsets = (Insets) UIManager.get("Slider.focusInsets");
      if (slider.getOrientation() == JSlider.HORIZONTAL) {
        int tickLength = 8;
        int contentHeight =
            height - insets.top - insets.bottom - focusInsets.top - focusInsets.bottom;
        int thumbHeight = 20;
        if (isMetal()) {
          tickLength = ((Integer) UIManager.get("Slider.majorTickLength")).intValue() + 5;
          thumbHeight = UIManager.getIcon("Slider.horizontalThumbIcon").getIconHeight();
        } else if (isWindows() && isXP()) {
          // NOTE: this is not correct, this should come from
          // the skin (in >= 1.5), but short of reflection
          // hacks we don't have access to the real value.
          thumbHeight++;
        }
        int centerSpacing = thumbHeight;
        if (isAqua || slider.getPaintTicks()) {
          // centerSpacing += getTickLength();
          centerSpacing += tickLength;
        }
        // Assume uniform labels.
        centerSpacing += metrics.getAscent() + metrics.getDescent();
        int trackY = insets.top + focusInsets.top + (contentHeight - centerSpacing - 1) / 2;
        if (isAqua) {
          if (slider.getPaintTicks()) {
            int prefHeight = slider.getUI().getPreferredSize(slider).height;
            int prefDelta = height - prefHeight;
            if (prefDelta > 0) {
              trackY -= Math.min(1, prefDelta);
            }
          } else {
            trackY--;
          }
        }

        int trackHeight = thumbHeight;
        int tickY = trackY + trackHeight;
        int tickHeight = tickLength;
        if (!isAqua && !slider.getPaintTicks()) {
          tickHeight = 0;
        }
        int labelY = tickY + tickHeight;
        return labelY + metrics.getAscent();
      } else { // vertical
        boolean inverted = slider.getInverted();
        Integer value = inverted ? getMinSliderValue(slider) : getMaxSliderValue(slider);
        if (value != null) {
          int thumbHeight = 11;
          if (isMetal()) {
            thumbHeight = UIManager.getIcon("Slider.verticalThumbIcon").getIconHeight();
          }
          int trackBuffer = Math.max(metrics.getHeight() / 2, thumbHeight / 2);
          int contentY = focusInsets.top + insets.top;
          int trackY = contentY + trackBuffer;
          int trackHeight =
              height
                  - focusInsets.top
                  - focusInsets.bottom
                  - insets.top
                  - insets.bottom
                  - trackBuffer
                  - trackBuffer;
          int maxValue = getMaxSliderValue(slider).intValue();
          int min = slider.getMinimum();
          int max = slider.getMaximum();
          double valueRange = (double) max - (double) min;
          double pixelsPerValue = (double) trackHeight / (double) valueRange;
          int trackBottom = trackY + (trackHeight - 1);
          if (isAqua) {
            trackY -= 3;
            trackBottom += 6;
          }
          int yPosition = trackY;
          double offset;

          if (!inverted) {
            offset = pixelsPerValue * ((double) max - value.intValue());
          } else {
            offset = pixelsPerValue * ((double) value.intValue() - min);
          }
          if (isAqua) {
            yPosition += Math.floor(offset);
          } else {
            yPosition += Math.round(offset);
          }
          yPosition = Math.max(trackY, yPosition);
          yPosition = Math.min(trackBottom, yPosition);
          if (isAqua) {
            return yPosition + metrics.getAscent();
          }
          return yPosition - metrics.getHeight() / 2 + metrics.getAscent();
        }
      }
    }
    return -1;
  }