📄 generatepassworddialog.java
字号:
/* * Copyright 2006 Marcel Schoffelmeer * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package com.s10r.manager.dialogs;import org.eclipse.jface.dialogs.IDialogConstants;import org.eclipse.jface.dialogs.TitleAreaDialog;import org.eclipse.swt.SWT;import org.eclipse.swt.events.KeyEvent;import org.eclipse.swt.events.KeyListener;import org.eclipse.swt.events.SelectionAdapter;import org.eclipse.swt.events.SelectionEvent;import org.eclipse.swt.graphics.RGB;import org.eclipse.swt.layout.FillLayout;import org.eclipse.swt.layout.GridData;import org.eclipse.swt.layout.GridLayout;import org.eclipse.swt.widgets.Button;import org.eclipse.swt.widgets.Composite;import org.eclipse.swt.widgets.Control;import org.eclipse.swt.widgets.Group;import org.eclipse.swt.widgets.Label;import org.eclipse.swt.widgets.Shell;import org.eclipse.swt.widgets.Spinner;import org.eclipse.swt.widgets.Table;import org.eclipse.swt.widgets.TableColumn;import org.eclipse.swt.widgets.TableItem;import org.eclipse.swt.widgets.Text;import com.s10r.util.PasswordGenerator;/** * TODO: remember what settings are used to generate passwords * * @author schoffem */public class GeneratePasswordDialog extends TitleAreaDialog{ private String msg; private TableItem upperAlphaChars; private TableItem lowerAlphaChars; private TableItem numericChars; private TableItem underlineChar; private TableItem minusChar; private TableItem spaceChar; private TableItem specialChars; private TableItem specialBrackets; private Text newPassword; private Table ot; private Spinner nrOfCharacters; private String passwordStr; private Text confirmPassword; private String initialPassword; public GeneratePasswordDialog(Shell shell, String originalPassword) { super(shell); setShellStyle(SWT.APPLICATION_MODAL | SWT.DIALOG_TRIM | SWT.RESIZE); setTitleAreaColor(new RGB(255, 255, 255)); // can't set title here.. setTitle(msg); this.msg = "Set password for password entry"; this.initialPassword = originalPassword; } @Override public void create() { super.create(); this.setTitle(msg); this.setMessage("You can either use the password generator or " + "supply your own in the password entry fields."); } protected Control createDialogArea(Composite parent) { final Composite area = new Composite(parent, SWT.NONE); // the parent uses a grid layout // so set the area layout data to fill in both // directions GridData data2 = new GridData(GridData.FILL_BOTH); area.setLayoutData(data2); final GridLayout gridLayout = new GridLayout(); gridLayout.numColumns = 4; gridLayout.marginWidth = 15; gridLayout.marginHeight = 10; area.setLayout(gridLayout); Group og = new Group(area, SWT.SHADOW_ETCHED_OUT); og.setLayout(new FillLayout()); og.setText("Password Generation Options"); GridData data = new GridData(GridData.FILL_BOTH); data.horizontalSpan = 4; og.setLayoutData(data); Composite left = new Composite(og, SWT.NULL); GridLayout leftLayout = new GridLayout(); leftLayout.numColumns = 2; leftLayout.verticalSpacing = 9; left.setLayout(leftLayout); createOptionsTable(left); /* * data = new GridData(GridData.FILL_BOTH); final Button checkCharsOnly = * new Button(left, SWT.CHECK); checkCharsOnly.setText("Only the * following characters :"); * * checkCharsOnly.addSelectionListener(new SelectionListener() { * * public void widgetDefaultSelected(SelectionEvent arg0) { } * * public void widgetSelected(SelectionEvent arg0) { if * (password.isEnabled()) { password.setEnabled(false); * ot.setEnabled(true); } else { password.setEnabled(true); * ot.setEnabled(false); } }}); * * data = new GridData(GridData.FILL_HORIZONTAL); data.widthHint = 200; * password = new Text(left, SWT.BORDER); password.setLayoutData(data); * password.setEnabled(false); * */ Label label2 = new Label(left, SWT.NONE); label2.setText("Number of characters:"); data = new GridData(GridData.FILL_HORIZONTAL); // data.widthHint = 200; nrOfCharacters = new Spinner(left, SWT.BORDER); nrOfCharacters.setMinimum(0); nrOfCharacters.setMaximum(1000); nrOfCharacters.setSelection(16); nrOfCharacters.setIncrement(1); nrOfCharacters.setPageIncrement(10); nrOfCharacters.setLayoutData(data); /* * Composite passComp = new Composite(area, SWT.NONE); RowLayout row = * new RowLayout(); row.wrap = false; passComp.setLayout(row); */ data = new GridData(GridData.BEGINNING); Label label3 = new Label(area, SWT.NONE); // data.grabExcessHorizontalSpace = false; // data.widthHint = 100; label3.setText("Enter/generate your password:"); label3.setLayoutData(data); data = new GridData(GridData.FILL_HORIZONTAL); data.horizontalSpan = 2; newPassword = new Text(area, SWT.BORDER); newPassword.setLayoutData(data); newPassword.setText(""); Button generate = new Button(area, SWT.NONE); generate.setText("Generate"); // generate.setLayoutData(data); generate.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent arg0) { generatePassword(); } }); // TODO: not clear what a good password strength check is... // Label quality = new Label(area, SWT.NONE); // quality.setText("Entropy:"); // quality.setLayoutData(data); // parent.pack(); data = new GridData(GridData.BEGINNING); Label label4 = new Label(area, SWT.NONE); // data.grabExcessHorizontalSpace = false; // data.widthHint = 100; label4.setText("Confirm password:"); label4.setLayoutData(data); data = new GridData(GridData.FILL_HORIZONTAL); data.horizontalSpan = 2; confirmPassword = new Text(area, SWT.BORDER); confirmPassword.setLayoutData(data); confirmPassword.setText(""); KeyListener keyListener = new KeyListener() { public void keyPressed(KeyEvent arg0) { } public void keyReleased(KeyEvent arg0) { validateEntry(); } }; newPassword.addKeyListener(keyListener); confirmPassword.addKeyListener(keyListener); if (initialPassword != null) { newPassword.setText(initialPassword); confirmPassword.setText(initialPassword); } return area; } private void createOptionsTable(Composite left) { ot = new Table(left, SWT.BORDER | SWT.CHECK | SWT.FULL_SELECTION); ot.setHeaderVisible(true); ot.setVisible(true); GridData data = new GridData(GridData.FILL_BOTH); data.horizontalSpan = 3; data.widthHint = 100; data.heightHint = 200; ot.setLayoutData(data); TableColumn tc = new TableColumn(ot, SWT.NONE); tc.setText("Options"); tc.setWidth(300); upperAlphaChars = new TableItem(ot, SWT.NONE); upperAlphaChars.setText("Upper alphabetic characters (A, B, C, ...)"); upperAlphaChars.setChecked(true); lowerAlphaChars = new TableItem(ot, SWT.NONE); lowerAlphaChars.setText("Lower alphabetic characters (a, b, c, ...)"); lowerAlphaChars.setChecked(true); numericChars = new TableItem(ot, SWT.NONE); numericChars.setText("Lower alphabetic characters (1, 2, 3, ...)"); numericChars.setChecked(true); underlineChar = new TableItem(ot, SWT.NONE); underlineChar.setText("Underline character '_'"); underlineChar.setChecked(false); minusChar = new TableItem(ot, SWT.NONE); minusChar.setText("Minus character '-'"); minusChar.setChecked(false); spaceChar = new TableItem(ot, SWT.NONE); spaceChar.setText("Space character ' '"); spaceChar.setChecked(false); specialChars = new TableItem(ot, SWT.NONE); specialChars.setText("Special characters (!,$, &, ...)"); specialChars.setChecked(false); /* * TODO: regroup these character sets higherAnsiChars = new * TableItem(ot, SWT.NONE); higherAnsiChars.svalidateEntryetText("Higher ANSI * characters"); higherAnsiChars.setChecked(false); */ specialBrackets = new TableItem(ot, SWT.NONE); specialBrackets.setText("Special brackets ({, }, [, ...)"); specialBrackets.setChecked(false); } @Override protected Control createButtonBar(Composite arg0) { Control c = super.createButtonBar(arg0); enableOK(true); return c; } /** * Enables the OK button if more than 1 character of password entry has been * entered and the confirmation matches the password. */ private void validateEntry() { if (newPassword.getText().length() > 0 && newPassword.getText().equals(confirmPassword.getText())) { // keep track of the password entered so callers of this // dialog can retrieve it after the dialog has been dismissed // (at this point, the password text field becomes invalid enableOK(true); this.setErrorMessage(null); } else { enableOK(false); setErrorMessage("Password may not be empty. The password field must " + "match the confirm password field."); } } private void enableOK(boolean enable) { getButton(IDialogConstants.OK_ID).setEnabled(enable); } /** * @return the password * @uml.property name="password" */ public String getPassword() { // does not work: password is already disposed at that time return this.passwordStr; } private void generatePassword() { PasswordGenerator gen = new PasswordGenerator(getSelectedCharSets(), getCustomCharSet()); String newPasswordStr = gen.generate(nrOfCharacters.getSelection()); newPassword.setText(newPasswordStr); confirmPassword.setText(newPasswordStr); // know for sure that entry is ok (password = confirm password). validateEntry(); } private char[] getCustomCharSet() { StringBuffer customChars = new StringBuffer(); if (underlineChar.getChecked()) { customChars.append('_'); } if (minusChar.getChecked()) { customChars.append('-'); } if (spaceChar.getChecked()) { customChars.append(' '); } return customChars.toString().toCharArray(); } private int getSelectedCharSets() { int selectedCharSets = 0; if (upperAlphaChars.getChecked()) { selectedCharSets |= PasswordGenerator.UPPER_ALPHA; } if (lowerAlphaChars.getChecked()) { selectedCharSets |= PasswordGenerator.LOWER_ALPHA; } if (numericChars.getChecked()) { selectedCharSets |= PasswordGenerator.NUMERIC; } if (specialChars.getChecked()) { selectedCharSets |= PasswordGenerator.SPECIAL; } if (specialBrackets.getChecked()) { selectedCharSets |= PasswordGenerator.BRACKET; } return selectedCharSets; } @Override protected void okPressed() { this.passwordStr = newPassword.getText(); super.okPressed(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -