Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy and paste MessageDialog message

I am creating a MessageDialog with some information.

MessageDialog.openInformation(getShell(), "Success", "Change "+getNumber()+" has been created.");

I am wanting to be able to copy the number in the dialog so I can paste it somewhere else. Is there a way to set the MessageDialog so I can accomplish this?

The API can be found here. I have not found anything in the API that really helps me out.

like image 215
Chris Bolton Avatar asked Nov 20 '25 22:11

Chris Bolton


2 Answers

No, MessageDialog uses a Label to display the message. In order to allow C&P, you'd need a Text widget instead. So you have to create your own subclass of org.eclipse.jface.dialogs.Dialog.

You may look at the source code of InputDialog as an example. In order to make the text widget read-only, create it with the SWT.READ_ONLY style flag.

like image 71
ralfstx Avatar answered Nov 22 '25 11:11

ralfstx


You can create a class derived from MessageDialog and override the createMessageArea method with something like:

public class MessageDialogWithCopy extends MessageDialog
{
  public MessageDialogWithCopy(Shell parentShell, String dialogTitle, Image dialogTitleImage,
                           String dialogMessage, int dialogImageType, String [] dialogButtonLabels, int defaultIndex)
  {
    super(parentShell, dialogTitle, dialogTitleImage, dialogMessage, dialogImageType,
         dialogButtonLabels, defaultIndex);
  }

  @Override
  protected Control createMessageArea(final Composite composite)
  {
    Image image = getImage();
    if (image != null)
     {
       imageLabel = new Label(composite, SWT.NULL);
       image.setBackground(imageLabel.getBackground());
       imageLabel.setImage(image);

       imageLabel.setLayoutData(new GridData(SWT.CENTER, SWT.BEGINNING, false, false));
     }

    // Use Text control for message to allow copy

    if (message != null)
     {
       Text msg = new Text(composite, SWT.READ_ONLY | SWT.MULTI);

       msg.setText(message);

       GridData data = new GridData(SWT.FILL, SWT.TOP, true, false);
       data.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH);

       msg.setLayoutData(data);
     }

     return composite;
   }


   public static void openInformation(Shell parent, String title,  String message)
   {
     MessageDialogWithCopy dialog
        = new MessageDialogWithCopy(parent, title, null,  message, INFORMATION,
                                    new String[] {IDialogConstants.OK_LABEL}, 0);

     dialog.open();
   }
}
like image 27
greg-449 Avatar answered Nov 22 '25 12:11

greg-449



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!