Java Code Examples for javax.swing.text.View#getPreferredSpan()

The following examples show how to use javax.swing.text.View#getPreferredSpan() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AquaTabbedPaneCopyFromBasicUI.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
protected int calculateTabHeight(final int tabPlacement, final int tabIndex, final int fontHeight) {
    int height = 0;
    final Component c = tabPane.getTabComponentAt(tabIndex);
    if (c != null) {
        height = c.getPreferredSize().height;
    } else {
        final View v = getTextViewForTab(tabIndex);
        if (v != null) {
            // html
            height += (int)v.getPreferredSpan(View.Y_AXIS);
        } else {
            // plain text
            height += fontHeight;
        }
        final Icon icon = getIconForTab(tabIndex);

        if (icon != null) {
            height = Math.max(height, icon.getIconHeight());
        }
    }
    final Insets tabInsets = getTabInsets(tabPlacement, tabIndex);
    height += tabInsets.top + tabInsets.bottom + 2;
    return height;
}
 
Example 2
Source File: BasicTabbedPaneUI.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
protected int calculateTabHeight(int tabPlacement, int tabIndex, int fontHeight) {
    int height = 0;
    Component c = tabPane.getTabComponentAt(tabIndex);
    if (c != null) {
        height = c.getPreferredSize().height;
    } else {
        View v = getTextViewForTab(tabIndex);
        if (v != null) {
            // html
            height += (int) v.getPreferredSpan(View.Y_AXIS);
        } else {
            // plain text
            height += fontHeight;
        }
        Icon icon = getIconForTab(tabIndex);

        if (icon != null) {
            height = Math.max(height, icon.getIconHeight());
        }
    }
    Insets tabInsets = getTabInsets(tabPlacement, tabIndex);
    height += tabInsets.top + tabInsets.bottom + 2;
    return height;
}
 
Example 3
Source File: SynthToolTipUI.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public Dimension getPreferredSize(JComponent c) {
    SynthContext context = getContext(c);
    Insets insets = c.getInsets();
    Dimension prefSize = new Dimension(insets.left+insets.right,
                                       insets.top+insets.bottom);
    String text = ((JToolTip)c).getTipText();

    if (text != null) {
        View v = (c != null) ? (View) c.getClientProperty("html") : null;
        if (v != null) {
            prefSize.width += (int) v.getPreferredSpan(View.X_AXIS);
            prefSize.height += (int) v.getPreferredSpan(View.Y_AXIS);
        } else {
            Font font = context.getStyle().getFont(context);
            FontMetrics fm = c.getFontMetrics(font);
            prefSize.width += context.getStyle().getGraphicsUtils(context).
                              computeStringWidth(context, font, fm, text);
            prefSize.height += fm.getHeight();
        }
    }
    return prefSize;
}
 
Example 4
Source File: BasicTabbedPaneUI.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
protected int calculateTabWidth(int tabPlacement, int tabIndex, FontMetrics metrics) {
    Insets tabInsets = getTabInsets(tabPlacement, tabIndex);
    int width = tabInsets.left + tabInsets.right + 3;
    Component tabComponent = tabPane.getTabComponentAt(tabIndex);
    if (tabComponent != null) {
        width += tabComponent.getPreferredSize().width;
    } else {
        Icon icon = getIconForTab(tabIndex);
        if (icon != null) {
            width += icon.getIconWidth() + textIconGap;
        }
        View v = getTextViewForTab(tabIndex);
        if (v != null) {
            // html
            width += (int) v.getPreferredSpan(View.X_AXIS);
        } else {
            // plain text
            String title = tabPane.getTitleAt(tabIndex);
            width += SwingUtilities2.stringWidth(tabPane, metrics, title);
        }
    }
    return width;
}
 
Example 5
Source File: AquaTabbedPaneCopyFromBasicUI.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
protected int calculateTabWidth(final int tabPlacement, final int tabIndex, final FontMetrics metrics) {
    final Insets tabInsets = getTabInsets(tabPlacement, tabIndex);
    int width = tabInsets.left + tabInsets.right + 3;
    final Component tabComponent = tabPane.getTabComponentAt(tabIndex);
    if (tabComponent != null) {
        width += tabComponent.getPreferredSize().width;
    } else {
        final Icon icon = getIconForTab(tabIndex);
        if (icon != null) {
            width += icon.getIconWidth() + textIconGap;
        }
        final View v = getTextViewForTab(tabIndex);
        if (v != null) {
            // html
            width += (int)v.getPreferredSpan(View.X_AXIS);
        } else {
            // plain text
            final String title = tabPane.getTitleAt(tabIndex);
            width += SwingUtilities2.stringWidth(tabPane, metrics, title);
        }
    }
    return width;
}
 
