Lines 43-93
Link Here
|
43 |
*/ |
43 |
*/ |
44 |
package org.netbeans.modules.extexecution.print; |
44 |
package org.netbeans.modules.extexecution.print; |
45 |
|
45 |
|
|
|
46 |
import java.awt.Desktop; |
46 |
import java.io.File; |
47 |
import java.io.File; |
47 |
import java.io.IOException; |
48 |
import java.io.IOException; |
|
|
49 |
import java.util.Collection; |
48 |
import java.util.logging.Level; |
50 |
import java.util.logging.Level; |
49 |
import java.util.logging.Logger; |
51 |
import java.util.logging.Logger; |
50 |
import javax.swing.SwingUtilities; |
|
|
51 |
import javax.swing.text.Document; |
52 |
|
53 |
import org.netbeans.api.extexecution.print.LineConvertors.FileLocator; |
52 |
import org.netbeans.api.extexecution.print.LineConvertors.FileLocator; |
54 |
import org.openide.cookies.EditorCookie; |
53 |
import org.netbeans.spi.extexecution.open.FileOpenHandler; |
55 |
import org.openide.cookies.LineCookie; |
|
|
56 |
import org.openide.cookies.OpenCookie; |
57 |
import org.openide.filesystems.FileObject; |
54 |
import org.openide.filesystems.FileObject; |
58 |
import org.openide.filesystems.FileUtil; |
55 |
import org.openide.filesystems.FileUtil; |
59 |
import org.openide.loaders.DataObject; |
|
|
60 |
import org.openide.text.Line; |
61 |
import org.openide.text.Line.ShowOpenType; |
62 |
import org.openide.text.Line.ShowVisibilityType; |
63 |
import org.openide.windows.OutputEvent; |
56 |
import org.openide.windows.OutputEvent; |
64 |
import org.openide.windows.OutputListener; |
57 |
import org.openide.windows.OutputListener; |
65 |
|
58 |
|
66 |
/** |
59 |
/** |
67 |
* An OutputProcessor takes filename and lineno information |
60 |
* An OutputProcessor takes filename and line information |
68 |
* and produces hyperlinks. Actually resolving filenames |
61 |
* and produces hyperlinks. Actually resolving filenames |
69 |
* into real FileObjects is done lazily via user-supplied |
62 |
* into real FileObjects is done lazily via user-supplied |
70 |
* FileLocators when the links are actually clicked. |
63 |
* FileLocators when the links are actually clicked. |
71 |
* |
64 |
* |
72 |
* @author Tor Norbye, Petr Hejl |
65 |
* @author Tor Norbye, Petr Hejl |
73 |
*/ |
66 |
*/ |
74 |
public class FindFileListener implements OutputListener { |
67 |
public class FileListener implements OutputListener { |
75 |
|
68 |
|
76 |
private static final Logger LOGGER = Logger.getLogger(FindFileListener.class.getName()); |
69 |
private static final Logger LOGGER = Logger.getLogger(FileListener.class.getName()); |
77 |
|
70 |
|
78 |
private final String file; |
71 |
private final String file; |
|
|
72 |
|
79 |
private final int lineno; |
73 |
private final int lineno; |
|
|
74 |
|
80 |
private final FileLocator fileLocator; |
75 |
private final FileLocator fileLocator; |
81 |
|
76 |
|
82 |
public FindFileListener(String file, int lineno, FileLocator fileLocator) { |
77 |
private final FileOpenHandler handler; |
83 |
if (lineno < 0) { |
78 |
|
84 |
lineno = 0; |
79 |
public FileListener(String file, int line, FileLocator fileLocator, |
|
|
80 |
FileOpenHandler handler) { |
81 |
|
82 |
if (line < 0) { |
83 |
line = 0; |
85 |
} |
84 |
} |
86 |
|
85 |
|
87 |
// TODO : columns? |
86 |
// TODO : columns? |
88 |
this.file = file; |
87 |
this.file = file; |
89 |
this.lineno = lineno; |
88 |
this.lineno = line; |
90 |
this.fileLocator = fileLocator; |
89 |
this.fileLocator = fileLocator; |
|
|
90 |
this.handler = handler; |
91 |
} |
91 |
} |
92 |
|
92 |
|
93 |
public void outputLineSelected(OutputEvent ev) { |
93 |
public void outputLineSelected(OutputEvent ev) { |
Lines 99-108
Link Here
|
99 |
FileObject fo = findFile(file); |
99 |
FileObject fo = findFile(file); |
100 |
|
100 |
|
101 |
if (fo != null) { |
101 |
if (fo != null) { |
102 |
open(fo, lineno); |
102 |
handler.open(fo, lineno); |
103 |
} |
103 |
} |
104 |
} |
104 |
} |
105 |
|
105 |
|
|
|
106 |
public void outputLineCleared(OutputEvent ev) { |
107 |
} |
108 |
|
106 |
private FileObject findFile(final String path) { |
109 |
private FileObject findFile(final String path) { |
107 |
if (fileLocator != null) { |
110 |
if (fileLocator != null) { |
108 |
FileObject fo = fileLocator.find(path); |
111 |
FileObject fo = fileLocator.find(path); |
Lines 113-185
Link Here
|
113 |
|
116 |
|
114 |
// Perhaps it's an absolute path of some sort... try to resolve those |
117 |
// Perhaps it's an absolute path of some sort... try to resolve those |
115 |
// Absolute path? Happens for stack traces in libraries and such |
118 |
// Absolute path? Happens for stack traces in libraries and such |
116 |
File file = new File(path); |
119 |
File realFile = new File(path); |
117 |
if (file.isFile()) { |
120 |
if (realFile.isFile()) { |
118 |
return FileUtil.toFileObject(FileUtil.normalizeFile(file)); |
121 |
return FileUtil.toFileObject(FileUtil.normalizeFile(realFile)); |
119 |
} else { |
122 |
} else { |
120 |
LOGGER.warning("Cannot resolve file for \"" + path + "\" path."); |
123 |
LOGGER.warning("Cannot resolve file for \"" + path + "\" path."); |
121 |
return null; |
124 |
return null; |
122 |
} |
125 |
} |
123 |
} |
126 |
} |
124 |
|
|
|
125 |
public void outputLineCleared(OutputEvent ev) { |
126 |
} |
127 |
|
128 |
public static boolean open(final FileObject fo, final int lineno) { |
129 |
if (!SwingUtilities.isEventDispatchThread()) { |
130 |
SwingUtilities.invokeLater(new Runnable() { |
131 |
public void run() { |
132 |
open(fo, lineno); |
133 |
} |
134 |
}); |
135 |
|
136 |
return true; // not exactly accurate, but.... |
137 |
} |
138 |
|
139 |
try { |
140 |
DataObject od = DataObject.find(fo); |
141 |
EditorCookie ec = od.getCookie(EditorCookie.class); |
142 |
LineCookie lc = od.getCookie(LineCookie.class); |
143 |
|
144 |
if ((ec != null) && (lc != null)) { |
145 |
Document doc = ec.openDocument(); |
146 |
|
147 |
if (doc != null) { |
148 |
int line = lineno; |
149 |
|
150 |
if (line < 1) { |
151 |
line = 1; |
152 |
} |
153 |
|
154 |
// XXX .size() call is super-slow for large files, see issue |
155 |
// #126531. So we fallback to catching IOOBE |
156 |
// int nOfLines = lines.getLines().size(); |
157 |
// if (line > nOfLines) { |
158 |
// line = nOfLines; |
159 |
// } |
160 |
try { |
161 |
Line.Set lines = lc.getLineSet(); |
162 |
Line l = lines.getCurrent(line - 1); |
163 |
if (l != null) { |
164 |
l.show(ShowOpenType.OPEN, ShowVisibilityType.FOCUS); |
165 |
return true; |
166 |
} |
167 |
} catch (IndexOutOfBoundsException ioobe) { |
168 |
// OK, since .size() cannot be used, see above |
169 |
} |
170 |
} |
171 |
} |
172 |
|
173 |
OpenCookie oc = od.getCookie(OpenCookie.class); |
174 |
|
175 |
if (oc != null) { |
176 |
oc.open(); |
177 |
return true; |
178 |
} |
179 |
} catch (IOException e) { |
180 |
LOGGER.log(Level.INFO, null, e); |
181 |
} |
182 |
|
183 |
return false; |
184 |
} |
185 |
} |
127 |
} |