当前位置: 首页 > 工具软件 > RCP100 > 使用案例 >

RCP 自定义启动界面

祁博雅
2023-12-01

自定义启动界面的工作主要为实现一个自定义扩展类(继承自AbstractSplashHandler)。



主要步骤;

1、为RCP工程增加org.eclipse.core.runtime.products扩展点,并设定ID属性,此ID即为RCP程序的ProductID。

2、在org.eclipse.core.runtime.products扩展点中增加product扩展项,设定application属性,绑定ProductID与Application。

3、为RCP工程增加org.eclipse.ui.splashHandlers扩展点。

4、在org.eclipse.ui.splashHandlers扩展点中增加splashHandlers扩展项,设定HandlerID跟HandlerClass.

5、在org.eclipse.ui.splashHandlers扩展点中增加splashHandlerProductBinding扩展项,设定splashId跟productId,绑定Handler与ProductID,即绑定SplashHandler与程序。

6、实现第4步中指定的HandlerClass,其需继承自AbstractSplashHandler。

示例代码:

  1. * The splash screen controller for the RCP application. This has been modified to also act as a login screen for the  
  2. package xxxx.SplashHandler; 
  3.  
  4. import org.eclipse.jface.dialogs.MessageDialog; 
  5.  
  6. /**
  7. * The splash handler overrides the default RCP splash handler.
  8. */ 
  9. public class LoginSplashHandler extends AbstractSplashHandler { 
  10.  
  11.     private Composite loginComposite; 
  12.     private Text usernameTextBox; 
  13.     private Text passwordTextBox; 
  14.     private Button okButton; 
  15.     private Button cancelButton; 
  16.     private boolean isAuthenticated; 
  17.     private Label usernameLabel; 
  18.     private Label passwordLabel; 
  19.  
  20.     public LoginSplashHandler() { 
  21.         isAuthenticated = false
  22.     } 
  23.  
  24.     public void init(final Shell splash) { 
  25.         splash.setMinimumSize(new Point(400, 165)); 
  26.         System.out.println("In splash windows."); 
  27.         super.init(splash); 
  28.  
  29.         configureUISplash(); 
  30.         createUI(); 
  31.         createUIListeners(); 
  32.  
  33.         // 显示各界面元素。 
  34.         splash.layout(true); 
  35.  
  36.         /**
  37.          * Create the event loop for the splash to prevent the application load
  38.          * from completion, and hold it at the splash until the login event is
  39.          * successful.
  40.          */ 
  41.         while (isAuthenticated == false) { 
  42.             if (splash.getDisplay().readAndDispatch() == false) { 
  43.                 splash.getDisplay().sleep(); 
  44.             } 
  45.         } 
  46.     } 
  47.  
  48.     /**
  49.      * Create the UI listeners for all the form components.
  50.      */ 
  51.     private void createUIListeners() { 
  52.         // Create the OK button listeners. 
  53.         okButton.addSelectionListener(new SelectionAdapter() { 
  54.             public void widgetSelected(SelectionEvent e) { 
  55.                 handleButtonOKWidgetSelected(); 
  56.             } 
  57.         }); 
  58.  
  59.         // Create the cancel button listeners. 
  60.         cancelButton.addSelectionListener(new SelectionAdapter() { 
  61.             public void widgetSelected(SelectionEvent e) { 
  62.                 /**
  63.                  * Abort the loading of the RCP application.
  64.                  */ 
  65.                 getSplash().getDisplay().close(); 
  66.                 System.exit(0); 
  67.             } 
  68.         }); 
  69.     } 
  70.  
  71.     /**
  72.      * Handles the OK button being pressed and the login attempted.
  73.      */ 
  74.     private void handleButtonOKWidgetSelected() { 
  75.         String username = usernameTextBox.getText(); 
  76.         String password = passwordTextBox.getText(); 
  77.  
  78.         // AuthenticationClient client = new AuthenticationClient(); 
  79.  
  80.         if (username.equals("") || password.equals("")) { 
  81.             MessageDialog.openError(getSplash(), "Authentication Failed", //$NON-NLS-1$ 
  82.                     "A username and password must be specified to login."); //$NON-NLS-1$ 
  83.         } else
  84.             // try { 
  85.             // if (client.authenticate(username, password)) { 
  86.             if (true) { 
  87.                 isAuthenticated = true
  88.             } else
  89.                 MessageDialog.openError(getSplash(), "Authentication Failed", //$NON-NLS-1$ 
  90.                         "The details you entered could not be verified."); //$NON-NLS-1$ 
  91.             } 
  92.             // } catch (MalformedURLException e) { 
  93.             //              MessageDialog.openError(getSplash(), "Authentication Failed", //$NON-NLS-1$ 
  94.             //                      "Service responded with an error."); //$NON-NLS-1$ 
  95.             // } 
  96.         } 
  97.     } 
  98.  
  99.     /**
  100.      * Calls the individual UI component creation functions.
  101.      */ 
  102.     private void createUI() { 
  103.         // Create the login panel. 
  104.         loginComposite = new Composite(getSplash(), SWT.BORDER); 
  105.         loginComposite.setLayout(null); 
  106.  
  107.         // Create the user name label. 
  108.         usernameLabel = new Label(loginComposite, SWT.NONE); 
  109.         usernameLabel.setBounds(153, 50, 69, 17); 
  110.         usernameLabel.setText("&User Name:"); 
  111.  
  112.         // Create the user name text widget. 
  113.         usernameTextBox = new Text(loginComposite, SWT.BORDER); 
  114.         usernameTextBox.setBounds(228, 76, 141, 23); 
  115.         usernameTextBox.setText("xxx"); 
  116.  
  117.         // Create the password label. 
  118.         passwordLabel = new Label(loginComposite, SWT.NONE); 
  119.         passwordLabel.setBounds(163, 79, 59, 17); 
  120.         passwordLabel.setText("&Password:"); 
  121.  
  122.         // Create the password text widget. 
  123.         int style = SWT.PASSWORD | SWT.BORDER; 
  124.         passwordTextBox = new Text(loginComposite, style); 
  125.         passwordTextBox.setBounds(228, 47, 141, 23); 
  126.         passwordTextBox.setText("xxx"); 
  127.  
  128.         // Create the OK button. 
  129.         okButton = new Button(loginComposite, SWT.PUSH); 
  130.         okButton.setBounds(156, 118, 100, 30); 
  131.         okButton.setText("OK"); 
  132.  
  133.         // Create the cancel button. 
  134.         cancelButton = new Button(loginComposite, SWT.PUSH); 
  135.         cancelButton.setBounds(269, 118, 100, 30); 
  136.         cancelButton.setText("Cancel"); 
  137.     } 
  138.  
  139.     /**
  140.      * Configures the splash screen SWT/UI components.
  141.      */ 
  142.     private void configureUISplash() { 
  143.         // Configure layout 
  144.         FillLayout layout = new FillLayout(); 
  145.         getSplash().setLayout(layout); 
  146.  
  147.         // Force shell to inherit the splash background 
  148.         getSplash().setBackgroundMode(SWT.INHERIT_DEFAULT); 
  149.     } 
 * The splash screen controller for the RCP application. This has been modified to also act as a login screen for the 
