Java Code Examples for javax.swing.text.BadLocationException#toString()

The following examples show how to use javax.swing.text.BadLocationException#toString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: netbeans   File: FixLineSyntaxState.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * @param offset to be examined.
 * @return offset that will be high enough to ensure that the given offset
 *  will be covered by token that can be returned from the syntax.nextToken()
 *  assuming that the syntax will be prepared with the returned token.
 *  <BR>It's not guaranteed how much bigger the returned offset will be.
 */
static int getTokenSafeOffset(BaseDocument doc, int offset) {
    if (offset == 0) { // no valid state-info at offset 0
        return offset;
    }

    try {
        Element lineRoot = getLineRoot(doc);
        int lineIndex = lineRoot.getElementIndex(offset);
        Element lineElem = lineRoot.getElement(lineIndex);
        int lineStartOffset = lineElem.getStartOffset();
        Syntax.StateInfo stateInfo = getValidSyntaxStateInfo(doc, lineIndex);
        if (offset == lineStartOffset && stateInfo.getPreScan() == 0) {
            // can be done with the given offset
            return offset;
        }

        // go to next line and maybe further for tokens
        // crossing several lines
        int lineCount = lineRoot.getElementCount();
        while (++lineIndex < lineCount) {
            lineElem = lineRoot.getElement(lineIndex);
            stateInfo = getValidSyntaxStateInfo(doc, lineIndex);
            lineStartOffset = lineElem.getStartOffset();
            if (lineStartOffset - stateInfo.getPreScan() >= offset) {
                return lineStartOffset;
            }
        }
    } catch (BadLocationException e) {
        throw new IllegalStateException(e.toString());
    }

    return doc.getLength();
}
 
Example 2
Source Project: netbeans   File: TestDocument.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected void insertUpdate(DefaultDocumentEvent chng, AttributeSet attr) {
    super.insertUpdate(chng, attr);
    DocumentUtilities.addEventPropertyStorage(chng);
    try {
        DocumentUtilities.putEventProperty(chng, String.class,
                getText(chng.getOffset(), chng.getLength()));
    } catch (BadLocationException e) {
        e.printStackTrace();
        throw new IllegalStateException(e.toString());
    }
}
 
Example 3
Source Project: netbeans   File: TestDocument.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected void removeUpdate(DefaultDocumentEvent chng) {
    super.removeUpdate(chng);
    DocumentUtilities.addEventPropertyStorage(chng);
    try {
        DocumentUtilities.putEventProperty(chng, String.class,
                getText(chng.getOffset(), chng.getLength()));
    } catch (BadLocationException e) {
        e.printStackTrace();
        throw new IllegalStateException(e.toString());
    }
}
 
Example 4
Source Project: netbeans   File: DemoTokenUpdater.java    License: Apache License 2.0 5 votes vote down vote up
public char textCharAt(int index) {
    synchronized (seg) {
        try {
            doc.getText(index, 1, seg);
            return seg.array[seg.offset];

        } catch (BadLocationException e) {
            throw new IllegalStateException(e.toString());
        }
    }
}
 
Example 5
protected void insertUpdate(DefaultDocumentEvent chng, AttributeSet attr) {
    super.insertUpdate(chng, attr);
    DocumentUtilities.addEventPropertyStorage(chng);
    try {
        DocumentUtilities.putEventProperty(chng, String.class,
                getText(chng.getOffset(), chng.getLength()));
    } catch (BadLocationException e) {
        e.printStackTrace();
        throw new IllegalStateException(e.toString());
    }
}
 
Example 6
protected void removeUpdate(DefaultDocumentEvent chng) {
    super.removeUpdate(chng);
    DocumentUtilities.addEventPropertyStorage(chng);
    try {
        DocumentUtilities.putEventProperty(chng, String.class,
                getText(chng.getOffset(), chng.getLength()));
    } catch (BadLocationException e) {
        e.printStackTrace();
        throw new IllegalStateException(e.toString());
    }
}
 
Example 7
protected void showHighlight(){
  try {
    highlight = textPane.getHighlighter().addHighlight(start, end,
                                    DefaultHighlighter.DefaultPainter);
  }catch(BadLocationException ble){
    throw new GateRuntimeException(ble.toString());
  }

}
 
Example 8
private void setText(final String text) {
  try {
    // remove all text and insert the completed string
    super.remove(0, getLength());
    super.insertString(0, text, null);
  } catch (final BadLocationException e) {
    throw new RuntimeException(e.toString());
  }
}
 
Example 9
/**
 * Sets the text of this AutoCompleteDocument to the given text.
 * 
 * @param text the text that will be set for this document
 */
private void setText(String text) {
    try {
        // remove all text and insert the completed string
        super.remove(0, getLength());
        super.insertString(0, text, null);
    } catch (BadLocationException e) {
        throw new RuntimeException(e.toString());
    }
}
 
Example 10
Source Project: netbeans   File: BaseCaret.java    License: Apache License 2.0 4 votes vote down vote up
/** Sets the caret position to some position. This
 * causes removal of the active selection. If expandFold set to true
 * fold containing offset position will be expanded.
 *
 * <p>
 * <b>Note:</b> This method is deprecated and the present implementation
 * ignores values of scrollRect and scrollPolicy parameters.
 *
 * @param offset offset in the document to which the caret should be positioned.
 * @param scrollRect rectangle to which the editor window should be scrolled.
 * @param scrollPolicy the way how scrolling should be done.
 *  One of <code>EditorUI.SCROLL_*</code> constants.
 * @param expandFold whether possible fold at the caret position should be expanded.
 *
 * @deprecated use #setDot(int, boolean) preceded by <code>JComponent.scrollRectToVisible()</code>.
 */

public void setDot(int offset, Rectangle scrollRect, int scrollPolicy, boolean expandFold) {
    if (LOG_EDT.isLoggable(Level.FINE)) { // Only permit operations in EDT
        if (!SwingUtilities.isEventDispatchThread()) {
            throw new IllegalStateException("BaseCaret.setDot() not in EDT: offset=" + offset); // NOI18N
        }
    }

    if (LOG.isLoggable(Level.FINE)) {
        LOG.fine("setDot: offset=" + offset); //NOI18N
        if (LOG.isLoggable(Level.FINEST)) {
            LOG.log(Level.INFO, "setDot call stack", new Exception());
        }
    }
    
    JTextComponent c = component;
    if (c != null) {
        BaseDocument doc = (BaseDocument)c.getDocument();
        boolean dotChanged = false;
        doc.readLock();
        try {
            if (doc != null && offset >= 0 && offset <= doc.getLength()) {
                dotChanged = true;
                try {
                    caretPos = doc.createPosition(offset);
                    markPos = doc.createPosition(offset);

                    Callable<Boolean> cc = (Callable<Boolean>)c.getClientProperty("org.netbeans.api.fold.expander");
                    if (cc != null && expandFold) {
                        // the caretPos/markPos were already called.
                        // nothing except the document is locked at this moment.
                        try {
                            cc.call();
                        } catch (Exception ex) {
                            Exceptions.printStackTrace(ex);
                        }
                    }
                    if (rectangularSelection) {
                        setRectangularSelectionToDotAndMark();
                    }
                    
                } catch (BadLocationException e) {
                    throw new IllegalStateException(e.toString());
                    // setting the caret to wrong position leaves it at current position
                }
            }
        } finally {
            doc.readUnlock();
        }
        
        if (dotChanged) {
            fireStateChanged();
            dispatchUpdate(true);
        }
    }
}
 
Example 11
Source Project: netbeans   File: BaseCaret.java    License: Apache License 2.0 4 votes vote down vote up
/** Makes selection by moving dot but leaving mark.
 * 
 * <p>
 * <b>Note:</b> This method is deprecated and the present implementation
 * ignores values of scrollRect and scrollPolicy parameters.
 *
 * @param offset offset in the document to which the caret should be positioned.
 * @param scrollRect rectangle to which the editor window should be scrolled.
 * @param scrollPolicy the way how scrolling should be done.
 *  One of <code>EditorUI.SCROLL_*</code> constants.
 *
 * @deprecated use #setDot(int) preceded by <code>JComponent.scrollRectToVisible()</code>.
 */
