BasicHTMLpublic class BasicHTML extends Object Support for providing html views for the swing components.
This translates a simple html string to a javax.swing.text.View
implementation that can render the html and provide the necessary
layout semantics. |
Fields Summary |
---|
private static final String | htmlDisableIf this client property of a JComponent is set to Boolean.TRUE
the component's 'text' property is never treated as HTML. | public static final String | propertyKeyKey to use for the html renderer when stored as a
client property of a JComponent. | public static final String | documentBaseKeyKey stored as a client property to indicate the base that relative
references are resolved against. For example, lets say you keep
your images in the directory resources relative to the code path,
you would use the following the set the base:
jComponent.putClientProperty(documentBaseKey,
xxx.class.getResource("resources/"));
| private static BasicEditorKit | basicHTMLFactoryThe source of the html renderers | private static ViewFactory | basicHTMLViewFactoryCreates the Views that visually represent the model. | private static final String | styleChangesOverrides to the default stylesheet. Should consider
just creating a completely fresh stylesheet. |
Methods Summary |
---|
public static javax.swing.text.View | createHTMLView(javax.swing.JComponent c, java.lang.String html)Create an html renderer for the given component and
string of html.
BasicEditorKit kit = getFactory();
Document doc = kit.createDefaultDocument(c.getFont(),
c.getForeground());
Object base = c.getClientProperty(documentBaseKey);
if (base instanceof URL) {
((HTMLDocument)doc).setBase((URL)base);
}
Reader r = new StringReader(html);
try {
kit.read(r, doc, 0);
} catch (Throwable e) {
}
ViewFactory f = kit.getViewFactory();
View hview = f.create(doc.getDefaultRootElement());
View v = new Renderer(c, f, hview);
return v;
| static int | getBaseline(javax.swing.JComponent c, int y, int ascent, int w, int h)Gets the baseline for the specified component. This digs out
the View client property, and if non-null the baseline is calculated
from it. Otherwise the baseline is the value y + ascent .
View view = (View)c.getClientProperty(BasicHTML.propertyKey);
if (view != null) {
int baseline = getHTMLBaseline(view, w, h);
if (baseline < 0) {
return baseline;
}
return y + baseline;
}
return y + ascent;
| static int | getBaseline(javax.swing.text.View view, int w, int h)Gets the baseline for the specified View.
if (hasParagraph(view)) {
view.setSize(w, h);
return getBaseline(view, new Rectangle(0, 0, w, h));
}
return -1;
| private static int | getBaseline(javax.swing.text.View view, java.awt.Shape bounds)
if (view.getViewCount() == 0) {
return -1;
}
AttributeSet attributes = view.getElement().getAttributes();
Object name = null;
if (attributes != null) {
name = attributes.getAttribute(StyleConstants.NameAttribute);
}
int index = 0;
if (name == HTML.Tag.HTML && view.getViewCount() > 1) {
// For html on widgets the header is not visible, skip it.
index++;
}
bounds = view.getChildAllocation(index, bounds);
if (bounds == null) {
return -1;
}
View child = view.getView(index);
if (view instanceof javax.swing.text.ParagraphView) {
Rectangle rect;
if (bounds instanceof Rectangle) {
rect = (Rectangle)bounds;
}
else {
rect = bounds.getBounds();
}
return rect.y + (int)(rect.height *
child.getAlignment(View.Y_AXIS));
}
return getBaseline(child, bounds);
| static javax.swing.plaf.basic.BasicHTML$BasicEditorKit | getFactory()
if (basicHTMLFactory == null) {
basicHTMLViewFactory = new BasicHTMLViewFactory();
basicHTMLFactory = new BasicEditorKit();
}
return basicHTMLFactory;
| public static int | getHTMLBaseline(javax.swing.text.View view, int w, int h)Returns the baseline for the html renderer.
if (w < 0 || h < 0) {
throw new IllegalArgumentException(
"Width and height must be >= 0");
}
if (view instanceof Renderer) {
return getBaseline(view.getView(0), w, h);
}
return -1;
| private static boolean | hasParagraph(javax.swing.text.View view)
if (view instanceof javax.swing.text.ParagraphView) {
return true;
}
if (view.getViewCount() == 0) {
return false;
}
AttributeSet attributes = view.getElement().getAttributes();
Object name = null;
if (attributes != null) {
name = attributes.getAttribute(StyleConstants.NameAttribute);
}
int index = 0;
if (name == HTML.Tag.HTML && view.getViewCount() > 1) {
// For html on widgets the header is not visible, skip it.
index = 1;
}
return hasParagraph(view.getView(index));
| public static boolean | isHTMLString(java.lang.String s)Check the given string to see if it should trigger the
html rendering logic in a non-text component that supports
html rendering.
if (s != null) {
if ((s.length() >= 6) && (s.charAt(0) == '<") && (s.charAt(5) == '>")) {
String tag = s.substring(1,5);
return tag.equalsIgnoreCase(propertyKey);
}
}
return false;
| public static void | updateRenderer(javax.swing.JComponent c, java.lang.String text)Stash the HTML render for the given text into the client
properties of the given JComponent. If the given text is
NOT HTML the property will be cleared of any
renderer.
This method is useful for ComponentUI implementations
that are static (i.e. shared) and get their state
entirely from the JComponent.
View value = null;
View oldValue = (View)c.getClientProperty(BasicHTML.propertyKey);
Boolean htmlDisabled = (Boolean) c.getClientProperty(htmlDisable);
if (htmlDisabled != Boolean.TRUE && BasicHTML.isHTMLString(text)) {
value = BasicHTML.createHTMLView(c, text);
}
if (value != oldValue && oldValue != null) {
for (int i = 0; i < oldValue.getViewCount(); i++) {
oldValue.getView(i).setParent(null);
}
}
c.putClientProperty(BasicHTML.propertyKey, value);
|
|