use of javax.swing.JPasswordField in project jmeter by apache.
the class MailReaderSamplerGui method init.
/*
* Helper method to set up the GUI screen
*/
private void init() {
// WARNING: called from ctor so must not be overridden (i.e. must be private or final)
setLayout(new BorderLayout());
setBorder(makeBorder());
JPanel settingsPanel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = getConstraints();
serverTypeBox = new JTextField(20);
serverTypeBox.addActionListener(this);
serverTypeBox.addFocusListener(this);
addField(settingsPanel, serverTypeLabel, serverTypeBox, gbc);
serverBox = new JTextField(20);
addField(settingsPanel, serverLabel, serverBox, gbc);
portBox = new JTextField(20);
addField(settingsPanel, portLabel, portBox, gbc);
usernameBox = new JTextField(20);
addField(settingsPanel, accountLabel, usernameBox, gbc);
passwordBox = new JPasswordField(20);
addField(settingsPanel, passwordLabel, passwordBox, gbc);
folderLabel = new JLabel(folderLabelStr);
folderBox = new JTextField(INBOX, 20);
addField(settingsPanel, folderLabel, folderBox, gbc);
HorizontalPanel numMessagesPanel = new HorizontalPanel();
numMessagesPanel.add(new JLabel(numMessagesLabel));
ButtonGroup nmbg = new ButtonGroup();
allMessagesButton = new JRadioButton(allMessagesLabel);
allMessagesButton.addChangeListener(e -> {
if (allMessagesButton.isSelected()) {
someMessagesField.setEnabled(false);
}
});
someMessagesButton = new JRadioButton();
someMessagesButton.addChangeListener(e -> {
if (someMessagesButton.isSelected()) {
someMessagesField.setEnabled(true);
}
});
nmbg.add(allMessagesButton);
nmbg.add(someMessagesButton);
someMessagesField = new JTextField(5);
allMessagesButton.setSelected(true);
numMessagesPanel.add(allMessagesButton);
numMessagesPanel.add(someMessagesButton);
numMessagesPanel.add(someMessagesField);
headerOnlyBox = new JCheckBox(headerOnlyLabel);
deleteBox = new JCheckBox(deleteLabel);
storeMimeMessageBox = new JCheckBox(storeMime);
securitySettingsPanel = new SecuritySettingsPanel();
JPanel settings = new VerticalPanel();
settings.add(Box.createVerticalStrut(5));
settings.add(settingsPanel);
settings.add(numMessagesPanel);
settings.add(headerOnlyBox);
settings.add(deleteBox);
settings.add(storeMimeMessageBox);
settings.add(securitySettingsPanel);
add(makeTitlePanel(), BorderLayout.NORTH);
add(settings, BorderLayout.CENTER);
}
use of javax.swing.JPasswordField in project processdash by dtuma.
the class TaskScheduleDialog method importNewSharedSchedule.
private String importNewSharedSchedule() {
String urlStr = "http://";
String passwordStr = "";
String errorMessage = null;
URL u = null;
while (true) {
// ask the user for the relevant information to locate the
// schedule.
JTextField url = new JTextField(urlStr, 40);
url.addFocusListener(new FocusHighlighter(url));
JTextField password = new JPasswordField(passwordStr, 10);
password.addFocusListener(new FocusHighlighter(password));
String urlLabel = resources.getString("Import_Schedule.URL_Label");
String passwordLabel = resources.getString("Import_Schedule.Password_Label");
Object message = new Object[] { errorMessage, resources.getString("Import_Schedule.Instructions"), newHBox(new JLabel(" " + urlLabel + " "), url), newHBox(new JLabel(" " + passwordLabel + " "), password) };
if (JOptionPane.showConfirmDialog(frame, message, resources.getString("Import_Schedule.Dialog_Title"), JOptionPane.OK_CANCEL_OPTION) != JOptionPane.OK_OPTION)
// if the user didn't hit the OK button, return null.
return null;
urlStr = url.getText();
passwordStr = password.getText();
if (urlStr == null || urlStr.trim().length() == 0) {
errorMessage = resources.getString("Import_Schedule.URL_Missing");
continue;
}
if (urlStr.indexOf("/ev+/") != -1) {
errorMessage = resources.getString("Import_Schedule.Pub_URL");
continue;
}
try {
u = new URL(urlStr.trim() + XML_QUERY_SUFFIX);
} catch (MalformedURLException mue) {
errorMessage = resources.getString("Import_Schedule.URL_Invalid");
continue;
}
break;
}
// fetch the specified schedule.
if (passwordStr != null) {
passwordStr = passwordStr.trim();
if (passwordStr.length() == 0)
passwordStr = null;
}
CachedObject importedSchedule = new CachedURLObject(dash.getCache(), EVTaskListCached.CACHED_OBJECT_TYPE, u, passwordStr == null ? null : "EV", passwordStr);
// check to see if there was an error in fetching the schedule.
errorMessage = importedSchedule.getErrorMessage();
String remoteName = null;
if (errorMessage == null) {
// if we were able to successfully fetch the schedule, try
// to interpret its contents as an XML schedule.
remoteName = EVTaskListCached.getNameFromXML(importedSchedule.getString("UTF-8"));
// record an error message.
if (remoteName == null)
errorMessage = resources.getString("Import_Schedule.Invalid_Schedule");
}
// if there was any error, ask the user if they want to continue.
if (errorMessage != null) {
errorMessage = CachedURLObject.translateMessage(resources, "Import_Schedule.", errorMessage);
Object message = new Object[] { resources.getString("Import_Schedule.Error.Header"), " " + errorMessage, resources.getString("Import_Schedule.Error.Footer") };
if (JOptionPane.showConfirmDialog(frame, message, resources.getString("Import_Schedule.Error.Title"), JOptionPane.YES_NO_OPTION, JOptionPane.ERROR_MESSAGE) != JOptionPane.YES_OPTION)
return null;
}
// get a local name to use for this schedule.
String localName = remoteName;
String owner = (String) importedSchedule.getLocalAttr(CachedURLObject.OWNER_ATTR);
if (owner != null)
localName = resources.format("Import_Schedule.Default_Name_FMT", localName, owner);
do {
localName = (String) JOptionPane.showInputDialog(frame, resources.getStrings("Import_Schedule.Name_Dialog.Prompt"), resources.getString("Import_Schedule.Name_Dialog.Title"), JOptionPane.PLAIN_MESSAGE, null, null, localName);
if (localName != null)
localName = localName.replace('/', ' ').trim();
} while (localName == null || localName.length() == 0);
importedSchedule.setLocalAttr(EVTaskListCached.LOCAL_NAME_ATTR, localName);
// return the name of the cached schedule object
return EVTaskListCached.buildTaskListName(importedSchedule.getID(), localName);
}
use of javax.swing.JPasswordField in project processdash by dtuma.
the class HttpAuthenticator method getPasswordAuthentication.
@Override
protected PasswordAuthentication getPasswordAuthentication() {
// only handle "server" auth requests (no proxy support for now)
if (getRequestorType() != RequestorType.SERVER)
return null;
// only handle Data Bridge requests (no support for non-PDES servers)
if (getRequestingURL().toString().indexOf("/DataBridge/") == -1 && getRequestingURL().toString().indexOf("/api/v1/") == -1)
return null;
// find out what state we are presently in.
determineCurrentState();
// if we're in a failure state, return no auth data.
if (state == State.Failed || state == State.Cancelled)
return null;
// possibly supply credentials that were stored in the keyring
if (state == State.Keyring) {
char[] password = getPasswordFromKeyring(lastUsername);
if (password != null)
return new PasswordAuthentication(lastUsername, password);
else if (lastUsername != null && lastPassword != null)
return new PasswordAuthentication(lastUsername, mask(lastPassword));
else
state = State.UserEntry1;
}
// create user interface components to prompt for username and password
JTextField username = new JTextField(2);
JPasswordField password = new JPasswordField(2);
FocusAdapter selectAll = new FocusAdapter() {
public void focusGained(FocusEvent e) {
if (e.getComponent() instanceof JTextField) {
((JTextField) e.getComponent()).selectAll();
}
}
};
username.addFocusListener(selectAll);
password.addFocusListener(selectAll);
JComponent focus = username;
if (StringUtils.hasValue(lastUsername)) {
username.setText(lastUsername);
focus = password;
}
JLabel usernameLabel = new JLabel(resources.getString("Username"), SwingConstants.RIGHT);
JLabel passwordLabel = new JLabel(resources.getString("Password"), SwingConstants.RIGHT);
Dimension d = usernameLabel.getPreferredSize();
d.width = Math.max(d.width, passwordLabel.getPreferredSize().width);
usernameLabel.setPreferredSize(d);
passwordLabel.setPreferredSize(d);
// if "remember me" support is enabled, create a checkbox
JCheckBox rememberMe = null;
if (rememberMeDays > 0) {
rememberMe = new JCheckBox(resources.getString("Remember_Me.Prompt"));
rememberMe.setToolTipText(resources.format("Remember_Me.Tooltip_FMT", rememberMeDays));
Font f = rememberMe.getFont();
f = f.deriveFont(f.getSize2D() * 0.8f);
rememberMe.setFont(f);
}
// prompt the user for credentials
final String title = (StringUtils.hasValue(this.title) ? this.title : resources.getString("Title"));
String[] promptLines = resources.formatStrings("Prompt_FMT", getRequestingPrompt());
Object[] prompt = new Object[promptLines.length];
System.arraycopy(promptLines, 0, prompt, 0, prompt.length);
// add a tooltip to the last line of the prompt, indicating the URL
JLabel promptLabel = new JLabel(promptLines[prompt.length - 1]);
promptLabel.setToolTipText(getEffectiveURL());
prompt[prompt.length - 1] = promptLabel;
final Object[] message = new Object[] { prompt, BoxUtils.vbox(5), BoxUtils.hbox(15, usernameLabel, 5, username), BoxUtils.hbox(15, passwordLabel, 5, password), BoxUtils.hbox(BoxUtils.GLUE, rememberMe), new JOptionPaneTweaker.GrabFocus(focus) };
final int[] userChoice = new int[1];
try {
Runnable r = new Runnable() {
public void run() {
userChoice[0] = JOptionPane.showConfirmDialog(parentComponent, message, title, JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
}
};
if (SwingUtilities.isEventDispatchThread())
r.run();
else
SwingUtilities.invokeAndWait(r);
} catch (Exception e) {
}
// record metadata about this password request
lastUrl = getEffectiveURL();
lastTimestamp = System.currentTimeMillis();
lastUsername = username.getText().trim();
prefs.put(prefsKey(LAST_USERNAME), lastUsername);
if (userChoice[0] == JOptionPane.OK_OPTION) {
// if the user entered credentials, return them.
if (rememberMe != null && rememberMe.isSelected())
savePasswordToKeyring(lastUsername, password.getPassword());
lastPassword = mask(password.getPassword());
return new PasswordAuthentication(lastUsername, password.getPassword());
} else {
// if the user cancelled the operation, abort.
state = State.Cancelled;
return null;
}
}
Aggregations