package xxxx.SplashHandler;

import org.eclipse.jface.dialogs.MessageDialog;

/**
 * The splash handler overrides the default RCP splash handler.
 */
public class LoginSplashHandler extends AbstractSplashHandler {

	private Composite loginComposite;
	private Text usernameTextBox;
	private Text passwordTextBox;
	private Button okButton;
	private Button cancelButton;
	private boolean isAuthenticated;
	private Label usernameLabel;
	private Label passwordLabel;

	public LoginSplashHandler() {
		isAuthenticated = false;
	}

	public void init(final Shell splash) {
		splash.setMinimumSize(new Point(400, 165));
		System.out.println("In splash windows.");
		super.init(splash);

		configureUISplash();
		createUI();
		createUIListeners();

		// 显示各界面元素。
		splash.layout(true);

		/**
		 * 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.
		 */
		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.
		okButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				handleButtonOKWidgetSelected();
			}
		});

		// Create the cancel button listeners.
		cancelButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				/**
				 * Abort the loading of the RCP application.
				 */
				getSplash().getDisplay().close();
				System.exit(0);
			}
		});
	}

	/**
	 * 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)) {
			if (true) {
				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.
		loginComposite = new Composite(getSplash(), SWT.BORDER);
		loginComposite.setLayout(null);

		// Create the user name label.
		usernameLabel = new Label(loginComposite, SWT.NONE);
		usernameLabel.setBounds(153, 50, 69, 17);
		usernameLabel.setText("&User Name:");

		// Create the user name text widget.
		usernameTextBox = new Text(loginComposite, SWT.BORDER);
		usernameTextBox.setBounds(228, 76, 141, 23);
		usernameTextBox.setText("xxx");

		// Create the password label.
		passwordLabel = new Label(loginComposite, SWT.NONE);
		passwordLabel.setBounds(163, 79, 59, 17);
		passwordLabel.setText("&Password:");

		// Create the password text widget.
		int style = SWT.PASSWORD | SWT.BORDER;
		passwordTextBox = new Text(loginComposite, style);
		passwordTextBox.setBounds(228, 47, 141, 23);
		passwordTextBox.setText("xxx");

		// Create the OK button.
		okButton = new Button(loginComposite, SWT.PUSH);
		okButton.setBounds(156, 118, 100, 30);
		okButton.setText("OK");

		// Create the cancel button.
		cancelButton = new Button(loginComposite, SWT.PUSH);
		cancelButton.setBounds(269, 118, 100, 30);
		cancelButton.setText("Cancel");
	}

	/**
	 * 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);
	}
}





参考资料:

1、Why isn't my RCP application splash showing in Windows?

2、Eclipse RCP application - custom splash screen

3、How to add a decorator to the splash screen of your RCP application?


 类似资料: