下面列出了怎么用javax.swing.plaf.basic.BasicHTML的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* {@inheritDoc}
*/
@Override
public void propertyChange(PropertyChangeEvent e) {
if (SynthLookAndFeel.shouldUpdateStyle(e)) {
updateStyle((JToolTip)e.getSource());
}
String name = e.getPropertyName();
if (name.equals("tiptext") || "font".equals(name) ||
"foreground".equals(name)) {
// remove the old html view client property if one
// existed, and install a new one if the text installed
// into the JLabel is html source.
JToolTip tip = ((JToolTip) e.getSource());
String text = tip.getTipText();
BasicHTML.updateRenderer(tip, text);
}
}
protected void updateHtmlViews(final int index, final boolean inserted) {
String title = ui.tabPane.getTitleAt(index);
boolean isHTML = BasicHTML.isHTMLString(title);
if (isHTML) {
if (ui.htmlViews == null) { // Initialize vector
ui.htmlViews = ui.createHTMLVector();
} else { // Vector already exists
View v = BasicHTML.createHTMLView(ui.tabPane, title);
setHtmlView(v, inserted, index);
}
} else { // Not HTML
if (ui.htmlViews != null) { // Add placeholder
setHtmlView(null, inserted, index);
} // else nada!
}
ui.updateMnemonics();
}
/**
* Creates a TitledBorder instance with the specified border,
* title, title-justification, title-position, title-font, and
* title-color.
*
* @param border the border
* @param title the title the border should display
* @param titleJustification the justification for the title
* @param titlePosition the position for the title
* @param titleFont the font of the title
* @param titleColor the color of the title
*/
@ConstructorProperties({"border", "title", "titleJustification", "titlePosition", "titleFont", "titleColor"})
public TitledBorder(Border border,
String title,
int titleJustification,
int titlePosition,
Font titleFont,
Color titleColor) {
this.title = title;
this.border = border;
this.titleFont = titleFont;
this.titleColor = titleColor;
setTitleJustification(titleJustification);
setTitlePosition(titlePosition);
this.label = new JLabel();
this.label.setOpaque(false);
this.label.putClientProperty(BasicHTML.propertyKey, null);
}
@Nonnull
public JTextPane configureMessagePaneUi(@Nonnull JTextPane messageComponent, @Nullable String message, @Nullable UIUtil.FontSize fontSize) {
UIUtil.FontSize fixedFontSize = fontSize == null ? UIUtil.FontSize.NORMAL : fontSize;
messageComponent.setFont(UIUtil.getLabelFont(fixedFontSize));
if (BasicHTML.isHTMLString(message)) {
HTMLEditorKit editorKit = new HTMLEditorKit();
Font font = UIUtil.getLabelFont(fixedFontSize);
editorKit.getStyleSheet().addRule(UIUtil.displayPropertiesToCSS(font, UIUtil.getLabelForeground()));
messageComponent.setEditorKit(editorKit);
messageComponent.setContentType(UIUtil.HTML_MIME);
}
messageComponent.setText(message);
messageComponent.setEditable(false);
if (messageComponent.getCaret() != null) {
messageComponent.setCaretPosition(0);
}
messageComponent.setBackground(UIUtil.getOptionPaneBackground());
messageComponent.setForeground(UIUtil.getLabelForeground());
return messageComponent;
}
/**
* Paints the specified component.
*
* @param context context for the component being painted
* @param g the {@code Graphics} object used for painting
* @see #update(Graphics,JComponent)
*/
protected void paint(SynthContext context, Graphics g) {
JToolTip tip = (JToolTip)context.getComponent();
Insets insets = tip.getInsets();
View v = (View)tip.getClientProperty(BasicHTML.propertyKey);
if (v != null) {
Rectangle paintTextR = new Rectangle(insets.left, insets.top,
tip.getWidth() - (insets.left + insets.right),
tip.getHeight() - (insets.top + insets.bottom));
v.paint(g, paintTextR);
} else {
g.setColor(context.getStyle().getColor(context,
ColorType.TEXT_FOREGROUND));
g.setFont(style.getFont(context));
context.getStyle().getGraphicsUtils(context).paintText(
context, g, tip.getTipText(), insets.left, insets.top, -1);
}
}
/**
* Returns the minimum size needed to properly render an icon and text.
*
* @param ss SynthContext
* @param font Font to use
* @param text Text to layout
* @param icon Icon to layout
* @param hAlign horizontal alignment
* @param vAlign vertical alignment
* @param hTextPosition horizontal text position
* @param vTextPosition vertical text position
* @param iconTextGap gap between icon and text
* @param mnemonicIndex Index into text to render the mnemonic at, -1
* indicates no mnemonic.
*/
public Dimension getMinimumSize(SynthContext ss, Font font, String text,
Icon icon, int hAlign, int vAlign, int hTextPosition,
int vTextPosition, int iconTextGap, int mnemonicIndex) {
JComponent c = ss.getComponent();
Dimension size = getPreferredSize(ss, font, text, icon, hAlign,
vAlign, hTextPosition, vTextPosition,
iconTextGap, mnemonicIndex);
View v = (View) c.getClientProperty(BasicHTML.propertyKey);
if (v != null) {
size.width -= v.getPreferredSpan(View.X_AXIS) -
v.getMinimumSpan(View.X_AXIS);
}
return size;
}
/**
* {@inheritDoc}
*/
@Override
public void propertyChange(PropertyChangeEvent e) {
if (SynthLookAndFeel.shouldUpdateStyle(e)) {
updateStyle((JToolTip)e.getSource());
}
String name = e.getPropertyName();
if (name.equals("tiptext") || "font".equals(name) ||
"foreground".equals(name)) {
// remove the old html view client property if one
// existed, and install a new one if the text installed
// into the JLabel is html source.
JToolTip tip = ((JToolTip) e.getSource());
String text = tip.getTipText();
BasicHTML.updateRenderer(tip, text);
}
}
/**
* {@inheritDoc}
*/
@Override
public void propertyChange(PropertyChangeEvent e) {
if (SynthLookAndFeel.shouldUpdateStyle(e)) {
updateStyle((JToolTip)e.getSource());
}
String name = e.getPropertyName();
if (name.equals("tiptext") || "font".equals(name) ||
"foreground".equals(name)) {
// remove the old html view client property if one
// existed, and install a new one if the text installed
// into the JLabel is html source.
JToolTip tip = ((JToolTip) e.getSource());
String text = tip.getTipText();
BasicHTML.updateRenderer(tip, text);
}
}
/**
* Paints the specified component.
*
* @param context context for the component being painted
* @param g the {@code Graphics} object used for painting
* @see #update(Graphics,JComponent)
*/
protected void paint(SynthContext context, Graphics g) {
JToolTip tip = (JToolTip)context.getComponent();
Insets insets = tip.getInsets();
View v = (View)tip.getClientProperty(BasicHTML.propertyKey);
if (v != null) {
Rectangle paintTextR = new Rectangle(insets.left, insets.top,
tip.getWidth() - (insets.left + insets.right),
tip.getHeight() - (insets.top + insets.bottom));
v.paint(g, paintTextR);
} else {
g.setColor(context.getStyle().getColor(context,
ColorType.TEXT_FOREGROUND));
g.setFont(style.getFont(context));
context.getStyle().getGraphicsUtils(context).paintText(
context, g, tip.getTipText(), insets.left, insets.top, -1);
}
}
/**
* {@inheritDoc}
*/
@Override
public void propertyChange(PropertyChangeEvent e) {
if (SynthLookAndFeel.shouldUpdateStyle(e)) {
updateStyle((JToolTip)e.getSource());
}
String name = e.getPropertyName();
if (name.equals("tiptext") || "font".equals(name) ||
"foreground".equals(name)) {
// remove the old html view client property if one
// existed, and install a new one if the text installed
// into the JLabel is html source.
JToolTip tip = ((JToolTip) e.getSource());
String text = tip.getTipText();
BasicHTML.updateRenderer(tip, text);
}
}
@Override
public void reshape(int x, int y, int w, int h) {
int oldH = getHeight();
super.reshape(x, y, w, h);
if (!isLineWrap()) {
return;
}
if (oldH == 0) {
return;
}
if (w > getVisibleRect().width) {
w = getVisibleRect().width;
}
View view = (View) getClientProperty(BasicHTML.propertyKey);
if (view != null && view instanceof Renderer) {
view.setSize(w - occupiedWidth, h);
}
}
/**
* Creates a TitledBorder instance with the specified border,
* title, title-justification, title-position, title-font, and
* title-color.
*
* @param border the border
* @param title the title the border should display
* @param titleJustification the justification for the title
* @param titlePosition the position for the title
* @param titleFont the font of the title
* @param titleColor the color of the title
*/
@ConstructorProperties({"border", "title", "titleJustification", "titlePosition", "titleFont", "titleColor"})
public TitledBorder(Border border,
String title,
int titleJustification,
int titlePosition,
Font titleFont,
Color titleColor) {
this.title = title;
this.border = border;
this.titleFont = titleFont;
this.titleColor = titleColor;
setTitleJustification(titleJustification);
setTitlePosition(titlePosition);
this.label = new JLabel();
this.label.setOpaque(false);
this.label.putClientProperty(BasicHTML.propertyKey, null);
}
/**
* Returns the maximum size needed to properly render an icon and text.
*
* @param ss SynthContext
* @param font Font to use
* @param text Text to layout
* @param icon Icon to layout
* @param hAlign horizontal alignment
* @param vAlign vertical alignment
* @param hTextPosition horizontal text position
* @param vTextPosition vertical text position
* @param iconTextGap gap between icon and text
* @param mnemonicIndex Index into text to render the mnemonic at, -1
* indicates no mnemonic.
*/
public Dimension getMaximumSize(SynthContext ss, Font font, String text,
Icon icon, int hAlign, int vAlign, int hTextPosition,
int vTextPosition, int iconTextGap, int mnemonicIndex) {
JComponent c = ss.getComponent();
Dimension size = getPreferredSize(ss, font, text, icon, hAlign,
vAlign, hTextPosition, vTextPosition,
iconTextGap, mnemonicIndex);
View v = (View) c.getClientProperty(BasicHTML.propertyKey);
if (v != null) {
size.width += v.getMaximumSpan(View.X_AXIS) -
v.getPreferredSpan(View.X_AXIS);
}
return size;
}
void setSizeForWidth(float width) {
if (width > MAX_WIDTH.get()) {
View v = (View)getClientProperty(BasicHTML.propertyKey);
if (v != null) {
width = 0.0f;
for (View row : getRows(v)) {
float rWidth = row.getPreferredSpan(View.X_AXIS);
if (width < rWidth) {
width = rWidth;
}
}
v.setSize(width, v.getPreferredSpan(View.Y_AXIS));
}
}
}
/**
* {@inheritDoc}
*/
@Override
public void propertyChange(PropertyChangeEvent e) {
if (SynthLookAndFeel.shouldUpdateStyle(e)) {
updateStyle((JToolTip)e.getSource());
}
String name = e.getPropertyName();
if (name.equals("tiptext") || "font".equals(name) ||
"foreground".equals(name)) {
// remove the old html view client property if one
// existed, and install a new one if the text installed
// into the JLabel is html source.
JToolTip tip = ((JToolTip) e.getSource());
String text = tip.getTipText();
BasicHTML.updateRenderer(tip, text);
}
}
/**
* Creates a TitledBorder instance with the specified border,
* title, title-justification, title-position, title-font, and
* title-color.
*
* @param border the border
* @param title the title the border should display
* @param titleJustification the justification for the title
* @param titlePosition the position for the title
* @param titleFont the font of the title
* @param titleColor the color of the title
*/
@ConstructorProperties({"border", "title", "titleJustification", "titlePosition", "titleFont", "titleColor"})
public TitledBorder(Border border,
String title,
int titleJustification,
int titlePosition,
Font titleFont,
Color titleColor) {
this.title = title;
this.border = border;
this.titleFont = titleFont;
this.titleColor = titleColor;
setTitleJustification(titleJustification);
setTitlePosition(titlePosition);
this.label = new JLabel();
this.label.setOpaque(false);
this.label.putClientProperty(BasicHTML.propertyKey, null);
}
/**
* Creates a TitledBorder instance with the specified border,
* title, title-justification, title-position, title-font, and
* title-color.
*
* @param border the border
* @param title the title the border should display
* @param titleJustification the justification for the title
* @param titlePosition the position for the title
* @param titleFont the font of the title
* @param titleColor the color of the title
*/
@ConstructorProperties({"border", "title", "titleJustification", "titlePosition", "titleFont", "titleColor"})
public TitledBorder(Border border,
String title,
int titleJustification,
int titlePosition,
Font titleFont,
Color titleColor) {
this.title = title;
this.border = border;
this.titleFont = titleFont;
this.titleColor = titleColor;
setTitleJustification(titleJustification);
setTitlePosition(titlePosition);
this.label = new JLabel();
this.label.setOpaque(false);
this.label.putClientProperty(BasicHTML.propertyKey, null);
}
/**
* Returns the minimum size needed to properly render an icon and text.
*
* @param ss SynthContext
* @param font Font to use
* @param text Text to layout
* @param icon Icon to layout
* @param hAlign horizontal alignment
* @param vAlign vertical alignment
* @param hTextPosition horizontal text position
* @param vTextPosition vertical text position
* @param iconTextGap gap between icon and text
* @param mnemonicIndex Index into text to render the mnemonic at, -1
* indicates no mnemonic.
*/
public Dimension getMinimumSize(SynthContext ss, Font font, String text,
Icon icon, int hAlign, int vAlign, int hTextPosition,
int vTextPosition, int iconTextGap, int mnemonicIndex) {
JComponent c = ss.getComponent();
Dimension size = getPreferredSize(ss, font, text, icon, hAlign,
vAlign, hTextPosition, vTextPosition,
iconTextGap, mnemonicIndex);
View v = (View) c.getClientProperty(BasicHTML.propertyKey);
if (v != null) {
size.width -= v.getPreferredSpan(View.X_AXIS) -
v.getMinimumSpan(View.X_AXIS);
}
return size;
}
/**
* Returns the maximum size needed to properly render an icon and text.
*
* @param ss SynthContext
* @param font Font to use
* @param text Text to layout
* @param icon Icon to layout
* @param hAlign horizontal alignment
* @param vAlign vertical alignment
* @param hTextPosition horizontal text position
* @param vTextPosition vertical text position
* @param iconTextGap gap between icon and text
* @param mnemonicIndex Index into text to render the mnemonic at, -1
* indicates no mnemonic.
*/
public Dimension getMaximumSize(SynthContext ss, Font font, String text,
Icon icon, int hAlign, int vAlign, int hTextPosition,
int vTextPosition, int iconTextGap, int mnemonicIndex) {
JComponent c = ss.getComponent();
Dimension size = getPreferredSize(ss, font, text, icon, hAlign,
vAlign, hTextPosition, vTextPosition,
iconTextGap, mnemonicIndex);
View v = (View) c.getClientProperty(BasicHTML.propertyKey);
if (v != null) {
size.width += v.getMaximumSpan(View.X_AXIS) -
v.getPreferredSpan(View.X_AXIS);
}
return size;
}
/**
* Returns the maximum size needed to properly render an icon and text.
*
* @param ss SynthContext
* @param font Font to use
* @param text Text to layout
* @param icon Icon to layout
* @param hAlign horizontal alignment
* @param vAlign vertical alignment
* @param hTextPosition horizontal text position
* @param vTextPosition vertical text position
* @param iconTextGap gap between icon and text
* @param mnemonicIndex Index into text to render the mnemonic at, -1
* indicates no mnemonic.
*/
public Dimension getMaximumSize(SynthContext ss, Font font, String text,
Icon icon, int hAlign, int vAlign, int hTextPosition,
int vTextPosition, int iconTextGap, int mnemonicIndex) {
JComponent c = ss.getComponent();
Dimension size = getPreferredSize(ss, font, text, icon, hAlign,
vAlign, hTextPosition, vTextPosition,
iconTextGap, mnemonicIndex);
View v = (View) c.getClientProperty(BasicHTML.propertyKey);
if (v != null) {
size.width += v.getMaximumSpan(View.X_AXIS) -
v.getPreferredSpan(View.X_AXIS);
}
return size;
}
/**
* {@inheritDoc}
*/
@Override
public void propertyChange(PropertyChangeEvent e) {
if (SynthLookAndFeel.shouldUpdateStyle(e)) {
updateStyle((JToolTip)e.getSource());
}
String name = e.getPropertyName();
if (name.equals("tiptext") || "font".equals(name) ||
"foreground".equals(name)) {
// remove the old html view client property if one
// existed, and install a new one if the text installed
// into the JLabel is html source.
JToolTip tip = ((JToolTip) e.getSource());
String text = tip.getTipText();
BasicHTML.updateRenderer(tip, text);
}
}
/**
* Returns the minimum size needed to properly render an icon and text.
*
* @param ss SynthContext
* @param font Font to use
* @param text Text to layout
* @param icon Icon to layout
* @param hAlign horizontal alignment
* @param vAlign vertical alignment
* @param hTextPosition horizontal text position
* @param vTextPosition vertical text position
* @param iconTextGap gap between icon and text
* @param mnemonicIndex Index into text to render the mnemonic at, -1
* indicates no mnemonic.
*/
public Dimension getMinimumSize(SynthContext ss, Font font, String text,
Icon icon, int hAlign, int vAlign, int hTextPosition,
int vTextPosition, int iconTextGap, int mnemonicIndex) {
JComponent c = ss.getComponent();
Dimension size = getPreferredSize(ss, font, text, icon, hAlign,
vAlign, hTextPosition, vTextPosition,
iconTextGap, mnemonicIndex);
View v = (View) c.getClientProperty(BasicHTML.propertyKey);
if (v != null) {
size.width -= v.getPreferredSpan(View.X_AXIS) -
v.getMinimumSpan(View.X_AXIS);
}
return size;
}
public static int getFirstLineCenterY(String text, AbstractButton c, Rectangle iconRect) {
int y = 0;
if (Objects.nonNull(text) && c.getVerticalTextPosition() == SwingConstants.TOP) {
View v = (View) c.getClientProperty(BasicHTML.propertyKey);
if (Objects.nonNull(v)) {
try {
Element e = v.getElement().getElement(0);
Shape s = new Rectangle();
Position.Bias b = Position.Bias.Forward;
s = v.modelToView(e.getStartOffset(), b, e.getEndOffset(), b, s);
// System.out.println("v.h: " + s.getBounds());
y = Math.round(Math.abs(s.getBounds().height - iconRect.height) / 2f);
} catch (BadLocationException ex) {
// should never happen
RuntimeException wrap = new StringIndexOutOfBoundsException(ex.offsetRequested());
wrap.initCause(ex);
throw wrap;
}
}
}
return y;
}
/**
* Paints the specified component.
*
* @param context context for the component being painted
* @param g the {@code Graphics} object used for painting
* @see #update(Graphics,JComponent)
*/
protected void paint(SynthContext context, Graphics g) {
JToolTip tip = (JToolTip)context.getComponent();
Insets insets = tip.getInsets();
View v = (View)tip.getClientProperty(BasicHTML.propertyKey);
if (v != null) {
Rectangle paintTextR = new Rectangle(insets.left, insets.top,
tip.getWidth() - (insets.left + insets.right),
tip.getHeight() - (insets.top + insets.bottom));
v.paint(g, paintTextR);
} else {
g.setColor(context.getStyle().getColor(context,
ColorType.TEXT_FOREGROUND));
g.setFont(style.getFont(context));
context.getStyle().getGraphicsUtils(context).paintText(
context, g, tip.getTipText(), insets.left, insets.top, -1);
}
}
/**
* {@inheritDoc}
*/
@Override
public void propertyChange(PropertyChangeEvent e) {
if (SynthLookAndFeel.shouldUpdateStyle(e)) {
updateStyle((JToolTip)e.getSource());
}
String name = e.getPropertyName();
if (name.equals("tiptext") || "font".equals(name) ||
"foreground".equals(name)) {
// remove the old html view client property if one
// existed, and install a new one if the text installed
// into the JLabel is html source.
JToolTip tip = ((JToolTip) e.getSource());
String text = tip.getTipText();
BasicHTML.updateRenderer(tip, text);
}
}
/**
* Creates a TitledBorder instance with the specified border,
* title, title-justification, title-position, title-font, and
* title-color.
*
* @param border the border
* @param title the title the border should display
* @param titleJustification the justification for the title
* @param titlePosition the position for the title
* @param titleFont the font of the title
* @param titleColor the color of the title
*/
@ConstructorProperties({"border", "title", "titleJustification", "titlePosition", "titleFont", "titleColor"})
public TitledBorder(Border border,
String title,
int titleJustification,
int titlePosition,
Font titleFont,
Color titleColor) {
this.title = title;
this.border = border;
this.titleFont = titleFont;
this.titleColor = titleColor;
setTitleJustification(titleJustification);
setTitlePosition(titlePosition);
this.label = new JLabel();
this.label.setOpaque(false);
this.label.putClientProperty(BasicHTML.propertyKey, null);
}
/**
* Creates a TitledBorder instance with the specified border,
* title, title-justification, title-position, title-font, and
* title-color.
*
* @param border the border
* @param title the title the border should display
* @param titleJustification the justification for the title
* @param titlePosition the position for the title
* @param titleFont the font of the title
* @param titleColor the color of the title
*/
@ConstructorProperties({"border", "title", "titleJustification", "titlePosition", "titleFont", "titleColor"})
public TitledBorder(Border border,
String title,
int titleJustification,
int titlePosition,
Font titleFont,
Color titleColor) {
this.title = title;
this.border = border;
this.titleFont = titleFont;
this.titleColor = titleColor;
setTitleJustification(titleJustification);
setTitlePosition(titlePosition);
this.label = new JLabel();
this.label.setOpaque(false);
this.label.putClientProperty(BasicHTML.propertyKey, null);
}
/**
* {@inheritDoc}
*/
@Override
public void propertyChange(PropertyChangeEvent e) {
if (SynthLookAndFeel.shouldUpdateStyle(e)) {
updateStyle((JToolTip)e.getSource());
}
String name = e.getPropertyName();
if (name.equals("tiptext") || "font".equals(name) ||
"foreground".equals(name)) {
// remove the old html view client property if one
// existed, and install a new one if the text installed
// into the JLabel is html source.
JToolTip tip = ((JToolTip) e.getSource());
String text = tip.getTipText();
BasicHTML.updateRenderer(tip, text);
}
}
/**
* Returns the maximum size needed to properly render an icon and text.
*
* @param ss SynthContext
* @param font Font to use
* @param text Text to layout
* @param icon Icon to layout
* @param hAlign horizontal alignment
* @param vAlign vertical alignment
* @param hTextPosition horizontal text position
* @param vTextPosition vertical text position
* @param iconTextGap gap between icon and text
* @param mnemonicIndex Index into text to render the mnemonic at, -1
* indicates no mnemonic.
*/
public Dimension getMaximumSize(SynthContext ss, Font font, String text,
Icon icon, int hAlign, int vAlign, int hTextPosition,
int vTextPosition, int iconTextGap, int mnemonicIndex) {
JComponent c = ss.getComponent();
Dimension size = getPreferredSize(ss, font, text, icon, hAlign,
vAlign, hTextPosition, vTextPosition,
iconTextGap, mnemonicIndex);
View v = (View) c.getClientProperty(BasicHTML.propertyKey);
if (v != null) {
size.width += v.getMaximumSpan(View.X_AXIS) -
v.getPreferredSpan(View.X_AXIS);
}
return size;
}
/**
* Creates a TitledBorder instance with the specified border,
* title, title-justification, title-position, title-font, and
* title-color.
*
* @param border the border
* @param title the title the border should display
* @param titleJustification the justification for the title
* @param titlePosition the position for the title
* @param titleFont the font of the title
* @param titleColor the color of the title
*/
@ConstructorProperties({"border", "title", "titleJustification", "titlePosition", "titleFont", "titleColor"})
public TitledBorder(Border border,
String title,
int titleJustification,
int titlePosition,
Font titleFont,
Color titleColor) {
this.title = title;
this.border = border;
this.titleFont = titleFont;
this.titleColor = titleColor;
setTitleJustification(titleJustification);
setTitlePosition(titlePosition);
this.label = new JLabel();
this.label.setOpaque(false);
this.label.putClientProperty(BasicHTML.propertyKey, null);
installPropertyChangeListeners();
}