📄 editpassworddialog.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.jface.window.Window;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.events.SelectionListener;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.manager.model.PasswordEntry;import com.s10r.util.DuplicateChecker;import com.s10r.util.PasswordGenerator;/** * TODO: remember what settings are used to generate passwords * * @author schoffem */public class EditPasswordDialog extends TitleAreaDialog{ private PasswordEntry entry; private Text nameText; private Text idText; private Text passwordText; private Text confirmPasswordText; private Text descriptionText; private Text urlText; private DuplicateChecker duplicateChecker; private DialogType dialogType; public EditPasswordDialog(Shell shell, DuplicateChecker duplicateChecker, DialogType dialogType) { super(shell); this.duplicateChecker = duplicateChecker; this.dialogType = dialogType; setShellStyle(SWT.APPLICATION_MODAL | SWT.DIALOG_TRIM | SWT.RESIZE); setTitleAreaColor(new RGB(255, 255, 255)); } @Override public void create() { super.create(); this.setTitle("Password Entry"); if (dialogType == DialogType.CREATE) { this.getShell().setText("Create Password Entry"); this.setMessage("Create a password entry"); } else if (dialogType == DialogType.UPDATE) { this.getShell().setText("Edit Password Entry"); this.setMessage("Edit a password entry"); } } 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 = 3; gridLayout.marginWidth = 15; gridLayout.marginHeight = 10; gridLayout.makeColumnsEqualWidth = false; area.setLayout(gridLayout); newLabel(area, "Name:"); nameText = new Text(area, SWT.BORDER); nameText.setLayoutData(createTextGridData()); newLabel(area, "Id:"); idText = new Text(area, SWT.BORDER); idText.setLayoutData(createTextGridData()); newLabel(area, "Password:"); passwordText = new Text(area, SWT.BORDER| SWT.PASSWORD); passwordText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false)); Button generateButton = new Button(area, SWT.BORDER); generateButton.setText("Generate..."); generateButton.addSelectionListener(new SelectionListener() { public void widgetDefaultSelected(SelectionEvent e) { } public void widgetSelected(SelectionEvent e) { GeneratePasswordDialog dlg = new GeneratePasswordDialog( getShell(), passwordText.getText()); int ret = dlg.open(); if (ret == Window.OK) { System.out.println("password accepted"); String newPassword = dlg.getPassword(); passwordText.setText(newPassword); confirmPasswordText.setText(newPassword); validateEntry(); } }}); newLabel(area, "Confirm password:"); confirmPasswordText = new Text(area, SWT.BORDER | SWT.PASSWORD); confirmPasswordText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1)); Button clearButton = new Button(area, SWT.BORDER); clearButton.setText("Clear"); clearButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1)); clearButton.addSelectionListener(new SelectionListener() { public void widgetDefaultSelected(SelectionEvent e) { } public void widgetSelected(SelectionEvent e) { passwordText.setText(""); confirmPasswordText.setText(""); }}); Label descLabel = newLabel(area, "Description:"); descLabel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false)); descriptionText = new Text(area, SWT.BORDER | SWT.MULTI); descriptionText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 3)); // fill up the blanks on the left side of the description field newLabel(area, ""); newLabel(area, ""); newLabel(area, "Url:"); urlText = new Text(area, SWT.BORDER); urlText.setLayoutData(createTextGridData()); KeyListener keyListener = new KeyListener() { public void keyPressed(KeyEvent arg0) { } public void keyReleased(KeyEvent arg0) { validateEntry(); } }; nameText.addKeyListener(keyListener); idText.addKeyListener(keyListener); passwordText.addKeyListener(keyListener); confirmPasswordText.addKeyListener(keyListener); descriptionText.addKeyListener(keyListener); urlText.addKeyListener(keyListener); transferEntryToFields(); return area; } private void transferEntryToFields() { setTextField(nameText, entry.getName()); setTextField(idText, entry.getId()); setTextField(passwordText, entry.getPassword()); setTextField(confirmPasswordText, entry.getPassword()); setTextField(descriptionText, entry.getDescription()); setTextField(urlText, entry.getUrl()); } private void transferFieldsToEntry() { entry.setName(nameText.getText()); entry.setId(idText.getText()); entry.setPassword(passwordText.getText()); entry.setDescription(descriptionText.getText()); entry.setUrl(urlText.getText()); } private void setTextField(Text text, String s) { if (s != null) { text.setText(s); } } private Label newLabel(Composite parent, String text) { Label label = new Label(parent, SWT.NONE); label.setText(text); label.setLayoutData(new GridData(SWT.LEFT, SWT.LEFT, false, false)); return label; } private GridData createTextGridData() { return createTextGridData(2, 1); } private GridData createTextGridData(int horizontalSpan, int verticalSpan) { return new GridData(SWT.FILL, SWT.FILL, true, false, horizontalSpan, verticalSpan); } @Override protected Control createButtonBar(Composite arg0) { Control c = super.createButtonBar(arg0); enableOK(false); return c; } @Override protected void okPressed() { transferFieldsToEntry(); super.okPressed(); } public void setEntry(PasswordEntry entry) { this.entry = entry; } private void validateEntry() { enableOK(false); if (nameText.getText().length() == 0) { setErrorMessage("The name may not be empty."); } else if (duplicateChecker != null && duplicateChecker.exists(nameText.getText())) { setErrorMessage("Password entry named " + nameText.getText() + " already exists"); } else if (! passwordText.getText().equals(confirmPasswordText.getText())) { setErrorMessage("The password field must " + "match the confirm password field."); } else { setErrorMessage(null); enableOK(true); } } private void enableOK(boolean enable) { getButton(IDialogConstants.OK_ID).setEnabled(enable); } public PasswordEntry getEntry() { return entry; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -