protected int getVisibleEnd(RTextArea textArea) { return textArea.getDocument().getLength(); }
/** * Serializes this text area. * * @param s The stream to write to. * @throws IOException If an IO error occurs. */ private void writeObject(ObjectOutputStream s) throws IOException { // UndoManagers cannot be serialized without Exceptions. See // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4275892 getDocument().removeUndoableEditListener(undoManager); s.defaultWriteObject(); getDocument().addUndoableEditListener(undoManager); }
@Override public void actionPerformedImpl(ActionEvent e, RTextArea textArea) { Document doc = textArea.getDocument(); textArea.setCaretPosition(0); textArea.moveCaretPosition(doc.getLength()); }
/** * De-serializes a text area. * * @param s The stream to read from. * @throws ClassNotFoundException * @throws IOException */ private void readObject(ObjectInputStream s) throws ClassNotFoundException, IOException { s.defaultReadObject(); // UndoManagers cannot be serialized without Exceptions. See // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4275892 undoManager = createUndoManager(); getDocument().addUndoableEditListener(undoManager); lineHighlightManager = null; // Keep FindBugs happy. }
/** * Returns a <code>CharSequence</code> for a text area that doesn't make a * copy of its contents for iteration. This conserves memory but is likely * just a tad slower. * * @param textArea The text area whose document is the basis for the * <code>CharSequence</code>. * @param start The starting offset of the sequence (or ending offset if * <code>forward</code> is <code>false</code>). * @param forward Whether we're searching forward or backward. * @return The character sequence. */ private static CharSequence getFindInCharSequence(RTextArea textArea, int start, boolean forward) { RDocument doc = (RDocument)textArea.getDocument(); int csStart = 0; int csEnd = 0; if (forward) { csStart = start; csEnd = doc.getLength(); } else { csStart = 0; csEnd = start; } return new RDocumentCharSequence(doc, csStart, csEnd); }
/** * Removes all undoable edits from this document's undo manager. This * method also makes the undo/redo actions disabled. */ /* * NOTE: For some reason, it appears I have to create an entirely new * <code>undoManager</code> for undo/redo to continue functioning * properly; if I don't, it only ever lets you do one undo. Not * too sure why this is... */ public void discardAllEdits() { undoManager.discardAllEdits(); getDocument().removeUndoableEditListener(undoManager); undoManager = createUndoManager(); getDocument().addUndoableEditListener(undoManager); undoManager.updateActions(); }
/** * Highlights the specified line. * * @param line The line to highlight. * @param color The color to highlight with. * @return A tag for the highlight. * @throws BadLocationException If <code>line</code> is not a valid line * number. * @see #removeLineHighlight(Object) */ public Object addLineHighlight(int line, Color color) throws BadLocationException { int offs = textArea.getLineStartOffset(line); LineHighlightInfo lhi = new LineHighlightInfo( textArea.getDocument().createPosition(offs), color); if (lineHighlights==null) { lineHighlights = new ArrayList<LineHighlightInfo>(1); } int index = Collections.binarySearch(lineHighlights, lhi, comparator); if (index<0) { // Common case index = -(index+1); } lineHighlights.add(index, lhi); repaintLine(lhi); return lhi; }
@Override protected int getWordStart(RTextArea textArea, int offs) throws BadLocationException { RSyntaxDocument doc = (RSyntaxDocument)textArea.getDocument(); Element root = doc.getDefaultRootElement(); int line = root.getElementIndex(offs); Element elem = root.getElement(line); return getWordStartImpl(doc, elem, offs); }
/** * Sets the document used by this text area. * * @param document The new document to use. * @throws IllegalArgumentException If the document is not an instance of * {@link RDocument}. */ @Override public void setDocument(Document document) { if (!(document instanceof RDocument)) { throw new IllegalArgumentException("RTextArea requires " + "instances of RDocument for its document"); } if (undoManager!=null) { // First time through, undoManager==null Document old = getDocument(); if (old!=null) { old.removeUndoableEditListener(undoManager); } } super.setDocument(document); if (undoManager!=null) { document.addUndoableEditListener(undoManager); discardAllEdits(); } }
@Override public void actionPerformedImpl(ActionEvent e, RTextArea textArea) { if (!textArea.isEditable() || !textArea.isEnabled()) { UIManager.getLookAndFeel().provideErrorFeedback(textArea); return; } try { int end = textArea.getSelectionStart(); int start = getPreviousWordStart(textArea, end); if (end>start) { textArea.getDocument().remove(start, end-start); } } catch (BadLocationException ex) { UIManager.getLookAndFeel().provideErrorFeedback(textArea); } }
public void install(RTextArea textArea) { if (installed) { uninstall(); } textArea.addComponentListener(this); textArea.getDocument().addDocumentListener(this); textArea.addPropertyChangeListener(this); if (textArea instanceof RSyntaxTextArea) { RSyntaxTextArea rsta = (RSyntaxTextArea)textArea; rsta.addActiveLineRangeListener(this); rsta.getFoldManager().addPropertyChangeListener(this); } installed = true; }
/** * We override this method because the super version gives us an entirely * new <code>Document</code>, thus requiring us to re-attach our Undo * manager. With this version we just replace the text. */ @Override public void read(Reader in, Object desc) throws IOException { RTextAreaEditorKit kit = (RTextAreaEditorKit)getUI().getEditorKit(this); setText(null); Document doc = getDocument(); if (desc != null) { doc.putProperty(Document.StreamDescriptionProperty, desc); } try { // NOTE: Resets the "line separator" property. kit.read(in, doc, 0); } catch (BadLocationException e) { throw new IOException(e.getMessage()); } }
/** * Repaints the line pointed to by the specified highlight information. * * @param lhi The highlight information. */ private void repaintLine(LineHighlightInfo lhi) { int offs = lhi.getOffset(); // May be > length if they deleted text including the highlight if (offs>=0 && offs<=textArea.getDocument().getLength()) { try { int y = textArea.yForLineContaining(offs); if (y>-1) { textArea.repaint(0, y, textArea.getWidth(), textArea.getLineHeight()); } } catch (BadLocationException ble) { ble.printStackTrace(); // Never happens } } }
public void uninstall() { if (installed) { textArea.removeComponentListener(this); textArea.getDocument().removeDocumentListener(this); textArea.removePropertyChangeListener(this); if (textArea instanceof RSyntaxTextArea) { RSyntaxTextArea rsta = (RSyntaxTextArea)textArea; rsta.removeActiveLineRangeListener(this); rsta.getFoldManager().removePropertyChangeListener(this); } installed = false; } }
/** * Adds a special "marked occurrence" highlight. * * @param start * @param end * @param p * @return A tag to reference the highlight later. * @throws BadLocationException * @see #clearMarkAllHighlights() */ Object addMarkAllHighlight(int start, int end, HighlightPainter p) throws BadLocationException { Document doc = textArea.getDocument(); TextUI mapper = textArea.getUI(); // Always layered highlights for marked occurrences. HighlightInfoImpl i = new LayeredHighlightInfoImpl(); i.setPainter(p); i.p0 = doc.createPosition(start); // HACK: Use "end-1" to prevent chars the user types at the "end" of // the highlight to be absorbed into the highlight (default Highlight // behavior). i.p1 = doc.createPosition(end-1); markAllHighlights.add(i); mapper.damageRange(textArea, start, end); return i; }
@Override protected int getWordEnd(RTextArea textArea, int offs) throws BadLocationException { RSyntaxDocument doc = (RSyntaxDocument)textArea.getDocument(); Element root = doc.getDefaultRootElement(); int line = root.getElementIndex(offs); Element elem = root.getElement(line); int end = elem.getEndOffset() - 1; int wordEnd = offs; while (wordEnd <= end) { if (!isIdentifierChar(doc.charAt(wordEnd))) { break; } wordEnd++; } return wordEnd; }
/** * Adds a special "marked occurrence" highlight. * * @param start * @param end * @param p * @return A tag to reference the highlight later. * @throws BadLocationException * @see #clearMarkOccurrencesHighlights() */ Object addMarkedOccurrenceHighlight(int start, int end, SmartHighlightPainter p) throws BadLocationException { Document doc = textArea.getDocument(); TextUI mapper = textArea.getUI(); // Always layered highlights for marked occurrences. SyntaxLayeredHighlightInfoImpl i = new SyntaxLayeredHighlightInfoImpl(); i.setPainter(p); i.setStartOffset(doc.createPosition(start)); // HACK: Use "end-1" to prevent chars the user types at the "end" of // the highlight to be absorbed into the highlight (default Highlight // behavior). i.setEndOffset(doc.createPosition(end-1)); markedOccurrences.add(i); mapper.damageRange(textArea, start, end); return i; }
@Override public void actionPerformedImpl(ActionEvent e, RTextArea textArea) { try { // We use the elements instead of calling getLineOfOffset(), // etc. to speed things up just a tad (i.e. micro-optimize). Document document = textArea.getDocument(); int caretPosition = textArea.getCaretPosition(); Element map = document.getDefaultRootElement(); int currentLineNum = map.getElementIndex(caretPosition); Element currentLineElement = map.getElement(currentLineNum); // Always take -1 as we don't want to remove the newline. int currentLineEnd = currentLineElement.getEndOffset()-1; if (caretPosition<currentLineEnd) { document.remove(caretPosition, currentLineEnd-caretPosition); } } catch (BadLocationException ble) { ble.printStackTrace(); } }
@Override public void actionPerformedImpl(ActionEvent e, RTextArea textArea) { int offs = textArea.getCaretPosition(); int endOffs = 0; try { if (textArea.getLineWrap()) { // Must check per character, since one logical line may be // many physical lines. // FIXME: Replace Utilities call with custom version to // cut down on all of the modelToViews, as each call causes // a getTokenList => expensive! endOffs = Utilities.getRowEnd(textArea, offs); } else { Element root = textArea.getDocument().getDefaultRootElement(); int line = root.getElementIndex(offs); endOffs = root.getElement(line).getEndOffset() - 1; } if (select) { textArea.moveCaretPosition(endOffs); } else { textArea.setCaretPosition(endOffs); } } catch (Exception ex) { UIManager.getLookAndFeel().provideErrorFeedback(textArea); } }
@Override public void actionPerformedImpl(ActionEvent e, RTextArea textArea) { if (!textArea.isEditable() || !textArea.isEnabled()) { UIManager.getLookAndFeel().provideErrorFeedback(textArea); return; } try { Caret c = textArea.getCaret(); int caretPos = c.getDot(); Document doc = textArea.getDocument(); Element map = doc.getDefaultRootElement(); int lineCount = map.getElementCount(); int line = map.getElementIndex(caretPos); if (line==lineCount-1) { UIManager.getLookAndFeel(). provideErrorFeedback(textArea); return; } Element lineElem = map.getElement(line); caretPos = lineElem.getEndOffset() - 1; c.setDot(caretPos); // Gets rid of any selection. doc.remove(caretPos, 1); // Should be '\n'. } catch (BadLocationException ble) { /* Shouldn't ever happen. */ ble.printStackTrace(); } textArea.requestFocusInWindow(); }