DevFaqOpenFileAtLine

(Difference between revisions)
m (How can I open a particular file at a particular line number and column?: Missing {)
 
(4 intermediate revisions not shown)
Line 1: Line 1:
__NOTOC__
__NOTOC__
-
===How can I open a particular file at a particular line number?===
+
===How can I open a particular file at a particular line number and column?===
Here is the basic idea (there are variations depending on your needs, so read the Javadoc):
Here is the basic idea (there are variations depending on your needs, so read the Javadoc):
Line 15: Line 15:
}
}
if (dobj != null)
if (dobj != null)
 +
{
     LineCookie lc = (LineCookie) dobj .getCookie(LineCookie.class);
     LineCookie lc = (LineCookie) dobj .getCookie(LineCookie.class);
     if (lc == null) {/* cannot do it */ return;}
     if (lc == null) {/* cannot do it */ return;}
Line 24: Line 25:
--------
--------
-
  Applies to: NetBeans 5.x, 6.0, 6.1
+
Applies to: NetBeans 5.x, 6.0, 6.1
-
For NetBeans 6.5 you should use something like:
+
For NetBeans 6.5 and later you should use something like:
<pre>
<pre>
Line 40: Line 41:
}
}
if (dobj != null)
if (dobj != null)
 +
{
     LineCookie lc = (LineCookie) dobj .getCookie(LineCookie.class);
     LineCookie lc = (LineCookie) dobj .getCookie(LineCookie.class);
     if (lc == null) {/* cannot do it */ return;}
     if (lc == null) {/* cannot do it */ return;}
     Line l = lc.getLineSet().getOriginal(lineNumber);
     Line l = lc.getLineSet().getOriginal(lineNumber);
-
     l.show(Line.ShowOpenType.OPEN, Line.ShowVisibilityType.FOCUS
+
     l.show(Line.ShowOpenType.OPEN, Line.ShowVisibilityType.FOCUS);
}
}
</pre>
</pre>
-
See the JavaDoc for Line.ShowOpenType and Line.ShowVisibilityType to see different options of showing the line (with focus, without focus, opening the editor if not open, etc.).
+
See the JavaDoc for <tt>Line.ShowOpenType</tt> and <tt>Line.ShowVisibilityType</tt> to see different options of showing the line (with focus, without focus, opening the editor if not open, etc.).
 +
 
 +
Example "How to open a document at line and at a specific column"
 +
<pre>
 +
    int lineNumber=42;
 +
    int colNumber=43;
 +
 
 +
    //misses checks for NPEs
 +
    FileObject fo = ...;
 +
    LineCookie lc = DataObject.find(fo).getLookup().lookup(LineCookie.class);
 +
    Line line = lc.getLineSet().getOriginal(lineNumber);
 +
    line.show(Line.ShowOpenType.OPEN, Line.ShowVisibilityType.FRONT, colNumber);
 +
</pre>

Current revision as of 10:23, 28 August 2016

How can I open a particular file at a particular line number and column?

Here is the basic idea (there are variations depending on your needs, so read the Javadoc):

File f = ...;
int lineNumber = ...;
FileObject fobj = FileUtil.toFileObject(f);
DataObject dobj = null;
try {
    dobj = DataObject.find(fobj);
} catch (DataObjectNotFoundException ex) {
    ex.printStackTrace();
}
if (dobj != null)
{
    LineCookie lc = (LineCookie) dobj .getCookie(LineCookie.class);
    if (lc == null) {/* cannot do it */ return;}
    Line l = lc.getLineSet().getOriginal(lineNumber);
    l.show(Line.SHOW_GOTO);
}

Applies to: NetBeans 5.x, 6.0, 6.1


For NetBeans 6.5 and later you should use something like:

File f = ...;
int lineNumber = ...;
FileObject fobj = FileUtil.toFileObject(f);
DataObject dobj = null;
try {
    dobj = DataObject.find(fobj);
} catch (DataObjectNotFoundException ex) {
    ex.printStackTrace();
}
if (dobj != null)
{
    LineCookie lc = (LineCookie) dobj .getCookie(LineCookie.class);
    if (lc == null) {/* cannot do it */ return;}
    Line l = lc.getLineSet().getOriginal(lineNumber);
    l.show(Line.ShowOpenType.OPEN, Line.ShowVisibilityType.FOCUS);
}

See the JavaDoc for Line.ShowOpenType and Line.ShowVisibilityType to see different options of showing the line (with focus, without focus, opening the editor if not open, etc.).

Example "How to open a document at line and at a specific column"

    int lineNumber=42;
    int colNumber=43;

    //misses checks for NPEs
    FileObject fo = ...;
    LineCookie lc = DataObject.find(fo).getLookup().lookup(LineCookie.class);
    Line line = lc.getLineSet().getOriginal(lineNumber);
    line.show(Line.ShowOpenType.OPEN, Line.ShowVisibilityType.FRONT, colNumber);
Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo