FileDocCategorySizeDatePackage
PasswordWindow.javaAPI DocAzureus 3.0.3.46778Tue Jul 31 13:03:52 BST 2007org.gudy.azureus2.ui.swt

PasswordWindow

public class PasswordWindow extends Object
author
Olivier

Fields Summary
private Shell
shell
private static boolean
bOk
private static long
lastSuccess
private static final long
REMEMBER_SUCCESS_MS
protected static AESemaphore
class_sem
private static PasswordWindow
window
Constructors Summary
protected PasswordWindow(Display display)

  	bOk = false;

    shell = new Shell(display,SWT.APPLICATION_MODAL | SWT.TITLE | SWT.CLOSE);
    shell.setText(MessageText.getString("PasswordWindow.title"));
    if(! Constants.isOSX) {
      shell.setImage(ImageRepository.getImage("azureus"));
    }
    GridLayout layout = new GridLayout();
    layout.numColumns = 2;
    layout.makeColumnsEqualWidth = true;
    shell.setLayout(layout);
    
    Label label = new Label(shell,SWT.NONE);
    label.setText(MessageText.getString("PasswordWindow.passwordprotected"));
    GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
    gridData.horizontalSpan = 2;
    label.setLayoutData(gridData);
    
    final Text password = new Text(shell,SWT.BORDER);
    password.setEchoChar('*");
    gridData = new GridData(GridData.FILL_HORIZONTAL);
    gridData.horizontalSpan = 2;
    password.setLayoutData(gridData);
    
    Button ok = new Button(shell,SWT.PUSH);
    ok.setText(MessageText.getString("Button.ok"));
    gridData = new GridData(GridData.HORIZONTAL_ALIGN_CENTER);
    gridData.widthHint = 70;
    ok.setLayoutData(gridData);
    shell.setDefaultButton(ok);
    ok.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event event) {
				try {
					SHA1Hasher hasher = new SHA1Hasher();
					byte[] passwordText = password.getText().getBytes();
					byte[] encoded = hasher.calculateHash(passwordText);
					byte[] correct = COConfigurationManager.getByteParameter("Password",
							"".getBytes());
					boolean same = true;
					for (int i = 0; i < correct.length; i++) {
						if (correct[i] != encoded[i])
							same = false;
					}
					if (same) {
						bOk = same;
						shell.dispose();
					} else {
						close();
					}
				} catch (Exception e) {
					Debug.printStackTrace(e);
				}
			}
		});    
    
    Button cancel = new Button(shell,SWT.PUSH);
    cancel.setText(MessageText.getString("Button.cancel"));
    gridData = new GridData(GridData.HORIZONTAL_ALIGN_CENTER);
    gridData.widthHint = 70;
    cancel.setLayoutData(gridData);
    cancel.addListener(SWT.Selection,new Listener() {
          /* (non-Javadoc)
           * @see org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets.Event)
           */
          public void handleEvent(Event event) {
             
             close();
          }
        });    
    
    
    shell.addDisposeListener(new DisposeListener() {
      public void widgetDisposed(DisposeEvent arg0) {
      	window = null;
      	class_sem.releaseAllWaiters();
      }
    });
    
    shell.addTraverseListener(new TraverseListener() {
    	public void keyTraversed(TraverseEvent e) {
    		if (e.detail == SWT.TRAVERSE_ESCAPE) {
    			close();
    			e.doit = false;
    		}
    	}
    });
    
    shell.addListener(SWT.Close,new Listener() {
      public void handleEvent(Event arg0) {
        close();
      }
    });

    shell.pack();
    
    Utils.centreWindow(shell);
    
    shell.open();
  
Methods Summary
private voidclose()

    shell.dispose();
    if(Constants.isOSX) {
    	UIFunctionsSWT uiFunctions = UIFunctionsManagerSWT.getUIFunctionsSWT();
    	if (uiFunctions != null) {
				Shell mainShell = uiFunctions.getMainShell();
				if (mainShell != null) {
					mainShell.setMinimized(true);
					mainShell.setVisible(true);
				}
			}
    } 
  
public static voidmain(java.lang.String[] args)

  	final Display display = new Display();
		new Thread(new Runnable() {
			
			public void run() {
				System.out.println("2: " + showPasswordWindow(display));
			}
		
		}).start();
		new Thread(new Runnable() {

			public void run() {
				display.syncExec(new Runnable() {
					public void run() {
						System.out.println("3: " + showPasswordWindow(display));
					}
				});
			}

		}).start();
		display.asyncExec(new Runnable() {
			public void run() {
				System.out.println("4: " + showPasswordWindow(display));
			}
		});
		System.out.println("1: " + showPasswordWindow(display));
	
protected voidrun()

    while (!shell.isDisposed()) {
    	if (!shell.getDisplay().readAndDispatch()) {
    		shell.getDisplay().sleep();
    	}
    }
  
public static booleanshowPasswordWindow(Display display)


        
  	if (lastSuccess + REMEMBER_SUCCESS_MS >= SystemTime.getCurrentTime()) {
  		return true;
  	}
  	
		final boolean bSWTThread = display.getThread() == Thread.currentThread ();
		display.syncExec(new AERunnable() {
			public void runSupport() {
				if (window == null) {
					window = new PasswordWindow(display);
				} else {
					window.shell.forceActive();
				}

				if (bSWTThread) {
					window.run();
				}
			}
		});

		if (!bSWTThread) {
			class_sem.reserve();
		}
		
		lastSuccess = bOk ? SystemTime.getCurrentTime() : 0;
		return bOk;