📄 userpasswordeditor.java
字号:
/**
* All pluggable editors must be in this package
*/
package com.ca.directory.jxplorer.editor;
import com.ca.commons.cbutil.*;
import com.ca.directory.jxplorer.HelpIDs;
import java.security.MessageDigest;
import com.ca.commons.cbutil.CBBase64;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.logging.Logger;
import java.util.logging.Level;
public class userpasswordeditor extends JDialog
implements abstractbinaryeditor
{
/**
* MD5.
*/
public static final String MD5 = "MD5";
/**
* SHA.
*/
public static final String SHA = "SHA";
protected JPasswordField oldPwd, newPwd;
protected CBButton btnOK, btnCancel, btnHelp;
protected editablebinary editMe = null;
protected CBPanel display;
protected JLabel oldLabel, newLabel;
protected CBJComboBox comboType;
protected boolean firstClick = true;
protected static int default_encryption = 0;
private static Logger log = Logger.getLogger(userpasswordeditor.class.getName());
/**
* Constructor - sets up the gui.
*/
public userpasswordeditor(Frame owner)
{
super(owner);
setModal(true);
setTitle(CBIntText.get("User Password"));
display = new CBPanel();
oldPwd = new JPasswordField();
oldPwd.addMouseListener(new MouseListener()
{
public void mouseClicked(MouseEvent e) { }
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
public void mouseReleased(MouseEvent e) { }
public void mousePressed(MouseEvent e)
{
if (firstClick) //TE: only clear the field on the first click b/c if the pwd exists it will be encoded.
{
oldPwd.setText("");
firstClick = false;
}
}
});
newPwd = new JPasswordField();
oldLabel = new JLabel(CBIntText.get("Enter Password:"));
newLabel = new JLabel(CBIntText.get("Re-enter Password:"));
btnOK = new CBButton(CBIntText.get("OK"), CBIntText.get("Click here to save the changes (remember to click Submit in the table editor)."));
btnOK.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
load();
}
});
btnCancel = new CBButton(CBIntText.get("Cancel"), CBIntText.get("Click here to exit."));
btnCancel.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
quit();
}
});
comboType = new CBJComboBox();
comboType.addItem(CBIntText.get("plain"));
comboType.addItem(CBIntText.get(MD5));
comboType.addItem(CBIntText.get(SHA));
comboType.setEditable(false);
comboType.setSelectedIndex(default_encryption);
comboType.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
default_encryption = comboType.getSelectedIndex();
}
});
btnHelp = new CBButton(CBIntText.get("Help"), CBIntText.get("Click here for Help."));
CBHelpSystem.useDefaultHelp(btnHelp, HelpIDs.ATTR_PASSWORD);
display.makeHeavy();
display.addln(oldLabel);
display.addln(oldPwd);
display.addln(new JLabel(" ")); //TE: space
display.addln(newLabel);
display.addln(newPwd);
display.add(comboType);
display.addln(new JLabel(" "));
display.makeLight();
JPanel buttonPanel = new JPanel();
buttonPanel.add(btnOK);
buttonPanel.add(btnCancel);
buttonPanel.add(btnHelp);
display.addln(buttonPanel);
//TE: better way to implement keystroke listening...
display.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("ENTER"), "enter");
display.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("ESCAPE"), "escape");
display.getActionMap().put("enter", new MyAction(CBAction.ENTER));
display.getActionMap().put("escape", new MyAction(CBAction.ESCAPE));
setSize(300, 170);
CBUtility.center(this, owner); //TE: Centres the window.
setTitle(CBIntText.get("User Password Data"));
getContentPane().add(display);
}
/**
* Apparently it is better to use key bindings rather than adding a KeyListener...
* "for reacting in a special way to particular keys, you usually should use key
* bindings instead of a key listener".
* This class lets the user set the key as an int. If a key is pressed and it
* matches the assigned int, a check is done for if it is an escape or enter key.
* (27 or 10). If escape, the quit method is called. If enter, the apply
* method is called.
* Bug 4646.
* @author Trudi.
*/
private class MyAction extends CBAction
{
/**
* Calls super constructor.
* @param key
*/
public MyAction(int key)
{
super(key);
}
/**
* quit is called if the Esc key pressed,
* load is called if Enter key is pressed.
* @param e never used.
*/
public void actionPerformed(ActionEvent e)
{
if (getKey() == ESCAPE)
quit();
else if (getKey() == ENTER)
load();
}
}
/**
* This is the AbstractBinaryEditor interface method which is
* called when the user wants to edit the password
*/
public void setValue(editablebinary editMe)
{
this.editMe = editMe;
oldPwd.setText(stringEncode(editMe.getValue()));
}
protected byte[] plainDecode(String s)
{
try
{
return s.getBytes("UTF-8");
}
catch (UnsupportedEncodingException e)
{
log.log(Level.WARNING, "Unexpected error encoding password ", e);
e.printStackTrace();
return new byte[0];
}
}
protected byte[] mdDecode(String s, int type)
{
try
{
MessageDigest md;
StringBuffer hexString = new StringBuffer();
if (type==2) {
md=MessageDigest.getInstance(SHA);
hexString.append("{" + SHA + "}");
} else {
md=MessageDigest.getInstance(MD5);
hexString.append("{" + MD5 + "}");
}
md.reset();
md.update(s.getBytes("UTF-8"));
byte[] buff = md.digest();
hexString.append(CBBase64.binaryToString(buff));
return hexString.toString().getBytes("UTF-8");
}
catch (UnsupportedEncodingException e)
{
log.log(Level.WARNING, "Unexpected error encoding password ",e);
e.printStackTrace();
return new byte[0];
}
catch (java.security.NoSuchAlgorithmException e)
{
log.log(Level.WARNING, "Unexpected error encoding password ",e);
e.printStackTrace();
return new byte[0];
}
}
/**
* converts between text and a byte array
*/
protected byte[] stringDecode(String s)
{
if (s == null)
return (new byte[0]);
else
{
switch (comboType.getSelectedIndex()) {
case 0:
return plainDecode(s);
case 1:
return mdDecode(s, 1);
case 2:
return mdDecode(s, 2);
default:
return mdDecode(s, 1);
}
}
}
/**
* converts between a byte array and text
*/
protected String stringEncode(byte[] b)
{
if (b == null || b.length == 0)
return new String();
else
try
{
return new String(b, "UTF-8");
}
catch (UnsupportedEncodingException e)
{
log.log(Level.WARNING, "Unexpected error decoding password ", e);
e.printStackTrace();
return new String(b); // fall back on the platform default... not sure this is the best thing to do - CB
}
}
/**
* sets the value of the EditableBinary object with whatever the
* user has entered into the password text field.
*/
protected void load()
{
if (passwordConfirm())
{
editMe.setValue(stringDecode(new String(newPwd.getPassword())));
quit();
}
}
/**
* Does some checks on the password.
* @return True - if the two password fields match.
* False - if the new password field is empty (an error message is displayed).
* False - if the password fields don't match (an error message is displayed).
*/
protected boolean passwordConfirm()
{
if (new String(newPwd.getPassword()).equals(new String(oldPwd.getPassword()))) //TE: if the two password fields match carry on saving the password.
{
return true;
}
else if (new String(newPwd.getPassword()).equals("")) //TE: if the new password field is empty display error message.
{
JOptionPane.showMessageDialog(display, CBIntText.get("Empty password field, please fill in both fields"), CBIntText.get("Warning message"), JOptionPane.INFORMATION_MESSAGE);
newPwd.setText("");
return false;
}
else //TE: if the password fields don't match display error message.
{
JOptionPane.showMessageDialog(display, CBIntText.get("Password typed incorrectly, please try again"), CBIntText.get("Warning message"), JOptionPane.INFORMATION_MESSAGE);
newPwd.setText("");
return false;
}
}
/**
* Shuts down the gui.
*/
protected void quit()
{
setVisible(false);
dispose();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -