Java tutorial
/******************************************************************************* * Copyright (c) 2009 Dominik Schadow - http://www.xml-sicherheit.de * 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 * * Contributors: * Dominik Schadow - initial API and implementation *******************************************************************************/ package org.jcryptool.crypto.xml.ui.encrypt; import org.eclipse.jface.dialogs.IDialogSettings; import org.eclipse.jface.dialogs.IMessageProvider; import org.eclipse.jface.wizard.IWizardPage; import org.eclipse.jface.wizard.WizardPage; import org.eclipse.swt.SWT; import org.eclipse.swt.events.ModifyEvent; import org.eclipse.swt.events.ModifyListener; 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.Event; import org.eclipse.swt.widgets.Group; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.Text; import org.eclipse.ui.PlatformUI; import org.jcryptool.crypto.xml.core.encrypt.Encryption; import org.jcryptool.crypto.xml.core.utils.IGlobals; import org.jcryptool.crypto.xml.ui.XSTUIPlugin; import org.jcryptool.crypto.xml.ui.utils.IContextHelpIds; /** * <p>Second default page of the Encryption Wizard. Lets the user select an existing <i>Public Key</i> in an existing * <i>Java KeyStore</i>.</p> * * @author Dominik Schadow * @version 0.5.0 */ public class PageKey extends WizardPage implements Listener { /** Wizard page name. */ public static final String PAGE_NAME = "EncryptPageKey"; //$NON-NLS-1$ /** Button <i>Echo Key Password</i>. */ private Button bEchoKeyPassword = null; /** Key name. */ private Text tKeyName = null; /** Key password. */ private Text tKeyPassword = null; /** Default label width. */ private static final int LABELWIDTH = 120; /** Model for the XML Encryption Wizard. */ private Encryption encryption = null; /** * Constructor for PageOpenKey. * * @param encryption The encryption wizard model */ public PageKey(final Encryption encryption) { super(PAGE_NAME); setTitle(Messages.encryptionTitle); setDescription(Messages.openKeyDescription); this.encryption = encryption; } /** * Creates the wizard page with the layout settings. * * @param parent The parent composite */ public void createControl(final Composite parent) { Composite container = new Composite(parent, SWT.NULL); container.setLayout(new FormLayout()); createPageContent(container); loadSettings(); addListeners(); setControl(container); setPageComplete(false); PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IContextHelpIds.WIZARD_ENCRYPTION_KEY); } /** * Fills this wizard page with content. Two groups (<i>Keystore</i> and <i>Key</i>) and all their widgets * are inserted. * * @param parent The parent composite */ private void createPageContent(final Composite parent) { FormLayout layout = new FormLayout(); layout.marginTop = IGlobals.MARGIN / 2; layout.marginBottom = IGlobals.MARGIN / 2; layout.marginLeft = IGlobals.MARGIN / 2; layout.marginRight = IGlobals.MARGIN / 2; parent.setLayout(layout); // One group Group gKey = new Group(parent, SWT.SHADOW_ETCHED_IN); gKey.setLayout(layout); gKey.setText(Messages.key); FormData data = new FormData(); data.top = new FormAttachment(0, 0); data.left = new FormAttachment(0, 0); data.right = new FormAttachment(IGlobals.GROUP_NUMERATOR); gKey.setLayoutData(data); // Elements for group "Key" Label lKeyName = new Label(gKey, SWT.SHADOW_IN); lKeyName.setText(Messages.name); data = new FormData(); data.top = new FormAttachment(gKey); data.left = new FormAttachment(gKey); data.width = LABELWIDTH; lKeyName.setLayoutData(data); tKeyName = new Text(gKey, SWT.SINGLE); tKeyName.setTextLimit(IGlobals.KEY_NAME_MAX_SIZE); data = new FormData(); data.top = new FormAttachment(lKeyName, 0, SWT.CENTER); data.left = new FormAttachment(lKeyName); data.width = IGlobals.SHORT_TEXT_WIDTH; tKeyName.setLayoutData(data); Label lKeyPassword = new Label(gKey, SWT.SHADOW_IN); lKeyPassword.setText(Messages.password); data = new FormData(); data.width = LABELWIDTH; data.top = new FormAttachment(lKeyName, IGlobals.MARGIN); data.left = new FormAttachment(gKey); lKeyPassword.setLayoutData(data); tKeyPassword = new Text(gKey, SWT.SINGLE); tKeyPassword.setTextLimit(IGlobals.KEY_PASSWORD_MAX_SIZE); data = new FormData(); data.width = IGlobals.SHORT_TEXT_WIDTH; data.top = new FormAttachment(lKeyPassword, 0, SWT.CENTER); data.left = new FormAttachment(lKeyPassword); tKeyPassword.setEchoChar('*'); tKeyPassword.setLayoutData(data); bEchoKeyPassword = new Button(gKey, SWT.PUSH); bEchoKeyPassword.setImage(XSTUIPlugin.getDefault().getImageRegistry().get("echo_password")); bEchoKeyPassword.setToolTipText(Messages.echoPassword); data = new FormData(); data.top = new FormAttachment(tKeyPassword, 0, SWT.CENTER); data.left = new FormAttachment(tKeyPassword, IGlobals.MARGIN); bEchoKeyPassword.setLayoutData(data); } /** * Adds all listeners for the current wizard page. */ private void addListeners() { bEchoKeyPassword.addListener(SWT.Selection, this); tKeyName.addModifyListener(new ModifyListener() { public void modifyText(final ModifyEvent e) { dialogChanged(); } }); tKeyPassword.addModifyListener(new ModifyListener() { public void modifyText(final ModifyEvent e) { dialogChanged(); } }); } /** * Determines the (error) message for the missing field. */ private void dialogChanged() { if (tKeyName.getText().length() == 0) { updateStatus(Messages.enterKeyName, IMessageProvider.INFORMATION); return; } if (tKeyPassword.getText().length() == 0) { updateStatus(Messages.enterKeyPassword, IMessageProvider.INFORMATION); return; } updateStatus(null, IMessageProvider.NONE); } /** * Shows a message to the user to complete the fields on this page. * * @param message The message for the user * @param status The status type of the message */ private void updateStatus(final String message, final int status) { setMessage(message, status); if (message == null && getErrorMessage() == null) { setPageComplete(true); saveDataToModel(); } else { setPageComplete(false); } } /** * Handles the events from this wizard page. * * @param e The triggered event */ public void handleEvent(final Event e) { if (e.widget == bEchoKeyPassword) { echoPassword(e); } } /** * Shows plain text (\0) or cipher text (*) in the password field. * * @param e The triggered event */ private void echoPassword(final Event e) { if (e.widget == bEchoKeyPassword) { if (tKeyPassword.getEchoChar() == '*') { tKeyPassword.setEchoChar('\0'); } else { tKeyPassword.setEchoChar('*'); } tKeyPassword.redraw(); } } /** * Returns the next wizard page after all the necessary data is entered correctly. * * @return IWizardPage The next wizard page */ public IWizardPage getNextPage() { saveDataToModel(); PageAlgorithms page = ((NewEncryptionWizard) getWizard()).getPageAlgorithms(); page.onEnterPage(); return page; } /** * Saves the selections on this wizard page to the model. Called on exit of the page. */ private void saveDataToModel() { encryption.setKeyName(tKeyName.getText()); encryption.setKeyPassword(tKeyPassword.getText().toCharArray()); storeSettings(); } /** * Loads the stored settings for this wizard page. */ private void loadSettings() { String previousKey = getDialogSettings().get(NewEncryptionWizard.SETTING_KEY_NAME); if (previousKey == null) { previousKey = ""; } tKeyName.setText(previousKey); } /** * Stores some settings of this wizard page in the current workspace. */ private void storeSettings() { IDialogSettings settings = getDialogSettings(); settings.put(NewEncryptionWizard.SETTING_KEY_NAME, tKeyName.getText()); } }