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, keyboard, playback, music, help;
16 private JFileChooser chooser;
17 private File directory;
18 private String[] openedFiles;
21 * Creates the menu bar.
24 file = createMenu("File", KeyEvent.VK_F);
27 addItem(file, "New", KeyEvent.VK_N, ActionEvent.CTRL_MASK);
28 addItem(file, "Open...", KeyEvent.VK_O, ActionEvent.CTRL_MASK);
29 addItem(file, "Save", KeyEvent.VK_S, ActionEvent.CTRL_MASK);
30 addItem(file, "Save as...", KeyEvent.VK_A);
32 addItem(file, "Exit", KeyEvent.VK_Q, ActionEvent.CTRL_MASK);
34 edit = createMenu("Edit", KeyEvent.VK_E);
37 addItem(edit, "Copy", KeyEvent.VK_C, ActionEvent.CTRL_MASK);
38 addItem(edit, "Cut", KeyEvent.VK_X, ActionEvent.CTRL_MASK);
39 addItem(edit, "Paste", KeyEvent.VK_V, ActionEvent.CTRL_MASK);
41 addItem(edit, "Select all", KeyEvent.VK_E, ActionEvent.CTRL_MASK);
42 addItem(edit, "Invert selection", KeyEvent.VK_I, ActionEvent.CTRL_MASK);
44 addItem(edit, "Preferences...", KeyEvent.VK_P, ActionEvent.CTRL_MASK);
46 playback = createMenu("Playback", KeyEvent.VK_P);
49 addItem(playback, "Play", "F5", KeyEvent.VK_P);
50 addItem(playback, "Pause", "F7", KeyEvent.VK_A);
51 addItem(playback, "Stop", "F6", KeyEvent.VK_S);
52 playback.addSeparator();
53 addItem(playback, "Set position...", KeyEvent.VK_E);
54 playback.addSeparator();
55 keyboard = createMenu("Set keyboard octave", KeyEvent.VK_K);
57 for (int i = 9; i >= 0; i--) addItem(keyboard, "Octave " + i, i + 48);
59 music = createMenu("Music", KeyEvent.VK_M);
62 addItem(music, "Add track...", KeyEvent.VK_A, ActionEvent.CTRL_MASK);
63 addItem(music, "Delete track...", KeyEvent.VK_D, ActionEvent.CTRL_MASK);
64 addItem(music, "Copy track...", KeyEvent.VK_Y, ActionEvent.CTRL_MASK);
65 addItem(music, "Move track...", KeyEvent.VK_M, ActionEvent.CTRL_MASK);
67 addItem(music, "Insert measure...", KeyEvent.VK_I);
68 addItem(music, "Delete measure...", KeyEvent.VK_E);
70 addItem(music, "Set time signature...", KeyEvent.VK_S);
71 addItem(music, "Set tempo...", KeyEvent.VK_M);
72 addItem(music, "Scale velocity...", KeyEvent.VK_V);
73 addItem(music, "Transpose...", KeyEvent.VK_T);
75 help = createMenu("Help", KeyEvent.VK_L);
78 addItem(help, "User manual", "F1", KeyEvent.VK_M);
80 addItem(help, "About", KeyEvent.VK_A);
83 * Creats a menu in the menubar.
84 * @param name The name of the menu.
85 * @param mnemnic The shortcut-key to access the menu.
86 * @return menu The menu created.
88 private JMenu createMenu(String name, int mnemonic) {
89 JMenu menu = new JMenu(name);
90 menu.setMnemonic(mnemonic);
95 * Creats a menuitem in the menu.
96 * @param menu The menu where to add the menuitem.
97 * @param name The name of the menuitem.
98 * @return item The menuitem created.
100 private JMenuItem addItem(JMenu menu, String name, int mnemonic) {
101 JMenuItem item = new JMenuItem(name);
102 item.addActionListener(this);
108 * Creats a menuitem in the menu.
109 * @param menu The menu to where to add the menuitem.
110 * @param name The name of the menuitem.
111 * @param key The keystroke to access this menuitem.
112 * @return item The menuitem created.
114 private JMenuItem addItem(JMenu menu, String name, String key, int mnemonic) {
115 JMenuItem item = new JMenuItem(name);
116 item.setAccelerator(KeyStroke.getKeyStroke(key));
117 item.setMnemonic(mnemonic);
118 item.addActionListener(this);
124 * Creats a menuitem in the menu.
125 * @param menu The menu to where to add the menuitem.
126 * @param name The name of the menuitem.
127 * @param key The keystroke to access this menuitem.
128 * @param mask The keyboard mask.
129 * @return item The menuitem created.
131 private JMenuItem addItem(JMenu menu, String name, int key, int mask) {
132 JMenuItem item = new JMenuItem(name);
133 item.setAccelerator(KeyStroke.getKeyStroke(key, mask));
134 item.setMnemonic(key);
135 item.addActionListener(this);
140 private boolean isMidiFile(File f) {
142 String extension = f.getName().substring(f.getName().lastIndexOf('.') + 1).toLowerCase().trim();
143 if (extension.equals("mid")) return true;
148 * Gets the users command of takes and properiate action
149 * @param e The action perfomed.
151 public void actionPerformed(ActionEvent e) {
152 String command = e.getActionCommand();
155 if(command == "New") {
156 if (!Moosique.promptOnUnsavedChanges()) Moosique.clearSequence();
157 } else if (command == "Open...") {
158 // Shows a file chooser. If shown previously, starts in the current directory.
159 if (directory != null) {
160 chooser = new JFileChooser(directory);
162 chooser = new JFileChooser();
164 chooser.addChoosableFileFilter(new MidiFileFilter());
165 int returnVal = chooser.showOpenDialog(this);
167 // Stores the current directory and loads the selected file.
168 File file = chooser.getSelectedFile();
169 if(returnVal == JFileChooser.APPROVE_OPTION && isMidiFile(file)) {
170 directory = chooser.getSelectedFile().getParentFile();
171 if (!Moosique.promptOnUnsavedChanges())
172 Moosique.load(chooser.getSelectedFile().getAbsolutePath());
174 } else if (command == "Save") {
175 if (!Moosique.save()) showSaveAsDialog();
176 } else if (command == "Save as...") {
178 } else if (command == "Exit") {
180 } else if (command == "Copy") {
182 } else if (command == "Cut") {
184 } else if (command == "Paste") {
186 } else if (command == "Select all") {
188 } else if (command == "Invert selection") {
190 } else if (command == "Preferences...") {
192 } else if (command == "Play") {
193 if (!Moosique.getSequencer().isRunning()) Moosique.play();
194 } else if (command == "Pause") {
195 if (Moosique.getSequencer().isRunning()) Moosique.pause();
196 } else if (command == "Stop") {
198 } else if (command == "Set position...") {
199 MooDialog newDialog = new MooDialog(MooDialog.SET_POSITION);
200 // Moosique.setPosition(???); Räkna ut från msr, beats, ticks, time sig.
201 } else if (command.startsWith("Octave")) {
202 MooKeyboard.setOctave(Integer.parseInt(command.substring(7,8)));
203 } else if (command == "Add track...") {
204 MooDialog newDialog = new MooDialog(MooDialog.ADD_TRACK);
205 Moosique.getSequence().createTrack();
206 } else if (command == "Delete track...") {
207 MooDialog newDialog = new MooDialog(MooDialog.DELETE_TRACK);
208 /* Let the user select a track from a list.
209 seq = Moosique.getSequence();
210 seq.deleteTrack(seq.getTracks()[NUMBER]);
212 } else if (command == "Copy track...") {
213 MooDialog newDialog = new MooDialog(MooDialog.COPY_TRACK);
214 } else if (command == "Move track...") {
215 MooDialog newDialog = new MooDialog(MooDialog.MOVE_TRACK);
216 } else if (command == "Insert measure...") {
217 MooDialog newDialog = new MooDialog(MooDialog.INSERT_MEASURE);
218 } else if (command == "Delete measure...") {
219 MooDialog newDialog = new MooDialog(MooDialog.DELETE_MEASURE);
220 } else if (command == "Set time signature...") {
222 } else if (command == "Set tempo...") {
223 MooDialog newDialog = new MooDialog(MooDialog.SET_TEMPO);
224 } else if (command == "Scale velocity...") {
225 MooDialog newDialog = new MooDialog(MooDialog.SCALE_VELOCITY);
226 } else if (command == "Transpose...") {
227 MooDialog newDialog = new MooDialog(MooDialog.TRANSPOSE);
228 } else if (command == "User manual") {
229 MooDialog manual = new MooDialog(MooDialog.MANUAL);
230 } else if (command == "About") {
231 JOptionPane.showMessageDialog(null,
232 "Moosique\nversion 1.0\n\n© 2003\nRoland Andersson\nMichael Andreen\nBjörn Lanneskog\nEinar Pehrson",
234 JOptionPane.INFORMATION_MESSAGE,
235 new ImageIcon(Moosique.getGUI().logo));
239 private void showSaveAsDialog() {
240 // Shows a file chooser. If shown previously, starts in the current directory.
241 if (directory != null) {
242 chooser = new JFileChooser(directory);
244 chooser = new JFileChooser();
246 chooser.addChoosableFileFilter(new MidiFileFilter());
247 int returnVal = chooser.showSaveDialog(this);
249 // Stores the current directory and loads the selected file.
250 File file = chooser.getSelectedFile();
251 if(returnVal == JFileChooser.APPROVE_OPTION && isMidiFile(file)) {
252 directory = file.getParentFile();
253 Moosique.saveAs(file.getAbsolutePath());
257 class MidiFileFilter extends javax.swing.filechooser.FileFilter {
258 public boolean accept(File f) {
260 if (f.isDirectory() || isMidiFile(f)) return true;
266 * gets the description of the filetype
267 * @return "Midifiles the only filetyp compatibel with the program
269 public String getDescription() {