下面列出了怎么用org.eclipse.ui.console.IHyperlink的API类实例代码及写法,或者点击链接到github查看源代码。
private static IHyperlink makeHyperlink(IFile file, int lineNumber, int lineOffset) {
return new IHyperlink() {
@Override
public void linkExited() {
// ignore
}
@Override
public void linkEntered() {
// ignore
}
@Override
public void linkActivated() {
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
try {
IEditorPart editorPart = IDE.openEditor(page, file);
jumpToPosition(editorPart, lineNumber, lineOffset);
} catch (PartInitException e) {
// nothing to do here
}
}
};
}
@SuppressWarnings("restriction")
private void processIOConsole(IOConsole ioConsole) {
IDocument document = ioConsole.getDocument();
try {
Position[] positions = document.getPositions(ConsoleHyperlinkPosition.HYPER_LINK_CATEGORY);
Arrays.sort(positions, new Comparator<Position>() {
@Override
public int compare(Position o1, Position o2) {
return Integer.compare(o1.getOffset(), o2.getOffset());
}
});
if (positions.length > 0) {
Position p = positions[positions.length - 1];
if (p instanceof ConsoleHyperlinkPosition) {
ConsoleHyperlinkPosition consoleHyperlinkPosition = (ConsoleHyperlinkPosition) p;
IHyperlink hyperLink = consoleHyperlinkPosition.getHyperLink();
hyperLink.linkActivated();
}
}
} catch (BadPositionCategoryException e) {
Log.log(e);
}
}
@Override
public void init(final IConsole console) {
IProcess process = console.getProcess();
if (process != null) {
ILaunch launch = process.getLaunch();
if (launch != null) {
initLaunchConfiguration(launch.getLaunchConfiguration());
}
}
this.linkContainer = new ILinkContainer() {
@Override
public void addLink(IHyperlink link, int offset, int length) {
if (length <= 0) {
// Log.log("Trying to create link with invalid len: " + length);
return;
}
console.addLink(link, offset, length);
}
@Override
public String getContents(int offset, int length) throws BadLocationException {
return console.getDocument().get(offset, length);
}
};
}
@Override
public void addLink(IHyperlink link, int offset, int length) {
if (testOutputText == null) {
return;
}
StyleRangeWithCustomData range = new StyleRangeWithCustomData();
range.underline = true;
try {
range.underlineStyle = SWT.UNDERLINE_LINK;
} catch (Throwable e) {
//Ignore (not available on earlier versions of eclipse)
}
//Set the proper color if it's available.
TextAttribute textAttribute = ColorManager.getDefault().getHyperlinkTextAttribute();
if (textAttribute != null) {
range.foreground = textAttribute.getForeground();
} else {
range.foreground = JFaceColors.getHyperlinkText(Display.getDefault());
}
range.start = offset;
range.length = length + 1;
range.customData = link;
testOutputText.setStyleRange(range);
}
@Override
public void mouseUp(MouseEvent e) {
Widget w = e.widget;
if (w instanceof StyledText) {
StyledText styledText = (StyledText) w;
int offset = styledText.getCaretOffset();
if (offset >= 0 && offset < styledText.getCharCount()) {
StyleRange styleRangeAtOffset = styledText.getStyleRangeAtOffset(offset);
if (styleRangeAtOffset instanceof StyleRangeWithCustomData) {
StyleRangeWithCustomData styleRangeWithCustomData = (StyleRangeWithCustomData) styleRangeAtOffset;
Object l = styleRangeWithCustomData.customData;
if (l instanceof IHyperlink) {
((IHyperlink) l).linkActivated();
}
}
}
}
}
@Override
public void matchFound(PatternMatchEvent event) {
try {
int offset = event.getOffset();
int length = event.getLength();
IProcess process = (IProcess) console.getAttribute(IDebugUIConstants.ATTR_CONSOLE_PROCESS);
if (process != null) {
ILaunch launch = process.getLaunch();
String projectAttribute = RustLaunchDelegateTools.PROJECT_ATTRIBUTE;
String launchConfigurationType = launch.getLaunchConfiguration().getType().getIdentifier();
if (launchConfigurationType.equals(RustLaunchDelegateTools.CORROSION_DEBUG_LAUNCH_CONFIG_TYPE)) {
// support debug launch configs
projectAttribute = ICDTLaunchConfigurationConstants.ATTR_PROJECT_NAME;
}
String projectName = launch.getLaunchConfiguration().getAttribute(projectAttribute, ""); //$NON-NLS-1$
if (projectName.trim().isEmpty()) {
return; // can't determine project so prevent error down
}
IWorkspaceRoot myWorkspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
IProject myProject = myWorkspaceRoot.getProject(projectName);
String errorString = console.getDocument().get(event.getOffset(), event.getLength());
String[] coordinates = errorString.split(":"); //$NON-NLS-1$
IHyperlink link = makeHyperlink(myProject.getFile(coordinates[0]), Integer.parseInt(coordinates[1]),
Integer.parseInt(coordinates[2]));
console.addHyperlink(link, offset, length);
}
} catch (BadLocationException | CoreException e) {
// ignore
}
}
@Override
public void matchFound(PatternMatchEvent event) {
if (event.getSource() instanceof TextConsole) {
try {
final TextConsole console = (TextConsole) event.getSource();
final int start = event.getOffset();
final int length = event.getLength();
IHyperlink link = new BrowserSupportBasedHyperlink(console.getDocument().get(start, length));
console.addHyperlink(link, start, length);
} catch (BadLocationException e) {
logger.log(Level.SEVERE, "Cannot create hyperlink", e);
}
}
}
public void handleEvent(Event event) {
IHyperlink hyperlink = getHyperlink();
if (hyperlink instanceof IHyperlink2) {
if (event.button == 1) {
((IHyperlink2) hyperlink).linkActivated(event);
}
}
}
/**
* IConsole: Add a link to the console
*/
public void addLink(IHyperlink link, int offset, int length) {
try {
super.addHyperlink(link, offset, length);
} catch (BadLocationException e) {
Log.log(e);
}
}
@Override
protected void onHandleClick(Object data) {
if (data instanceof IHyperlink) {
//The order is important (when activating it'll do a hide automatically,
//but we want to do a hide without focusing the previous editor).
this.hideInformationControl(false, false);
((IHyperlink) data).linkActivated();
}
}
/**
* IConsole: Add a link to the console
*/
public void addLink(IConsoleHyperlink link, int offset, int length) {
this.addLink((IHyperlink) link, offset, length);
}
public IHyperlink getHyperlink() {
return viewer.getHyperlink();
}
public IHyperlink getHyperlink(int offset) {
return viewer.getHyperlink(offset);
}
@Override
public void addLink(IHyperlink link, int offset, int length) {
linkRegions.add(new Region(offset, length));
}
void addLink(IHyperlink link, int offset, int length);