ContainerSelectionDialog

ContainerSelectionDialog

我在Eclipse RCP应用程序中使用了ContainerSelectionDialog。我现在想在对话框中添加标签,并在其中添加一些其他内容。

这是我的ContainerSelectionDialog的样子:

// ...
ContainerSelectionDialog dialog = new ContainerSelectionDialog(
        Display.getDefault().getActiveShell(), c, true,
        "Please select target folder");
int open = dialog.open();
if (!(open == org.eclipse.jface.window.Window.OK))
    return null;
Object[] result = dialog.getResult();

IPath path = (IPath) result[0];
targetFolder = ResourcesPlugin.getWorkspace().getRoot()
        .findMember(path);
containerPath = targetFolder.getLocation().toPortableString();
// ...


如何在此对话框中添加标签?

最佳答案

最好的办法是在选择树下添加控件。即使ContainerSelectionDialog中的错误也很难做到这一点。此代码显示了如何执行此操作并解决该错误:

public class MyContainerSelectionDialog extends ContainerSelectionDialog
{
  public MyContainerSelectionDialog(final Shell parentShell, final IContainer initialRoot, final boolean allowNewContainerName, final String message)
  {
    super(parentShell, initialRoot, allowNewContainerName, message);
  }

  @Override
  protected Control createDialogArea(final Composite parent)
  {
    Composite body = (Composite)super.createDialogArea(parent);

    // Bug in ContainerSelectionDialog is returning null for the body!

    if (body == null)
     {
       // body is the last control added to the parent

       final Control [] children = parent.getChildren();

       if (children[children.length - 1] instanceof Composite)
         body = (Composite)children[children.length - 1];
     }

    // TODO add your controls here, this example just adds a Label

    final Label label = new Label(body, SWT.NONE);
    label.setText("My label");

    return body;
  }
}

09-10 12:56