Skip to content

Commit fa6c931

Browse files
author
Federico Fissore
committed
Made PasswordAuthorizationDialog react to ESC key. Some code cleanup and a pitch of lambda sugar
1 parent 9165af4 commit fa6c931

File tree

2 files changed

+59
-79
lines changed

2 files changed

+59
-79
lines changed

app/src/cc/arduino/view/findreplace/FindReplace.java

+4-9
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@
3535
import processing.app.helpers.OSUtils;
3636

3737
import java.awt.*;
38-
import java.awt.event.ActionEvent;
39-
import java.awt.event.ActionListener;
4038
import java.awt.event.WindowAdapter;
4139
import java.awt.event.WindowEvent;
4240
import java.util.HashMap;
@@ -70,12 +68,9 @@ public FindReplace(Editor editor, Map<String, Object> state) {
7068

7169
getRootPane().setDefaultButton(findButton);
7270

73-
Base.registerWindowCloseKeys(getRootPane(), new ActionListener() {
74-
@Override
75-
public void actionPerformed(ActionEvent e) {
76-
setVisible(false);
77-
Base.FIND_DIALOG_STATE = findDialogState();
78-
}
71+
Base.registerWindowCloseKeys(getRootPane(), e -> {
72+
setVisible(false);
73+
Base.FIND_DIALOG_STATE = findDialogState();
7974
});
8075

8176
Base.setIcon(this);
@@ -91,7 +86,7 @@ public void windowActivated(WindowEvent e) {
9186
}
9287

9388
private Map<String, Object> findDialogState() {
94-
Map<String, Object> state = new HashMap<String, Object>();
89+
Map<String, Object> state = new HashMap<>();
9590
state.put(FIND_TEXT, findField.getText());
9691
state.put(REPLACE_TEXT, replaceField.getText());
9792
state.put(IGNORE_CASE, ignoreCaseBox.isSelected());

app/src/processing/app/forms/PasswordAuthorizationDialog.java

+55-70
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,30 @@
55
import javax.swing.*;
66
import java.awt.*;
77
import java.awt.event.ActionEvent;
8-
import java.awt.event.ActionListener;
8+
import java.awt.event.WindowEvent;
99
import java.io.File;
1010

1111
import static processing.app.I18n._;
1212

1313
public class PasswordAuthorizationDialog extends JDialog {
1414

15-
protected final JButton uploadButton;
16-
protected final JButton cancelButton;
17-
protected final JLabel typePasswordLabel;
18-
protected final JLabel passwordLabel;
19-
protected final JLabel icon;
20-
protected final JPasswordField passwordField;
15+
private final JPasswordField passwordField;
2116

22-
protected boolean cancelled;
23-
protected String password;
17+
private boolean cancelled;
18+
private String password;
2419

2520
public PasswordAuthorizationDialog(Frame parent, String dialogText) {
2621
super(parent, true);
2722

2823
this.cancelled = false;
2924
this.password = null;
3025

31-
typePasswordLabel = new JLabel();
32-
icon = new JLabel();
33-
passwordLabel = new JLabel();
26+
JLabel typePasswordLabel = new JLabel();
27+
JLabel icon = new JLabel();
28+
JLabel passwordLabel = new JLabel();
3429
passwordField = new JPasswordField();
35-
uploadButton = new JButton();
36-
cancelButton = new JButton();
30+
JButton uploadButton = new JButton();
31+
JButton cancelButton = new JButton();
3732

3833
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
3934

@@ -44,81 +39,71 @@ public PasswordAuthorizationDialog(Frame parent, String dialogText) {
4439
passwordLabel.setText(_("Password:"));
4540

4641
passwordField.setText("");
47-
passwordField.addActionListener(new ActionListener() {
48-
public void actionPerformed(ActionEvent evt) {
49-
uploadButtonPressed(evt);
50-
}
51-
});
42+
passwordField.addActionListener(PasswordAuthorizationDialog.this::uploadButtonPressed);
5243

5344
uploadButton.setText(_("Upload"));
54-
uploadButton.addActionListener(new ActionListener() {
55-
public void actionPerformed(ActionEvent evt) {
56-
uploadButtonPressed(evt);
57-
}
58-
});
45+
uploadButton.addActionListener(PasswordAuthorizationDialog.this::uploadButtonPressed);
5946

6047
cancelButton.setText(_("Cancel"));
61-
cancelButton.addActionListener(new ActionListener() {
62-
public void actionPerformed(ActionEvent evt) {
63-
cancelButtonPressed(evt);
64-
}
65-
});
48+
cancelButton.addActionListener(PasswordAuthorizationDialog.this::cancelButtonPressed);
49+
50+
Base.registerWindowCloseKeys(getRootPane(), this::cancelButtonPressed);
6651

6752
GroupLayout layout = new GroupLayout(getContentPane());
6853
getContentPane().setLayout(layout);
6954
layout.setHorizontalGroup(
70-
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
71-
.addGroup(layout.createSequentialGroup()
72-
.addContainerGap()
73-
.addComponent(icon, GroupLayout.PREFERRED_SIZE, 66, GroupLayout.PREFERRED_SIZE)
74-
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
75-
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
76-
.addComponent(typePasswordLabel)
77-
.addGroup(layout.createSequentialGroup()
78-
.addComponent(passwordLabel)
79-
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
80-
.addComponent(passwordField, GroupLayout.PREFERRED_SIZE, 300, GroupLayout.PREFERRED_SIZE)))
81-
.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
82-
.addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
83-
.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
84-
.addComponent(cancelButton)
85-
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
86-
.addComponent(uploadButton)
87-
.addContainerGap())
55+
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
56+
.addGroup(layout.createSequentialGroup()
57+
.addContainerGap()
58+
.addComponent(icon, GroupLayout.PREFERRED_SIZE, 66, GroupLayout.PREFERRED_SIZE)
59+
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
60+
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
61+
.addComponent(typePasswordLabel)
62+
.addGroup(layout.createSequentialGroup()
63+
.addComponent(passwordLabel)
64+
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
65+
.addComponent(passwordField, GroupLayout.PREFERRED_SIZE, 300, GroupLayout.PREFERRED_SIZE)))
66+
.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
67+
.addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
68+
.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
69+
.addComponent(cancelButton)
70+
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
71+
.addComponent(uploadButton)
72+
.addContainerGap())
8873
);
8974
layout.setVerticalGroup(
90-
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
91-
.addGroup(layout.createSequentialGroup()
92-
.addContainerGap()
93-
.addComponent(typePasswordLabel)
94-
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
95-
.addGroup(layout.createSequentialGroup()
96-
.addGap(53, 53, 53)
97-
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
98-
.addComponent(passwordLabel)
99-
.addComponent(passwordField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
100-
.addGap(18, 18, 18))
101-
.addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
102-
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
103-
.addComponent(icon)
104-
.addGap(9, 9, 9)))
105-
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
106-
.addComponent(uploadButton)
107-
.addComponent(cancelButton))
108-
.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
75+
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
76+
.addGroup(layout.createSequentialGroup()
77+
.addContainerGap()
78+
.addComponent(typePasswordLabel)
79+
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
80+
.addGroup(layout.createSequentialGroup()
81+
.addGap(53, 53, 53)
82+
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
83+
.addComponent(passwordLabel)
84+
.addComponent(passwordField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
85+
.addGap(18, 18, 18))
86+
.addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
87+
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
88+
.addComponent(icon)
89+
.addGap(9, 9, 9)))
90+
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
91+
.addComponent(uploadButton)
92+
.addComponent(cancelButton))
93+
.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
10994
);
11095

11196
pack();
11297
}
11398

11499
private void cancelButtonPressed(ActionEvent event) {
115100
this.cancelled = true;
116-
this.dispose();
101+
dispatchEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
117102
}
118103

119-
public void uploadButtonPressed(ActionEvent event) {
104+
private void uploadButtonPressed(ActionEvent event) {
120105
this.password = new String(passwordField.getPassword());
121-
this.dispose();
106+
dispatchEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
122107
}
123108

124109
public String getPassword() {
@@ -128,4 +113,4 @@ public String getPassword() {
128113
public boolean isCancelled() {
129114
return cancelled;
130115
}
131-
}
116+
}

0 commit comments

Comments
 (0)