如何从Eclipse RCP中的现有Perspective获取新窗口

时间:2015-02-19 14:25:35

标签: java swt eclipse-rcp

我目前是Eclipse RCP / SWT和Jface的新手。我试图通过使用按钮从我现有的视图创建一个新的弹出窗口。它类似于MessageBox,但MessageBox具有有限的功能,我需要在我的第二个" shell" / Window上使用SWT.graphics。所以,一旦我得到第二个窗口,我就应该得到一个数字。

现在有两个问题:

  1. 我不知道如何从当前视角点击创建新窗口。我提到这个here,但不满意。

  2. 我不知道如何整合我的两个文件DrawExamples.javaView.java,以便在View中打开一个窗口。 Java和它需要DrawExample.java的抽象代码。

  3. 到目前为止我的进展:

     View.java()
    
    
         {
            public void createPartControl(final Composite parent) {
                viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL
                        | SWT.V_SCROLL);
                viewer.setContentProvider(new ViewContentProvider());
                viewer.setLabelProvider(new ViewLabelProvider());
                // Provide the input to the ContentProvider
                viewer.setInput(new String[] {"One", "Two", "Three"});
                final FormLayout layout = new FormLayout();
                layout.marginHeight = 5;
                layout.marginWidth = 5;
    
                //set layout for parent
                parent.setLayout(layout);
    
                //create a button or any other widget
                Button button2 = new Button(parent, SWT.PUSH);
                button2.setText("B2");
                button2.addSelectionListener(new SelectionAdapter() {
                    @Override
    
                    public void widgetSelected(SelectionEvent e)
         {// I want to generate new Window here. Not a new View like the one i have created above. From the Object of DrawExample Class So that i can keep things modular. Drawing in Once class and windowing in one class.` 
    
            }); 
            //create FormData and set each of its sides
            FormData formData = new FormData();
            formData.top = new FormAttachment(0, 0);
            formData.bottom = new FormAttachment(50, 0);
            formData.left = new FormAttachment(10, 0);
            formData.right = new FormAttachment(60, 0);
    
            //set FormDate for button
            button2.setLayoutData(formData);
        }
        }
    

    DrawExample Class。

        import org.eclipse.swt.SWT;
        import org.eclipse.swt.graphics.Font;
        import org.eclipse.swt.graphics.GC;
        import org.eclipse.swt.widgets.Canvas;
        import org.eclipse.swt.widgets.Display;
        import org.eclipse.swt.widgets.Shell;
        public class DrawExample {
        public static void main(String[] args){
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("Drawing Example");
    
            Canvas canvas = new Canvas(shell, SWT.NONE);
            canvas.setSize(150, 150);
            canvas.setLocation(20, 20);
            shell.open();
            shell.setSize(200, 220);
    
            GC gc = new GC(canvas);
            gc.drawRoundRectangle(11, 11, 89, 44, 25, 15);
            gc.setBackground(display.getSystemColor(SWT.COLOR_CYAN)); 
            gc.fillRoundRectangle(10,10,90,45,25,15);
            Font font = new Font(display,"Arial",12,SWT.BOLD | SWT.ITALIC); 
            //gc.drawText("Hello World",5,5); 
            gc.setFont(font); 
            gc.setForeground(display.getSystemColor(SWT.COLOR_WHITE)); 
            gc.drawText("Hello World",10,20,true); 
    
            gc.dispose(); 
        font.dispose();
        //gc.drawOval(65, 10, 30, 35);
        //gc.drawLine(130, 10, 90, 80);
        //gc.drawPolygon(new int[] { 20, 70, 45, 90, 70, 70 });
        //gc.drawPolyline(new int[] { 10, 120, 70, 100, 100, 130, 130, 75 });
        gc.dispose();
    
        while (!shell.isDisposed()) {
          if (!display.readAndDispatch())
            display.sleep();
        }
        display.dispose();
      }
    }
    

1 个答案:

答案 0 :(得分:1)

如果要在视图中生成通用shell,可以始终使用

生成一个
Shell=new Shell(getViewSite().getPage().getWorkbenchWindow().getShell());

但我不认为这是推荐的方式。