Java Code Examples for org.openide.cookies.LineCookie#getLineSet()

The following examples show how to use org.openide.cookies.LineCookie#getLineSet() . 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: ActionProviderImpl.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private static void open(FileObject file, int line) {
    LineCookie lc = file.getLookup().lookup(LineCookie.class);

    if (lc != null) {
        Line.Set ls = lc.getLineSet();
        try {
            Line originalLine = ls.getOriginal(line);
            originalLine.show(ShowOpenType.OPEN, ShowVisibilityType.FOCUS);
        } catch (IndexOutOfBoundsException ex) {
            LOG.log(Level.FINE, null, ex);
        }
    }

    OpenCookie oc = file.getLookup().lookup(OpenCookie.class);

    if (oc != null) {
        oc.open();
    }
}
 
Example 2
Source File: NbEditorUtilities.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/** Get the line object from the given position.
 * @param doc document for which the line is being retrieved
 * @param offset position in the document
 * @param original whether to retrieve the original line (true) before
 *   the modifications were done or the current line (false)
 * @return the line object
 */
public static Line getLine(Document doc, int offset, boolean original) {
    DataObject dob = getDataObject(doc);
    if (dob != null) {
        LineCookie lc = (LineCookie)dob.getCookie(LineCookie.class);
        if (lc != null) {
            Line.Set lineSet = lc.getLineSet();
            if (lineSet != null) {
                Element lineRoot = (doc instanceof AbstractDocument)
                    ? ((AbstractDocument)doc).getParagraphElement(0).getParentElement()
                    : doc.getDefaultRootElement();
                int lineIndex = lineRoot.getElementIndex(offset);
                return original
                       ? lineSet.getOriginal(lineIndex)
                       : lineSet.getCurrent(lineIndex);
            }
        }
    }
    return null;
}
 
Example 3
Source File: NbEditorUtilities.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/** Get the line object from the given position.
* @param doc document for which the line is being retrieved
* @param offset position in the document
* @param original whether to retrieve the original line (true) before
*   the modifications were done or the current line (false)
* @return the line object
* @deprecated Replaced by more generic method having {@link javax.swing.text.Document} parameter.
*/
public static Line getLine(BaseDocument doc, int offset, boolean original) {
    DataObject dob = getDataObject(doc);
    if (dob != null) {
        LineCookie lc = (LineCookie)dob.getCookie(LineCookie.class);
        if (lc != null) {
            Line.Set lineSet = lc.getLineSet();
            if (lineSet != null) {
                try {
                    int lineOffset = Utilities.getLineOffset(doc, offset);
                    return original
                           ? lineSet.getOriginal(lineOffset)
                           : lineSet.getCurrent(lineOffset);
                } catch (BadLocationException e) {
                }

            }
        }
    }
    return null;
}
 
Example 4
Source File: LineDelegate.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public void setLineNumber(int lineNumber) {
    lineNumber--; // Line works with 0-based lines.
    if (line.getLineNumber() == lineNumber) {
        return ;
    }
    LineCookie lineCookie = line.getLookup().lookup(LineCookie.class);
    Line.Set lineSet = lineCookie.getLineSet();
    List<? extends Line> lines = lineSet.getLines();
    if (lines.size() > 0) {
        int lastLineNumber = lines.get(lines.size() - 1).getLineNumber();
        if (lineNumber > lastLineNumber) {
            lineNumber = lastLineNumber;
        }
    }
    Line cline;
    try {
        cline = lineSet.getCurrent(lineNumber);
    } catch (IndexOutOfBoundsException ioobex) {
        cline = lineSet.getCurrent(0);
    }
    setLine(cline);
}
 
Example 5
Source File: JSUtils.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public static Line getLine(final FileObject fileObject, final int lineNumber) {
    if (fileObject != null) {
        LineCookie lineCookie = JSUtils.getLineCookie(fileObject);
        if (lineCookie != null) {
            Line.Set ls = lineCookie.getLineSet();
            if (ls != null) {
                try {
                    return ls.getCurrent(lineNumber - 1);
                } catch (IndexOutOfBoundsException ioob) {
                    List<? extends Line> lines = ls.getLines();
                    if (lines.size() > 0) {
                        return lines.get(lines.size() - 1);
                    } else {
                        return null;
                    }
                }
            }
        }
    }
    return null;
}
 
Example 6
Source File: WatchAnnotationProvider.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private Line getLine (FileObject file, int lineNumber) {
    if (file == null) return null;
    DataObject dataObject;
    try {
        dataObject = DataObject.find (file);
    } catch (DataObjectNotFoundException ex) {
        return null;
    }
    if (dataObject == null) return null;
    LineCookie lineCookie = dataObject.getLookup().lookup(LineCookie.class);
    if (lineCookie == null) return null;
    Line.Set ls = lineCookie.getLineSet ();
    if (ls == null) return null;
    try {
        return ls.getCurrent (lineNumber);
    } catch (IndexOutOfBoundsException | IllegalArgumentException e) {
    }
    return null;
}
 
Example 7
Source File: Utils.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public Line getLine(int line, String remoteFileName, SessionId id) {
    DataObject dataObject = Utils.getDataObjectByRemote(id, remoteFileName);
    if (dataObject == null) {
        return null;
    }
    LineCookie lineCookie = (LineCookie) dataObject.getLookup().lookup(LineCookie.class);
    if (lineCookie == null) {
        return null;
    }
    Line.Set set = lineCookie.getLineSet();
    if (set == null) {
        return null;
    }
    try {
        return set.getCurrent(line - 1);
    } catch (IndexOutOfBoundsException | IllegalArgumentException e) {
        Logger.getLogger(Utils.class.getName()).log(Level.FINE, e.getMessage(), e);
    }
    return null;
}
 
Example 8
Source File: MiscEditorUtil.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public static Line getLine(final FileObject fileObject, final int lineNumber) {
    if (fileObject != null) {
        LineCookie lineCookie = MiscEditorUtil.getLineCookie(fileObject);
        if (lineCookie != null) {
            Line.Set ls = lineCookie.getLineSet();
            if (ls != null) {
                try {
                    return ls.getCurrent(lineNumber - 1);
                } catch (IndexOutOfBoundsException ioob) {
                    List<? extends Line> lines = ls.getLines();
                    if (lines.size() > 0) {
                        return lines.get(lines.size() - 1);
                    } else {
                        return null;
                    }
                }
            }
        }
    }
    return null;
}
 
Example 9
Source File: TextDetail.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void prepareLine() {
    if (dobj == null || !dobj.isValid()) {
        lineObj = null;
    } else if (lineObj == null) { // try to get Line from DataObject
        LineCookie lineCookie = dobj.getLookup().lookup(LineCookie.class);
        if (lineCookie != null) {
            Line.Set lineSet = lineCookie.getLineSet();
            try {
                lineObj = lineSet.getOriginal(line - 1);
            } catch (IndexOutOfBoundsException ioobex) {
                // The line doesn't exist - go to the last line
                lineObj = lineSet.getOriginal(findMaxLine(lineSet));
                column = markLength = 0;
            }
        }
    }
}
 
Example 10
Source File: LineTranslations.java    From netbeans with Apache License 2.0 6 votes vote down vote up
Line.Set getLineSet (String url, Object timeStamp) {
    DataObject dataObject = getDataObject (url);
    if (dataObject == null) {
        return null;
    }
    
    if (timeStamp != null) {
        // get original
        synchronized (this) {
            Registry registry = timeStampToRegistry.get (timeStamp);
            if (registry != null) {
                Line.Set ls = registry.getLineSet (dataObject);
                if (ls != null) {
                    return ls;
                }
            }
        }
    }
    
    // get current
    LineCookie lineCookie = dataObject.getLookup().lookup(LineCookie.class);
    if (lineCookie == null) {
        return null;
    }
    return lineCookie.getLineSet ();
}
 
Example 11
Source File: Utils.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Get the Line from FileObject.
 *
 * @param file the FileObject
 * @param lineNumber the line number
 * @return the line if it is found with the file and the line number,
 * otherwise {@code null}
 */
@CheckForNull
public static Line getLine(FileObject file, int lineNumber) {
    if (file == null || lineNumber < 0) {
        return null;
    }

    DataObject dataObject;
    try {
        dataObject = DataObject.find(file);
    } catch (DataObjectNotFoundException ex) {
        return null;
    }
    if (dataObject == null) {
        return null;
    }
    LineCookie lineCookie = dataObject.getLookup().lookup(LineCookie.class);
    if (lineCookie == null) {
        return null;
    }
    Line.Set ls = lineCookie.getLineSet();
    if (ls == null) {
        return null;
    }
    try {
        return ls.getCurrent(lineNumber);
    } catch (IndexOutOfBoundsException e) {
        return null;
    }
}
 
Example 12
Source File: LanguagesNavigatorModel.java    From netbeans with Apache License 2.0 5 votes vote down vote up
void show () {
    DataObject dataObject = NbEditorUtilities.getDataObject (document);
    LineCookie lineCookie = dataObject.getCookie (LineCookie.class);
    Line.Set lineSet = lineCookie.getLineSet ();
    Line line = lineSet.getCurrent (NbDocument.findLineNumber (document, item.getOffset ()));
    int column = NbDocument.findLineColumn (document, item.getOffset ());
    line.show (ShowOpenType.OPEN, ShowVisibilityType.FOCUS, column);
}
 
Example 13
Source File: OpenTaskAction.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private boolean openAt( LineCookie lineCookie, int lineNo ) {
    Line.Set lines = lineCookie.getLineSet();
    try {
        Line line = lines.getCurrent( lineNo );
        if( null == line )
            line = lines.getCurrent( 0 );
        if( null != line ) {
            line.show( ShowOpenType.OPEN , ShowVisibilityType.FOCUS);
            return true;
        }
    } catch( IndexOutOfBoundsException e ) {
        //probably the document has been modified but not saved yet
    }
    return false;
}
 
Example 14
Source File: FodDataObjectFactory.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public Line.Set getLineSet() {
    delegate(null);
    DataObject obj;
    try {
        obj = DataObject.find(fo);
    } catch (DataObjectNotFoundException ex) {
        return null;
    }
    if (this == obj) {
        return null;
    }
    LineCookie lc = obj.getLookup().lookup(LineCookie.class);
    return lc.getLineSet();
}
 
Example 15
Source File: FileUtils.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/**
 * Open the file and optionally set cursor to the line. If the file does not exist,
 * nothing is done.
 * <p>
 * <i>Note:</i> This action is always run in AWT thread.
 * @param file file to be opened
 * @param line line of a file to set cursor to, {@code -1} if no specific line is needed
 * @see #openFile(File)
 * @since 2.54
 */
public static void openFile(@NonNull File file, int line) {
    Parameters.notNull("file", file); // NOI18N

    FileObject fileObject = FileUtil.toFileObject(FileUtil.normalizeFile(file));
    if (fileObject == null) {
        LOGGER.log(Level.INFO, "FileObject not found for {0}", file);
        return;
    }

    DataObject dataObject;
    try {
        dataObject = DataObject.find(fileObject);
    } catch (DataObjectNotFoundException ex) {
        LOGGER.log(Level.INFO, "DataObject not found for {0}", file);
        return;
    }

    if (line == -1) {
        // simply open file
        EditorCookie ec = dataObject.getLookup().lookup(EditorCookie.class);
        ec.open();
        return;
    }

    // open at specific line
    LineCookie lineCookie = dataObject.getLookup().lookup(LineCookie.class);
    if (lineCookie == null) {
        LOGGER.log(Level.INFO, "LineCookie not found for {0}", file);
        return;
    }
    Line.Set lineSet = lineCookie.getLineSet();
    try {
        final Line currentLine = lineSet.getOriginal(line - 1);
        Mutex.EVENT.readAccess(new Runnable() {
            @Override
            public void run() {
                currentLine.show(Line.ShowOpenType.OPEN, Line.ShowVisibilityType.FOCUS);
            }
        });
    } catch (IndexOutOfBoundsException exc) {
        LOGGER.log(Level.FINE, null, exc);
    }
}
 
Example 16
Source File: NbFileOpenHandler.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
    public void open(final FileObject file, final int lineno) {
        // FIXME this should not be needed
        if (!SwingUtilities.isEventDispatchThread()) {
            SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        open(file, lineno);
                    }
                });

            return; // not exactly accurate, but....
        }

        try {
            DataObject od = DataObject.find(file);
            EditorCookie ec = od.getCookie(EditorCookie.class);
            LineCookie lc = od.getCookie(LineCookie.class);

            if ((ec != null) && (lc != null)) {
                Document doc = ec.openDocument();

                if (doc != null) {
                    int line = lineno;

                    if (line < 1) {
                        line = 1;
                    }

                    // XXX .size() call is super-slow for large files, see issue
                    // #126531. So we fallback to catching IOOBE
//                    int nOfLines = lines.getLines().size();
//                    if (line > nOfLines) {
//                        line = nOfLines;
//                    }
                    try {
                        Line.Set lines = lc.getLineSet();
                        Line l = lines.getCurrent(line - 1);
                        if (l != null) {
                            l.show(ShowOpenType.OPEN, ShowVisibilityType.FOCUS);
                            return;
                        }
                    } catch (IndexOutOfBoundsException ioobe) {
                        // OK, since .size() cannot be used, see above
                    }
                }
            }

            OpenCookie oc = od.getCookie(OpenCookie.class);

            if (oc != null) {
                oc.open();
                return;
            }
        } catch (IOException e) {
            LOGGER.log(Level.INFO, null, e);
        }
    }
 
Example 17
Source File: FodDataObjectLineSetTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@RandomlyFails // ergonomics #3485
public void testCreateRecognize() throws Exception {
    assertFalse("Autoupdate is disabled", au.isEnabled());
    FileUtil.setMIMEType("huh", "text/x-huh");
    FileObject fo = FileUtil.createData(FileUtil.getConfigRoot(), "test/my.huh");
    DataObject obj = DataObject.find(fo);
    FileObject fo2 = FileUtil.createData(FileUtil.getConfigRoot(), "test/subdir/my.huh");
    DataObject obj2 = DataObject.find(fo2);
    CharSequence log = Log.enable("org.openide.loaders", Level.WARNING);
    LineCookie lineCookie = obj.getLookup().lookup(LineCookie.class);
    assertNotNull("Line cookie found", lineCookie);
    assertEquals("Cookie is OK too", lineCookie, obj.getCookie(LineCookie.class));
    assertEquals("Node is OK too", lineCookie, obj.getNodeDelegate().getCookie(LineCookie.class));
    assertEquals("Node lookup is OK too", lineCookie, obj.getNodeDelegate().getLookup().lookup(LineCookie.class));
    assertTrue("It is our cookie: " + lineCookie, lineCookie.getClass().getName().contains("ergonomics"));
    assertEquals("No warnings: " + log, 0, log.length());

    Line.Set lineSet = lineCookie.getLineSet();
    assertTrue("Autoupdate is enabled", au.isEnabled());
    assertNotNull("Line set found", lineSet);
    for (int i = 0; ; i++) {
        DataObject newObj = DataObject.find(fo);
        if (obj == newObj) {
            if (i < 50) {
                Thread.sleep(1000);
                continue;
            }
            fail("New object shall be created: " + newObj);
        }
        break;
    }
    assertFalse("Old is no longer valid", obj.isValid());

    DataObject newObj2 = DataObject.find(fo2);
    if (obj2 == newObj2) {
        fail("New object shall be created for all objects: " + newObj2);
    }

    DataObject folder = FodDataObjectFactory.create(fo).findDataObject(fo.getParent(), new HashSet<FileObject>());
    assertNull("Folders are not recognized", folder);
}