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") {
164 if (!Moosique.save()) showSaveAsDialog();
165 } else if (command == "Save as...") {
167 } else if (command == "Exit") {
169 } else if (command == "Copy") {
171 } else if (command == "Cut") {
173 } else if (command == "Paste") {
175 } else if (command == "Select all") {
177 } else if (command == "Invert selection") {
179 } else if (command == "Preferences...") {
181 } else if (command == "Play") {
182 if (!Moosique.getSequencer().isRunning()) Moosique.play();
183 } else if (command == "Pause") {
184 if (Moosique.getSequencer().isRunning()) Moosique.pause();
185 } else if (command == "Stop") {
187 } else if (command == "Jump...") {
188 MooDialog newDialog = new MooDialog(MooDialog.JUMP);
189 // Moosique.setPosition(???); Räkna ut från msr, beats, ticks, time sig.
190 } else if (command == "Add track...") {
191 MooDialog newDialog = new MooDialog(MooDialog.ADD_TRACK);
192 Moosique.getSequence().createTrack();
193 } else if (command == "Delete track...") {
195 MooDialog newDialog = new MooDialog(MooDialog.DELETE_TRACK);
197 /* Let the user select a track from a list.
198 seq = Moosique.getSequence();
199 seq.deleteTrack(seq.getTracks()[NUMBER]);
201 } else if (command == "Copy track...") {
202 MooDialog newDialog = new MooDialog(MooDialog.COPY_TRACK);
203 } else if (command == "Move track...") {
204 MooDialog newDialog = new MooDialog(MooDialog.MOVE_TRACK);
205 } else if (command == "Insert measure...") {
206 MooDialog newDialog = new MooDialog(MooDialog.INSERT_MEASURE);
207 } else if (command == "Delete measure...") {
208 MooDialog newDialog = new MooDialog(MooDialog.DELETE_MEASURE);
209 } else if (command == "Set time signature...") {
211 } else if (command == "Set tempo...") {
213 } else if (command == "Scale velocity...") {
215 } else if (command == "Transpose...") {
217 } else if (command == "Contents") {
218 MooDialog contents = new MooDialog(MooDialog.CONTENTS);
219 } else if (command == "Getting started") {
221 JOptionPane.showMessageDialog(null, "här kommer getting started komma");
223 } else if (command == "About") {
225 JOptionPane.showMessageDialog(null, "här kommer about att komma");
229 private void showSaveAsDialog() {
230 // Shows a file chooser. If shown previously, starts in the current directory.
231 if (directory != null) {
232 chooser = new JFileChooser(directory);
234 chooser = new JFileChooser();
236 chooser.addChoosableFileFilter(new MidiFileFilter());
237 int returnVal = chooser.showSaveDialog(this);
239 // Stores the current directory and loads the selected file.
240 File file = chooser.getSelectedFile();
241 if(returnVal == JFileChooser.APPROVE_OPTION && isMidiFile(file)) {
242 directory = file.getParentFile();
243 Moosique.saveAs(file.getAbsolutePath());
247 class MidiFileFilter extends javax.swing.filechooser.FileFilter {
248 public boolean accept(File f) {
250 if (f.isDirectory() || isMidiFile(f)) return true;
256 * gets the description of the filetype
257 * @return "Midifiles the only filetyp compatibel with the program
259 public String getDescription() {