3

我创建了一个带有交互式启动画面的 RCP 应用程序,用于登录我的系统。我在 Mac 上构建它,应用程序运行良好,但是当我为 Windows 创建新产品配置并运行应用程序时,它启动时没有启动,控制台中也没有出现错误。

飞溅处理程序代码如下

/**
 * The splash screen controller for the RCP application. This has been modified to also act as a login screen for the 
 * application. Failure to correctly authenticate results in the application termination.
 */
package com.myproject.plugins.core.splashHandlers;

import java.net.MalformedURLException;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.splash.AbstractSplashHandler;

/**
 * The splash handler overrides the default RCP splash handler.
 * @since 3.3
 */
public class InteractiveSplashHandler extends AbstractSplashHandler {

    /**
     * Composite container for login form.
     */
    private Composite loginComposite;

    /**
     * Text box input for login username.
     */
    private Text usernameTextBox;

    /**
     * Text box input for login password.
     */
    private Text passwordTextBox;

    /**
     * OK button for submission of the login form.
     */
    private Button okButton;

    /**
     * Cancel button for cancelling the login attempt and exiting the application.
     */
    private Button cancelButton;

    /**
     * Simple boolean flag to store login success/status.
     */
    private boolean isAuthenticated;

    /**
     * SWT form label for username.
     */
    private Label usernameLabel;

    /**
     * SWT form label for password.
     */
    private Label passwordLabel;

    /**
     * Form/layout data for username label. 
     */
    private FormData usernameLabelFormData;

    /**
     * Form/layout data for password label.
     */
    private FormData passwordLabelFormData;

    /**
     * Form/layout data for username text box.
     */
    private FormData usernameTextBoxFormData;

    /**
     * Form/layout data for password text box.
     */
    private FormData passwordTextBoxFormData;

    /**
     * Form/layout data for OK button.
     */
    private FormData okButtonFormData;

    /**
 * Constructor for the splash handler.
     */
    public InteractiveSplashHandler() {
        passwordTextBox = null;
        cancelButton = null;
        isAuthenticated = false;
    }

    /**
     * Initialiser for the splash screen.
     * @see org.eclipse.ui.splash.AbstractSplashHandler#init(org.eclipse.swt.widgets.Shell)
     */
    public void init(final Shell splash) {
        /**
         * Initialising the parent SplashHandler with the splash shell.
         */
        super.init(splash);

        /**
         * Configure the shell UI layout.
         */
        configureUISplash();

        /**
         * Create UI components.
         */
        createUI();

        /**
         * Create UI listeners.
         */
        createUIListeners();

        /**
         * Force the splash screen to layout.
         */
        splash.layout(true);

        /**
         * Keep the splash screen visible and prevent the RCP application from loading until the close button is 
         * clicked.
         */
        doEventLoop();
    }

    /**
     * Create the event loop for the splash to prevent the application load from completion, and hold it at the splash 
     * until the login event is successful.
     */
    private void doEventLoop() {
        Shell splash = getSplash();
        while (isAuthenticated == false) {
            if (splash.getDisplay().readAndDispatch() == false) {
                splash.getDisplay().sleep();
            }
        }
    }

    /**
     * Create the UI listeners for all the form components.
     */
    private void createUIListeners() {
        /**
         * Create the OK button listeners.
         */
        createUIListenersButtonOK();

        /**
         * Create the cancel button listeners.
         */
        createUIListenersButtonCancel();
    }

