1 import javax.sound.midi.*;
3 import javax.swing.filechooser.*;
4 import java.awt.event.*;
9 * Moosiques GUI representing a menubar, menus and menuitems
11 * @author Björn Lanneskog
13 public class MooMenu extends JMenuBar implements ActionListener {
15 private JMenu file, edit, playback, music, help;
16 private JFileChooser chooser;
17 private File directory;
20 * Creates the menu bar.
23 file = createMenu("File", KeyEvent.VK_F);
26 addItem(file, "New", KeyEvent.VK_N, ActionEvent.CTRL_MASK);
27 addItem(file, "Open...", KeyEvent.VK_O, ActionEvent.CTRL_MASK);
28 addItem(file, "Save", KeyEvent.VK_S, ActionEvent.CTRL_MASK);
29 addItem(file, "Save as...");
30 addItem(file, "Exit", KeyEvent.VK_Q, ActionEvent.CTRL_MASK);
32 edit = createMenu("Edit", KeyEvent.VK_E);
35 addItem(edit, "Copy", KeyEvent.VK_C, ActionEvent.CTRL_MASK);
36 addItem(edit, "Cut", KeyEvent.VK_X, ActionEvent.CTRL_MASK);
37 addItem(edit, "Paste", KeyEvent.VK_V, ActionEvent.CTRL_MASK);
38 addItem(edit, "Select all", KeyEvent.VK_E, ActionEvent.CTRL_MASK);
39 addItem(edit, "Invert selection", KeyEvent.VK_I, ActionEvent.CTRL_MASK);
41 addItem(edit, "Preferences...", KeyEvent.VK_P, ActionEvent.CTRL_MASK);
43 playback = createMenu("Playback", KeyEvent.VK_P);
46 addItem(playback, "Play", "F5");
47 addItem(playback, "Pause", "F7");
48 addItem(playback, "Stop", "F6");
49 addItem(playback, "Jump...");
51 music = createMenu("Music", KeyEvent.VK_M);
54 addItem(music, "Add track...", KeyEvent.VK_A, ActionEvent.CTRL_MASK);
55 addItem(music, "Delete track...", KeyEvent.VK_D, ActionEvent.CTRL_MASK);
56 addItem(music, "Copy track...", KeyEvent.VK_Y, ActionEvent.CTRL_MASK);
57 addItem(music, "Move track...", KeyEvent.VK_M, ActionEvent.CTRL_MASK);
59 addItem(music, "Insert measure...");
60 addItem(music, "Delete measure...");
62 addItem(music, "Set time signature...");
63 addItem(music, "Set tempo...");
64 addItem(music, "Scale velocity...");
65 addItem(music, "Transpose...");
67 help = createMenu("Help", KeyEvent.VK_L);
70 addItem(help, "Contents", "F1");
71 addItem(help, "Getting started");
72 addItem(help, "About");
75 private JMenu createMenu(String name, int mnemonic) {
76 JMenu menu = new JMenu(name);
77 menu.setMnemonic(mnemonic);
81 private JMenuItem addItem(JMenu menu, String name) {
82 JMenuItem item = new JMenuItem(name);
83 item.addActionListener(this);
88 private JMenuItem addItem(JMenu menu, String name, String key) {
89 JMenuItem item = new JMenuItem(name);
90 item.setAccelerator(KeyStroke.getKeyStroke(key));
91 item.addActionListener(this);
96 private JMenuItem addItem(JMenu menu, String name, int key, int mask) {
97 JMenuItem item = new JMenuItem(name);
98 item.setAccelerator(KeyStroke.getKeyStroke(key, mask));
99 item.addActionListener(this);
104 private boolean isMidiFile(File f) {
106 String extension = f.getName().substring(f.getName().lastIndexOf('.') + 1).toLowerCase().trim();
107 if (extension.equals("mid")) return true;
112 public void actionPerformed(ActionEvent e) {
113 String command = e.getActionCommand();
116 if(command == "New") {
117 Moosique.clearSequence();
118 } else if (command == "Open...") {
119 // Shows a file chooser. If shown previously, starts in the current directory.
120 if (directory != null) {
121 chooser = new JFileChooser(directory);
123 chooser = new JFileChooser();
125 chooser.addChoosableFileFilter(new MidiFileFilter());
126 int returnVal = chooser.showOpenDialog(this);
128 // Stores the current directory and loads the selected file.
129 File file = chooser.getSelectedFile();
130 if(returnVal == JFileChooser.APPROVE_OPTION && isMidiFile(file)) {
131 directory = chooser.getSelectedFile().getParentFile();
132 Moosique.load(chooser.getSelectedFile().getAbsolutePath());
134 } else if (command == "Save") {
136 } else if (command == "Save as...") {
137 // Shows a file chooser. If shown previously, starts in the current directory.
138 if (directory != null) {
139 chooser = new JFileChooser(directory);
141 chooser = new JFileChooser();
143 chooser.addChoosableFileFilter(new MidiFileFilter());
144 int returnVal = chooser.showSaveDialog(this);
146 // Stores the current directory and loads the selected file.
147 File file = chooser.getSelectedFile();
148 if(returnVal == JFileChooser.APPROVE_OPTION && isMidiFile(file)) {
149 directory = file.getParentFile();
150 Moosique.saveAs(file.getAbsolutePath());
152 } else if (command == "Exit") {
154 } else if (command == "Copy") {
156 } else if (command == "Cut") {
158 } else if (command == "Paste") {
160 } else if (command == "Select all") {
162 } else if (command == "Invert selection") {
164 } else if (command == "Preferences...") {
166 } else if (command == "Play") {
167 if (!Moosique.getSequencer().isRunning()) Moosique.play();
168 } else if (command == "Pause") {
169 if (Moosique.getSequencer().isRunning()) Moosique.pause();
170 } else if (command == "Stop") {
172 } else if (command == "Jump...") {
173 MooDialog what = new MooDialog(MooDialog.JUMP);
174 // Moosique.setPosition(???); Räkna ut från msr, beats, ticks, time sig.
175 } else if (command == "Add track...") {
176 MooDialog what = new MooDialog(MooDialog.ADD_TRACK);
177 Moosique.getSequence().createTrack();
178 } else if (command == "Delete track...") {
180 MooDialog what = new MooDialog(MooDialog.DELETE_TRACK);
182 /* Let the user select a track from a list.
183 seq = Moosique.getSequence();
184 seq.deleteTrack(seq.getTracks()[NUMBER]);
186 } else if (command == "Copy track...") {
188 MooDialog what = new MooDialog(MooDialog.COPY_TRACK);
190 } else if (command == "Move track...") {
192 MooDialog what = new MooDialog(MooDialog.MOVE_TRACK);
194 } else if (command == "Insert measure...") {
196 } else if (command == "Delete measure...") {
198 } else if (command == "Set time signature...") {
200 } else if (command == "Set tempo...") {
202 } else if (command == "Scale velocity...") {
204 } else if (command == "Transpose...") {
206 } else if (command == "Contents") {
207 // contents to be filled in
208 JOptionPane.showMessageDialog(this, "här kommer contents komma");
210 } else if (command == "Getting started") {
211 // getting started to be filled in
212 JOptionPane.showMessageDialog(null, "här kommer getting started komma");
214 } else if (command == "About") {
215 // about to be filled in
216 JOptionPane.showMessageDialog(null, "här kommer about att komma");
220 class MidiFileFilter extends javax.swing.filechooser.FileFilter {
221 public boolean accept(File f) {
223 if (f.isDirectory() || isMidiFile(f)) return true;
229 * gets the description of the filetype
230 * @return "Midifiles the only filetyp compatibel with the program
232 public String getDescription() {