private JMenu file, reopen, edit, keyboard, playback, music, help;
private JFileChooser chooser;
private File directory;
- private ArrayList recentFiles;
/**
* Creates the menu bar.
addItem(file, "Open...", KeyEvent.VK_O, true);
reopen = createMenu("Reopen", KeyEvent.VK_R);
Iterator it = recentFiles.iterator();
- while (it.hasNext()) {
- JMenuItem recentFile = new JMenuItem((String)it.next());
- recentFile.addActionListener(this);
- reopen.add(recentFile);
- }
+ while (it.hasNext()) addReopenItem((String)it.next());
file.add(reopen);
addItem(file, "Save", KeyEvent.VK_S, true);
addItem(file, "Save as...", KeyEvent.VK_A, false);
playback.addSeparator();
keyboard = createMenu("Set keyboard octave", KeyEvent.VK_K);
edit.add(keyboard);
- for (int i = 9; i >= 0; i--) addItem(keyboard, "Octave " + i, i + 48, false);
+ for (int i = 9; i >= 0; i--) addItem(keyboard, "" + i, i + 48, false);
music = createMenu("Music", KeyEvent.VK_M);
add(music);
return item;
}
+ /**
+ * Adds an item for this file to the top of the reopen menu.
+ */
+ private void addReopenItem(String file) {
+ for (int i = 0; i < reopen.getMenuComponentCount(); i++) {
+ if (file.equals(((JMenuItem)reopen.getMenuComponent(i)).getText()))
+ reopen.remove(i);
+ }
+ JMenuItem recentFile = new JMenuItem(file);
+ recentFile.addActionListener(this);
+ reopen.insert(recentFile, 0);
+ if (reopen.getMenuComponentCount() > 5) reopen.remove(5);
+ }
+
+ /**
+ * Checks if the given file has the extension ".mid".
+ */
private boolean isMidiFile(File f) {
if(f != null) {
String extension = f.getName().substring(f.getName().lastIndexOf('.') + 1).toLowerCase().trim();
if(returnVal == JFileChooser.APPROVE_OPTION && isMidiFile(file)) {
directory = file.getParentFile();
if (!Moosique.promptOnUnsavedChanges()) {
- if (Moosique.load(file)) {
- // Adds an item for this file to the reopen menu.
- String filename = file.getAbsolutePath();
- JMenuItem recentFile = new JMenuItem(filename);
- for (int i = 0; i < reopen.getMenuComponentCount(); i++) {
- if (filename.equals(((JMenuItem)reopen.getMenuComponent(i)).getText()))
- reopen.remove(i);
- }
- reopen.insert(recentFile, 0);
- if (reopen.getMenuComponentCount() > 5) reopen.remove(5);
- }
+ if (Moosique.load(file)) addReopenItem(file.getAbsolutePath());
}
}
} else if (command == "Save") {
} else if (command == "Set position...") {
MooDialog newDialog = new MooDialog(MooDialog.SET_POSITION);
// Moosique.setPosition(???); Räkna ut från msr, beats, ticks, time sig.
- } else if (command.startsWith("Octave")) {
- MooKeyboard.setOctave(Integer.parseInt(command.substring(7,8)));
+ } else if (keyboard.isMenuComponent((JMenuItem)e.getSource())) {
+ MooKeyboard.setOctave(Integer.parseInt(command));
} else if (command == "Add track...") {
MooDialog newDialog = new MooDialog(MooDialog.ADD_TRACK);
Moosique.getSequence().createTrack();
new ImageIcon(Moosique.getGUI().logo));
} else if (reopen.isMenuComponent((JMenuItem)e.getSource())) {
String recentFile = ((JMenuItem)e.getSource()).getText();
- if (!Moosique.promptOnUnsavedChanges()) Moosique.load(new File(recentFile));
+ if (!Moosique.promptOnUnsavedChanges()) {
+ if (Moosique.load(new File(recentFile))) addReopenItem(recentFile);
+ }
}
}
File file = chooser.getSelectedFile();
if(returnVal == JFileChooser.APPROVE_OPTION && isMidiFile(file)) {
directory = file.getParentFile();
- Moosique.saveAs(file);
+ if (Moosique.saveAs(file)) addReopenItem(file.getAbsolutePath());
}
}