1 import javax.sound.midi.*;
3 import javax.swing.filechooser.*;
4 import java.awt.event.*;
8 * Moosiques GUI representing a menubar, menus and menuitems
10 * @author Björn Lanneskog
12 public class MooMenu extends JMenuBar implements ActionListener {
14 private JMenu file, edit, playback, music, help;
17 * Creates the menu bar.
20 file = createMenu("File", KeyEvent.VK_F);
23 addItem(file, "New", KeyEvent.VK_N);
24 addItem(file, "Open...", KeyEvent.VK_O);
25 addItem(file, "Save", KeyEvent.VK_S);
26 addItem(file, "Save as...");
27 addItem(file, "Exit", KeyEvent.VK_Q);
29 edit = createMenu("Edit", KeyEvent.VK_E);
32 addItem(edit, "Copy", KeyEvent.VK_C);
33 addItem(edit, "Cut", KeyEvent.VK_X);
34 addItem(edit, "Paste", KeyEvent.VK_V);
35 addItem(edit, "Select all", KeyEvent.VK_E);
36 addItem(edit, "Invert selection", KeyEvent.VK_I);
37 addItem(edit, "Preferences...", KeyEvent.VK_P);
39 playback = createMenu("Playback", KeyEvent.VK_P);
42 addItem(playback, "Play", KeyEvent.VK_SPACE);
43 addItem(playback, "Pause");
44 addItem(playback, "Stop");
45 addItem(playback, "Jump...");
47 music = createMenu("Music", KeyEvent.VK_M);
50 addItem(music, "Add track...", KeyEvent.VK_A);
51 addItem(music, "Delete track...", KeyEvent.VK_D);
52 addItem(music, "Copy track...", KeyEvent.VK_Y);
53 addItem(music, "Move track...", KeyEvent.VK_M);
54 addItem(music, "Insert measure...");
55 addItem(music, "Delete measure...");
56 addItem(music, "Set time signature...");
57 addItem(music, "Set tempo...");
58 addItem(music, "Scale velocity...");
59 addItem(music, "Transpose...");
61 help = createMenu("Help", KeyEvent.VK_L);
64 addItem(help, "Contents");
65 addItem(help, "Getting started");
66 addItem(help, "About");
70 * creates a menu for the menubar
71 * @param name the name of the menu
72 * @param mnemonic the shortcut to activate the menu
73 * @return menu the menu to be added to the menubar
75 private JMenu createMenu(String name, int mnemonic) {
76 JMenu menu = new JMenu(name);
77 menu.setMnemonic(mnemonic);
82 * Creates a menu item.
83 * @param menu the menu to which the item is being added to
84 * @param name the name of this menuitem
85 * @return item the item to add to the menu
87 private JMenuItem addItem(JMenu menu, String name) {
88 JMenuItem item = new JMenuItem(name);
89 item.addActionListener(this);
95 * Creates a menu item with a keyboard accelerator.
96 * @param menu the menu to which the item is being added to
97 * @param name the name of this menuitem
98 * @param key the shortcut to activate the command
99 * @return item the item to add to the menu
101 private JMenuItem addItem(JMenu menu, String name, int key) {
102 JMenuItem item = new JMenuItem(name);
103 item.setAccelerator(KeyStroke.getKeyStroke(key, ActionEvent.CTRL_MASK));
104 item.addActionListener(this);
110 * checks if the fileformat is compatible with our program
111 * @param f the file to check
112 * @return true or false
114 private boolean isMidiFile(File f) {
116 String extension = f.getName().substring(f.getName().lastIndexOf('.') + 1).toLowerCase().trim();
117 if (extension.equals("mid")) return true;
122 public void actionPerformed(ActionEvent e) {
123 String command = e.getActionCommand();
126 if(command == "New") {
127 Moosique.clearSequence();
128 } else if (command == "Open...") {
129 JFileChooser chooser = new JFileChooser();
130 chooser.addChoosableFileFilter(new MidiFileFilter());
131 int returnVal = chooser.showOpenDialog(this);
132 if(returnVal == JFileChooser.APPROVE_OPTION && isMidiFile(chooser.getSelectedFile())) {
133 Moosique.load(chooser.getSelectedFile().getAbsolutePath());
135 } else if (command == "Save") {
137 } else if (command == "Save as...") {
138 JFileChooser chooser = new JFileChooser();
139 chooser.addChoosableFileFilter(new MidiFileFilter());
140 int returnVal = chooser.showSaveDialog(this);
141 if(returnVal == JFileChooser.APPROVE_OPTION && isMidiFile(chooser.getSelectedFile())) {
142 Moosique.saveAs(chooser.getSelectedFile().getAbsolutePath());
144 } else if (command == "Exit") {
146 } else if (command == "Copy") {
148 } else if (command == "Cut") {
150 } else if (command == "Paste") {
152 } else if (command == "Select all") {
154 } else if (command == "Invert selection") {
156 } else if (command == "Preferences...") {
158 } else if (command == "Play") {
159 if (!Moosique.getSequencer().isRunning()) Moosique.play();
160 } else if (command == "Pause") {
161 if (Moosique.getSequencer().isRunning()) Moosique.pause();
162 } else if (command == "Stop") {
164 } else if (command == "Jump...") {
166 } else if (command == "Add track...") {
167 Moosique.getSequence().createTrack();
168 } else if (command == "Delete track...") {
169 /* Let the user select a track from a list.
170 seq = Moosique.getSequence();
171 seq.deleteTrack(seq.getTracks()[NUMBER]);
173 } else if (command == "Copy track...") {
175 } else if (command == "Move track...") {
177 } else if (command == "Insert measure...") {
179 } else if (command == "Delete measure...") {
181 } else if (command == "Set time signature...") {
183 } else if (command == "Set tempo...") {
185 } else if (command == "Scale velocity...") {
187 } else if (command == "Transpose...") {
189 } else if (command == "Contents") {
190 // contents to be filled in
191 JOptionPane.showMessageDialog(this, "här kommer contents komma");
193 } else if (command == "Getting started") {
194 // getting started to be filled in
195 JOptionPane.showMessageDialog(null, "här kommer getting started komma");
197 } else if (command == "About") {
198 // about to be filled in
199 JOptionPane.showMessageDialog(null, "här kommer about att komma");
203 class MidiFileFilter extends javax.swing.filechooser.FileFilter {
204 public boolean accept(File f) {
206 if (f.isDirectory() || isMidiFile(f)) return true;
212 * gets the description of the filetype
213 * @return "Midifiles the only filetyp compatibel with the program
215 public String getDescription() {