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);
40 addItem(edit, "Preferences...", KeyEvent.VK_P, ActionEvent.CTRL_MASK);
42 playback = createMenu("Playback", KeyEvent.VK_P);
45 addItem(playback, "Play", "F5");
46 addItem(playback, "Pause", "F7");
47 addItem(playback, "Stop", "F6");
48 addItem(playback, "Jump...");
50 music = createMenu("Music", KeyEvent.VK_M);
53 addItem(music, "Add track...", KeyEvent.VK_A, ActionEvent.CTRL_MASK);
54 addItem(music, "Delete track...", KeyEvent.VK_D, ActionEvent.CTRL_MASK);
55 addItem(music, "Copy track...", KeyEvent.VK_Y, ActionEvent.CTRL_MASK);
56 addItem(music, "Move track...", KeyEvent.VK_M, ActionEvent.CTRL_MASK);
57 addItem(music, "Insert measure...");
58 addItem(music, "Delete measure...");
59 addItem(music, "Set time signature...");
60 addItem(music, "Set tempo...");
61 addItem(music, "Scale velocity...");
62 addItem(music, "Transpose...");
64 help = createMenu("Help", KeyEvent.VK_L);
67 addItem(help, "Contents", "F1");
68 addItem(help, "Getting started");
69 addItem(help, "About");
72 private JMenu createMenu(String name, int mnemonic) {
73 JMenu menu = new JMenu(name);
74 menu.setMnemonic(mnemonic);
78 private JMenuItem addItem(JMenu menu, String name) {
79 JMenuItem item = new JMenuItem(name);
80 item.addActionListener(this);
85 private JMenuItem addItem(JMenu menu, String name, String key) {
86 JMenuItem item = new JMenuItem(name);
87 item.setAccelerator(KeyStroke.getKeyStroke(key));
88 item.addActionListener(this);
93 private JMenuItem addItem(JMenu menu, String name, int key, int mask) {
94 JMenuItem item = new JMenuItem(name);
95 item.setAccelerator(KeyStroke.getKeyStroke(key, mask));
96 item.addActionListener(this);
101 private boolean isMidiFile(File f) {
103 String extension = f.getName().substring(f.getName().lastIndexOf('.') + 1).toLowerCase().trim();
104 if (extension.equals("mid")) return true;
109 public void actionPerformed(ActionEvent e) {
110 String command = e.getActionCommand();
113 if(command == "New") {
114 Moosique.clearSequence();
115 } else if (command == "Open...") {
116 // Shows a file chooser. If shown previously, starts in the current directory.
117 if (directory != null) {
118 chooser = new JFileChooser(directory);
120 chooser = new JFileChooser();
122 chooser.addChoosableFileFilter(new MidiFileFilter());
123 int returnVal = chooser.showOpenDialog(this);
125 // Stores the current directory and loads the selected file.
126 File file = chooser.getSelectedFile();
127 if(returnVal == JFileChooser.APPROVE_OPTION && isMidiFile(file)) {
128 directory = chooser.getSelectedFile().getParentFile();
129 Moosique.load(chooser.getSelectedFile().getAbsolutePath());
131 } else if (command == "Save") {
133 } else if (command == "Save as...") {
134 // Shows a file chooser. If shown previously, starts in the current directory.
135 if (directory != null) {
136 chooser = new JFileChooser(directory);
138 chooser = new JFileChooser();
140 chooser.addChoosableFileFilter(new MidiFileFilter());
141 int returnVal = chooser.showSaveDialog(this);
143 // Stores the current directory and loads the selected file.
144 File file = chooser.getSelectedFile();
145 if(returnVal == JFileChooser.APPROVE_OPTION && isMidiFile(file)) {
146 directory = file.getParentFile();
147 Moosique.saveAs(file.getAbsolutePath());
149 } else if (command == "Exit") {
151 } else if (command == "Copy") {
153 } else if (command == "Cut") {
155 } else if (command == "Paste") {
157 } else if (command == "Select all") {
159 } else if (command == "Invert selection") {
161 } else if (command == "Preferences...") {
163 } else if (command == "Play") {
164 if (!Moosique.getSequencer().isRunning()) Moosique.play();
165 } else if (command == "Pause") {
166 if (Moosique.getSequencer().isRunning()) Moosique.pause();
167 } else if (command == "Stop") {
169 } else if (command == "Jump...") {
171 } else if (command == "Add track...") {
173 MooDialog what = new MooDialog(MooDialog.ADD_TRACK);
175 Moosique.getSequence().createTrack();
177 } else if (command == "Delete track...") {
179 MooDialog what = new MooDialog(MooDialog.DELETE_TRACK);
181 /* Let the user select a track from a list.
182 seq = Moosique.getSequence();
183 seq.deleteTrack(seq.getTracks()[NUMBER]);
185 } else if (command == "Copy track...") {
187 MooDialog what = new MooDialog(MooDialog.COPY_TRACK);
189 } else if (command == "Move track...") {
191 MooDialog what = new MooDialog(MooDialog.MOVE_TRACK);
193 } else if (command == "Insert measure...") {
195 } else if (command == "Delete measure...") {
197 } else if (command == "Set time signature...") {
199 } else if (command == "Set tempo...") {
201 } else if (command == "Scale velocity...") {
203 } else if (command == "Transpose...") {
205 } else if (command == "Contents") {
206 // contents to be filled in
207 JOptionPane.showMessageDialog(this, "här kommer contents komma");
209 } else if (command == "Getting started") {
210 // getting started to be filled in
211 JOptionPane.showMessageDialog(null, "här kommer getting started komma");
213 } else if (command == "About") {
214 // about to be filled in
215 JOptionPane.showMessageDialog(null, "här kommer about att komma");
219 class MidiFileFilter extends javax.swing.filechooser.FileFilter {
220 public boolean accept(File f) {
222 if (f.isDirectory() || isMidiFile(f)) return true;
228 * gets the description of the filetype
229 * @return "Midifiles the only filetyp compatibel with the program
231 public String getDescription() {