Tuesday, February 28, 2012

Listening to part activation:  views and editor extends  viewPart and EditorPart

senario 1:  show  view1 and view 2 on selection of view3
senario 2:  hide the view1 and view 2 on selection of  view3
senario 3:  on selection of editor bring the view2

To listen any par, first need to implement "org.eclipse.ui.IPartListener" of  "org.eclipse.ui.IPartListener2"

step 1: Register the listener to workbench
         getSite().getWorkbenchWindow().getPartService().addPartListener(_partListenerList);

step 2: get the  page - As view and editor is inside the WorkbenchPage
        in view  -:  getViewSite().getPage();
        in Editor - : getEditorSite().getPage();
         using platformUI : -   
          PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()           


step3:  Once you have access to the page,  get the available views reference stored by workbench.
               
        // show all the views using reference
if (iViewReference.getId().equals("<view ID>")) {
try {
part.getSite().getWorkbenchWindow().getActivePage()
.showView( iViewReference.getId() );

} catch (PartInitException e) {
e.printStackTrace();
}
}



example: 



public class viewExample extends viewPart {


public static final String ID = "com.khalid.view.activation.onselection";


private IPartListener _partListenerList = new IPartListener() {


@Override
public void partOpened(IWorkbenchPart part) {
System.out.println("partOpened");
}


@Override
public void partDeactivated(IWorkbenchPart part) {
System.out.println("partDeactivated");
}


@Override
public void partClosed(IWorkbenchPart part) {
System.out.println("partClosed");
}


@Override
public void partBroughtToTop(IWorkbenchPart part) {
System.out.println("partBroughtToTop");
}


@Override
public void partActivated(final IWorkbenchPart part) {


if (part.getSite().getPage().getPerspective() == null) {
return;
}


if (!part.getSite().getPage().getPerspective().getId().equals(
"<Perspective ID>")) {
return;
}
// System.out.println(getViewSite().getPage().getActivePart().getSite().getId());


if (getViewSite().getPage().getActivePart().getSite().getId()
.equals("<view ID-1>")) {
Display.getCurrent().asyncExec(new Runnable() {


@Override
public void run() {
try {
part.getSite().getWorkbenchWindow().getActivePage()
.showView("<view ID-2>");
} catch (PartInitException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}
});
}
}
};


public  viewExample() {

}


@Override
public void createControls(Composite theParent) {
theParent.setLayout(new FillLayout(SWT.HORIZONTAL));
// Create UI control
                  ......


                //Register the part listener
getSite().getWorkbenchWindow().getPartService().addPartListener(
_partListenerList);


}


/**
* Passing the focus request to the viewer's control.
*/
@Override
public void setFocus() {


}


@Override
public void dispose() {
super.dispose();
getSite().getWorkbenchWindow().getPartService().removePartListener(
_partListenerList);
}
}



@NOTE: i will adding the video for this in future.


No comments:

Post a Comment