Slightly simplify EditorHeader tab selection menu items

Previously, this would use a single ActionListener object, and pass the
filename of the file to switch to in the action command. This means that
whenever switching the filename needs to be looked up. This commit
instead uses a lambda to capture the index of the tab to switch to for
every tab (so it uses a different ActionListener for each tab).
This commit is contained in:
Matthijs Kooijman 2015-12-10 15:13:46 +01:00
parent 2f5375d523
commit a3ba935a57
1 changed files with 5 additions and 8 deletions

View File

@ -292,17 +292,14 @@ public class EditorHeader extends JComponent {
Sketch sketch = editor.getSketch();
if (sketch != null) {
menu.addSeparator();
ActionListener jumpListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
editor.getSketch().setCurrentCode(e.getActionCommand());
}
};
int i = 0;
for (SketchCode code : sketch.getCodes()) {
final int index = i++;
item = new JMenuItem(code.isExtension(sketch.getDefaultExtension()) ?
code.getPrettyName() : code.getFileName());
item.setActionCommand(code.getFileName());
item.addActionListener(jumpListener);
item.addActionListener((ActionEvent e) -> {
editor.getSketch().setCurrentCode(index);
});
menu.add(item);
}
}