Rectangle r = component.modelToView(rowStartOffset); int lineHeight = fontMetrics.getHeight(); int y = r.y + r.height; fonts = new HashMap<>(); 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; fm = component.getFontMetrics(font); fonts.put(key, fm);
private HTML.Tag findTag( final AttributeSet attr ) { final Enumeration names = attr.getAttributeNames(); while ( names.hasMoreElements() ) { final Object name = names.nextElement(); final Object o = attr.getAttribute( name ); if ( o instanceof HTML.Tag ) { if ( HTML.Tag.CONTENT == o ) { continue; } if ( HTML.Tag.COMMENT == o ) { continue; } return (HTML.Tag) o; } } return null; }
JEditorPane editor = new JEditorPane(); editor.setContentType( "text/html" ); editor.setEditable( false ); editor.read(reader, null); HTMLDocument doc = (HTMLDocument)editor.getDocument(); ElementIterator it = new ElementIterator(doc); Element element; AttributeSet as = element.getAttributes(); Enumeration enumm = as.getAttributeNames(); while( enumm.hasMoreElements() ) Object name = enumm.nextElement(); Object value = as.getAttribute( name ); System.out.println( "\t" + name + " : " + value );
public void run() { commChannelsDisplayer.setText(_istr); commChannelsDisplayer.validate(); commChannelsDisplayer.setCaretPosition(0); HTMLDocument htm = (HTMLDocument) commChannelsDisplayer.getDocument(); Element e = htm.getElement(CHAT_BUTTON); if (e != null) { AttributeSet attr = e.getAttributes(); Enumeration enu = attr.getAttributeNames(); while (enu.hasMoreElements()) { Object name = enu.nextElement(); Object value = attr.getAttribute(name); if ("model".equals(name.toString())) { //NOI18N final DefaultButtonModel model = (DefaultButtonModel) value; model.setActionCommand(CHAT_BUTTON); model.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { ChatTopComponent ct = ChatTopComponent.findInstance(); ct.open(); ct.requestActive(); ct.setActiveGroup(instProj.getName() + "@muc." + instProj.getKenai().getUrl().getHost()); // NOI18N } }); } } } } });
JFrame f = new JFrame("Test"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JEditorPane jep = new JEditorPane("text/html", TEXT); jep.setEditable(false); f.add(jep); f.pack(); f.setVisible(true); HTMLDocument htmlDoc = (HTMLDocument) jep.getDocument(); System.out.println(htmlDoc.getElement("unique_id")); ElementIterator iterator = new ElementIterator(htmlDoc); AttributeSet attrSet = element.getAttributes(); System.out.println("" + "Element: '" + element.toString().trim() + "', name: '" + element.getName() + "', children: " + element.getElementCount() + ", attributes: " + attrSet.getAttributeCount() + ", leaf: " + element.isLeaf()); Enumeration attrNames = attrSet.getAttributeNames(); while (attrNames.hasMoreElements()) { Object attr = attrNames.nextElement(); System.out.println(" Attribute: '" + attr + "', Value: '" + attrSet.getAttribute(attr) + "'"); Object tag = attrSet.getAttribute(StyleConstants.NameAttribute); if (attr == StyleConstants.NameAttribute && tag == HTML.Tag.CONTENT) {
pad.setContentType("text/html"); HTMLEditorKit kit = (HTMLEditorKit)pad.getEditorKit(); HTMLDocument htmldoc = (HTMLDocument)kit.createDefaultDocument(); kit.insertHTML(htmldoc, htmldoc.getLength(), "<p>paragraph <b>1</b></p>", 0, 0, null); Element line = doc.getParagraphElement(lastPos+1); lastPos = line.getEndOffset(); XWPFParagraph paragraph = docX.createParagraph(); for (int elIdx=0; elIdx < line.getElementCount(); elIdx++) { final Element frag = line.getElement(elIdx); final AttributeSet as = frag.getAttributes(); final Enumeration<?> ae = as.getAttributeNames(); while (ae.hasMoreElements()) { final Object attrib = ae.nextElement(); Field f = as.getAttribute(attrib).getClass().getDeclaredField("c"); f.setAccessible(true); Color c = (Color)f.get(as.getAttribute(attrib)); run.setColor(String.format("%1$02X%2$02X%3$02X", c.getRed(),c.getGreen(),c.getBlue())); } else if (CSS.Attribute.FONT_WEIGHT.equals(attrib)) { if ("bold".equals(as.getAttribute(attrib).toString())) { run.setBold(true);
HTMLDocument document = (HTMLDocument) messageEditorPaneHTMLEditorKit.createDefaultDocument(); JEditorPane editorPane = new JEditorPane("text/html", ""); editorPane.setEditorKit(messageEditorPaneHTMLEditorKit); editorPane.setDocument(document); Enumeration styleAttributes = style.getAttributeNames(); while (styleAttributes.hasMoreElements()) { Object attribute = styleAttributes.nextElement(); String attributeName = attribute.toString(); Object attributeValue = style.getAttribute(attribute); System.out.println(attributeName + ": " + attributeValue);
try { writeLock(); int start = e.getStartOffset(); DefaultDocumentEvent changes = new DefaultDocumentEvent(start, e.getEndOffset() - start, DocumentEvent.EventType.CHANGE); AttributeSet sCopy = a.copyAttributes(); changes.addEdit(new AttributeUndoableEdit(e, sCopy, false)); MutableAttributeSet attr = (MutableAttributeSet) e.getAttributes(); Enumeration aNames = attr.getAttributeNames(); Object value; Object aName; while (aNames.hasMoreElements()) { aName = aNames.nextElement(); value = attr.getAttribute(aName); if (value != null && !value.toString().equalsIgnoreCase(tag.toString())) {
JEditorPane edPane = new JEditorPane(); edPane.setContentType("text/html"); System.out.println(edPane.getText()); edPane.setEditorKit(hek); HTMLDocument doc = (HTMLDocument) edPane.getDocument(); for( int i = 0; i < roots[0].getElementCount(); i++ ) { Element element = roots[0].getElement( i ); if( element.getAttributes().getAttribute( StyleConstants.NameAttribute ) == HTML.Tag.BODY ) { body = element; break;
JEditorPane jEditorPaneIsFollower = new JEditorPane(); jEditorPaneIsFollower.setEditable(false); jEditorPaneIsFollower.setContentType("text/html"); jEditorPaneIsFollower.setDocument(new HTMLDocument()); jEditorPaneIsFollower.setEditorKit(new HTMLEditorKit()); jEditorPaneIsFollower.setText(HTML_TEXT); ToolTipManager.sharedInstance().registerComponent(jEditorPaneIsFollower); Element elem = e.getSourceElement(); if (elem != null) { AttributeSet attr = elem.getAttributes(); AttributeSet a = (AttributeSet) attr.getAttribute(HTML.Tag.A); if (a != null) { editor.setToolTipText((String) a.getAttribute(HTML.Attribute.TITLE));
fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JEditorPane pane = new JEditorPane(); pane.setEditorKit(new NewEditorKit()); pane.setText("test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test "); StyledDocument doc = (StyledDocument) pane.getDocument(); MutableAttributeSet attr = new SimpleAttributeSet(); attr.addAttribute("strike-color", Color.red); doc.setCharacterAttributes(0, 9, attr, false); doc.setCharacterAttributes(10, 19, attr, false); JScrollPane sp = new JScrollPane(pane); String kind = elem.getName(); if (kind != null) { if (kind.equals(AbstractDocument.ContentElementName)) { Color c=(Color)getElement().getAttributes().getAttribute("strike-color"); if (c!=null) { int y = a.getBounds().y + a.getBounds().height - (int) getGlyphPainter().getDescent(this);
textPane.setText("Smaple String"); this.add(textPane, BorderLayout.CENTER); JPanel panel = new JPanel(); int start = textPane.getSelectionStart(); int end = textPane.getSelectionEnd(); StyledDocument doc = (StyledDocument) textPane.getDocument(); Element element = doc.getCharacterElement(start); AttributeSet as = element.getAttributes(); MutableAttributeSet asNew = new SimpleAttributeSet(as.copyAttributes()); StyleConstants.setBold(asNew, !StyleConstants.isBold(as)); doc.setCharacterAttributes(start, textPane.getSelectedText().length(), asNew, true); String text = (StyleConstants.isBold(as) ? "Cancel Bold" : "Bold"); btnStyle.setText(text);
pane.setContentType("text/html"); pane.setText("<html>Here is the text with a <a href=\"http://google.com\">link</a></html>"); frm.add(new JScrollPane(pane)); final JButton btn = new JButton(new AbstractAction("Find link") { final HTMLDocument doc = (HTMLDocument) pane.getDocument(); final Collection<String> links = new LinkedHashSet<String>(); final AttributeSet a = el.getAttributes(); final AttributeSet anchor = (AttributeSet)a.getAttribute(HTML.Tag.A); if (anchor != null) { links.add((String)anchor.getAttribute(HTML.Attribute.HREF));
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.computeIfAbsent(key, k -> {
pane.setText("Underline With Different Color"); StyledDocument doc = (StyledDocument)pane.getDocument(); MutableAttributeSet attrs = new SimpleAttributeSet(); attrs.addAttribute("Underline-Color", Color.red); doc.setCharacterAttributes(0, doc.getLength()-1, attrs, true); public View create(Element elem){ View result = null; String kind = elem.getName(); if(kind != null){ if(kind.equals(AbstractDocument.ContentElementName)){ super.paint(g, a); Color c = (Color)getElement().getAttributes().getAttribute("Underline-Color"); if(c != null){ int y = a.getBounds().y + (int)getGlyphPainter().getAscent(this);
private final String s1 = String.format(s + "aaaaaaaaaaaaaa<br>", SO, "blue", SO); private final String s2 = String.format(s + "cccc", SO, "#0000FF", "bbbbbbbbbbb"); private final JEditorPane editor = new JEditorPane( "text/html", "<html>" + s1 + s2); private JComponent makeUI() { editor.setEditable(false); editor.addHyperlinkListener(new HyperlinkListener() { @Override public void hyperlinkUpdate(HyperlinkEvent e) { if (e.getEventType() == HyperlinkEvent.EventType.ENTERED) { AttributeSet attrs = element.getAttributes(); Object o = attrs.getAttribute(HTML.Tag.A); if (o instanceof MutableAttributeSet) { MutableAttributeSet a = (MutableAttributeSet) o;
public int findTabLocation(int offset) { // find first { boolean cont = true; while (offset > -1 && cont) { Element el = doc.getCharacterElement(offset); Object color = el.getAttributes().getAttribute(StyleConstants.Foreground); if (!COMMENT_COLOR.equals(color)) { cont = segment.array[offset] != '{' && segment.array[offset] != '}'; } offset -= cont ? 1 : 0; } if (offset > -1 && segment.array[offset] == '{') { while (offset > -1 && !Character.isWhitespace(segment.array[offset--])) { } } int index = offset < 0 || segment.array[offset] == '}' ? -4 : 0; if (offset > -1) { Element top = doc.getDefaultRootElement(); offset = top.getElement(top.getElementIndex(offset)).getStartOffset(); while (Character.isWhitespace(segment.array[offset++])) { index++; } } return index; } }
HTMLDocument doc = (HTMLDocument)component.getDocument(); int position = component.getCaretPosition(); Element e = doc.getCharacterElement( position ); AttributeSet as = e.getAttributes(); AttributeSet anchor = (AttributeSet)as.getAttribute(HTML.Tag.A); Rectangle r = component.modelToView(position);
private ActionListener getAction(final MouseEvent event) { Element e = document.getCharacterElement(textPane.viewToModel(event.getPoint())); ActionListener action = (ActionListener)e.getAttributes().getAttribute(ACTION_ATTRIBUTE); return action; }
JEditorPane editorPane = new JEditorPane() { @Override public String getToolTipText(MouseEvent evt) { String text = null; int pos = viewToModel(evt.getPoint()); if (pos >= 0) { HTMLDocument hdoc = (HTMLDocument) getDocument(); javax.swing.text.Element e = hdoc.getCharacterElement(pos); AttributeSet a = e.getAttributes(); SimpleAttributeSet value = (SimpleAttributeSet) a.getAttribute(HTML.Tag.A); if (value != null) { String href = (String) value.getAttribute(HTML.Attribute.HREF); if (href != null) { text = href; } } } return text; } };