public void moveDot(int offset, Rectangle scrollRect, int scrollPolicy) {
    if (LOG_EDT.isLoggable(Level.FINE)) { // Only permit operations in EDT
        if (!SwingUtilities.isEventDispatchThread()) {
            throw new IllegalStateException("BaseCaret.moveDot() not in EDT: offset=" + offset); // NOI18N
        }
    }

    if (LOG.isLoggable(Level.FINE)) {
        LOG.fine("moveDot: offset=" + offset); //NOI18N
    }

    JTextComponent c = component;
    if (c != null) {
        BaseDocument doc = (BaseDocument)c.getDocument();
        if (doc != null && offset >= 0 && offset <= doc.getLength()) {
            doc.readLock();
            try {
                int oldCaretPos = getDot();
                if (offset == oldCaretPos) { // no change
                    return;
                }
                caretPos = doc.createPosition(offset);
                if (selectionVisible) { // selection already visible
                    Utilities.getEditorUI(c).repaintBlock(oldCaretPos, offset);
                }
                if (rectangularSelection) {
                    Rectangle r = c.modelToView(offset);
                    if (rsDotRect != null) {
                        rsDotRect.y = r.y;
                        rsDotRect.height = r.height;
                    } else {
                        rsDotRect = r;
                    }
                    updateRectangularSelectionPaintRect();
                }
            } catch (BadLocationException e) {
                throw new IllegalStateException(e.toString());
                // position is incorrect
            } finally {
                doc.readUnlock();
            }
        }
        fireStateChanged();
        dispatchUpdate(true);
    }
}
 
Example 12
Source Project: netbeans   File: LineRootElement.java    License: Apache License 2.0 4 votes vote down vote up
public void insertUpdate(DocumentEvent evt, UndoableEdit edit, AttributeSet attr) {
        int insertOffset = evt.getOffset();
        int insertEndOffset = insertOffset + evt.getLength();
        CharSequence text = DocumentUtilities.getText(doc);
        if (insertOffset > 0) { // [Swing] marks (and elements) at offset zero do not move up
            insertOffset--;
        }
        try {
            int index = -1; // Index of the elements modification - computed lazily
            List<LineElement> addedLines = null; // Collected added lines
            LineElement removedLine = null; // Removed line element
            Position lastAddedLineEndPos = null;
            for (int offset = insertOffset; offset < insertEndOffset; offset++) {
                if (text.charAt(offset) == '\n') {
                    if (index == -1) { // Not computed yet
                        index = getElementIndex(offset);
                        removedLine = (LineElement)getElement(index);
                        lastAddedLineEndPos = removedLine.getStartPosition();
                        addedLines = new ArrayList<LineElement>(2);
                    }
                    Position lineEndPos = doc.createPosition(offset + 1);
                    addedLines.add(new LineElement(this, lastAddedLineEndPos, lineEndPos));
                    lastAddedLineEndPos = lineEndPos;
                }
            }
            if (index != -1) { // Some lines were added
                // If the text was inserted at the line boundary i.e. right after existing '\n'
                // and the ending char of the inserted text was not '\n' (otherwise
                // it would be a "clean" line insert) then there must be two line elements
                // removed.
                Position removedLineEndPos = removedLine.getEndPosition();
                int removedLineEndOffset = removedLineEndPos.getOffset();
                Element[] removed; // removed line elements
                int lastAddedLineEndOffset = lastAddedLineEndPos.getOffset();
                if (insertEndOffset == removedLineEndOffset
                        && lastAddedLineEndOffset != removedLineEndOffset
//                        && index + 1 < getElementCount()
                ) {
                    LineElement removedLine2 = (LineElement)getElement(index + 1);
                    removed = new Element[] { removedLine, removedLine2 };
                    removedLineEndPos = removedLine2.getEndPosition();
                    removedLineEndOffset = removedLineEndPos.getOffset();
                } else { // just one line removed
                    removed = new Element[] { removedLine };
                }
                if (lastAddedLineEndOffset < removedLineEndOffset) {
                    addedLines.add(new LineElement(this, lastAddedLineEndPos, removedLineEndPos));
                }

                Element[] added = new Element[addedLines.size()];
                addedLines.toArray(added);

                edit.addEdit(new Edit(index, removed, added));
                replace(index, removed.length, added);
            }
        } catch (BadLocationException e) {
            throw new IllegalStateException(e.toString());
        }
    }