Add link to MessageDialog message

旧城冷巷雨未停 提交于 2019-12-23 21:07:19

问题


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?


回答1:


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:



来源:https://stackoverflow.com/questions/29374160/add-link-to-messagedialog-message

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!