public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new RowLayout()); Text text = new Text(shell, SWT.MULTI | SWT.BORDER); String modifier = SWT.MOD1 == SWT.CTRL ? "Ctrl" : "Command"; text.setText("Hit " + modifier + "+Return\nto see\nthe default button\nrun"); text.addTraverseListener( e -> { switch (e.detail) { case SWT.TRAVERSE_RETURN: if ((e.stateMask & SWT.MOD1) != 0) e.doit = true; } }); Button button = new Button(shell, SWT.PUSH); button.pack(); button.setText("OK"); button.addSelectionListener( new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { System.out.println("OK selected"); } }); shell.setDefaultButton(button); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
protected authDialog( AESemaphore _sem, Display display, String realm, boolean is_tracker, String target, String details) { sem = _sem; if (display.isDisposed()) { sem.releaseForever(); return; } final String ignore_key = "IgnoreAuth:" + realm + ":" + target + ":" + details; if (RememberedDecisionsManager.getRememberedDecision(ignore_key) == 1) { Debug.out( "Authentication for " + realm + "/" + target + "/" + details + " ignored as told not to ask again"); sem.releaseForever(); return; } shell = ShellFactory.createMainShell(SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL); Utils.setShellIcon(shell); 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); // target Label target_label = new Label(shell, SWT.NULL); Messages.setLanguageText( target_label, is_tracker ? "authenticator.tracker" : "authenticator.location"); gridData = new GridData(GridData.FILL_BOTH); gridData.horizontalSpan = 1; target_label.setLayoutData(gridData); Label target_value = new Label(shell, SWT.NULL); target_value.setText(target.replaceAll("&", "&&")); gridData = new GridData(GridData.FILL_BOTH); gridData.horizontalSpan = 2; target_value.setLayoutData(gridData); if (details != null) { Label details_label = new Label(shell, SWT.NULL); Messages.setLanguageText( details_label, is_tracker ? "authenticator.torrent" : "authenticator.details"); gridData = new GridData(GridData.FILL_BOTH); gridData.horizontalSpan = 1; details_label.setLayoutData(gridData); Label details_value = new Label(shell, SWT.NULL); details_value.setText(details.replaceAll("&", "&&")); gridData = new GridData(GridData.FILL_BOTH); gridData.horizontalSpan = 2; details_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 = 1; checkBox.setLayoutData(gridData); checkBox.addListener( SWT.Selection, new Listener() { public void handleEvent(Event e) { persist = checkBox.getSelection(); } }); final Button dontAsk = new Button(shell, SWT.CHECK); dontAsk.setText(MessageText.getString("general.dont.ask.again")); gridData = new GridData(GridData.FILL_BOTH); gridData.horizontalSpan = 1; dontAsk.setLayoutData(gridData); dontAsk.addListener( SWT.Selection, new Listener() { public void handleEvent(Event e) { RememberedDecisionsManager.setRemembered(ignore_key, dontAsk.getSelection() ? 1 : 0); } }); // 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 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); Utils.setShellIcon(shell); 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(); shell.forceActive(); }