Java Code Examples for org.openide.text.NbDocument#getDocument()

The following examples show how to use org.openide.text.NbDocument#getDocument() . 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 File: LineTranslations.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void attach() throws IOException {
    DataObject dobj;
    synchronized (this) {
        dobj = this.dataObject;
    }
    LineCookie lc = dobj.getLookup().lookup (LineCookie.class);
    if (lc == null) {
        return ;
    }
    lb.addPropertyChangeListener(this);
    try {
        final Line lineNew = lc.getLineSet().getCurrent(lb.getLineNumber() - 1);
        synchronized (this) {
            if (line != null) {
                line.removePropertyChangeListener(this);
            }
            this.line = lineNew;
        }
        lineNew.addPropertyChangeListener(this);
        StyledDocument document = NbDocument.getDocument(new Lookup.Provider() {
                                      @Override
                                      public Lookup getLookup() {
                                          return lineNew.getLookup();
                                      }
                                  });
        if (document instanceof BaseDocument) {
            BaseDocument bd = (BaseDocument) document;
            bd.addPostModificationDocumentListener(this);
        }
    } catch (IndexOutOfBoundsException ioobex) {
        // ignore document changes for BP with bad line number
    }
}
 
Example 2
Source File: GsfUtilities.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private static boolean doOpen(FileObject fo, int offset, String search) {
    try {
        DataObject od = DataObject.find(fo);
        EditorCookie ec = od.getCookie(EditorCookie.class);
        LineCookie lc = od.getCookie(LineCookie.class);

        // If the caller hasn't specified an offset, and the document is
        // already open, don't jump to a particular line!
        if (ec != null && offset == -1 && ec.getDocument() != null && search == null) {
            ec.open();
            return true;
        }

        // Simple text search if no known offset (e.g. broken/unparseable source)
        if ((search != null) && (offset == -1)) {
            StyledDocument doc = NbDocument.getDocument(od);

            try {
                String text = doc.getText(0, doc.getLength());
                int caretDelta = search.indexOf('^');
                if (caretDelta != -1) {
                    search = search.substring(0, caretDelta) + search.substring(caretDelta+1);
                } else {
                    caretDelta = 0;
                }
                offset = text.indexOf(search);
                if (offset != -1) {
                    offset += caretDelta;
                }
            } catch (BadLocationException ble) {
                LOG.log(Level.WARNING, null, ble);
            }
        }
        
        return NbDocument.openDocument(od, offset, Line.ShowOpenType.OPEN, Line.ShowVisibilityType.FOCUS);
    } catch (IOException e) {
        ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
    }

    return false;
}