Saras Arya
Saras Arya

Reputation: 3112

How to get new window from existing Perspective in Eclipse RCP

I am currently new to Eclipse RCP/SWT and Jface. I am trying to create a new pop up window from my existing view by using buttons. It is something like MessageBox but MessageBox have limited functionality I need to use SWT.graphics on my second "shell"/Window. So as soon as I get my second window I should get a figure.

Now there are two problems:

  1. I don't know how to create a new window from a click in current perspective. I referred to this here but with dissatisfaction.

  2. I don't know how to integrate my two files DrawExamples.java and View.java so that I open a window in View. Java and it takes drawcodes from DrawExample.java.

My progress so far:

 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);
    }
    }

The 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();
  }
}

Upvotes: 0

Views: 795

Answers (1)

Ralf M Petter
Ralf M Petter

Reputation: 106

If you want to generate a generic shell in a view, you can always generate one with

Shell=new Shell(getViewSite().getPage().getWorkbenchWindow().getShell());

But i do not think that this is the recommended way.

Upvotes: 1

Related Questions