    /**
     * Listeners setup for the cancel button.
     */
    private void createUIListenersButtonCancel() {
        cancelButton.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                handleButtonCancelWidgetSelected();
            }
        });     
    }

    /**
     * Handles the cancel action by shutting down the RCP application.
     */
    private void handleButtonCancelWidgetSelected() {
        /**
         * Abort the loading of the RCP application.
         */
        getSplash().getDisplay().close();
        System.exit(0);     
    }

    /**
     * Listeners setup for the OK button.
     */
    private void createUIListenersButtonOK() {
        okButton.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                handleButtonOKWidgetSelected();
            }
        });
    }

    /**
     * Handles the OK button being pressed and the login attempted.
     */
    private void handleButtonOKWidgetSelected() {
        String username = usernameTextBox.getText();
        String password = passwordTextBox.getText();

        AuthenticationClient client = new AuthenticationClient();

        if (username.equals("") || password.equals("")) {
            MessageDialog.openError(getSplash(), 
                "Authentication Failed",  //$NON-NLS-1$
                "A username and password must be specified to login.");  //$NON-NLS-1$
        } else {
            try {
                if (client.authenticate(username, password)) {
                    isAuthenticated = true;
                } else {
                    MessageDialog.openError(getSplash(), 
                        "Authentication Failed",  //$NON-NLS-1$
                        "The details you entered could not be verified.");  //$NON-NLS-1$
                }
            } catch (MalformedURLException e) {
                MessageDialog.openError(getSplash(), 
                    "Authentication Failed",  //$NON-NLS-1$
                    "Service responded with an error.");  //$NON-NLS-1$
            }
        }
    }

    /**
     * Calls the individual UI component creation functions.
     */
    private void createUI() {
        /**
         * Create the login panel.
         */
        createUICompositeLogin();

        /**
         * Create the user name label.
         */
        createUILabelUserName();

        /**
         * Create the user name text widget.
         */
        createUITextUserName();

        /**
         * Create the password label.
         */
        createUILabelPassword();

        /**
         * Create the password text widget.
         */
        createUITextPassword();

        /**
         * Create the OK button.
         */
        createUIButtonOK();

        /**
         * Create the cancel button.
         */
        createUIButtonCancel();
    }       

    /**
     * Creates the SWT component for the cancel button.
     */
    private void createUIButtonCancel() {
        /**
         * Create the button.
         */
        cancelButton = new Button(loginComposite, SWT.PUSH);
        okButtonFormData.right = new FormAttachment(cancelButton, -6);

        FormData cancelButtonFormData = new FormData();
        cancelButtonFormData.left = new FormAttachment(0, 392);
        cancelButtonFormData.right = new FormAttachment(100, -10);
        cancelButtonFormData.bottom = new FormAttachment(100, -10);
        cancelButton.setLayoutData(cancelButtonFormData);

        cancelButton.setText("Cancel");
    }

    /**
     * Creates the SWT component for the OK button.
     */
    private void createUIButtonOK() {
        /**
         * Create the button.
         */
        okButton = new Button(loginComposite, SWT.PUSH);
        passwordTextBoxFormData.bottom = new FormAttachment(okButton, -6);

        okButtonFormData = new FormData();
        okButtonFormData.left = new FormAttachment(0, 279);
        okButtonFormData.bottom = new FormAttachment(100, -10);
        okButton.setLayoutData(okButtonFormData);

        okButton.setText("OK");
    }

    /**
     * Creates the SWT component for the password text box.
     */
    private void createUITextPassword() {
        /**
         * Create the text widget.
         */
        int style = SWT.PASSWORD | SWT.BORDER;
        passwordTextBox = new Text(loginComposite, style);

        passwordLabelFormData.right = new FormAttachment(passwordTextBox, -6);

        passwordTextBoxFormData = new FormData();
        passwordTextBoxFormData.right = new FormAttachment(100, -10);
        passwordTextBoxFormData.left = new FormAttachment(0, 279);
        passwordTextBox.setLayoutData(passwordTextBoxFormData);
    }

    /**
     * Creates the SWT component for the password label.
     */
    private void createUILabelPassword() {
        /**
         * Create the label.
         */
        passwordLabel = new Label(loginComposite, SWT.NONE);

        passwordLabelFormData = new FormData();
        passwordLabelFormData.top = new FormAttachment(usernameLabel, 11);
        passwordLabel.setLayoutData(passwordLabelFormData);

        passwordLabel.setText("&Password:");
    }

    /**
     * Creates SWT component for the username text box.
     */
    private void createUITextUserName() {
        /**
         * Create the text widget.
         */
        usernameTextBox = new Text(loginComposite, SWT.BORDER);

        usernameLabelFormData.top = new FormAttachment(usernameTextBox, 3, SWT.TOP);
        usernameLabelFormData.right = new FormAttachment(usernameTextBox, -6);

        usernameTextBoxFormData = new FormData();
        usernameTextBoxFormData.top = new FormAttachment(0, 233);
        usernameTextBoxFormData.right = new FormAttachment(100, -10);
        usernameTextBoxFormData.left = new FormAttachment(0, 279);
        usernameTextBox.setLayoutData(usernameTextBoxFormData);
    }

    /**
     * Creates SWT component for the username label.
     */
    private void createUILabelUserName() {
        /**
         * Create the label
         */
        usernameLabel = new Label(loginComposite, SWT.NONE);
        usernameLabelFormData = new FormData();
        usernameLabel.setLayoutData(usernameLabelFormData);
        usernameLabel.setText("&User Name:");
    }

    /**
     * Creates SWT component for the login composite.
     */
    private void createUICompositeLogin() {
        /**
         * Create the composite and set the layout.
         */
        loginComposite = new Composite(getSplash(), SWT.BORDER);
        loginComposite.setLayout(new FormLayout());
    }

    /**
     * Configures the splash screen SWT/UI components.
     */
    private void configureUISplash() {
        /**
         * Configure layout
         */
        FillLayout layout = new FillLayout(); 
        getSplash().setLayout(layout);

        /**
         * Force shell to inherit the splash background
         */
        getSplash().setBackgroundMode(SWT.INHERIT_DEFAULT);
    }
}
4

1 回答 1

2

奇怪的是,位图(在 Photoshop/Mac 中创建)图像在 Eclipse 中被视为损坏,但在所有图形应用程序(包括 Photoshop/Win)中都可以正常打开。我在 MS Paint 中打开了该文件并保存而没有更改。飞溅开始工作正常。

如果您收到此错误,请检查您的位图!

于 2012-09-23T13:35:56.973 回答