2012-03-16 131 views
1

我正在使用WindowBuilder Pro構建一個簡單的SWT Eclipse插件。當用戶點擊一個工具欄項時,我希望有一個彈出菜單顯示下面創建的對話框類,該對話框類以ViewPart爲中心。有誰知道如何做到這一點?它在Swing中非常直觀...Eclipse RCP SWT問題彈出窗口

public class MyApp extends ViewPart { 

    public void createPartControl(final Composite arg0) { 
     arg0.setLayout(new GridLayout(1, false)); 

     final ToolBar toolBar = new ToolBar(arg0, SWT.FLAT | SWT.RIGHT); 
     toolBar.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false, 1, 1)); 

     final ToolItem connectItem = new ToolItem(toolBar, SWT.NONE); 
     connectItem.addSelectionListener(new SelectionAdapter() { 
      @Override 
      public void widgetSelected(final SelectionEvent e) { 
       System.out.println("connect button clicked!"); 
       final Shell newShell = new Shell(); 
       newShell.setText("Connect to box"); 
       newShell.setLayout(new GridLayout(2, false)); 
       newShell.setSize(400, 400); 
       newShell.pack(); 
       newShell.open(); 
       final ConnectSUVDialog dialog = new ConnectBoxDialog(newShell); 
      } 
     }); 
     . 
     . 
     . 
    } 
} 


public class ConnectSUVDialog 
    extends Dialog { 
    private Text txtHostName; 
    private Text txtUserName; 
    private Text txtPassword; 

    public ConnectSUVDialog(final Shell parentShell) { 
     super(parentShell); 
    } 

    @Override 
    protected Control createDialogArea(final Composite parent) { 
     final Composite container = (Composite) super.createDialogArea(parent); 
     container.setLayout(new GridLayout(2, false)); 


     this.txtHostName = new Text(container, SWT.BORDER); 
     this.txtHostName.setText("host name"); 
     this.txtHostName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1)); 

     this.txtUserName = new Text(container, SWT.BORDER); 
     this.txtUserName.setText("user name"); 
     this.txtUserName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1)); 

     this.txtPassword = new Text(container, SWT.BORDER); 
     this.txtPassword.setText("password"); 
     this.txtPassword.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1)); 

     return container; 
    } 

    /** 
    * Create contents of the button bar. 
    * @param parent 
    */ 
    @Override 
    protected void createButtonsForButtonBar(final Composite parent) { 
     createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true); 
     createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false); 
    } 

    /** 
    * Return the initial size of the dialog. 
    */ 
    @Override 
    protected Point getInitialSize() { 
     return new Point(450, 300); 
    } 

} 

回答

2

您不需要創建一個新的shell來打開一個對話框。

ConnectSUVDialog dialog = new ConnectSUVDialog(arg0.getShell()); 
dialog.open();