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 * Creats a menu in the menubar.
76 * @param name The name of the menu.
77 * @param mnemnic The shortcut-key to access the menu.
78 * @return menu The menu created.
80 private JMenu createMenu(String name, int mnemonic) {
81 JMenu menu = new JMenu(name);
82 menu.setMnemonic(mnemonic);
87 * Creats a menuitem in the menu.
88 * @param menu The menu where to add the menuitem.
89 * @param name The name of the menuitem.
90 * @return item The menuitem created.
92 private JMenuItem addItem(JMenu menu, String name) {
93 JMenuItem item = new JMenuItem(name);
94 item.addActionListener(this);
100 * Creats a menuitem in the menu.
101 * @param menu The menu to where to add the menuitem.
102 * @param name The name of the menuitem.
103 * @param key The keystroke to access this menuitem.
104 * @return item The menuitem created.
106 private JMenuItem addItem(JMenu menu, String name, String key) {
107 JMenuItem item = new JMenuItem(name);
108 item.setAccelerator(KeyStroke.getKeyStroke(key));
109 item.addActionListener(this);
115 * Creats a menuitem in the menu.
116 * @param menu The menu to where to add the menuitem.
117 * @param name The name of the menuitem.
118 * @param key The keystroke to access this menuitem.
119 * @param mask The keyboard mask.
120 * @return item The menuitem created.
122 private JMenuItem addItem(JMenu menu, String name, int key, int mask) {
123 JMenuItem item = new JMenuItem(name);
124 item.setAccelerator(KeyStroke.getKeyStroke(key, mask));
125 item.addActionListener(this);
130 private boolean isMidiFile(File f) {
132 String extension = f.getName().substring(f.getName().lastIndexOf('.') + 1).toLowerCase().trim();
133 if (extension.equals("mid")) return true;
138 * Gets the users command of takes and properiate action
139 * @param e the action perfomed
141 public void actionPerformed(ActionEvent e) {
142 String command = e.getActionCommand();
145 if(command == "New") {
146 Moosique.clearSequence();
147 } else if (command == "Open...") {
148 // Shows a file chooser. If shown previously, starts in the current directory.
149 if (directory != null) {
150 chooser = new JFileChooser(directory);
152 chooser = new JFileChooser();
154 chooser.addChoosableFileFilter(new MidiFileFilter());
155 int returnVal = chooser.showOpenDialog(this);
157 // Stores the current directory and loads the selected file.
158 File file = chooser.getSelectedFile();
159 if(returnVal == JFileChooser.APPROVE_OPTION && isMidiFile(file)) {
160 directory = chooser.getSelectedFile().getParentFile();
161 Moosique.load(chooser.getSelectedFile().getAbsolutePath());
163 } else if (command == "Save") {
165 } else if (command == "Save as...") {
166 // Shows a file chooser. If shown previously, starts in the current directory.
167 if (directory != null) {
168 chooser = new JFileChooser(directory);
170 chooser = new JFileChooser();
172 chooser.addChoosableFileFilter(new MidiFileFilter());
173 int returnVal = chooser.showSaveDialog(this);
175 // Stores the current directory and loads the selected file.
176 File file = chooser.getSelectedFile();
177 if(returnVal == JFileChooser.APPROVE_OPTION && isMidiFile(file)) {
178 directory = file.getParentFile();
179 Moosique.saveAs(file.getAbsolutePath());
181 } else if (command == "Exit") {
183 } else if (command == "Copy") {
185 } else if (command == "Cut") {
187 } else if (command == "Paste") {
189 } else if (command == "Select all") {
191 } else if (command == "Invert selection") {
193 } else if (command == "Preferences...") {
195 } else if (command == "Play") {
196 if (!Moosique.getSequencer().isRunning()) Moosique.play();
197 } else if (command == "Pause") {
198 if (Moosique.getSequencer().isRunning()) Moosique.pause();
199 } else if (command == "Stop") {
201 } else if (command == "Jump...") {
202 MooDialog what = new MooDialog(MooDialog.JUMP);
203 // Moosique.setPosition(???); Räkna ut från msr, beats, ticks, time sig.
204 } else if (command == "Add track...") {
205 MooDialog what = new MooDialog(MooDialog.ADD_TRACK);
206 Moosique.getSequence().createTrack();
207 } else if (command == "Delete track...") {
209 MooDialog what = new MooDialog(MooDialog.DELETE_TRACK);
211 /* Let the user select a track from a list.
212 seq = Moosique.getSequence();
213 seq.deleteTrack(seq.getTracks()[NUMBER]);
215 } else if (command == "Copy track...") {
217 MooDialog what = new MooDialog(MooDialog.COPY_TRACK);
219 } else if (command == "Move track...") {
221 MooDialog what = new MooDialog(MooDialog.MOVE_TRACK);
223 } else if (command == "Insert measure...") {
225 } else if (command == "Delete measure...") {
227 } else if (command == "Set time signature...") {
229 } else if (command == "Set tempo...") {
231 } else if (command == "Scale velocity...") {
233 } else if (command == "Transpose...") {
235 } else if (command == "Contents") {
237 JOptionPane.showMessageDialog(this, "här kommer contents komma");
239 } else if (command == "Getting started") {
241 JOptionPane.showMessageDialog(null, "här kommer getting started komma");
243 } else if (command == "About") {
245 JOptionPane.showMessageDialog(null, "här kommer about att komma");
249 class MidiFileFilter extends javax.swing.filechooser.FileFilter {
250 public boolean accept(File f) {
252 if (f.isDirectory() || isMidiFile(f)) return true;
258 * gets the description of the filetype
259 * @return "Midifiles the only filetyp compatibel with the program
261 public String getDescription() {