views:

641

answers:

2

I'm developing a an eclipse plugin that uses an SWT interface. I need to display text, and within that text there needs to be links. The only two widgets that I've found that will allow me to include clickable links in text are Link and Browser. Browser, however, is overkill for my needs, and I couldn't properly customize the look of it. This only leaves the Link widget.

The problem is I need the Link widget to inherit a gradient from the Composite in which it is in. It does this correctly, only when it is resized or scrolled the Link component flickers. The Link is the only component in which I have seen this effect.

In an attempt to fix this I've tried manipulating other components into having clickable links, but I haven't found a good solution yet.

Is there anyway to fix the flickering effect on the Link, or is there a different component which would support links?

Thanks,

Brian

+1  A: 

After spending the day working on this, I came up with a workaround. I created a Composite for the text area. For each word that isn't part of a url,got its own label. For links, each letter got its own label. Then the labels for the url characters got a listener to launch a browser. Using this method provided the Link functionality, handled resizing properly, and has no flicker.

Brian Gianforcaro
A: 

Have you tried passing SWT.NO_BACKGROUND to your Link widget? It might get a little strange... and you may have to do a little more work to get the gui drawing properly, but that would be my first guess.

Other than that, here's my Quick n' dirty implementation of a link inside of a StyledText. You will need to fill in for changing the cursor (if that's something you want), as well as coming up with a good "text to link" mapping scheme.

The only thing is I'm not sure if StyledText will inherit your background... give it a shot.

public class StyledTextExample {
   public static void main(String [] args) {
 // create the widget's shell
 Shell shell = new Shell();
 shell.setLayout(new FillLayout());
 shell.setSize(200, 100);
 Display display = shell.getDisplay();
 // create the styled text widget
 final StyledText widget = new StyledText(shell, SWT.NONE);

 String text = "This is the StyledText widget.";
 widget.setText(text);
 widget.setEditable(false);

 final StyleRange hyperlinkStyle = new StyleRange();
 String linkWord = "StyledText";
 hyperlinkStyle.start = text.indexOf(linkWord);
 hyperlinkStyle.length = linkWord.length();
 hyperlinkStyle.fontStyle = SWT.BOLD;
 hyperlinkStyle.foreground = display.getSystemColor(SWT.COLOR_BLUE);
 widget.setStyleRange(hyperlinkStyle);


 widget.addMouseListener(new MouseAdapter() {
  public void mouseUp(MouseEvent arg0) {
   Point clickPoint = new Point(arg0.x, arg0.y);
   try {
    int offset = widget.getOffsetAtLocation(clickPoint);
    if (widget.getStyleRangeAtOffset(offset) != null) {
     System.out.println("link");
    }
   } catch (IllegalArgumentException e) {
    //ignore, clicked out of text range.
   }
  }});
 shell.open();
 while (!shell.isDisposed())
 if (!display.readAndDispatch()) display.sleep();
   }
}
James Van Huis