Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SwingMessageWidget: make hyperlinks work #55

Merged
merged 1 commit into from
Dec 8, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 37 additions & 5 deletions src/main/java/org/scijava/ui/swing/widget/SwingMessageWidget.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,17 @@

package org.scijava.ui.swing.widget;

import javax.swing.JLabel;
import java.io.IOException;

import javax.swing.JEditorPane;
import javax.swing.JPanel;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;

import org.scijava.Priority;
import org.scijava.log.LogService;
import org.scijava.platform.PlatformService;
import org.scijava.plugin.Parameter;
import org.scijava.plugin.Plugin;
import org.scijava.widget.InputWidget;
import org.scijava.widget.MessageWidget;
Expand All @@ -47,8 +54,13 @@
public class SwingMessageWidget extends SwingInputWidget<String> implements
MessageWidget<JPanel>
{
@Parameter
private PlatformService platformService;

@Parameter
private LogService logService;

private JLabel label;
private JEditorPane pane;

// -- InputWidget methods --

Expand Down Expand Up @@ -76,8 +88,28 @@ public void set(final WidgetModel model) {

final String text = model.getText();

label = new JLabel(text);
getComponent().add(label);
pane = new JEditorPane("text/html", text);

// NB: use format (font etc.) from parent component
pane.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE);

pane.setEditable(false);
pane.setOpaque(false);
pane.addHyperlinkListener(new HyperlinkListener() {

@Override
public void hyperlinkUpdate(HyperlinkEvent hle) {
if (HyperlinkEvent.EventType.ACTIVATED.equals(hle.getEventType())) {
try {
platformService.open(hle.getURL());
}
catch (IOException exc) {
logService.error("Error while opening " + hle.getURL(), exc);
}
}
}
});
getComponent().add(pane);
}

// -- Typed methods --
Expand All @@ -92,6 +124,6 @@ public boolean supports(final WidgetModel model) {
@Override
public void doRefresh() {
// maybe dialog owner changed message content
label.setText(get().getText());
pane.setText(get().getText());
}
}