Open File from Balloon

I am creating a balloon at some point in my code & I want to put in a clickable link to a file. I currently have the file as a VirtualFile object:

VirtualFile file = _profilerManager.getCurentFile();

and I am creating & showing the balloon this way:

Balloon balloon = UiUtil.createWidthLimitedTextBalloonBuilder(ProfilerBundle.message("action.gw.profiler.AutoAnalysis.Complete", _file.getName()), MessageType.INFO, 600)
.setCloseButtonEnabled(true)
.setDisposable(_profilerManager.getProject())
.setHideOnAction(true)
.setHideOnClickOutside(true)
.setHideOnKeyOutside(true)
.createBalloon();

balloon.show(new RelativePoint(selectedCompoenent, new Point(50, selectedCompoenent.getY()

The current solution results in the file name being shown in the balloon, which is not bad, but it would be even nicer if it was clickable- and IJ navigated to that file. I am not too fussy about the balloon's appearance.

0
2 comments
Avatar
Permanently deleted user

Try using JBPopupFactory.createHTMLTextBaloonBuilder(..)

1
Avatar
Permanently deleted user

Thank you Kirill. Eventually I got back around to this. Here is the solution:

Balloon balloon = JBPopupFactory.getInstance().createHtmlTextBalloonBuilder(ProfilerBundle.message("action.gw.profiler.AutoAnalysis.Complete", linkIfy(_file.getName())), Icons.AUTO_ANALYSIS, Color.CYAN, new HyperlinkListener() {
@Override
public void hyperlinkUpdate(HyperlinkEvent e) {
if(! (e.getEventType() == ACTIVATED)){return;}
FileEditorManager.getInstance(_profilerManager.getProject()).openFile(_file, true);
}
}).setCloseButtonEnabled(true)
.setDisposable(_profilerManager.getProject())
.setHideOnAction(true)
.setHideOnClickOutside(true)
.setHideOnLinkClick(true)
.setHideOnKeyOutside(true)
.createBalloon();

balloon.show(new RelativePoint(selectedCompoenent, new Point(50, selectedCompoenent.getY() * 3 / 2)), Balloon.Position.above);

I'm using these auxiliary functions to turn the string into a link:

  //WARNING
//This method requires that s is a plain string that requires
//no further escaping
private static String linkIfy(String s) {
final String A_HREF = "<a href=\"";
final String HREF_CLOSED = "\">";
final String HREF_END = "</a>";
return A_HREF.concat(s).concat(HREF_CLOSED).concat(s).concat(HREF_END);
}

References:

0

Please sign in to leave a comment.