List of usage examples for javax.swing.text Element getAttributes
public AttributeSet getAttributes();
From source file:Main.java
private Element getHyperlinkElement(MouseEvent event) { Point p = event.getPoint();//from w w w . j a v a2 s . co m int selRow = tree.getRowForLocation(p.x, p.y); TreeCellRenderer r = tree.getCellRenderer(); if (selRow == -1 || r == null) { return null; } TreePath path = tree.getPathForRow(selRow); Object lastPath = path.getLastPathComponent(); Component rComponent = r.getTreeCellRendererComponent(tree, lastPath, tree.isRowSelected(selRow), tree.isExpanded(selRow), tree.getModel().isLeaf(lastPath), selRow, true); if (rComponent instanceof JEditorPane == false) { return null; } Rectangle pathBounds = tree.getPathBounds(path); JEditorPane editor = (JEditorPane) rComponent; editor.setBounds(tree.getRowBounds(selRow)); p.translate(-pathBounds.x, -pathBounds.y); int pos = editor.getUI().viewToModel(editor, p); if (pos >= 0 && editor.getDocument() instanceof HTMLDocument) { HTMLDocument hdoc = (HTMLDocument) editor.getDocument(); Element elem = hdoc.getCharacterElement(pos); if (elem.getAttributes().getAttribute(HTML.Tag.A) != null) { return elem; } } return null; }
From source file:Main.java
@Override public void mouseClicked(MouseEvent e) { Element h = getHyperlinkElement(e); if (h != null) { Object attribute = h.getAttributes().getAttribute(HTML.Tag.A); if (attribute instanceof AttributeSet) { AttributeSet set = (AttributeSet) attribute; String href = (String) set.getAttribute(HTML.Attribute.HREF); if (href != null) { try { Desktop.getDesktop().browse(new URI(href)); } catch (Exception e1) { e1.printStackTrace(); }/*from w w w.j a v a 2s .c o m*/ } } } }
From source file:Main.java
public void reapplyStyles() { Element sectionElem = bodyElement.getElement(bodyElement.getElementCount() - 1); int paraCount = sectionElem.getElementCount(); for (int i = 0; i < paraCount; i++) { Element e = sectionElem.getElement(i); int rangeStart = e.getStartOffset(); int rangeEnd = e.getEndOffset(); htmlDocument.setParagraphAttributes(rangeStart, rangeEnd - rangeStart, e.getAttributes(), true); }/* w w w . java 2 s .co m*/ }
From source file:MainClass.java
public void actionPerformed(ActionEvent e) { JTextPane editor = (JTextPane) getEditor(e); if (editor == null) { JOptionPane.showMessageDialog(null, "You need to select the editor pane before you can change the color.", "Error", JOptionPane.ERROR_MESSAGE); return;//from w ww . jav a2 s. co m } int p0 = editor.getSelectionStart(); StyledDocument doc = getStyledDocument(editor); Element paragraph = doc.getCharacterElement(p0); AttributeSet as = paragraph.getAttributes(); fg = StyleConstants.getForeground(as); if (fg == null) { fg = Color.BLACK; } colorChooser.setColor(fg); JButton accept = new JButton("OK"); accept.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { fg = colorChooser.getColor(); dialog.dispose(); } }); JButton cancel = new JButton("Cancel"); cancel.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { cancelled = true; dialog.dispose(); } }); JButton none = new JButton("None"); none.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { noChange = true; dialog.dispose(); } }); JPanel buttons = new JPanel(); buttons.add(accept); buttons.add(none); buttons.add(cancel); dialog.getContentPane().setLayout(new BorderLayout()); dialog.getContentPane().add(colorChooser, BorderLayout.CENTER); dialog.getContentPane().add(buttons, BorderLayout.SOUTH); dialog.setModal(true); dialog.pack(); dialog.setVisible(true); if (!cancelled) { MutableAttributeSet attr = null; if (editor != null) { if (fg != null && !noChange) { attr = new SimpleAttributeSet(); StyleConstants.setForeground(attr, fg); setCharacterAttributes(editor, attr, false); } } } // end if color != null noChange = false; cancelled = false; }
From source file:MainClass.java
public void actionPerformed(ActionEvent e) { JTextPane editor = (JTextPane) getEditor(e); int p0 = editor.getSelectionStart(); StyledDocument doc = getStyledDocument(editor); Element paragraph = doc.getCharacterElement(p0); AttributeSet as = paragraph.getAttributes(); family = StyleConstants.getFontFamily(as); fontSize = StyleConstants.getFontSize(as); formatText = new JDialog(new JFrame(), "Font and Size", true); formatText.getContentPane().setLayout(new BorderLayout()); JPanel choosers = new JPanel(); choosers.setLayout(new GridLayout(2, 1)); JPanel fontFamilyPanel = new JPanel(); fontFamilyPanel.add(new JLabel("Font")); GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); String[] fontNames = ge.getAvailableFontFamilyNames(); fontFamilyChooser = new JComboBox(); for (int i = 0; i < fontNames.length; i++) { fontFamilyChooser.addItem(fontNames[i]); }/* w ww .ja va 2 s . c o m*/ fontFamilyChooser.setSelectedItem(family); fontFamilyPanel.add(fontFamilyChooser); choosers.add(fontFamilyPanel); JPanel fontSizePanel = new JPanel(); fontSizePanel.add(new JLabel("Size")); fontSizeChooser = new JComboBox(); fontSizeChooser.setEditable(true); fontSizeChooser.addItem(new Float(4)); fontSizeChooser.addItem(new Float(8)); fontSizeChooser.addItem(new Float(12)); fontSizeChooser.addItem(new Float(16)); fontSizeChooser.addItem(new Float(20)); fontSizeChooser.addItem(new Float(24)); fontSizeChooser.setSelectedItem(new Float(fontSize)); fontSizePanel.add(fontSizeChooser); choosers.add(fontSizePanel); JButton ok = new JButton("OK"); ok.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { accept = true; formatText.dispose(); family = (String) fontFamilyChooser.getSelectedItem(); fontSize = Float.parseFloat(fontSizeChooser.getSelectedItem().toString()); } }); JButton cancel = new JButton("Cancel"); cancel.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { formatText.dispose(); } }); JPanel buttons = new JPanel(); buttons.add(ok); buttons.add(cancel); formatText.getContentPane().add(choosers, BorderLayout.CENTER); formatText.getContentPane().add(buttons, BorderLayout.SOUTH); formatText.pack(); formatText.setVisible(true); MutableAttributeSet attr = null; if (editor != null && accept) { attr = new SimpleAttributeSet(); StyleConstants.setFontFamily(attr, family); StyleConstants.setFontSize(attr, (int) fontSize); setCharacterAttributes(editor, attr, false); } }
From source file:au.org.ala.delta.ui.rtf.RTFWriter.java
private void writeElement(Element element, int pos, int length) throws BadLocationException, IOException { AttributeSet elementAttributes = element.getAttributes(); writeAttributeChangesAsRTF(elementAttributes); if (element.isLeaf()) { String plainText = _document.getText(pos, length); for (int i = 0; i < plainText.length(); ++i) { char ch = plainText.charAt(i); if (ch > 127) { CharacterKeyword kwd = Keyword.findKeywordForCharacter(ch); if (kwd != null) { _writer.write("\\"); _writer.write(kwd.getKeyword()); _writer.write(" "); } else { _writer.write("\\u"); _writer.write(Integer.toString(ch)); _writer.write("?"); }/*from w w w . j a v a 2s. c om*/ } else { if (ch == '\n') { _writer.write("\\par "); } else { _writer.write(ch); } } } } else { for (int i = 0; i < element.getElementCount(); i++) { writeElement(element.getElement(i)); } } }
From source file:net.sf.jasperreports.engine.util.JEditorPaneRtfMarkupProcessor.java
@Override public String convert(String srcText) { JEditorPane editorPane = new JEditorPane("text/rtf", srcText); editorPane.setEditable(false);//ww w . ja v a 2 s .co m List<Element> elements = new ArrayList<Element>(); Document document = editorPane.getDocument(); Element root = document.getDefaultRootElement(); if (root != null) { addElements(elements, root); } String chunk = null; Element element = null; int startOffset = 0; int endOffset = 0; JRStyledText styledText = new JRStyledText(); styledText.setGlobalAttributes(new HashMap<Attribute, Object>()); for (int i = 0; i < elements.size(); i++) { if (chunk != null) { styledText.append(chunk); styledText.addRun( new JRStyledText.Run(getAttributes(element.getAttributes()), startOffset, endOffset)); } chunk = null; element = elements.get(i); startOffset = element.getStartOffset(); endOffset = element.getEndOffset(); try { chunk = document.getText(startOffset, endOffset - startOffset); } catch (BadLocationException e) { if (log.isDebugEnabled()) { log.debug("Error converting markup.", e); } } } if (chunk != null && !"\n".equals(chunk)) { styledText.append(chunk); styledText.addRun(new JRStyledText.Run(getAttributes(element.getAttributes()), startOffset, endOffset)); } return JRStyledTextParser.getInstance().write(styledText); }
From source file:edu.ku.brc.af.ui.forms.formatters.DataObjFieldFormatSinglePanel.java
/** * @param formatter//from w w w .ja v a 2 s .c o m */ protected void setFormatterFromTextPane(final DataObjSwitchFormatter formatter) { // visit every character in the document text looking for fields // store characters not associated with components (jbutton) to make up the separator text DefaultStyledDocument doc = (DefaultStyledDocument) formatEditor.getStyledDocument(); String text = formatEditor.getText(); int docLen = doc.getLength(); int lastFieldPos = 0; Vector<DataObjDataField> fields = new Vector<DataObjDataField>(); //int cnt = 0; for (int i = 0; i < docLen; ++i) { Element element = doc.getCharacterElement(i); AttributeSet attrs = element.getAttributes(); Object obj = attrs.getAttribute(StyleConstants.ComponentAttribute); //System.out.print(String.format("i: %d, lastFieldPos: %d cnt: %d, F: %s", i, lastFieldPos, cnt, (obj instanceof FieldDefinitionComp ? "Y" : "N"))); if (obj instanceof FieldDefinitionComp) { //System.out.println(cnt+" "+(obj instanceof FieldDefinitionComp)); // found button at the current position // create corresponding field String sepStr = (lastFieldPos <= i - 1) ? text.substring(lastFieldPos, i) : ""; FieldDefinitionComp fieldDefBtn = (FieldDefinitionComp) obj; DataObjDataField fmtField = fieldDefBtn.getValue(); fmtField.setSep(sepStr); fields.add(fmtField); //System.out.print(" Sep: ["+sepStr+"]"); lastFieldPos = i + 1; //cnt++; } } // XXX: what do we do with the remaining of the text? right now we ignore it // That's because we can't create an empty formatter field just to use the separator... DataObjDataField[] fieldsArray = new DataObjDataField[fields.size()]; for (int i = 0; i < fields.size(); ++i) { fieldsArray[i] = fields.elementAt(i); } DataObjDataFieldFormat singleFormatter = fieldsArray.length == 0 ? null : new DataObjDataFieldFormat("", tableInfo.getClassObj(), false, "", "", fieldsArray); formatter.setSingle(singleFormatter); }
From source file:EditorPaneExample16.java
public Heading getNextHeading(Document doc, ElementIterator iter) { Element elem; while ((elem = iter.next()) != null) { AttributeSet attrs = elem.getAttributes(); Object type = attrs.getAttribute(StyleConstants.NameAttribute); int level = getHeadingLevel(type); if (level > 0) { // It is a heading - get the text String headingText = ""; int count = elem.getElementCount(); for (int i = 0; i < count; i++) { Element child = elem.getElement(i); AttributeSet cattrs = child.getAttributes(); if (cattrs.getAttribute(StyleConstants.NameAttribute) == HTML.Tag.CONTENT) { try { int offset = child.getStartOffset(); headingText += doc.getText(offset, child.getEndOffset() - offset); } catch (BadLocationException e) { }/*from w w w .ja v a2 s . co m*/ } } headingText = headingText.trim(); return new Heading(headingText, level, elem.getStartOffset()); } } return null; }
From source file:com.hexidec.ekit.component.ExtendedHTMLDocument.java
@Override public void setParagraphAttributes(int offset, int length, AttributeSet s, boolean replace) { for (HTMLDocumentBehavior b : behaviors) { s = b.beforeSetParagraphAttributes(this, offset, length, s, replace); }// w w w .j a v a2s . c o m try { writeLock(); // Make sure we send out a change for the length of the paragraph. int end = Math.min(offset + length, getLength()); Element e = getParagraphElement(offset); offset = e.getStartOffset(); e = getParagraphElement(end); length = Math.max(0, e.getEndOffset() - offset); DefaultDocumentEvent changes = new DefaultDocumentEvent(offset, length, DocumentEvent.EventType.CHANGE); AttributeSet sCopy = s.copyAttributes(); int lastEnd = Integer.MAX_VALUE; for (int pos = offset; pos <= end; pos = lastEnd) { Element paragraph = getParagraphElement(pos); if (lastEnd == paragraph.getEndOffset()) { lastEnd++; } else { lastEnd = paragraph.getEndOffset(); } MutableAttributeSet attr = (MutableAttributeSet) paragraph.getAttributes(); changes.addEdit(new AttributeUndoableEdit(paragraph, sCopy, replace)); if (replace) { attr.removeAttributes(attr); } DocumentUtil.copyAllAttributesRemovingMarked(attr, s); } changes.end(); fireChangedUpdate(changes); fireUndoableEditUpdate(new UndoableEditEvent(this, changes)); } finally { writeUnlock(); } }