示例#1
0
 private static int getProgressBarBaseline(JProgressBar pb, int height) {
   if (pb.isStringPainted() && pb.getOrientation() == JProgressBar.HORIZONTAL) {
     FontMetrics metrics = pb.getFontMetrics(pb.getFont());
     Insets insets = pb.getInsets();
     int y = insets.top;
     if (isWindows() && isXP()) {
       if (pb.isIndeterminate()) {
         y = -1;
         height--;
       } else {
         y = 0;
         height -= 3;
       }
     } else if (isGTK()) {
       return (height - metrics.getAscent() - metrics.getDescent()) / 2 + metrics.getAscent();
     } else if (isAqua()) {
       if (pb.isIndeterminate()) {
         // Aqua doesn't appear to support text on indeterminate
         // progress bars.
         return -1;
       }
       y -= 1;
       height -= (insets.top + insets.bottom);
     } else {
       height -= insets.top + insets.bottom;
     }
     return y + (height + metrics.getAscent() - metrics.getLeading() - metrics.getDescent()) / 2;
   }
   return -1;
 }
示例#2
0
  /*
   *  Determine the Y offset for the current row
   */
  private int getOffsetY(int rowStartOffset, FontMetrics fontMetrics) throws BadLocationException {
    //  Get the bounding rectangle of the row

    Rectangle r = component.modelToView(rowStartOffset);
    int lineHeight = fontMetrics.getHeight();
    int y = r.y + r.height;
    int descent = 0;

    //  The text needs to be positioned above the bottom of the bounding
    //  rectangle based on the descent of the font(s) contained on the row.
    if (r.height == lineHeight) {
      // default font is being used
      descent = fontMetrics.getDescent();
    } else {
      // We need to check all the attributes for font changes
      if (fonts == null) {
        fonts = new HashMap<String, FontMetrics>();
      }

      Element root = component.getDocument().getDefaultRootElement();
      int index = root.getElementIndex(rowStartOffset);
      Element line = root.getElement(index);

      for (int i = 0; i < line.getElementCount(); i++) {
        Element child = line.getElement(i);
        AttributeSet as = child.getAttributes();
        String fontFamily = (String) as.getAttribute(StyleConstants.FontFamily);
        Integer fontSize = (Integer) as.getAttribute(StyleConstants.FontSize);
        String key = fontFamily + fontSize;

        FontMetrics fm = fonts.get(key);

        if (fm == null) {
          Font font = new Font(fontFamily, Font.PLAIN, fontSize);
          fm = component.getFontMetrics(font);
          fonts.put(key, fm);
        }

        descent = Math.max(descent, fm.getDescent());
      }
    }

    return y - descent;
  }
示例#3
0
 private static int getPanelBaseline(JPanel panel, int height) {
   Border border = panel.getBorder();
   if (border instanceof TitledBorder) {
     TitledBorder titledBorder = (TitledBorder) border;
     if (titledBorder.getTitle() != null && !"".equals(titledBorder.getTitle())) {
       Font font = titledBorder.getTitleFont();
       if (font == null) {
         font = panel.getFont();
         if (font == null) {
           font = new Font("Dialog", Font.PLAIN, 12);
         }
       }
       Border border2 = titledBorder.getBorder();
       Insets borderInsets;
       if (border2 != null) {
         borderInsets = border2.getBorderInsets(panel);
       } else {
         borderInsets = EMPTY_INSETS;
       }
       FontMetrics fm = panel.getFontMetrics(font);
       int fontHeight = fm.getHeight();
       int descent = fm.getDescent();
       int ascent = fm.getAscent();
       int y = EDGE_SPACING;
       int h = height - EDGE_SPACING * 2;
       int diff;
       switch (((TitledBorder) border).getTitlePosition()) {
         case TitledBorder.ABOVE_TOP:
           diff = ascent + descent + (Math.max(EDGE_SPACING, TEXT_SPACING * 2) - EDGE_SPACING);
           return y + diff - (descent + TEXT_SPACING);
         case TitledBorder.TOP:
         case TitledBorder.DEFAULT_POSITION:
           diff = Math.max(0, ((ascent / 2) + TEXT_SPACING) - EDGE_SPACING);
           return (y + diff - descent) + (borderInsets.top + ascent + descent) / 2;
         case TitledBorder.BELOW_TOP:
           return y + borderInsets.top + ascent + TEXT_SPACING;
         case TitledBorder.ABOVE_BOTTOM:
           return (y + h) - (borderInsets.bottom + descent + TEXT_SPACING);
         case TitledBorder.BOTTOM:
           h -= fontHeight / 2;
           return ((y + h) - descent) + ((ascent + descent) - borderInsets.bottom) / 2;
         case TitledBorder.BELOW_BOTTOM:
           h -= fontHeight;
           return y + h + ascent + TEXT_SPACING;
       }
     }
   }
   return -1;
 }
示例#4
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;
  }