Skip to content

Commit 82e79ba

Browse files
committed
SwingMessageWidget: make hyperlinks work
This replaces JLabel by JEditorPane to allow HTML rendering including hyperlinks. We refer to PlatformService to open any clicked URLs. Note that this makes hyperlinks work in 'value', but not in 'label' attributes of message (String) parameters.
1 parent e68111f commit 82e79ba

File tree

1 file changed

+37
-5
lines changed

1 file changed

+37
-5
lines changed

src/main/java/org/scijava/ui/swing/widget/SwingMessageWidget.java

+37-5
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,17 @@
2929

3030
package org.scijava.ui.swing.widget;
3131

32-
import javax.swing.JLabel;
32+
import java.io.IOException;
33+
34+
import javax.swing.JEditorPane;
3335
import javax.swing.JPanel;
36+
import javax.swing.event.HyperlinkEvent;
37+
import javax.swing.event.HyperlinkListener;
3438

3539
import org.scijava.Priority;
40+
import org.scijava.log.LogService;
41+
import org.scijava.platform.PlatformService;
42+
import org.scijava.plugin.Parameter;
3643
import org.scijava.plugin.Plugin;
3744
import org.scijava.widget.InputWidget;
3845
import org.scijava.widget.MessageWidget;
@@ -47,8 +54,13 @@
4754
public class SwingMessageWidget extends SwingInputWidget<String> implements
4855
MessageWidget<JPanel>
4956
{
57+
@Parameter
58+
private PlatformService platformService;
59+
60+
@Parameter
61+
private LogService logService;
5062

51-
private JLabel label;
63+
private JEditorPane pane;
5264

5365
// -- InputWidget methods --
5466

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

7789
final String text = model.getText();
7890

79-
label = new JLabel(text);
80-
getComponent().add(label);
91+
pane = new JEditorPane("text/html", text);
92+
93+
// NB: use format (font etc.) from parent component
94+
pane.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE);
95+
96+
pane.setEditable(false);
97+
pane.setOpaque(false);
98+
pane.addHyperlinkListener(new HyperlinkListener() {
99+
100+
@Override
101+
public void hyperlinkUpdate(HyperlinkEvent hle) {
102+
if (HyperlinkEvent.EventType.ACTIVATED.equals(hle.getEventType())) {
103+
try {
104+
platformService.open(hle.getURL());
105+
}
106+
catch (IOException exc) {
107+
logService.error("Error while opening " + hle.getURL(), exc);
108+
}
109+
}
110+
}
111+
});
112+
getComponent().add(pane);
81113
}
82114

83115
// -- Typed methods --
@@ -92,6 +124,6 @@ public boolean supports(final WidgetModel model) {
92124
@Override
93125
public void doRefresh() {
94126
// maybe dialog owner changed message content
95-
label.setText(get().getText());
127+
pane.setText(get().getText());
96128
}
97129
}

0 commit comments

Comments
 (0)