/** * Appends the text at the end. * * @param text the text to append */ public synchronized void append(String text) { append(text, null); }
/** * Returns the current position of the cursor. * * @return the cursor position */ public int getCaretPosition() { return m_TextPane.getCaretPosition(); }
/** * Sets the position of the cursor at the end. */ public void setCaretPositionLast() { setCaretPosition(getDocument().getLength()); } }
/** * Pops up a dialog for selecting the font. */ public void selectFont() { FontChooser chooser; if (getParentDialog() != null) chooser = new FontChooser(getParentDialog()); else chooser = new FontChooser(getParentFrame()); chooser.setCurrent(getFont()); chooser.setModalityType(ModalityType.DOCUMENT_MODAL); chooser.setVisible(true); setTextFont(chooser.getCurrent()); }
/** * Pops up a print dialog. */ public void printText() { String msg; try { print(null, null, true, null, null, true); } catch (Exception ex) { ex.printStackTrace(); msg = "Failed to print:\n" + ex; if (getParentDialog() != null) GUIHelper.showErrorMessage(getParentDialog(), msg); else GUIHelper.showErrorMessage(getParentFrame(), msg); } }
/** * Appends the text at the end. * * @param text the text to append * @param a the attribute set, null if to use current */ public synchronized void append(String text, AttributeSet a) { StyledDocument doc; doc = getStyledDocument(); try { doc.insertString(doc.getLength(), text, a); } catch (Exception e) { System.err.println("Failed to insert text: " + text); e.printStackTrace(); } setCaretPosition(doc.getLength()); }
/** * Sets the font of the text pane. * * 2003-2007 Philip Isenhour, public domain, http://javatechniques.com/blog/setting-jtextpane-font-and-color/ * * @param font the font to use */ public void setTextFont(Font font) { // Start with the current input attributes for the JTextPane. This // should ensure that we do not wipe out any existing attributes // (such as alignment or other paragraph attributes) currently // set on the text area. MutableAttributeSet attrs = getInputAttributes(); // Set the font family, size, and style, based on properties of // the Font object. Note that JTextPane supports a number of // character attributes beyond those supported by the Font class. // For example, underline, strike-through, super- and sub-script. StyleConstants.setFontFamily(attrs, font.getFamily()); StyleConstants.setFontSize(attrs, font.getSize()); StyleConstants.setItalic(attrs, (font.getStyle() & Font.ITALIC) != 0); StyleConstants.setBold(attrs, (font.getStyle() & Font.BOLD) != 0); // Retrieve the pane's document object StyledDocument doc = getStyledDocument(); // Replace the style for the entire document. We exceed the length // of the document by 1 so that text entered at the end of the // document uses the attributes. doc.setCharacterAttributes(0, doc.getLength() + 1, attrs, false); }
/** * Returns the underlying document. * * @return the document */ public Document getDocument() { return m_TextPane.getDocument(); }
/** * Returns the underlying text. * * @return the underlying text */ public String getSelectedText() { return m_TextPane.getSelectedText(); }
/** * For initializing the GUI. */ @Override protected void initGUI() { super.initGUI(); setLayout(new BorderLayout()); m_PanelWrapper = new JPanel(new BorderLayout()); m_ScrollPane = new BaseScrollPane(m_PanelWrapper); add(m_ScrollPane, BorderLayout.CENTER); m_TextPane = new BaseTextPane(); m_PanelWrapper.add(m_TextPane, BorderLayout.CENTER); }
/** * Returns the number of lines in the document (= # of elements). * * @return the number of lines */ public int getLineCount() { return getDocument().getDefaultRootElement().getElementCount(); }
/** * Determines the line number for the caret position. * * @param position the caret position * @return the line number, -1 if failed to determine */ public int caretToLine(int position) { int result; Element root; Element child; int i; result = -1; synchronized(getDocument()) { root = getDocument().getDefaultRootElement(); for (i = 0; i < root.getElementCount(); i++) { child = root.getElement(i); if ((child.getStartOffset() >= position) && (position <= child.getEndOffset())) { result = i; break; } } } return result; }
/** * Appends the text at the end. * * @param text the text to append */ public void append(String text) { m_TextPane.append(text); }
/** * Appends the text at the end. * * @param text the text to append * @param a the attribute set, null if to use current */ public void append(String text, AttributeSet a) { m_TextPane.append(text, a); }