Search code examples
performancepluginseclipse-plugineclipse-rcpm2eclipse

Eclipse Plugin Development---PopupMenuCreation


I am developing a wizard using eclipse plugin development.

Requirement:

I have to create a context menu that needs to get populated as soon as the user right clicks on the source folder in java project. once the User performs the first step my handler needs to get the selected src folder in my wizard. My wizard contains a treeviewer where i need to get the selected src folder packaged.

My analysis:

i have my handler class that gets the selected packages

SampleHandler.java

public Object execute(ExecutionEvent event) throws ExecutionException {

    shell = HandlerUtil.getActiveShell(event);
    // Initializing workbench window object
    IWorkbenchWindow window = (IWorkbenchWindow) HandlerUtil.getActiveWorkbenchWindow(event);

    ISelection sel = HandlerUtil.getActiveMenuSelection(event);
    final IStructuredSelection selection = (IStructuredSelection) sel;

    Object firstElement = selection.getFirstElement();

    if (firstElement instanceof IPackageFragment) {
        // Get the selected fragment
        IPackageFragment packageFragment = (IPackageFragment) firstElement;
        modelPackage = packageFragment.getElementName();
        boolean a =!ProjectResourceHelper.isEntityBasePackage(modelPackage);
        if(a == true){

            MessageDialog.openInformation(shell, "Warning", "Please click from entity base package");
            Shell shell = HandlerUtil.getActiveShell(event);      
            GreenWizard wizard = new GreenWizard();
            WizardDialog dialog = new WizardDialog( part.getSite().getShell(), wizard);
            dialog.create();


            dialog.open();      
            return null;


        }

        try{            
            window.run(true, true, new IRunnableWithProgress(){                 
                @Override
                public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                    monitor.beginTask("Layer codes are being generated...", 1);

                    // Invocation of generate layers method


                    monitor.worked(1);

                    // Done with operation completion. 
                    monitor.done();
                }

            });
        }
        catch(InvocationTargetException ite){
            MessageDialog.openError(shell, "Greenfield Code Generation Exception", ite.getMessage());
        }
        catch (InterruptedException ie) {
            MessageDialog.openError(shell, "Greenfield Code Generation Exception", ie.getMessage());
        }


    }

I have my main wizard class that is called within this method.

GreenWizard wizard = new GreenWizard();

and my main wizard in return calls my wizard page where i need to get the selection performed on right click by the user.

My Wizardpageclass

public GenerateGreenfieldLayer(IWorkbench workbench,
        IStructuredSelection selection) {
    super("Greenfield");
    setImageDescriptor(ResourceManager
            .getImageDescriptor("\\icons\\greenfield-new-wiz.png"));
    setTitle("GreenField Generate layer");
    setDescription("Select specfic class to grenerate Layers"); 
}

/**
 * Create contents of the wizard.
 * 
 * @param parent
 */
@Override
public void createControl(Composite parent) {

    Composite container = new Composite(parent, SWT.NULL);

    setControl(container);
    container.setLayout(new GridLayout(2, false));

    final CheckboxTreeViewer treeViewer = new CheckboxTreeViewer(container,
            SWT.BORDER);
    tree = treeViewer.getTree();
    tree.setToolTipText("Choose package");
    GridData gd_tree = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
    gd_tree.widthHint = 280;
    gd_tree.heightHint = 140;
    tree.setLayoutData(gd_tree);
    treeViewer.setContentProvider(new GreenfieldTreeContentProvider());
    treeViewer.setLabelProvider(new WorkbenchLabelProvider());
    treeViewer.addSelectionChangedListener(new ISelectionChangedListener () { 
        public void selectionChanged(SelectionChangedEvent event) {

        } 
        }); 


    }

Can anyone please guide me how to get the selection from object method and pass as treeviewer initial input in my wizard page.

Please correct me if i am missing any steps as i am very new to this.

Thanks in advance


Solution

  • You should separate the code into the following pieces and dataflows:

    • Handler: get the selection and create the wizard and wizard dialog (as you do already)
    • Handler->Wizard: use the Wizard's constructor or a custom init(foo) method (which you call from the handler) to set the selected object (or whatever you want to pass as initial data) from the handler
    • Wizard->WizardPage: When creating the Wizard, instantiate the WizardPage(s) and pass the selection to the wizard pages. (If you need a more complex model which is shared between the Wizard and its pages consider creating an instantiating a simple value-holder class as your wizard model; i.e., a simple java class with your data and getters/setters. That object can then be shared across the pages if you pass it to every page's constructor)
    • WizardPage: create UI for wizard page, let user modify the model
    • WizardPage->Wizard: if you do not use the shared wizard model via a value-holder class, have a getXxx() method to let the wizard access the user's input from the page
    • Wizard: implement Wizard.performFinish() to do the work at the end of the wizard using getContainer().run() instead of having the window.run() call in your handler.