alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*******************************************************************************
 * Copyright (c) 2004, 2005 Jean-Michel Lemieux, Jeff McAffer and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Hyperbola is an RCP application developed for the book 
 *     Eclipse Rich Client Platform - 
 *         Designing, Coding, and Packaging Java Applications 
 * Addison-Wesley, Summer 2005
 *
 * Contributors:
 *     Jean-Michel Lemieux and Jeff McAffer - initial implementation
 *     Nick Edgar and Pascal Rapicault - additional work for EclipseCon 2005 tutorial
 *         http://www.eclipsecon.org/presentations/EclipseCon2005_Tutorial26.pdf
 *******************************************************************************/
package org.eclipsercp.hyperbola;

import org.eclipse.jface.dialogs.*;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.*;
import org.eclipsercp.hyperbola.model.ConnectionDetails;

/**
 * Login dialog, which prompts for the user's account info, and has Login and Cancel buttons.
 */
public class LoginDialog extends Dialog {

	private Text userIdText;
	private Text serverText;
	private Text passwordText;
	private ConnectionDetails connectionDetails;

	public LoginDialog(Shell parentShell) {
		super(parentShell);
	}

	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		String productName = HyperbolaPlugin.getProductName();
		newShell.setText(productName + " Login");
	}

	protected Control createDialogArea(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		GridLayout layout = new GridLayout(2, false);
		composite.setLayout(layout);

		Label accountLabel = new Label(composite, SWT.NONE);
		accountLabel.setText("Account details:");
		accountLabel.setLayoutData(new GridData(GridData.BEGINNING, GridData.CENTER, false, false, 2, 1));

		Label userIdLabel = new Label(composite, SWT.NONE);
		userIdLabel.setText("&User ID:");
		userIdLabel.setLayoutData(new GridData(GridData.END, GridData.CENTER, false, false));

		userIdText = new Text(composite, SWT.BORDER);
		GridData gridData = new GridData(GridData.FILL, GridData.FILL, true, false);
		gridData.widthHint = convertHeightInCharsToPixels(20);
		userIdText.setLayoutData(gridData);

		Label serverLabel = new Label(composite, SWT.NONE);
		serverLabel.setText("&Server:");
		serverLabel.setLayoutData(new GridData(GridData.END, GridData.CENTER, false, false));

		serverText = new Text(composite, SWT.BORDER);
		serverText.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, false));

		Label passwordLabel = new Label(composite, SWT.NONE);
		passwordLabel.setText("&Password:");
		passwordLabel.setLayoutData(new GridData(GridData.END, GridData.CENTER, false, false));

		passwordText = new Text(composite, SWT.BORDER | SWT.PASSWORD);
		passwordText.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, false));

		restoreSettings();

		return composite;
	}

	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.OK_ID, "Login", true);
		createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
	}

	protected void okPressed() {
		String userId = userIdText.getText();
		String server = serverText.getText();
		String password = passwordText.getText();
		if (userId.equals("")) {
			MessageDialog.openError(getShell(), "Invalid User ID", "User ID field must not be blank.");
			return;
		}
		if (server.equals("")) {
			MessageDialog.openError(getShell(), "Invalid Server", "Server field must not be blank.");
			return;
		}
		connectionDetails = new ConnectionDetails(userId, server, password);
		saveSettings();
		super.okPressed();
	}

	/**
	 * Returns the connection details entered by the user, or <code>null
	 * if the dialog was canceled.
	 */
	public ConnectionDetails getConnectionDetails() {
		return connectionDetails;
	}

	private void saveSettings() {
		IDialogSettings settings = HyperbolaPlugin.getDefault().getDialogSettings();
		settings.put("userId", connectionDetails.getUserId());
		settings.put("server", connectionDetails.getServer());
		// should encrypt this or something
		settings.put("password", connectionDetails.getPassword());
	}

	private void restoreSettings() {
		IDialogSettings settings = HyperbolaPlugin.getDefault().getDialogSettings();
		String userId = settings.get("userId");
		String server = settings.get("server");
		String password = settings.get("password");
		if (userId != null)
			userIdText.setText(userId);
		if (server != null)
			serverText.setText(server);
		if (password != null)
			passwordText.setText(password);
	}
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.