Example 6
Source File: BasicMenuItemUI.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public Dimension getMaximumSize(JComponent c) {
    Dimension d = null;
    View v = (View) c.getClientProperty(BasicHTML.propertyKey);
    if (v != null) {
        d = getPreferredSize(c);
        d.width += v.getMaximumSpan(View.X_AXIS) - v.getPreferredSpan(View.X_AXIS);
    }
    return d;
}
 
Example 7
Source File: BasicLabelUI.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @return getPreferredSize(c)
 */
public Dimension getMaximumSize(JComponent c) {
    Dimension d = getPreferredSize(c);
    View v = (View) c.getClientProperty(BasicHTML.propertyKey);
    if (v != null) {
        d.width += v.getMaximumSpan(View.X_AXIS) - v.getPreferredSpan(View.X_AXIS);
    }
    return d;
}
 
Example 8
Source File: BasicButtonUI.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
public Dimension getMinimumSize(JComponent c) {
    Dimension d = getPreferredSize(c);
    View v = (View) c.getClientProperty(BasicHTML.propertyKey);
    if (v != null) {
        d.width -= v.getPreferredSpan(View.X_AXIS) - v.getMinimumSpan(View.X_AXIS);
    }
    return d;
}
 
Example 9
Source File: BasicButtonUI.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public Dimension getMinimumSize(JComponent c) {
    Dimension d = getPreferredSize(c);
    View v = (View) c.getClientProperty(BasicHTML.propertyKey);
    if (v != null) {
        d.width -= v.getPreferredSpan(View.X_AXIS) - v.getMinimumSpan(View.X_AXIS);
    }
    return d;
}
 
Example 10
Source File: BasicButtonUI.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
public Dimension getMinimumSize(JComponent c) {
    Dimension d = getPreferredSize(c);
    View v = (View) c.getClientProperty(BasicHTML.propertyKey);
    if (v != null) {
        d.width -= v.getPreferredSpan(View.X_AXIS) - v.getMinimumSpan(View.X_AXIS);
    }
    return d;
}
 
Example 11
Source File: BasicButtonUI.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
public Dimension getMaximumSize(JComponent c) {
    Dimension d = getPreferredSize(c);
    View v = (View) c.getClientProperty(BasicHTML.propertyKey);
    if (v != null) {
        d.width += v.getMaximumSpan(View.X_AXIS) - v.getPreferredSpan(View.X_AXIS);
    }
    return d;
}
 
Example 12
Source File: BasicMenuItemUI.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public Dimension getMaximumSize(JComponent c) {
    Dimension d = null;
    View v = (View) c.getClientProperty(BasicHTML.propertyKey);
    if (v != null) {
        d = getPreferredSize(c);
        d.width += v.getMaximumSpan(View.X_AXIS) - v.getPreferredSpan(View.X_AXIS);
    }
    return d;
}
 
Example 13
Source File: AquaButtonUI.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public Dimension getMaximumSize(final JComponent c) {
    final Dimension d = getPreferredSize(c);

    final View v = (View)c.getClientProperty(BasicHTML.propertyKey);
    if (v != null) {
        d.width += v.getMaximumSpan(View.X_AXIS) - v.getPreferredSpan(View.X_AXIS);
    }

    return d;
}
 
Example 14
Source File: BasicToolTipUI.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public Dimension getMaximumSize(JComponent c) {
    Dimension d = getPreferredSize(c);
    View v = (View) c.getClientProperty(BasicHTML.propertyKey);
    if (v != null) {
        d.width += v.getMaximumSpan(View.X_AXIS) - v.getPreferredSpan(View.X_AXIS);
    }
    return d;
}
 
Example 15
Source File: AquaButtonUI.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public Dimension getMaximumSize(final JComponent c) {
    final Dimension d = getPreferredSize(c);

    final View v = (View)c.getClientProperty(BasicHTML.propertyKey);
    if (v != null) {
        d.width += v.getMaximumSpan(View.X_AXIS) - v.getPreferredSpan(View.X_AXIS);
    }

    return d;
}
 
Example 16
Source File: BasicButtonUI.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public Dimension getMinimumSize(JComponent c) {
    Dimension d = getPreferredSize(c);
    View v = (View) c.getClientProperty(BasicHTML.propertyKey);
    if (v != null) {
        d.width -= v.getPreferredSpan(View.X_AXIS) - v.getMinimumSpan(View.X_AXIS);
    }
    return d;
}
 
Example 17
Source File: BasicLabelUI.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @return getPreferredSize(c)
 */
public Dimension getMaximumSize(JComponent c) {
    Dimension d = getPreferredSize(c);
    View v = (View) c.getClientProperty(BasicHTML.propertyKey);
    if (v != null) {
        d.width += v.getMaximumSpan(View.X_AXIS) - v.getPreferredSpan(View.X_AXIS);
    }
    return d;
}
 
Example 18
Source File: BasicLabelUI.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @return getPreferredSize(c)
 */
public Dimension getMinimumSize(JComponent c) {
    Dimension d = getPreferredSize(c);
    View v = (View) c.getClientProperty(BasicHTML.propertyKey);
    if (v != null) {
        d.width -= v.getPreferredSpan(View.X_AXIS) - v.getMinimumSpan(View.X_AXIS);
    }
    return d;
}
 
Example 19
Source File: ImageView.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
/**
 * Determines the preferred span for this view along an
 * axis.
 *
 * @param axis may be either X_AXIS or Y_AXIS
 * @return   the span the view would like to be rendered into;
 *           typically the view is told to render into the span
 *           that is returned, although there is no guarantee;
 *           the parent may choose to resize or break the view
 */
public float getPreferredSpan(int axis) {
    sync();

    // If the attributes specified a width/height, always use it!
    if (axis == View.X_AXIS && (state & WIDTH_FLAG) == WIDTH_FLAG) {
        getPreferredSpanFromAltView(axis);
        return width + leftInset + rightInset;
    }
    if (axis == View.Y_AXIS && (state & HEIGHT_FLAG) == HEIGHT_FLAG) {
        getPreferredSpanFromAltView(axis);
        return height + topInset + bottomInset;
    }

    Image image = getImage();

    if (image != null) {
        switch (axis) {
        case View.X_AXIS:
            return width + leftInset + rightInset;
        case View.Y_AXIS:
            return height + topInset + bottomInset;
        default:
            throw new IllegalArgumentException("Invalid axis: " + axis);
        }
    }
    else {
        View view = getAltView();
        float retValue = 0f;

        if (view != null) {
            retValue = view.getPreferredSpan(axis);
        }
        switch (axis) {
        case View.X_AXIS:
            return retValue + (float)(width + leftInset + rightInset);
        case View.Y_AXIS:
            return retValue + (float)(height + topInset + bottomInset);
        default:
            throw new IllegalArgumentException("Invalid axis: " + axis);
        }
    }
}
 
Example 20
Source File: ComponentValidator.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nonnull
public static ComponentPopupBuilder createPopupBuilder(@Nonnull ValidationInfo info, @Nullable Consumer<? super JEditorPane> configurator) {
  JEditorPane tipComponent = new JEditorPane();
  View v = BasicHTML.createHTMLView(tipComponent, String.format("<html>%s</html>", info.message));
  String text = v.getPreferredSpan(View.X_AXIS) > MAX_WIDTH.get()
                ? String.format("<html><body><div width=%d>%s</div><body></html>", MAX_WIDTH.get(), trimMessage(info.message, tipComponent))
                : String.format("<html><body><div>%s</div></body></html>", info.message);

  tipComponent.setContentType("text/html");
  tipComponent.setEditable(false);
  tipComponent.setEditorKit(UIUtil.getHTMLEditorKit());

  EditorKit kit = tipComponent.getEditorKit();
  if (kit instanceof HTMLEditorKit) {
    StyleSheet css = ((HTMLEditorKit)kit).getStyleSheet();

    css.addRule("a, a:link {color:#" + ColorUtil.toHex(JBUI.CurrentTheme.Link.linkColor()) + ";}");
    css.addRule("a:visited {color:#" + ColorUtil.toHex(JBUI.CurrentTheme.Link.linkVisitedColor()) + ";}");
    css.addRule("a:hover {color:#" + ColorUtil.toHex(JBUI.CurrentTheme.Link.linkHoverColor()) + ";}");
    css.addRule("a:active {color:#" + ColorUtil.toHex(JBUI.CurrentTheme.Link.linkPressedColor()) + ";}");
    css.addRule("body {background-color:#" + ColorUtil.toHex(info.warning ? warningBackgroundColor() : errorBackgroundColor()) + ";}");
  }

  if (tipComponent.getCaret() instanceof DefaultCaret) {
    ((DefaultCaret)tipComponent.getCaret()).setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
  }

  tipComponent.setCaretPosition(0);
  tipComponent.setText(text);

  tipComponent.setBackground(info.warning ? warningBackgroundColor() : errorBackgroundColor());
  tipComponent.setOpaque(true);
  tipComponent.setBorder(getBorder());

  if (configurator != null) {
    configurator.accept(tipComponent);
  }

  return JBPopupFactory.getInstance().createComponentPopupBuilder(tipComponent, null).
          setBorderColor(info.warning ? warningBorderColor() : errorBorderColor()).
          setCancelOnClickOutside(false).
          setShowShadow(true);
}