Java tutorial
/******************************************************************************* * Copyright (c) 2013 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.decrypt; import org.eclipse.jface.dialogs.IDialogSettings; import org.eclipse.jface.dialogs.IMessageProvider; 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.decrypt.Decryption; 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 page of the <strong>XML Decryption Wizard</strong>. Lets the user select the key used for encryption and the * corresponding password. * </p> * * <p> * After all information is entered the user can finish the Wizard and the XML fragment identified by the given * encryption id will be decrypted and restored in plain text. Any wrong entered information that was not already * discovered in the wizard will cause decryption to fail. * </p> * * @author Dominik Schadow * @version 1.0.0 */ public class PageKey extends WizardPage implements Listener { /** Wizard page name. */ public static final String PAGE_NAME = "DecryptPageKeystore"; //$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; /** Model for the XML Decryption Wizard. */ private Decryption decryption = null; /** Default label width. */ private static final int LABELWIDTH = 120; public PageKey(final Decryption decryption) { super(PAGE_NAME); setTitle(Messages.decryptionTitle); setDescription(Messages.PageKeystore_keystoreDescription); this.decryption = decryption; } /** * 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(); } } /** * Creates the wizard page with the layout settings. * * @param parent Parent composite */ public void createControl(final Composite parent) { Composite container = new Composite(parent, SWT.NULL); FormLayout formLayout = new FormLayout(); container.setLayout(formLayout); createPageContent(container); addListeners(); setControl(container); loadSettings(); PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IContextHelpIds.WIZARD_DECRYPTION_KEY); } /** * Fills this wizard page with content. Two groups (<i>Keystore</i> and <i>Key</i>) and all their widgets are * inserted. * * @param parent 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); gKey.setLayoutData(data); // Elements for group "Key" Label lKeyName = new Label(gKey, SWT.SHADOW_IN); lKeyName.setText(Messages.name); data = new FormData(); data.width = LABELWIDTH; data.top = new FormAttachment(gKey); data.left = new FormAttachment(gKey); lKeyName.setLayoutData(data); tKeyName = new Text(gKey, SWT.SINGLE); tKeyName.setTextLimit(IGlobals.KEY_NAME_MAX_SIZE); data = new FormData(); data.width = IGlobals.SHORT_TEXT_WIDTH; data.top = new FormAttachment(lKeyName, 0, SWT.CENTER); data.left = new FormAttachment(lKeyName); 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")); //$NON-NLS-1$ 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); } 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.missingKeyName, IMessageProvider.INFORMATION); return; } if (tKeyPassword.getText().length() == 0) { updateStatus(Messages.missingKeyPassword, 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); } } /** * Called when finishing the wizard. * * @return Finishing status */ public boolean performFinish() { return saveDataToModel(); } /** * Saves the selections on this wizard page to the model. Called on exit of the page. * * @return Saving state */ private boolean saveDataToModel() { // decryption.setFile(file.getLocation().toString()); decryption.setKeyName(tKeyName.getText()); decryption.setKeyPassword(tKeyPassword.getText().toCharArray()); storeSettings(); return true; } /** * Stores some settings of this wizard page in the current workspace. */ private void storeSettings() { IDialogSettings settings = getDialogSettings(); settings.put(NewDecryptionWizard.SETTING_KEY_NAME, tKeyName.getText()); } /** * Loads the stored settings for this wizard page. */ private void loadSettings() { String previousKey = getDialogSettings().get(NewDecryptionWizard.SETTING_KEY_NAME); if (previousKey == null) { previousKey = ""; //$NON-NLS-1$ } tKeyName.setText(previousKey); } }