From fa6c931e7c67e6f3a1679910cf5f8bbc98e5a3c1 Mon Sep 17 00:00:00 2001 From: Federico Fissore Date: Fri, 19 Jun 2015 10:39:40 +0200 Subject: [PATCH] Made PasswordAuthorizationDialog react to ESC key. Some code cleanup and a pitch of lambda sugar --- .../arduino/view/findreplace/FindReplace.java | 13 +- .../forms/PasswordAuthorizationDialog.java | 125 ++++++++---------- 2 files changed, 59 insertions(+), 79 deletions(-) diff --git a/app/src/cc/arduino/view/findreplace/FindReplace.java b/app/src/cc/arduino/view/findreplace/FindReplace.java index 881774e24..09d40bfbd 100644 --- a/app/src/cc/arduino/view/findreplace/FindReplace.java +++ b/app/src/cc/arduino/view/findreplace/FindReplace.java @@ -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 findDialogState() { - Map state = new HashMap(); + Map state = new HashMap<>(); state.put(FIND_TEXT, findField.getText()); state.put(REPLACE_TEXT, replaceField.getText()); state.put(IGNORE_CASE, ignoreCaseBox.isSelected()); diff --git a/app/src/processing/app/forms/PasswordAuthorizationDialog.java b/app/src/processing/app/forms/PasswordAuthorizationDialog.java index e79c05426..754f4be79 100644 --- a/app/src/processing/app/forms/PasswordAuthorizationDialog.java +++ b/app/src/processing/app/forms/PasswordAuthorizationDialog.java @@ -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; } -} \ No newline at end of file +}