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());
145 } else if (command == "Exit") {
147 } else if (command == "Copy") {
149 } else if (command == "Cut") {
151 } else if (command == "Paste") {
153 } else if (command == "Select all") {
155 } else if (command == "Invert selection") {
157 } else if (command == "Preferences...") {
159 } else if (command == "Play") {
160 if (Moosique.getSequencer().isRunning()) {
165 } else if (command == "Pause") {
166 if (Moosique.getSequencer().isRunning()) {
171 } else if (command == "Stop") {
173 } else if (command == "Jump...") {
175 } else if (command == "Add track...") {
176 Moosique.getSequence().createTrack();
177 } else if (command == "Delete track...") {
178 /* Let the user select a track from a list.
179 seq = Moosique.getSequence();
180 seq.deleteTrack(seq.getTracks()[NUMBER]);
182 } else if (command == "Copy track...") {
184 } else if (command == "Move track...") {
186 } else if (command == "Insert measure...") {
188 } else if (command == "Delete measure...") {
190 } else if (command == "Set time signature...") {
192 } else if (command == "Set tempo...") {
194 } else if (command == "Scale velocity...") {
196 } else if (command == "Transpose...") {
198 } else if (command == "Contents") {
199 // contents to be filled in
200 JOptionPane.showMessageDialog(this, "här kommer contents komma");
202 } else if (command == "Getting started") {
203 // getting started to be filled in
204 JOptionPane.showMessageDialog(null, "här kommer getting started komma");
206 } else if (command == "About") {
207 // about to be filled in
208 JOptionPane.showMessageDialog(null, "här kommer about att komma");
212 class MidiFileFilter extends javax.swing.filechooser.FileFilter {
213 public boolean accept(File f) {
215 if (f.isDirectory() || isMidiFile(f)) return true;
221 * gets the description of the filetype
222 * @return "Midifiles the only filetyp compatibel with the program
224 public String getDescription() {