psuzzi

E4 SWTBot : Switch perspective and Select part

Mar 4th, 2016
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.70 KB | None | 0 0
  1. package my.e4test.swtbot;
  2.  
  3. import java.util.List;
  4.  
  5. import org.eclipse.e4.core.contexts.IEclipseContext;
  6. import org.eclipse.e4.ui.internal.workbench.E4Workbench;
  7. import org.eclipse.e4.ui.model.application.MApplication;
  8. import org.eclipse.e4.ui.model.application.ui.advanced.MPerspective;
  9. import org.eclipse.e4.ui.model.application.ui.basic.MPart;
  10. import org.eclipse.e4.ui.model.application.ui.basic.MWindow;
  11. import org.eclipse.e4.ui.workbench.modeling.EModelService;
  12. import org.eclipse.e4.ui.workbench.modeling.EPartService;
  13. import org.eclipse.swt.widgets.Display;
  14. import org.eclipse.swtbot.swt.finder.SWTBot;
  15.  
  16. public class E4Bot extends SWTBot {
  17.  
  18.     private IEclipseContext serviceContext;
  19.  
  20.     @SuppressWarnings("restriction")
  21.     public E4Bot() {
  22.         serviceContext = E4Workbench.getServiceContext();
  23.     }
  24.  
  25.     /** used to switch perspective */
  26.     public void switchPerspective(final String perspectiveId) {
  27.         Display.getDefault().asyncExec(new Runnable() {
  28.  
  29.             @Override
  30.             public void run() {
  31.  
  32.                 @SuppressWarnings("restriction")
  33.                 IEclipseContext serviceContext =
  34.                         E4Workbench.getServiceContext();
  35.  
  36.                 IEclipseContext context = serviceContext.getActiveLeaf();
  37.  
  38.                 // model and app needed
  39.                 EModelService modelService = context.get(EModelService.class);
  40.                 MApplication application = context.get(MApplication.class);
  41.                 // service to switch
  42.                 EPartService partService = context.get(EPartService.class);
  43.  
  44.                 // get the element
  45.                 MPerspective element =
  46.                         (MPerspective) modelService.find(perspectiveId,
  47.                                 application);
  48.  
  49.                 // Now switch perspective
  50.                 partService.switchPerspective(element);
  51.             }
  52.         });
  53.  
  54.     }
  55.  
  56.     /** used to show view */
  57.     public void showPart(final String partId) {
  58.         Display.getDefault().asyncExec(new Runnable() {
  59.  
  60.             @Override
  61.             public void run() {
  62.  
  63.                 @SuppressWarnings("restriction")
  64.                 IEclipseContext serviceContext =
  65.                         E4Workbench.getServiceContext();
  66.  
  67.                 IEclipseContext context = serviceContext.getActiveLeaf();
  68.  
  69.                 // model and app needed
  70.                 EModelService modelService = context.get(EModelService.class);
  71.                 MApplication app = context.get(MApplication.class);
  72.                 MWindow window = (MWindow) app.getChildren().get(0);
  73.                 // service to switch
  74.                 EPartService partService = context.get(EPartService.class);
  75.  
  76.                 // get the matching parts
  77.                 List<MPart> parts =
  78.                         modelService.findElements(window, partId, MPart.class,
  79.                                 null, EModelService.OUTSIDE_PERSPECTIVE
  80.                                         | EModelService.IN_ANY_PERSPECTIVE
  81.                                         | EModelService.IN_SHARED_AREA);
  82.  
  83.                 // activate and bring to top
  84.                 for (MPart part : parts) {
  85.                     partService.activate(part, true);
  86.                     partService.bringToTop(part);
  87.                 }
  88.  
  89.             }
  90.         });
  91.  
  92.     }
  93.  
  94. }
Add Comment
Please, Sign In to add comment