Skip to content

Commit e2426f2

Browse files
author
Tom Gottfried
committed
Unify dialog behavior and reduce code duplication
Makes PasswordChangeDialog and OkCancelApplyDialog an OkCancelDialog and moves keyboard handling to OkCancelDialog, plus some minor cleanup.
1 parent 7dd8de6 commit e2426f2

File tree

4 files changed

+41
-123
lines changed

4 files changed

+41
-123
lines changed

src/de/bielefeld/umweltamt/aui/HauptFrame.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,7 @@ private JMenuItem getPasswordChangeItem() {
670670
passwordChangeMenuItem.setText("Password ändern");
671671
passwordChangeMenuItem.setMnemonic(KeyEvent.VK_P);
672672
passwordChangeMenuItem.setEnabled(true);
673-
final JFrame owner = this;
673+
final HauptFrame owner = this;
674674
passwordChangeMenuItem.addActionListener(new java.awt.event.ActionListener() {
675675
@Override
676676
public void actionPerformed(java.awt.event.ActionEvent e) {

src/de/bielefeld/umweltamt/aui/gui/PasswordChangeDialog.java

Lines changed: 16 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,13 @@
2222

2323
import java.awt.Dimension;
2424
import java.awt.HeadlessException;
25-
import java.awt.KeyboardFocusManager;
26-
import java.awt.KeyEventDispatcher;
27-
import java.awt.event.ActionEvent;
28-
import java.awt.event.ActionListener;
29-
import java.awt.event.KeyEvent;
3025
import java.util.Arrays;
31-
import java.util.List;
3226

33-
import javax.swing.AbstractAction;
34-
import javax.swing.Action;
35-
import javax.swing.JButton;
36-
import javax.swing.JDialog;
37-
import javax.swing.JFrame;
27+
import javax.swing.JComponent;
3828
import javax.swing.JLabel;
3929
import javax.swing.JOptionPane;
4030
import javax.swing.JPanel;
4131
import javax.swing.JPasswordField;
42-
import javax.swing.KeyStroke;
4332

4433
import com.jgoodies.forms.builder.PanelBuilder;
4534
import com.jgoodies.forms.factories.Paddings;
@@ -51,15 +40,16 @@
5140
import org.hibernate.Transaction;
5241
import org.hibernate.query.NativeQuery;
5342
import org.jfree.ui.tabbedui.VerticalLayout;
54-
import org.postgresql.util.PSQLException;
5543

44+
import de.bielefeld.umweltamt.aui.HauptFrame;
5645
import de.bielefeld.umweltamt.aui.HibernateSessionFactory;
5746
import de.bielefeld.umweltamt.aui.utils.AuikLogger;
47+
import de.bielefeld.umweltamt.aui.utils.dialogbase.OkCancelDialog;
5848

5949
/**
6050
* Dialog, allowing the user to change the current password
6151
*/
62-
public class PasswordChangeDialog extends JDialog {
52+
public class PasswordChangeDialog extends OkCancelDialog {
6353

6454
/** Logging */
6555
private static final AuikLogger log = AuikLogger.getLogger();
@@ -72,76 +62,27 @@ public class PasswordChangeDialog extends JDialog {
7262
private JPasswordField newPasswordField;
7363
private JPasswordField newPasswordConfirmField;
7464

75-
private JButton okButton;
76-
private JButton cancelButton;
77-
78-
private KeyEventDispatcher keyEventDispatcher;
79-
8065
/**
8166
* Constructor
8267
* @param owner Owning frame this dialog will be placed above.
8368
* @throws HeadlessException
8469
*/
85-
public PasswordChangeDialog(JFrame owner){
86-
super(owner, "Passwort ändern", true);
87-
this.setSize(450, 150);
88-
initialize();
70+
public PasswordChangeDialog(HauptFrame owner){
71+
super("Passwort ändern", owner);
72+
this.setSize(450, 200);
8973
}
9074

91-
/**
92-
* Initialize UI
93-
*/
94-
private void initialize() {
75+
@Override
76+
protected JComponent buildContentArea() {
9577
currentPasswordLabel = new JLabel("Aktuelles Passwort:");
9678
newPasswordLabel = new JLabel("Neues Passwort:");
9779
newPasswordConfirmLabel = new JLabel("Passwort bestätigen:");
9880
currentPasswordField = new JPasswordField();
9981
newPasswordField = new JPasswordField();
10082
newPasswordConfirmField = new JPasswordField();
101-
okButton = new JButton("OK");
102-
cancelButton = new JButton("Abbrechen");
10383

10484
currentPasswordField.setPreferredSize(new Dimension(150, 22));
10585

106-
//Button handlers
107-
okButton.addActionListener(new ActionListener() {
108-
@Override
109-
public void actionPerformed(ActionEvent e) {
110-
submit();
111-
}
112-
});
113-
cancelButton.addActionListener(new ActionListener() {
114-
@Override
115-
public void actionPerformed(ActionEvent e) {
116-
close();
117-
}
118-
});
119-
120-
//Key handler
121-
keyEventDispatcher = new KeyEventDispatcher() {
122-
@Override
123-
public boolean dispatchKeyEvent(KeyEvent e) {
124-
if (e.getID() != KeyEvent.KEY_PRESSED) {
125-
return false;
126-
}
127-
switch (e.getKeyCode()) {
128-
//Use ESC to cancel
129-
case KeyEvent.VK_ESCAPE:
130-
close();
131-
break;
132-
//Use enter to submit if focus is on new password confirmation field
133-
case KeyEvent.VK_ENTER:
134-
if (newPasswordConfirmField.isFocusOwner()) {
135-
submit();
136-
}
137-
break;
138-
}
139-
return false;
140-
}
141-
};
142-
KeyboardFocusManager.getCurrentKeyboardFocusManager()
143-
.addKeyEventDispatcher(keyEventDispatcher);
144-
14586
//Create form
14687
FormLayout layout = new FormLayout(
14788
"right:pref, 4dlu, pref:grow, 4dlu, pref", // Spalten
@@ -164,19 +105,14 @@ public boolean dispatchKeyEvent(KeyEvent e) {
164105
formPanel.setBorder(Paddings.DIALOG);
165106
contentPanel.add(formPanel);
166107

167-
JPanel buttonPanel = new JPanel();
168-
buttonPanel.add(okButton);
169-
buttonPanel.add(cancelButton);
170-
contentPanel.add(buttonPanel);
171-
172-
this.setContentPane(contentPanel);
173-
this.pack();
108+
return contentPanel;
174109
}
175110

176111
/**
177112
* Check input and submit password change
178113
*/
179-
private void submit() {
114+
@Override
115+
protected void doOk() {
180116
if (!checkInput()) {
181117
return;
182118
}
@@ -187,24 +123,17 @@ private void submit() {
187123
}
188124
}
189125

190-
/**
191-
* Cancel action and dispose this dialog
192-
*/
193-
private void close() {
194-
PasswordChangeDialog.this.dispose();
195-
}
196-
197126
private boolean checkInput() {
198127
if (checkPasswordEmpty()) {
199-
JOptionPane.showMessageDialog(PasswordChangeDialog.this, "Bitte geben Sie ein neues Passwort ein");
200-
return false;
128+
JOptionPane.showMessageDialog(this, "Bitte geben Sie ein neues Passwort ein");
129+
return false;
201130
}
202131
if (!checkPasswordConfirmation()) {
203-
JOptionPane.showMessageDialog(PasswordChangeDialog.this, "Die Passwörter stimmen nicht überein");
132+
JOptionPane.showMessageDialog(this, "Die Passwörter stimmen nicht überein");
204133
return false;
205134
}
206135
if (!checkCurrentPassword()) {
207-
JOptionPane.showMessageDialog(PasswordChangeDialog.this, "Das eingegebene Passwort ist nicht korrekt");
136+
JOptionPane.showMessageDialog(this, "Das eingegebene Passwort ist nicht korrekt");
208137
return false;
209138
}
210139
return true;

src/de/bielefeld/umweltamt/aui/utils/dialogbase/OkCancelApplyDialog.java

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
* "Ok"- und einem "Abbrechen"-Button.
5858
* @author David Klotz
5959
*/
60-
public abstract class OkCancelApplyDialog extends SimpleDialog {
60+
public abstract class OkCancelApplyDialog extends OkCancelDialog {
6161
private static final long serialVersionUID = 8544344498787710223L;
6262

6363
public OkCancelApplyDialog(HauptFrame frame) {
@@ -68,30 +68,6 @@ public OkCancelApplyDialog(String title, HauptFrame frame) {
6868
super(title, frame);
6969
}
7070

71-
@Override
72-
public Action getFirstButtonAction() {
73-
return new AbstractAction(getOkButtonText()) {
74-
private static final long serialVersionUID = 5631309928023384305L;
75-
76-
@Override
77-
public void actionPerformed(ActionEvent e) {
78-
doOk();
79-
}
80-
};
81-
}
82-
83-
@Override
84-
public Action getSecondButtonAction() {
85-
return new AbstractAction("Abbrechen") {
86-
private static final long serialVersionUID = -4608327257436896003L;
87-
88-
@Override
89-
public void actionPerformed(ActionEvent e) {
90-
doCancel();
91-
}
92-
};
93-
}
94-
9571
@Override
9672
public Action getThirdButtonAction() {
9773
return new AbstractAction("Parameter auswählen") {
@@ -104,15 +80,5 @@ public void actionPerformed(ActionEvent e) {
10480
};
10581
}
10682

107-
protected String getOkButtonText() {
108-
return "OK";
109-
}
110-
111-
protected abstract void doOk();
112-
113-
protected void doCancel() {
114-
close();
115-
}
116-
11783
protected abstract void doApply();
11884
}

src/de/bielefeld/umweltamt/aui/utils/dialogbase/OkCancelDialog.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@
4242
*/
4343
package de.bielefeld.umweltamt.aui.utils.dialogbase;
4444

45+
import java.awt.KeyEventDispatcher;
46+
import java.awt.KeyboardFocusManager;
4547
import java.awt.event.ActionEvent;
48+
import java.awt.event.KeyEvent;
4649

4750
import javax.swing.AbstractAction;
4851
import javax.swing.Action;
@@ -57,12 +60,32 @@
5760
public abstract class OkCancelDialog extends SimpleDialog {
5861
private static final long serialVersionUID = -1999038310745668506L;
5962

63+
private KeyEventDispatcher keyEventDispatcher;
64+
6065
public OkCancelDialog(HauptFrame frame) {
6166
this(null, frame);
6267
}
6368

6469
public OkCancelDialog(String title, HauptFrame frame) {
6570
super(title, frame);
71+
72+
// Use enter to submit
73+
this.getRootPane().setDefaultButton(button1);
74+
75+
// Use ESC to cancel
76+
keyEventDispatcher = new KeyEventDispatcher() {
77+
@Override
78+
public boolean dispatchKeyEvent(KeyEvent e) {
79+
if (e.getID() == KeyEvent.KEY_PRESSED
80+
&& e.getKeyCode() == KeyEvent.VK_ESCAPE
81+
) {
82+
close();
83+
}
84+
return false;
85+
}
86+
};
87+
KeyboardFocusManager.getCurrentKeyboardFocusManager()
88+
.addKeyEventDispatcher(keyEventDispatcher);
6689
}
6790

6891
@Override

0 commit comments

Comments
 (0)