1
0
mirror of https://github.com/arduino/Arduino.git synced 2024-12-01 12:24:14 +01:00

Made PasswordAuthorizationDialog react to ESC key. Some code cleanup and a pitch of lambda sugar

This commit is contained in:
Federico Fissore 2015-06-19 10:39:40 +02:00
parent 9165af4751
commit fa6c931e7c
2 changed files with 59 additions and 79 deletions

View File

@ -35,8 +35,6 @@ import processing.app.Sketch;
import processing.app.helpers.OSUtils;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.HashMap;
@ -70,12 +68,9 @@ public class FindReplace extends javax.swing.JFrame {
getRootPane().setDefaultButton(findButton);
Base.registerWindowCloseKeys(getRootPane(), new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
setVisible(false);
Base.FIND_DIALOG_STATE = findDialogState();
}
Base.registerWindowCloseKeys(getRootPane(), e -> {
setVisible(false);
Base.FIND_DIALOG_STATE = findDialogState();
});
Base.setIcon(this);
@ -91,7 +86,7 @@ public class FindReplace extends javax.swing.JFrame {
}
private Map<String, Object> findDialogState() {
Map<String, Object> state = new HashMap<String, Object>();
Map<String, Object> state = new HashMap<>();
state.put(FIND_TEXT, findField.getText());
state.put(REPLACE_TEXT, replaceField.getText());
state.put(IGNORE_CASE, ignoreCaseBox.isSelected());

View File

@ -5,22 +5,17 @@ import processing.app.Base;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.io.File;
import static processing.app.I18n._;
public class PasswordAuthorizationDialog extends JDialog {
protected final JButton uploadButton;
protected final JButton cancelButton;
protected final JLabel typePasswordLabel;
protected final JLabel passwordLabel;
protected final JLabel icon;
protected final JPasswordField passwordField;
private final JPasswordField passwordField;
protected boolean cancelled;
protected String password;
private boolean cancelled;
private String password;
public PasswordAuthorizationDialog(Frame parent, String dialogText) {
super(parent, true);
@ -28,12 +23,12 @@ public class PasswordAuthorizationDialog extends JDialog {
this.cancelled = false;
this.password = null;
typePasswordLabel = new JLabel();
icon = new JLabel();
passwordLabel = new JLabel();
JLabel typePasswordLabel = new JLabel();
JLabel icon = new JLabel();
JLabel passwordLabel = new JLabel();
passwordField = new JPasswordField();
uploadButton = new JButton();
cancelButton = new JButton();
JButton uploadButton = new JButton();
JButton cancelButton = new JButton();
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
@ -44,68 +39,58 @@ public class PasswordAuthorizationDialog extends JDialog {
passwordLabel.setText(_("Password:"));
passwordField.setText("");
passwordField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
uploadButtonPressed(evt);
}
});
passwordField.addActionListener(PasswordAuthorizationDialog.this::uploadButtonPressed);
uploadButton.setText(_("Upload"));
uploadButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
uploadButtonPressed(evt);
}
});
uploadButton.addActionListener(PasswordAuthorizationDialog.this::uploadButtonPressed);
cancelButton.setText(_("Cancel"));
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
cancelButtonPressed(evt);
}
});
cancelButton.addActionListener(PasswordAuthorizationDialog.this::cancelButtonPressed);
Base.registerWindowCloseKeys(getRootPane(), this::cancelButtonPressed);
GroupLayout layout = new GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(icon, GroupLayout.PREFERRED_SIZE, 66, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(typePasswordLabel)
.addGroup(layout.createSequentialGroup()
.addComponent(passwordLabel)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(passwordField, GroupLayout.PREFERRED_SIZE, 300, GroupLayout.PREFERRED_SIZE)))
.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(cancelButton)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(uploadButton)
.addContainerGap())
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(icon, GroupLayout.PREFERRED_SIZE, 66, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(typePasswordLabel)
.addGroup(layout.createSequentialGroup()
.addComponent(passwordLabel)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(passwordField, GroupLayout.PREFERRED_SIZE, 300, GroupLayout.PREFERRED_SIZE)))
.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(cancelButton)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(uploadButton)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(typePasswordLabel)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(53, 53, 53)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(passwordLabel)
.addComponent(passwordField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18))
.addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(icon)
.addGap(9, 9, 9)))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(uploadButton)
.addComponent(cancelButton))
.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(typePasswordLabel)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(53, 53, 53)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(passwordLabel)
.addComponent(passwordField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18))
.addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(icon)
.addGap(9, 9, 9)))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(uploadButton)
.addComponent(cancelButton))
.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
@ -113,12 +98,12 @@ public class PasswordAuthorizationDialog extends JDialog {
private void cancelButtonPressed(ActionEvent event) {
this.cancelled = true;
this.dispose();
dispatchEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
}
public void uploadButtonPressed(ActionEvent event) {
private void uploadButtonPressed(ActionEvent event) {
this.password = new String(passwordField.getPassword());
this.dispose();
dispatchEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
}
public String getPassword() {
@ -128,4 +113,4 @@ public class PasswordAuthorizationDialog extends JDialog {
public boolean isCancelled() {
return cancelled;
}
}
}