use of javax.swing.text.View in project adempiere by adempiere.
the class HTMLRenderer method get.
/**
* Get View from HTML String
* @param html html string
* @return renderer view
*/
public static HTMLRenderer get(String html) {
HTMLEditorKit kit = new HTMLEditorKit();
HTMLDocument doc = (HTMLDocument) kit.createDefaultDocument();
try {
doc.remove(0, doc.getLength());
Reader r = new StringReader(html);
kit.read(r, doc, 0);
} catch (Exception e) {
log.log(Level.SEVERE, "", e);
}
// Create Renderer
Element element = doc.getDefaultRootElement();
ViewFactory factory = kit.getViewFactory();
// Y_AXIS is main
View view = factory.create(element);
HTMLRenderer renderer = new HTMLRenderer(factory, view);
renderer.preferenceChanged(null, true, true);
return renderer;
}
use of javax.swing.text.View in project intellij-community by JetBrains.
the class DarculaCheckBoxUI method drawText.
protected void drawText(JComponent c, Graphics2D g, JCheckBox b, FontMetrics fm, Rectangle textRect, String text) {
//text
if (text != null) {
View view = (View) c.getClientProperty(BasicHTML.propertyKey);
if (view != null) {
view.paint(g, textRect);
} else {
g.setColor(b.isEnabled() ? b.getForeground() : getDisabledTextColor());
SwingUtilities2.drawStringUnderlineCharAt(c, g, text, b.getDisplayedMnemonicIndex(), textRect.x, textRect.y + fm.getAscent());
}
}
}
use of javax.swing.text.View in project intellij-community by JetBrains.
the class MacIntelliJButtonUI method paint.
@Override
public void paint(Graphics g, JComponent c) {
if (!(c.getBorder() instanceof MacIntelliJButtonBorder) && !isComboButton(c)) {
super.paint(g, c);
return;
}
int w = c.getWidth();
int h = c.getHeight();
if (isHelpButton(c)) {
Icon icon = MacIntelliJIconCache.getIcon("helpButton", false, c.hasFocus());
int x = (w - icon.getIconWidth()) / 2;
int y = (h - icon.getIconHeight()) / 2;
icon.paintIcon(c, g, x, y);
} else {
AbstractButton b = (AbstractButton) c;
String text = layout(b, SwingUtilities2.getFontMetrics(b, g), b.getWidth(), b.getHeight());
boolean isFocused = c.hasFocus();
if (isSquare(c)) {
Icon icon = MacIntelliJIconCache.getIcon("browseButton");
int x = (c.getWidth() - icon.getIconWidth()) / 2;
int y = (c.getHeight() - icon.getIconHeight()) / 2;
icon.paintIcon(c, g, x, y);
return;
} else {
int x = isFocused ? 0 : 2;
int y = isFocused ? 0 : (h - viewRect.height) / 2;
Icon icon;
icon = getLeftIcon(b);
icon.paintIcon(b, g, x, y);
x += icon.getIconWidth();
int stop = w - (isFocused ? 0 : 2) - (getRightIcon(b).getIconWidth());
Graphics gg = g.create(0, 0, w, h);
gg.setClip(x, y, stop - x, h);
icon = getMiddleIcon(b);
while (x < stop) {
icon.paintIcon(b, gg, x, y);
x += icon.getIconWidth();
}
gg.dispose();
icon = getRightIcon(b);
icon.paintIcon(b, g, stop, y);
clearTextShiftOffset();
}
// Paint the Icon
if (b.getIcon() != null) {
paintIcon(g, c, iconRect);
}
if (text != null && !text.isEmpty()) {
View v = (View) c.getClientProperty(BasicHTML.propertyKey);
if (v != null) {
v.paint(g, textRect);
} else {
UISettings.setupAntialiasing(g);
paintText(g, b, textRect, text);
}
}
}
}
use of javax.swing.text.View in project android by JetBrains.
the class SimpleTabUI method paintText.
// Appears to be based on http://stackoverflow.com/questions/7903657/basictabbedpaneui-paint-html-text
@Override
protected void paintText(Graphics g, int tabPlacement, Font font, FontMetrics metrics, int tabIndex, String title, Rectangle textRect, boolean isSelected) {
g.setFont(font);
View v = getTextViewForTab(tabIndex);
if (v != null) {
// html
v.paint(g, textRect);
} else {
// plain text
int mnemIndex = tabPane.getDisplayedMnemonicIndexAt(tabIndex);
if (tabPane.isEnabled() && tabPane.isEnabledAt(tabIndex)) {
Color fg = tabPane.getForegroundAt(tabIndex);
if (isSelected && (fg instanceof UIResource)) {
// CHANGE FROM DEFAULT
Color selectedFG = JBColor.BLUE;
if (selectedFG != null) {
fg = selectedFG;
}
}
g.setColor(fg);
SwingUtilities2.drawStringUnderlineCharAt(tabPane, g, title, mnemIndex, textRect.x, textRect.y + metrics.getAscent());
} else {
// tab disabled
g.setColor(tabPane.getBackgroundAt(tabIndex).brighter());
SwingUtilities2.drawStringUnderlineCharAt(tabPane, g, title, mnemIndex, textRect.x, textRect.y + metrics.getAscent());
g.setColor(tabPane.getBackgroundAt(tabIndex).darker());
SwingUtilities2.drawStringUnderlineCharAt(tabPane, g, title, mnemIndex, textRect.x - 1, textRect.y + metrics.getAscent() - 1);
}
}
}
use of javax.swing.text.View in project jdk8u_jdk by JetBrains.
the class SwingUtilities method layoutCompoundLabelImpl.
/**
* Compute and return the location of the icons origin, the
* location of origin of the text baseline, and a possibly clipped
* version of the compound labels string. Locations are computed
* relative to the viewR rectangle.
* This layoutCompoundLabel() does not know how to handle LEADING/TRAILING
* values in horizontalTextPosition (they will default to RIGHT) and in
* horizontalAlignment (they will default to CENTER).
* Use the other version of layoutCompoundLabel() instead.
*/
private static String layoutCompoundLabelImpl(JComponent c, FontMetrics fm, String text, Icon icon, int verticalAlignment, int horizontalAlignment, int verticalTextPosition, int horizontalTextPosition, Rectangle viewR, Rectangle iconR, Rectangle textR, int textIconGap) {
if (icon != null) {
iconR.width = icon.getIconWidth();
iconR.height = icon.getIconHeight();
} else {
iconR.width = iconR.height = 0;
}
/* Initialize the text bounds rectangle textR. If a null
* or and empty String was specified we substitute "" here
* and use 0,0,0,0 for textR.
*/
boolean textIsEmpty = (text == null) || text.equals("");
int lsb = 0;
int rsb = 0;
/* Unless both text and icon are non-null, we effectively ignore
* the value of textIconGap.
*/
int gap;
View v;
if (textIsEmpty) {
textR.width = textR.height = 0;
text = "";
gap = 0;
} else {
int availTextWidth;
gap = (icon == null) ? 0 : textIconGap;
if (horizontalTextPosition == CENTER) {
availTextWidth = viewR.width;
} else {
availTextWidth = viewR.width - (iconR.width + gap);
}
v = (c != null) ? (View) c.getClientProperty("html") : null;
if (v != null) {
textR.width = Math.min(availTextWidth, (int) v.getPreferredSpan(View.X_AXIS));
textR.height = (int) v.getPreferredSpan(View.Y_AXIS);
} else {
textR.width = SwingUtilities2.stringWidth(c, fm, text);
lsb = SwingUtilities2.getLeftSideBearing(c, fm, text);
if (lsb < 0) {
// If lsb is negative, add it to the width and later
// adjust the x location. This gives more space than is
// actually needed.
// This is done like this for two reasons:
// 1. If we set the width to the actual bounds all
// callers would have to account for negative lsb
// (pref size calculations ONLY look at width of
// textR)
// 2. You can do a drawString at the returned location
// and the text won't be clipped.
textR.width -= lsb;
}
if (textR.width > availTextWidth) {
text = SwingUtilities2.clipString(c, fm, text, availTextWidth);
textR.width = SwingUtilities2.stringWidth(c, fm, text);
}
textR.height = fm.getHeight();
}
}
if (verticalTextPosition == TOP) {
if (horizontalTextPosition != CENTER) {
textR.y = 0;
} else {
textR.y = -(textR.height + gap);
}
} else if (verticalTextPosition == CENTER) {
textR.y = (iconR.height / 2) - (textR.height / 2);
} else {
// (verticalTextPosition == BOTTOM)
if (horizontalTextPosition != CENTER) {
textR.y = iconR.height - textR.height;
} else {
textR.y = (iconR.height + gap);
}
}
if (horizontalTextPosition == LEFT) {
textR.x = -(textR.width + gap);
} else if (horizontalTextPosition == CENTER) {
textR.x = (iconR.width / 2) - (textR.width / 2);
} else {
// (horizontalTextPosition == RIGHT)
textR.x = (iconR.width + gap);
}
// WARNING: DefaultTreeCellEditor uses a shortened version of
// this algorithm to position it's Icon. If you change how this
// is calculated, be sure and update DefaultTreeCellEditor too.
/* labelR is the rectangle that contains iconR and textR.
* Move it to its proper position given the labelAlignment
* properties.
*
* To avoid actually allocating a Rectangle, Rectangle.union
* has been inlined below.
*/
int labelR_x = Math.min(iconR.x, textR.x);
int labelR_width = Math.max(iconR.x + iconR.width, textR.x + textR.width) - labelR_x;
int labelR_y = Math.min(iconR.y, textR.y);
int labelR_height = Math.max(iconR.y + iconR.height, textR.y + textR.height) - labelR_y;
int dx, dy;
if (verticalAlignment == TOP) {
dy = viewR.y - labelR_y;
} else if (verticalAlignment == CENTER) {
dy = (viewR.y + (viewR.height / 2)) - (labelR_y + (labelR_height / 2));
} else {
// (verticalAlignment == BOTTOM)
dy = (viewR.y + viewR.height) - (labelR_y + labelR_height);
}
if (horizontalAlignment == LEFT) {
dx = viewR.x - labelR_x;
} else if (horizontalAlignment == RIGHT) {
dx = (viewR.x + viewR.width) - (labelR_x + labelR_width);
} else {
// (horizontalAlignment == CENTER)
dx = (viewR.x + (viewR.width / 2)) - (labelR_x + (labelR_width / 2));
}
/* Translate textR and glypyR by dx,dy.
*/
textR.x += dx;
textR.y += dy;
iconR.x += dx;
iconR.y += dy;
if (lsb < 0) {
// lsb is negative. Shift the x location so that the text is
// visually drawn at the right location.
textR.x -= lsb;
textR.width += lsb;
}
if (rsb > 0) {
textR.width -= rsb;
}
return text;
}
Aggregations