Skip to content

Commit

Permalink
Use an anonymous inner class as listener instead of FileFormatBox its…
Browse files Browse the repository at this point in the history
…elf.
  • Loading branch information
nvcleemp committed Apr 1, 2015
1 parent 2da015a commit a59519c
Showing 1 changed file with 22 additions and 20 deletions.
42 changes: 22 additions & 20 deletions cage/FileFormatBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,33 @@
* with a textfield of which the name is altered based on the selected
* file format.
*/
public class FileFormatBox extends JComboBox<String> implements ActionListener {
public class FileFormatBox extends JComboBox<String> {

private final List<CaGeWriter> writers = new ArrayList<>();
private final List<WriterConfigurationHandler> handlers = new ArrayList<>();
private int dimension = 0;
private JTextComponent filenameField;
private String oldExtension;
private boolean addExtension;

private final ActionListener actionListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String currentName = filenameField.getText();
if (currentName.trim().startsWith("|")) {
return;
}
int cut = currentName.length() - oldExtension.length() - 1;
if (cut >= 0 &&
currentName.substring(cut).equalsIgnoreCase("." + oldExtension)) {
filenameField.setText(currentName.substring(0, cut));
}
addExtension();
if (e.getActionCommand().length() == 0) {
filenameField.requestFocus();
}
}
};

public FileFormatBox(String variety, JTextComponent filenameField) {
this(variety, filenameField, true);
Expand All @@ -50,8 +69,8 @@ public FileFormatBox(String variety, JTextComponent filenameField, boolean addEx
writers.add(writer);
handlers.add(handler);
}
addActionListener(this);
registerKeyboardAction(this, "",
addActionListener(actionListener);
registerKeyboardAction(actionListener, "",
KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0),
WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
// new onActionFocusSwitcher(filenameField, this);
Expand Down Expand Up @@ -92,21 +111,4 @@ public void addExtension() {
filenameField.setText(currentName + "." + extension);
oldExtension = extension;
}

@Override
public void actionPerformed(ActionEvent e) {
String currentName = filenameField.getText();
if (currentName.trim().startsWith("|")) {
return;
}
int cut = currentName.length() - oldExtension.length() - 1;
if (cut >= 0 &&
currentName.substring(cut).equalsIgnoreCase("." + oldExtension)) {
filenameField.setText(currentName.substring(0, cut));
}
addExtension();
if (e.getActionCommand().length() == 0) {
filenameField.requestFocus();
}
}
}

0 comments on commit a59519c

Please sign in to comment.