Search code examples
javaeclipseeclipse-plugineclipse-rcp

Add link to MessageDialog message


I am doing Eclipse plugin development. I am using the class MessageDialog. The API can be found here.

I want to add a link like I did about with "here" in the message of the MessageDialog.

Here is what I am doing:

String errorMessage = "You have received an error. Please visit " + URL_NEEDED_HERE

MessageDialog.openError(getShell(), "Get Existing Data Sources Error", errorMessage);

The URL keeps showing up as just a String. Is it possible for it to show as a link?


Solution

  • As @greg-449 said, the MessageDialogdoes not support links. If you don't mind the hackish approach, you can save some work and override createMessageArea like so:

      @Override
      protected Control createMessageArea( Composite composite ) {
        Image image = getImage();
        if( image != null ) {
          imageLabel = new Label( composite, SWT.NULL );
          image.setBackground( imageLabel.getBackground() );
          imageLabel.setImage( image );
          GridDataFactory.fillDefaults().align( SWT.CENTER, SWT.BEGINNING ).applyTo( imageLabel );
        }
        if( message != null ) {
          Link link = new Link( composite, getMessageLabelStyle() );
          link.setText( "This is a longer nonsense message to show that the link widget wraps text if specified so. Please visit <a>this link</a>." );
          GridDataFactory.fillDefaults()
            .align( SWT.FILL, SWT.BEGINNING )
            .grab( true, false )
            .hint( convertHorizontalDLUsToPixels( IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH ), SWT.DEFAULT )
            .applyTo( link );
        }
        return composite;
      }
    };
    

    The code is copied form the IconAndMessageDialog and just replaces the Label with a Link widget.

    Alternatively you can override createCustomArea like so:

      @Override
      protected Control createCustomArea( Composite parent ) {
        Link link = new Link( parent, SWT.WRAP );
        link.setText( "Please visit <a>this link</a>." );
        return link;
      }
    

    This is the designated way to add custom controls to a MessageDialg but leads to a slightly different layout:

    MessageDialog with link