+import javax.sound.midi.*;
import javax.swing.*;
+import javax.swing.filechooser.*;
+import java.awt.event.*;
+import java.io.*;
+import java.awt.*;
-public class MooMenu extends JMenuBar
-{
- // empty contructor that anropar the createMooMenu in the mainmethod
- public MooMenu()
- {
- JMenuBar menubar = new JMenuBar();
- menubar.add(createFileMenu());
- menubar.add(createEditMenu());
- menubar.add(createSequenceMenu());
- menubar.add(createTrackMenu());
- menubar.add(createNoteMenu());
- menubar.add(createHelpMenu());
+/**
+ * Moosiques GUI representing a menubar, menus and menuitems
+ *
+ * @author Björn Lanneskog
+ */
+public class MooMenu extends JMenuBar implements ActionListener {
+
+ private JMenu file, edit, playback, music, help;
+ private JFileChooser chooser;
+ private File directory;
+
+ /**
+ * Creates the menu bar.
+ */
+ public MooMenu() {
+ file = createMenu("File", KeyEvent.VK_F);
+ add(file);
+
+ addItem(file, "New", KeyEvent.VK_N);
+ addItem(file, "Open...", KeyEvent.VK_O);
+ addItem(file, "Save", KeyEvent.VK_S);
+ addItem(file, "Save as...");
+ addItem(file, "Exit", KeyEvent.VK_Q);
+
+ edit = createMenu("Edit", KeyEvent.VK_E);
+ add(edit);
+
+ addItem(edit, "Copy", KeyEvent.VK_C);
+ addItem(edit, "Cut", KeyEvent.VK_X);
+ addItem(edit, "Paste", KeyEvent.VK_V);
+ addItem(edit, "Select all", KeyEvent.VK_E);
+ addItem(edit, "Invert selection", KeyEvent.VK_I);
+ addItem(edit, "Preferences...", KeyEvent.VK_P);
+
+ playback = createMenu("Playback", KeyEvent.VK_P);
+ add(playback);
+
+ addItem(playback, "Play", KeyEvent.VK_SPACE);
+ addItem(playback, "Pause");
+ addItem(playback, "Stop");
+ addItem(playback, "Jump...");
+
+ music = createMenu("Music", KeyEvent.VK_M);
+ add(music);
+
+ addItem(music, "Add track...", KeyEvent.VK_A);
+ addItem(music, "Delete track...", KeyEvent.VK_D);
+ addItem(music, "Copy track...", KeyEvent.VK_Y);
+ addItem(music, "Move track...", KeyEvent.VK_M);
+ addItem(music, "Insert measure...");
+ addItem(music, "Delete measure...");
+ addItem(music, "Set time signature...");
+ addItem(music, "Set tempo...");
+ addItem(music, "Scale velocity...");
+ addItem(music, "Transpose...");
+
+ help = createMenu("Help", KeyEvent.VK_L);
+ add(help);
+
+ addItem(help, "Contents");
+ addItem(help, "Getting started");
+ addItem(help, "About");
}
- // creates the filemenu
- public JMenu createFileMenu()
- {
- JMenu file = new JMenu("File");
-
- JMenuItem neww = new JMenuItem("New");
- file.add(neww);
- JMenuItem open = new JMenuItem("Open");
- file.add(open);
- JMenuItem saveas = new JMenuItem("Save as...");
- file.add(saveas);
- JMenuItem save = new JMenuItem("Save");
- file.add(save);
- JMenuItem exporttomidi = new JMenuItem("Export to MIDI...");
- file.add(exporttomidi);
- JMenuItem exit = new JMenuItem("Exit");
- file.add(exporttomidi);
- return file;
+ /**
+ * creates a menu for the menubar
+ * @param name the name of the menu
+ * @param mnemonic the shortcut to activate the menu
+ * @return menu the menu to be added to the menubar
+ */
+ private JMenu createMenu(String name, int mnemonic) {
+ JMenu menu = new JMenu(name);
+ menu.setMnemonic(mnemonic);
+ return menu;
}
- // creates the editmenu
- public JMenu createEditMenu()
- {
- JMenu edit = new JMenu("Edit");
-
- JMenuItem copy = new JMenuItem("Copy");
- edit.add(copy);
- JMenuItem cut = new JMenuItem("Cut");
- edit.add(cut);
- JMenuItem paste = new JMenuItem("Paste");
- edit.add(paste);
- JMenuItem selectall = new JMenuItem("Select All");
- edit.add(selectall);
- JMenuItem invertselection = new JMenuItem("Invert selection");
- edit.add(invertselection);
- return edit;
+ /**
+ * Creates a menu item.
+ * @param menu the menu to which the item is being added to
+ * @param name the name of this menuitem
+ * @return item the item to add to the menu
+ */
+ private JMenuItem addItem(JMenu menu, String name) {
+ JMenuItem item = new JMenuItem(name);
+ item.addActionListener(this);
+ menu.add(item);
+ return item;
}
-
- //creates the sequencemenu
- public JMenu createSequenceMenu()
- {
-
- JMenu sequence = new JMenu("Sequence");
- return sequence;
+
+ /**
+ * Creates a menu item with a keyboard accelerator.
+ * @param menu the menu to which the item is being added to
+ * @param name the name of this menuitem
+ * @param key the shortcut to activate the command
+ * @return item the item to add to the menu
+ */
+ private JMenuItem addItem(JMenu menu, String name, int key) {
+ JMenuItem item = new JMenuItem(name);
+ item.setAccelerator(KeyStroke.getKeyStroke(key, ActionEvent.CTRL_MASK));
+ item.addActionListener(this);
+ menu.add(item);
+ return item;
}
-
- // creates the trackmenu
- public JMenu createTrackMenu()
- {
- JMenu track = new JMenu("Track");
- return track;
+
+ /**
+ * creates a JDialog popupmenu, containing diffrent choices
+ * @param title the title of the dialog
+ * @return trackframe the JDialog....ffaaaaaaaaaaaaaaaan!
+ */
+ //private JDialog makeDialog(String title){
+ //
+ //}
+ /**
+ * checks if the fileformat is compatible with our program
+ * @param f the file to check
+ * @return true or false
+ */
+ private boolean isMidiFile(File f) {
+ if(f != null) {
+ String extension = f.getName().substring(f.getName().lastIndexOf('.') + 1).toLowerCase().trim();
+ if (extension.equals("mid")) return true;
+ }
+ return false;
}
- // creates the notemenu
- public JMenu createNoteMenu()
- {
- JMenu note = new JMenu("Note");
- return note;
- }
+ public void actionPerformed(ActionEvent e) {
+ String command = e.getActionCommand();
+ Sequence seq;
+
+ if(command == "New") {
+ Moosique.clearSequence();
+ } else if (command == "Open...") {
+ // Shows a file chooser. If shown previously, starts in the current directory.
+ if (directory != null) {
+ chooser = new JFileChooser(directory);
+ } else {
+ chooser = new JFileChooser();
+ }
+ chooser.addChoosableFileFilter(new MidiFileFilter());
+ int returnVal = chooser.showOpenDialog(this);
- // creates the aboutmenu
- public JMenu createHelpMenu()
- {
- JMenu help = new JMenu("Help");
-
- JMenuItem contents = new JMenuItem("Contents");
- help.add(contents);
- JMenuItem gettingstarted = new JMenuItem("Getting started");
- help.add(gettingstarted);
- JMenuItem about = new JMenuItem("About");
- help.add(about);
+ // Stores the current directory and loads the selected file.
+ File file = chooser.getSelectedFile();
+ if(returnVal == JFileChooser.APPROVE_OPTION && isMidiFile(file)) {
+ directory = chooser.getSelectedFile().getParentFile();
+ Moosique.load(chooser.getSelectedFile().getAbsolutePath());
+ }
+ } else if (command == "Save") {
+ Moosique.save();
+ } else if (command == "Save as...") {
+ // Shows a file chooser. If shown previously, starts in the current directory.
+ if (directory != null) {
+ chooser = new JFileChooser(directory);
+ } else {
+ chooser = new JFileChooser();
+ }
+ chooser.addChoosableFileFilter(new MidiFileFilter());
+ int returnVal = chooser.showSaveDialog(this);
- return help;
+ // Stores the current directory and loads the selected file.
+ File file = chooser.getSelectedFile();
+ if(returnVal == JFileChooser.APPROVE_OPTION && isMidiFile(file)) {
+ directory = file.getParentFile();
+ Moosique.saveAs(file.getAbsolutePath());
+ }
+ } else if (command == "Exit") {
+ Moosique.quit();
+ } else if (command == "Copy") {
+
+ } else if (command == "Cut") {
+
+ } else if (command == "Paste") {
+
+ } else if (command == "Select all") {
+
+ } else if (command == "Invert selection") {
+
+ } else if (command == "Preferences...") {
+
+ } else if (command == "Play") {
+ if (!Moosique.getSequencer().isRunning()) Moosique.play();
+ } else if (command == "Pause") {
+ if (Moosique.getSequencer().isRunning()) Moosique.pause();
+ } else if (command == "Stop") {
+ Moosique.stop();
+ } else if (command == "Jump...") {
+
+ } else if (command == "Add track...") {
+
+ MooDialog what = new MooDialog(MooDialog.ADD_TRACK);
+
+ Moosique.getSequence().createTrack();
+
+ } else if (command == "Delete track...") {
+
+ MooDialog what = new MooDialog(MooDialog.DELETE_TRACK);
+
+ /* Let the user select a track from a list.
+ seq = Moosique.getSequence();
+ seq.deleteTrack(seq.getTracks()[NUMBER]);
+ */
+ } else if (command == "Copy track...") {
+
+ MooDialog what = new MooDialog(MooDialog.COPY_TRACK);
+
+ } else if (command == "Move track...") {
+
+ MooDialog what = new MooDialog(MooDialog.MOVE_TRACK);
+
+ } else if (command == "Insert measure...") {
+
+ } else if (command == "Delete measure...") {
+
+ } else if (command == "Set time signature...") {
+
+ } else if (command == "Set tempo...") {
+
+ } else if (command == "Scale velocity...") {
+
+ } else if (command == "Transpose...") {
+
+ } else if (command == "Contents") {
+ // contents to be filled in
+ JOptionPane.showMessageDialog(this, "här kommer contents komma");
+
+ } else if (command == "Getting started") {
+ // getting started to be filled in
+ JOptionPane.showMessageDialog(null, "här kommer getting started komma");
+
+ } else if (command == "About") {
+ // about to be filled in
+ JOptionPane.showMessageDialog(null, "här kommer about att komma");
+ }
}
- /*******************************************************************
- public static void main(String[] args)
- {
- MooMenu menu = new MooMenu();
- JFrame frame = new JFrame();
- JPanel panel = new JPanel();
- panel.add(menu.createMooMenu());
- frame.setContentPane(panel);
- frame.pack();
- frame.show();
+
+ class MidiFileFilter extends javax.swing.filechooser.FileFilter {
+ public boolean accept(File f) {
+ if(f != null) {
+ if (f.isDirectory() || isMidiFile(f)) return true;
+ }
+ return false;
+ }
+
+ /*
+ * gets the description of the filetype
+ * @return "Midifiles the only filetyp compatibel with the program
+ */
+ public String getDescription() {
+ return "MIDI files";
+ }
}
- ****************************************************************/
+
+
}