List of usage examples for javax.swing.text Element getName
public String getName();
From source file:net.java.sip.communicator.impl.gui.main.chat.ChatConversationPanel.java
/** * Finds the first element with <tt>name</tt> among the child elements of * <tt>element</tt>.//from w ww .ja v a 2s . c om * @param element the element to searh for. * @param name the name to search for. * @return the first element with <tt>name</tt>. */ private Element findFirstElement(Element element, String name) { if (element.getName().equalsIgnoreCase(name)) return element; Element resultElement = null; // Count how many messages we have in the document. for (int i = 0; i < element.getElementCount(); i++) { resultElement = findFirstElement(element.getElement(i), name); if (resultElement != null) return resultElement; } return null; }
From source file:net.java.sip.communicator.impl.gui.main.chat.ChatConversationPanel.java
/** * Deletes all messages "div"s that are missing their header the table tag. * The method calls itself recursively./*from w w w. ja va2 s .co m*/ */ private void deleteAllMessagesWithoutHeader() { String[] ids = new String[] { ChatHtmlUtils.MESSAGE_TEXT_ID, "statusMessage", "systemMessage", "actionMessage" }; Element firstMsgElement = findElement(Attribute.ID, ids); if (firstMsgElement == null || !firstMsgElement.getName().equals("div")) { return; } int startIndex = firstMsgElement.getStartOffset(); int endIndex = firstMsgElement.getEndOffset(); try { // Remove the message. if (endIndex - startIndex < document.getLength()) this.document.remove(startIndex, endIndex - startIndex); else { // currently there is a problem of deleting the last message // if it is the last message on the view return; } } catch (BadLocationException e) { logger.error("Error removing messages from chat: ", e); return; } deleteAllMessagesWithoutHeader(); }
From source file:net.java.sip.communicator.impl.gui.main.chat.ChatConversationPanel.java
/** * Ensures that the document won't become too big. When the document reaches * a certain size the first message in the page is removed. *///w w w .j av a 2 s . c o m private void ensureDocumentSize() { if (document.getLength() > Chat.CHAT_BUFFER_SIZE) { String[] ids = new String[] { ChatHtmlUtils.MESSAGE_TEXT_ID, "statusMessage", "systemMessage", "actionMessage" }; Element firstMsgElement = findElement(Attribute.ID, ids); int startIndex = firstMsgElement.getStartOffset(); int endIndex = firstMsgElement.getEndOffset(); try { // Remove the message. this.document.remove(startIndex, endIndex - startIndex); } catch (BadLocationException e) { logger.error("Error removing messages from chat: ", e); } if (firstMsgElement.getName().equals("table")) { // as we have removed a header for maybe several messages, // delete all messages without header deleteAllMessagesWithoutHeader(); } } }
From source file:net.sf.jasperreports.engine.util.JEditorPaneHtmlMarkupProcessor.java
@Override public String convert(String srcText) { JEditorPane editorPane = new JEditorPane("text/html", srcText); editorPane.setEditable(false);//from w ww .j a v a 2 s .c o m List<Element> elements = new ArrayList<Element>(); Document document = editorPane.getDocument(); Element root = document.getDefaultRootElement(); if (root != null) { addElements(elements, root); } int startOffset = 0; int endOffset = 0; int crtOffset = 0; String chunk = null; JRPrintHyperlink hyperlink = null; Element element = null; Element parent = null; boolean bodyOccurred = false; int[] orderedListIndex = new int[elements.size()]; String whitespace = " "; String[] whitespaces = new String[elements.size()]; for (int i = 0; i < elements.size(); i++) { whitespaces[i] = ""; } StringBuilder text = new StringBuilder(); List<JRStyledText.Run> styleRuns = new ArrayList<>(); for (int i = 0; i < elements.size(); i++) { if (bodyOccurred && chunk != null) { text.append(chunk); Map<Attribute, Object> styleAttributes = getAttributes(element.getAttributes()); if (hyperlink != null) { styleAttributes.put(JRTextAttribute.HYPERLINK, hyperlink); hyperlink = null; } if (!styleAttributes.isEmpty()) { styleRuns.add( new JRStyledText.Run(styleAttributes, startOffset + crtOffset, endOffset + crtOffset)); } } chunk = null; element = elements.get(i); parent = element.getParentElement(); startOffset = element.getStartOffset(); endOffset = element.getEndOffset(); AttributeSet attrs = element.getAttributes(); Object elementName = attrs.getAttribute(AbstractDocument.ElementNameAttribute); Object object = (elementName != null) ? null : attrs.getAttribute(StyleConstants.NameAttribute); if (object instanceof HTML.Tag) { HTML.Tag htmlTag = (HTML.Tag) object; if (htmlTag == Tag.BODY) { bodyOccurred = true; crtOffset = -startOffset; } else if (htmlTag == Tag.BR) { chunk = "\n"; } else if (htmlTag == Tag.OL) { orderedListIndex[i] = 0; String parentName = parent.getName().toLowerCase(); whitespaces[i] = whitespaces[elements.indexOf(parent)] + whitespace; if (parentName.equals("li")) { chunk = ""; } else { chunk = "\n"; ++crtOffset; } } else if (htmlTag == Tag.UL) { whitespaces[i] = whitespaces[elements.indexOf(parent)] + whitespace; String parentName = parent.getName().toLowerCase(); if (parentName.equals("li")) { chunk = ""; } else { chunk = "\n"; ++crtOffset; } } else if (htmlTag == Tag.LI) { whitespaces[i] = whitespaces[elements.indexOf(parent)]; if (element.getElement(0) != null && (element.getElement(0).getName().toLowerCase().equals("ol") || element.getElement(0).getName().toLowerCase().equals("ul"))) { chunk = ""; } else if (parent.getName().equals("ol")) { int index = elements.indexOf(parent); Object type = parent.getAttributes().getAttribute(HTML.Attribute.TYPE); Object startObject = parent.getAttributes().getAttribute(HTML.Attribute.START); int start = startObject == null ? 0 : Math.max(0, Integer.valueOf(startObject.toString()) - 1); String suffix = ""; ++orderedListIndex[index]; if (type != null) { switch (((String) type).charAt(0)) { case 'A': suffix = getOLBulletChars(orderedListIndex[index] + start, true); break; case 'a': suffix = getOLBulletChars(orderedListIndex[index] + start, false); break; case 'I': suffix = JRStringUtil.getRomanNumeral(orderedListIndex[index] + start, true); break; case 'i': suffix = JRStringUtil.getRomanNumeral(orderedListIndex[index] + start, false); break; case '1': default: suffix = String.valueOf(orderedListIndex[index] + start); break; } } else { suffix += orderedListIndex[index] + start; } chunk = whitespaces[index] + suffix + DEFAULT_BULLET_SEPARATOR + " "; } else { chunk = whitespaces[elements.indexOf(parent)] + DEFAULT_BULLET_CHARACTER + " "; } crtOffset += chunk.length(); } else if (element instanceof LeafElement) { if (element instanceof RunElement) { RunElement runElement = (RunElement) element; AttributeSet attrSet = (AttributeSet) runElement.getAttribute(Tag.A); if (attrSet != null) { hyperlink = new JRBasePrintHyperlink(); hyperlink.setHyperlinkType(HyperlinkTypeEnum.REFERENCE); hyperlink.setHyperlinkReference((String) attrSet.getAttribute(HTML.Attribute.HREF)); hyperlink.setLinkTarget((String) attrSet.getAttribute(HTML.Attribute.TARGET)); } } try { chunk = document.getText(startOffset, endOffset - startOffset); } catch (BadLocationException e) { if (log.isDebugEnabled()) { log.debug("Error converting markup.", e); } } } } } if (chunk != null) { if (!"\n".equals(chunk)) { text.append(chunk); Map<Attribute, Object> styleAttributes = getAttributes(element.getAttributes()); if (hyperlink != null) { styleAttributes.put(JRTextAttribute.HYPERLINK, hyperlink); hyperlink = null; } if (!styleAttributes.isEmpty()) { styleRuns.add( new JRStyledText.Run(styleAttributes, startOffset + crtOffset, endOffset + crtOffset)); } } else { //final newline, not appending //check if there's any style run that would have covered it, that can happen if there's a <li> tag with style int length = text.length(); for (ListIterator<JRStyledText.Run> it = styleRuns.listIterator(); it.hasNext();) { JRStyledText.Run run = it.next(); //only looking at runs that end at the position where the newline should have been //we don't want to hide bugs in which runs that span after the text length are created if (run.endIndex == length + 1) { if (run.startIndex < run.endIndex - 1) { it.set(new JRStyledText.Run(run.attributes, run.startIndex, run.endIndex - 1)); } else { it.remove(); } } } } } JRStyledText styledText = new JRStyledText(null, text.toString()); for (JRStyledText.Run run : styleRuns) { styledText.addRun(run); } styledText.setGlobalAttributes(new HashMap<Attribute, Object>()); return JRStyledTextParser.getInstance().write(styledText); }
From source file:com.hexidec.ekit.EkitCore.java
public void removeEmptyLists() { javax.swing.text.ElementIterator ei = new javax.swing.text.ElementIterator(htmlDoc); Element ele; while ((ele = ei.next()) != null) { if (ele.getName().equals("ul") || ele.getName().equals("ol")) { int listChildren = 0; for (int i = 0; i < ele.getElementCount(); i++) { if (ele.getElement(i).getName().equals("li")) { listChildren++;/*from w ww . j a va 2s .c om*/ } } if (listChildren <= 0) { htmlUtilities.removeTag(ele, true); } } } refreshOnUpdate(); }
From source file:org.alder.fotobuchconvert.scribus.RtfToScribusConverter.java
void output(XmlBuilder xml, DefaultStyledDocument doc, ScribusWriter scribus) { log.debug("Starting conversion of RTF data"); if (log.isTraceEnabled()) doc.dump(System.err);//from ww w.j a v a2 s . c o m try { Element section = doc.getDefaultRootElement(); log.trace(section); assert section.getName().equals("section"); final int nj = section.getElementCount(); for (int j = 0; j < nj; j++) { Element paragraph = section.getElement(j); log.trace(paragraph); assert section.getName().equals("paragraph"); // boolean firstInPara = true; AttributeSet attr = paragraph.getAttributes(); Integer alignment = (Integer) attr.getAttribute(StyleConstants.Alignment); boolean elementsInThisLine = false; final int ni = paragraph.getElementCount(); for (int i = 0; i < ni; i++) { Element content = paragraph.getElement(i); assert section.getName().equals("content"); int start = content.getStartOffset(); int end = content.getEndOffset(); attr = content.getAttributes(); Boolean italic = (Boolean) attr.getAttribute(StyleConstants.Italic); Boolean bold = (Boolean) attr.getAttribute(StyleConstants.Bold); Boolean underline = (Boolean) attr.getAttribute(StyleConstants.Underline); String family = (String) attr.getAttribute(StyleConstants.Family); Integer fontSize = (Integer) attr.getAttribute(StyleConstants.Size); Color color = (Color) attr.getAttribute(StyleConstants.ColorConstants.Foreground); String text = doc.getText(start, end - start); // if (firstInPara && text.trim().isEmpty() && family == // null // && fontSize == null) // continue; // else // firstInPara = false; if (i == ni - 1 && text.trim().isEmpty() && text.length() < 3) continue; elementsInThisLine = true; while (text.endsWith("\n") || text.endsWith("\r")) text = text.substring(0, text.length() - 1); log.debug(italic + " " + bold + " " + underline + " " + family + " " + fontSize + " " + color + "\t\"" + text + "\""); XmlBuilder el = xml.add(C.EL_ITEXT).set(C.CH, text); if (bold == Boolean.TRUE && italic == Boolean.TRUE) el.set(C.FONT, family + " Bold Italic"); else if (bold == Boolean.TRUE) el.set(C.FONT, family + " Bold"); else if (italic == Boolean.TRUE) el.set(C.FONT, family + " Italic"); else el.set(C.FONT, family + " Regular"); if (fontSize != null) el.set(C.FONTSIZE, fontSize); if (color != null && color.equals(Color.BLACK) && scribus != null) { String colname = scribus.colorManager.getColorName(color); el.set(C.FCOLOR, colname); } } if (!elementsInThisLine && j == nj - 1) break; // don't convert last line if empty XmlBuilder el = xml.add(C.EL_PARA); if (alignment != null) switch (alignment) { case StyleConstants.ALIGN_LEFT: el.set(C.ALIGN, 0); break; case StyleConstants.ALIGN_CENTER: el.set(C.ALIGN, 1); break; case StyleConstants.ALIGN_RIGHT: el.set(C.ALIGN, 2); break; case StyleConstants.ALIGN_JUSTIFIED: el.set(C.ALIGN, 3); break; } } } catch (BadLocationException e) { throw new RuntimeException("This error should not occour", e); } }
From source file:tk.tomby.tedit.editorkits.HighlightPlainContext.java
/** * DOCUMENT ME!/* w w w . j a va 2s .com*/ * * @param element DOCUMENT ME! * * @return DOCUMENT ME! */ public View create(Element element) { if (log.isDebugEnabled()) { log.debug(element.getName()); } return new HighlightView(element); }
From source file:tufts.vue.RichTextBox.java
/*********** *******NOTES FROM FONT EDITOR PANEL TO HELP WITH COPY/PASTE OF STYLES. globalSizeListener = new ActionListener(){ /*ww w .ja v a 2 s . c o m*/ public void actionPerformed(ActionEvent fe) { if (suspendItemListeners) return; if (lwtext == null) return; lwtext.richLabelBox.selectAll(); final String textSize = mSizeField.getSelectedItem().toString(); SHTMLDocument doc = (SHTMLDocument)lwtext.richLabelBox.getDocument(); SimpleAttributeSet set = new SimpleAttributeSet(); Util.styleSheet().addCSSAttribute(set, CSS.Attribute.FONT_SIZE, textSize); set.addAttribute(HTML.Attribute.SIZE, textSize); lwtext.richLabelBox.applyAttributes(set, false); lwtext.richLabelBox.select(0,0); //lwtext.setLabel0(lwtext.richLabelBox.getRichText(), false); lwtext.richLabelBox.setSize(lwtext.richLabelBox.getPreferredSize()); if (lwtext.getParent() !=null) lwtext.getParent().layout(); lwtext.notify(lwtext.richLabelBox, LWKey.Repaint); } }; globalFaceListener = new ActionListener(){ public void actionPerformed(ActionEvent fe) { if (suspendItemListeners) return; if (lwtext == null) return; lwtext.richLabelBox.selectAll(); SHTMLDocument doc = (SHTMLDocument)lwtext.richLabelBox.getDocument(); SimpleAttributeSet set = new SimpleAttributeSet(); Util.styleSheet().addCSSAttribute(set, CSS.Attribute.FONT_FAMILY, mFontCombo.getSelectedItem().toString()); set.addAttribute(HTML.Attribute.FACE, mFontCombo.getSelectedItem().toString()); lwtext.richLabelBox.applyAttributes(set, false); lwtext.richLabelBox.select(0,0); lwtext.richLabelBox.setSize(lwtext.richLabelBox.getPreferredSize()); if (lwtext.getParent() !=null) lwtext.getParent().layout(); lwtext.notify(lwtext.richLabelBox, LWKey.Repaint); } }; private final PropertyChangeListener RichTextColorChangeListener = new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent e) { if (e instanceof LWPropertyChangeEvent == false) return; final RichTextBox activeRTB = (RichTextBox) VUE.getActive(RichTextBox.class); if (activeRTB == null && lwtext == null) { // no problem: just ignore if there's no active edit //tufts.Util.printStackTrace("FEP propertyChange: no active RichTextBox: " + e); return; } if (lwtext !=null) lwtext.richLabelBox.selectAll(); final Color color = (Color) e.getNewValue(); final SimpleAttributeSet set = new SimpleAttributeSet(); final String colorString = "#" + Integer.toHexString(color.getRGB()).substring(2); toggleBulletsAction.setColor(colorString); toggleNumbersAction.setColor(colorString); Util.styleSheet().addCSSAttribute(set, CSS.Attribute.COLOR, colorString); set.addAttribute(HTML.Attribute.COLOR, colorString); if (activeRTB != null) activeRTB.applyAttributes(set, false); else lwtext.richLabelBox.applyAttributes(set, false); if (lwtext !=null) { lwtext.richLabelBox.select(0,0); lwtext.richLabelBox.select(0,0); //lwtext.setLabel0(lwtext.richLabelBox.getRichText(), false); lwtext.richLabelBox.setSize(lwtext.richLabelBox.getPreferredSize()); if (lwtext.getParent() !=null) lwtext.getParent().layout(); lwtext.notify(lwtext.richLabelBox, LWKey.Repaint); } } }; ************/ // this called every time setText is called to ensure we get // the font style encoded in our owning LWComponent void copyStyle(LWComponent c) { if (DEBUG.TEXT) out("copyStyle " + c); //Basic Setup LWText srcText = (LWText) c; RichTextBox srcBox = srcText.getRichLabelBox(); selectAll(); SHTMLDocument srcDoc = (SHTMLDocument) srcBox.getDocument(); SHTMLDocument doc = (SHTMLDocument) getDocument(); SimpleAttributeSet set = new SimpleAttributeSet(); SimpleAttributeSet alignSet = new SimpleAttributeSet(); //Gather source information Element paragraphElement = srcDoc.getParagraphElement(1); if (paragraphElement.getName().equals("p-implied")) //we're in a list item paragraphElement = paragraphElement.getParentElement(); AttributeSet paragraphAttributeSet = paragraphElement.getAttributes(); Element charElem = srcDoc.getCharacterElement(1); AttributeSet charSet = charElem.getAttributes(); Enumeration characterAttributeEnum = charSet.getAttributeNames(); Enumeration elementEnum = paragraphAttributeSet.getAttributeNames(); //Apply some attributes // System.out.println(paragraphElement.toString()); while (elementEnum.hasMoreElements()) { Object o = elementEnum.nextElement(); boolean isAlignSet = false; //System.out.println("P :: " +o.toString()); if (o.toString().equals("text-align") && paragraphAttributeSet.getAttribute(o).toString().equals("left") && !isAlignSet) { //Left Align Util.styleSheet().addCSSAttribute(alignSet, CSS.Attribute.TEXT_ALIGN, paragraphAttributeSet.getAttribute(o).toString()); alignSet.addAttribute(HTML.Attribute.ALIGN, paragraphAttributeSet.getAttribute(o).toString()); } else if (o.toString().equals("text-align") && paragraphAttributeSet.getAttribute(o).toString().equals("center") && !isAlignSet) { //Center Align Util.styleSheet().addCSSAttribute(alignSet, CSS.Attribute.TEXT_ALIGN, paragraphAttributeSet.getAttribute(o).toString()); alignSet.addAttribute(HTML.Attribute.ALIGN, paragraphAttributeSet.getAttribute(o).toString()); } else if (o.toString().equals("text-align") && paragraphAttributeSet.getAttribute(o).toString().equals("right") && !isAlignSet) { //Right Align Util.styleSheet().addCSSAttribute(alignSet, CSS.Attribute.TEXT_ALIGN, paragraphAttributeSet.getAttribute(o).toString()); alignSet.addAttribute(HTML.Attribute.ALIGN, paragraphAttributeSet.getAttribute(o).toString()); } if ((o.toString().equals("font-size")) || (o.toString().equals("size"))) { //Font Size Util.styleSheet().addCSSAttribute(set, CSS.Attribute.FONT_SIZE, paragraphAttributeSet.getAttribute(o).toString()); set.addAttribute(HTML.Attribute.SIZE, paragraphAttributeSet.getAttribute(o).toString()); } else if ((o.toString().equals("font-family")) || (o.toString().equals("font-face")) || (o.toString().equals("face"))) { //Font Face Util.styleSheet().addCSSAttribute(set, CSS.Attribute.FONT_FAMILY, paragraphAttributeSet.getAttribute(o).toString()); set.addAttribute(HTML.Attribute.FACE, paragraphAttributeSet.getAttribute(o).toString()); } } boolean isBold = false; boolean isItalic = false; boolean isUnderline = false; while (characterAttributeEnum.hasMoreElements()) { Object o = characterAttributeEnum.nextElement(); //System.out.println("C :: " +o.toString() + "XXX " + charSet.getAttribute(o).toString()); //System.out.println("Character element : " + o.toString() + " , " + charSet.getAttribute(o)); if ((o.toString().equals("color"))) { //Color Util.styleSheet().addCSSAttribute(set, CSS.Attribute.COLOR, charSet.getAttribute(o).toString()); set.addAttribute(HTML.Attribute.COLOR, charSet.getAttribute(o).toString()); } if ((o.toString().equals("font-size")) || (o.toString().equals("size"))) { //Font Size Util.styleSheet().addCSSAttribute(set, CSS.Attribute.FONT_SIZE, charSet.getAttribute(o).toString()); set.addAttribute(HTML.Attribute.SIZE, charSet.getAttribute(o).toString()); } if ((o.toString().equals("font-family")) || (o.toString().equals("font-face")) || (o.toString().equals("face"))) { //Font Face Util.styleSheet().addCSSAttribute(set, CSS.Attribute.FONT_FAMILY, charSet.getAttribute(o).toString()); set.addAttribute(HTML.Attribute.FACE, charSet.getAttribute(o).toString()); } if ((o.toString().equals("font-weight") && charSet.getAttribute(o).toString().equals("bold")) || o.toString().equals("b")) { Util.styleSheet().addCSSAttribute(set, CSS.Attribute.FONT_WEIGHT, charSet.getAttribute(o).toString()); } if ((o.toString().equals("font-style") && charSet.getAttribute(o).toString().equals("italic")) || o.toString().equals("i")) { Util.styleSheet().addCSSAttribute(set, CSS.Attribute.FONT_STYLE, charSet.getAttribute(o).toString()); } if ((o.toString().equals("text-decoration") && charSet.getAttribute(o).toString().equals("underline")) || o.toString().equals("u")) { Util.styleSheet().addCSSAttribute(set, CSS.Attribute.TEXT_DECORATION, charSet.getAttribute(o).toString()); } } //done looking at character attributes applyAttributes(set, false); lwc.notify(this, LWKey.Repaint); applyAttributes(alignSet, true); lwc.notify(this, LWKey.Repaint); clearSelection(); try { setSize(getPreferredSize()); } catch (NullPointerException npe) { } /* set = new SimpleAttributeSet(); final String colorString = "#" + Integer.toHexString(color.getRGB()).substring(2); toggleBulletsAction.setColor(colorString); toggleNumbersAction.setColor(colorString); Util.styleSheet().addCSSAttribute(set, CSS.Attribute.COLOR, colorString); set.addAttribute(HTML.Attribute.COLOR, colorString); if (activeRTB != null) activeRTB.applyAttributes(set, false); else lwtext.richLabelBox.applyAttributes(set, false); if (lwtext !=null) { lwtext.richLabelBox.select(0,0); lwtext.richLabelBox.select(0,0); //lwtext.setLabel0(lwtext.richLabelBox.getRichText(), false); lwtext.richLabelBox.setSize(lwtext.richLabelBox.getPreferredSize()); if (lwtext.getParent() !=null) lwtext.getParent().layout(); lwtext.notify(lwtext.richLabelBox, LWKey.Repaint); }*/ }