• Eclipse 4 RCP窗口居中-使用LifeCycleHandler

    • 2020-01-03 15:27
    • 字数 1,984
    • 阅读 720

    最近几年随着互联网技术的发展,特别是出现React等前端组件化技术后,网页开发越来越方便,使用Eclipse RCP技术的开发人员也越来越少了。原本觉得Eclipse RCP (Rich Client Platform) - Tutorial已经有较详细的文档说明了,更把以前的Eclipse RCP抛掉,没想到这个网站把以前很多高级技术部分的文档改为收费了。最近想学习一下Eclipse 4 RCP,遇到窗口居中的问题,值得记录下来。

    1、在plugin.xml文件的product扩展点增加lifeCycleURI内容如下:
    
    <xml version="1.0" encoding="UTF-8">
    <eclipse version="3.4">
    <plugin>
       <extension
             id="product"
             point="org.eclipse.core.runtime.products">
          <product
                application="org.eclipse.e4.ui.workbench.swt.E4Application"
                name="tech.scut.e4.rcp">
             <property
                   name="lifeCycleURI"
                   value="bundleclass://tech.scut.e4.rcp/tech.scut.e4.rcp.E4LifeCycle">
             </property>
             <property
                   name="applicationCSS"
                   value="platform:/plugin/tech.scut.e4.rcp/css/default.css">
             </property>
             <property
                   name="appName"
                   value="tech.scut.e4.rcp">
             </property>
          </product>
       </extension>
    </plugin>
    
    2、E4LifeCycle类内容如下,其中MWINDOW_ID为Application.e4xmi里的Trimmed Window的ID:
    
    package tech.scut.e4.rcp;
     
    import org.eclipse.e4.core.contexts.IEclipseContext;
    import org.eclipse.e4.ui.model.application.MApplication;
    import org.eclipse.e4.ui.model.application.ui.basic.MTrimmedWindow;
    import org.eclipse.e4.ui.model.application.ui.basic.MWindow;
    import org.eclipse.e4.ui.workbench.lifecycle.PostContextCreate;
    import org.eclipse.e4.ui.workbench.lifecycle.PreSave;
    import org.eclipse.e4.ui.workbench.lifecycle.ProcessAdditions;
    import org.eclipse.e4.ui.workbench.lifecycle.ProcessRemovals;
    import org.eclipse.e4.ui.workbench.modeling.EModelService;
    import org.eclipse.swt.graphics.Rectangle;
    import org.eclipse.swt.widgets.Display;
    import org.eclipse.swt.widgets.Monitor;
     
    /**
     * This is a stub implementation containing e4 LifeCycle annotated
     * methods.
    * There is a corresponding entry in plugin.xml (under the * org.eclipse.core.runtime.products' extension point) that references * this class. **/ public class E4LifeCycle { public static final String MWINDOW_ID="tech.scut.e4.rcp.window.main"; @PostContextCreate void postContextCreate(IEclipseContext workbenchContext) { } @PreSave void preSave(IEclipseContext workbenchContext) { } @ProcessAdditions void processAdditions(MApplication app, EModelService modelService) { MTrimmedWindow window=(MTrimmedWindow)modelService.find(MWINDOW_ID, app); // 窗口居中 Display display = Display.getDefault(); this.setLocation(display, window); } /** * 窗口居中 * * @param display * @param window */ private void setLocation(Display display, MWindow window) { Monitor monitor = display.getPrimaryMonitor(); Rectangle monitorRect = monitor.getBounds(); int x=monitorRect.x+(monitorRect.width-window.getWidth())/2; int y=monitorRect.y+(monitorRect.height-window.getHeight())/2; window.setX(x); window.setY(y); } @ProcessRemovals void processRemovals(IEclipseContext workbenchContext) { } }
    至此,完成E4 RCP的窗口居中。
back_to_top