应用前显示登录对话框

时间:2017-04-04 07:25:09

标签: java eclipse-plugin

我想要实现的非常简单:我希望用户在未登录时无法看到应用程序。

所以我需要做的是显示一个登录对话框,然后创建应用程序。我试图在IApplication#start(IApplicationContext)中做到这一点,但是这个方法显然没有在UI线程中运行,所以我没有运气让对话框显示在那里。

之后我尝试了WorkbenchAdvisor#preStartup()

public void preStartup() {
    if (openLoginDialog() != Window.OK) {
        getWorkbenchConfigurer().emergencyClose();
    }
}

我不认为emergencyClose()是在这里调用的正确方法,但是IWorkbench#close()会抛出NullPointerException并仍然打开一个窗口存根。

好吧,emergencyClose()也会抛出NullPointerException,但除此之外的工作方式正常:

java.lang.NullPointerException
at org.eclipse.ui.internal.Workbench.busyClose(Workbench.java:1204)
at org.eclipse.ui.internal.Workbench.access$22(Workbench.java:1113)
at org.eclipse.ui.internal.Workbench$19.run(Workbench.java:1454)
at org.eclipse.swt.custom.BusyIndicator.showWhile(BusyIndicator.java:70)
at org.eclipse.ui.internal.Workbench.close(Workbench.java:1451)
at org.eclipse.ui.internal.WorkbenchConfigurer.emergencyClose(WorkbenchConfigurer.java:159)
at org.acme.project.ApplicationWorkbenchAdvisor.preStartup(ApplicationWorkbenchAdvisor.java:36)
at org.eclipse.ui.internal.Workbench$24.runWithException(Workbench.java:1718)
at org.eclipse.ui.internal.StartupThreading$StartupRunnable.run(StartupThreading.java:32)
at org.eclipse.swt.widgets.RunnableLock.run(RunnableLock.java:35)
at org.eclipse.swt.widgets.Synchronizer.runAsyncMessages(Synchronizer.java:135)
at org.eclipse.swt.widgets.Display.runAsyncMessages(Display.java:4155)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3772)
at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2989)
at org.eclipse.ui.internal.Workbench.access$9(Workbench.java:2894)
at org.eclipse.ui.internal.Workbench$5.run(Workbench.java:685)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:337)
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:606)
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:150)

有没有办法防止这种异常?或者是否有更好的方法在第一个应用程序窗口打开之前打开登录对话框?

1 个答案:

答案 0 :(得分:0)

你可以像这样调用IApplication #start(IApplicationContext)中的UI线程上的一些方法。但我不确定这是正确的方法。

final int[] result = new int[]{0};
Display.syncExec(new Runnable() {
    result[0] = new YourLoginDialog(Display.getCurrent().getShell()).open();
});
if (result[0] != Windows.OK) {
    // failed to login
    return IApplicaton.EXIT_OK;
}