📄 authenticatorwindow.java
字号:
Debug.printStackTrace( e );
return( null );
}
sem.reserve();
String user = dialog[0].getUsername();
String pw = dialog[0].getPassword();
String persist = dialog[0].savePassword()?"true":"false";
if ( user == null ){
return( null );
}
return( new String[]{ user, pw == null?"":pw, persist });
}
protected class
authDialog
{
private Shell shell;
private AESemaphore sem;
private String username;
private String password;
private boolean persist;
protected
authDialog(
AESemaphore _sem,
Display display,
String realm,
String tracker,
String torrent_name )
{
sem = _sem;
if ( display.isDisposed()){
sem.release();
return;
}
shell = new Shell (display,SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
if(! Constants.isOSX) {
shell.setImage(ImageRepository.getImage("azureus"));
}
Messages.setLanguageText(shell, "authenticator.title");
GridLayout layout = new GridLayout();
layout.numColumns = 3;
shell.setLayout (layout);
GridData gridData;
// realm
Label realm_label = new Label(shell,SWT.NULL);
Messages.setLanguageText(realm_label, "authenticator.realm");
gridData = new GridData(GridData.FILL_BOTH);
gridData.horizontalSpan = 1;
realm_label.setLayoutData(gridData);
Label realm_value = new Label(shell,SWT.NULL);
realm_value.setText(realm.replaceAll("&", "&&"));
gridData = new GridData(GridData.FILL_BOTH);
gridData.horizontalSpan = 2;
realm_value.setLayoutData(gridData);
// tracker
Label tracker_label = new Label(shell,SWT.NULL);
Messages.setLanguageText(tracker_label, "authenticator.tracker");
gridData = new GridData(GridData.FILL_BOTH);
gridData.horizontalSpan = 1;
tracker_label.setLayoutData(gridData);
Label tracker_value = new Label(shell,SWT.NULL);
tracker_value.setText(tracker.replaceAll("&", "&&"));
gridData = new GridData(GridData.FILL_BOTH);
gridData.horizontalSpan = 2;
tracker_value.setLayoutData(gridData);
if ( torrent_name != null ){
Label torrent_label = new Label(shell,SWT.NULL);
Messages.setLanguageText(torrent_label, "authenticator.torrent");
gridData = new GridData(GridData.FILL_BOTH);
gridData.horizontalSpan = 1;
torrent_label.setLayoutData(gridData);
Label torrent_value = new Label(shell,SWT.NULL);
torrent_value.setText(torrent_name.replaceAll("&", "&&"));
gridData = new GridData(GridData.FILL_BOTH);
gridData.horizontalSpan = 2;
torrent_value.setLayoutData(gridData);
}
// user
Label user_label = new Label(shell,SWT.NULL);
Messages.setLanguageText(user_label, "authenticator.user");
gridData = new GridData(GridData.FILL_BOTH);
gridData.horizontalSpan = 1;
user_label.setLayoutData(gridData);
final Text user_value = new Text(shell,SWT.BORDER);
user_value.setText("");
gridData = new GridData(GridData.FILL_BOTH);
gridData.horizontalSpan = 2;
user_value.setLayoutData(gridData);
user_value.addListener(SWT.Modify, new Listener() {
public void handleEvent(Event event) {
username = user_value.getText();
}});
// password
Label password_label = new Label(shell,SWT.NULL);
Messages.setLanguageText(password_label, "authenticator.password");
gridData = new GridData(GridData.FILL_BOTH);
gridData.horizontalSpan = 1;
password_label.setLayoutData(gridData);
final Text password_value = new Text(shell,SWT.BORDER);
password_value.setEchoChar('*');
password_value.setText("");
gridData = new GridData(GridData.FILL_BOTH);
gridData.horizontalSpan = 2;
password_value.setLayoutData(gridData);
password_value.addListener(SWT.Modify, new Listener() {
public void handleEvent(Event event) {
password = password_value.getText();
}});
// persist
Label blank_label = new Label(shell,SWT.NULL);
gridData = new GridData(GridData.FILL_BOTH);
gridData.horizontalSpan = 1;
blank_label.setLayoutData(gridData);
final Button checkBox = new Button(shell, SWT.CHECK);
checkBox.setText(MessageText.getString( "authenticator.savepassword" ));
gridData = new GridData(GridData.FILL_BOTH);
gridData.horizontalSpan = 2;
checkBox.setLayoutData(gridData);
checkBox.addListener(SWT.Selection,new Listener() {
public void handleEvent(Event e) {
persist = checkBox.getSelection();
}
});
// line
Label labelSeparator = new Label(shell,SWT.SEPARATOR | SWT.HORIZONTAL);
gridData = new GridData(GridData.FILL_HORIZONTAL);
gridData.horizontalSpan = 3;
labelSeparator.setLayoutData(gridData);
// buttons
new Label(shell,SWT.NULL);
Button bOk = new Button(shell,SWT.PUSH);
Messages.setLanguageText(bOk, "Button.ok");
gridData = new GridData(GridData.FILL_HORIZONTAL | GridData.HORIZONTAL_ALIGN_END | GridData.HORIZONTAL_ALIGN_FILL);
gridData.grabExcessHorizontalSpace = true;
gridData.widthHint = 70;
bOk.setLayoutData(gridData);
bOk.addListener(SWT.Selection,new Listener() {
public void handleEvent(Event e) {
close(true);
}
});
Button bCancel = new Button(shell,SWT.PUSH);
Messages.setLanguageText(bCancel, "Button.cancel");
gridData = new GridData(GridData.HORIZONTAL_ALIGN_END);
gridData.grabExcessHorizontalSpace = false;
gridData.widthHint = 70;
bCancel.setLayoutData(gridData);
bCancel.addListener(SWT.Selection,new Listener() {
public void handleEvent(Event e) {
close(false);
}
});
shell.setDefaultButton( bOk );
shell.addListener(SWT.Traverse, new Listener() {
public void handleEvent(Event e) {
if ( e.character == SWT.ESC){
close( false );
}
}
});
shell.pack ();
Utils.centreWindow( shell );
shell.open ();
}
protected void
close(
boolean ok )
{
if ( ok ){
if ( username == null ){
username = "";
}
if ( password == null ){
password = "";
}
}else{
username = null;
password = null;
}
shell.dispose();
sem.release();
}
protected String
getUsername()
{
return( username );
}
protected String
getPassword()
{
return( password );
}
protected boolean
savePassword()
{
return( persist );
}
}
protected class
authCache
{
private String key;
private PasswordAuthentication auth;
private boolean persist;
private int life = 5;
private boolean succeeded;
protected
authCache(
String _key,
PasswordAuthentication _auth,
boolean _persist )
{
key = _key;
auth = _auth;
persist = _persist;
}
protected String
getKey()
{
return( key );
}
protected boolean
isPersistent()
{
return( persist );
}
protected void
setOutcome(
boolean success)
{
if ( success ){
succeeded = true;
}else{
if ( persist ){
persist = false;
saveAuthCache();
}
if ( !succeeded ){
auth = null;
}
}
}
protected PasswordAuthentication
getAuth()
{
if ( succeeded ){
return( auth );
}
life--;
if ( life >= 0 ){
return( auth );
}
if ( persist ){
persist = false;
saveAuthCache();
}
return( null );
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -