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;
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...", KeyEvent.VK_A);
31 addItem(file, "Exit", KeyEvent.VK_Q, ActionEvent.CTRL_MASK);
33 edit = createMenu("Edit", KeyEvent.VK_E);
36 addItem(edit, "Copy", KeyEvent.VK_C, ActionEvent.CTRL_MASK);
37 addItem(edit, "Cut", KeyEvent.VK_X, ActionEvent.CTRL_MASK);
38 addItem(edit, "Paste", KeyEvent.VK_V, ActionEvent.CTRL_MASK);
40 addItem(edit, "Select all", KeyEvent.VK_E, ActionEvent.CTRL_MASK);
41 addItem(edit, "Invert selection", KeyEvent.VK_I, ActionEvent.CTRL_MASK);
43 addItem(edit, "Preferences...", KeyEvent.VK_P, ActionEvent.CTRL_MASK);
45 playback = createMenu("Playback", KeyEvent.VK_P);
48 addItem(playback, "Play", "F5", KeyEvent.VK_P);
49 addItem(playback, "Pause", "F7", KeyEvent.VK_A);
50 addItem(playback, "Stop", "F6", KeyEvent.VK_S);
51 playback.addSeparator();
52 addItem(playback, "Set position...", KeyEvent.VK_E);
53 playback.addSeparator();
54 keyboard = createMenu("Set keyboard octave", KeyEvent.VK_K);
56 for (int i = 9; i >= 0; i--) addItem(keyboard, "Octave " + i, i + 48);
58 music = createMenu("Music", KeyEvent.VK_M);
61 addItem(music, "Add track...", KeyEvent.VK_A, ActionEvent.CTRL_MASK);
62 addItem(music, "Delete track...", KeyEvent.VK_D, ActionEvent.CTRL_MASK);
63 addItem(music, "Copy track...", KeyEvent.VK_Y, ActionEvent.CTRL_MASK);
64 addItem(music, "Move track...", KeyEvent.VK_M, ActionEvent.CTRL_MASK);
66 addItem(music, "Insert measure...", KeyEvent.VK_I);
67 addItem(music, "Delete measure...", KeyEvent.VK_E);
69 addItem(music, "Set time signature...", KeyEvent.VK_S);
70 addItem(music, "Set tempo...", KeyEvent.VK_M);
71 addItem(music, "Scale velocity...", KeyEvent.VK_V);
72 addItem(music, "Transpose...", KeyEvent.VK_T);
74 help = createMenu("Help", KeyEvent.VK_L);
77 addItem(help, "User manual", "F1", KeyEvent.VK_M);
79 addItem(help, "About", KeyEvent.VK_A);
82 * Creats a menu in the menubar.
83 * @param name The name of the menu.
84 * @param mnemnic The shortcut-key to access the menu.
85 * @return menu The menu created.
87 private JMenu createMenu(String name, int mnemonic) {
88 JMenu menu = new JMenu(name);
89 menu.setMnemonic(mnemonic);
94 * Creats a menuitem in the menu.
95 * @param menu The menu where to add the menuitem.
96 * @param name The name of the menuitem.
97 * @return item The menuitem created.
99 private JMenuItem addItem(JMenu menu, String name, int mnemonic) {
100 JMenuItem item = new JMenuItem(name);
101 item.addActionListener(this);
107 * Creats a menuitem in the menu.
108 * @param menu The menu to where to add the menuitem.
109 * @param name The name of the menuitem.
110 * @param key The keystroke to access this menuitem.
111 * @return item The menuitem created.
113 private JMenuItem addItem(JMenu menu, String name, String key, int mnemonic) {
114 JMenuItem item = new JMenuItem(name);
115 item.setAccelerator(KeyStroke.getKeyStroke(key));
116 item.setMnemonic(mnemonic);
117 item.addActionListener(this);
123 * Creats a menuitem in the menu.
124 * @param menu The menu to where to add the menuitem.
125 * @param name The name of the menuitem.
126 * @param key The keystroke to access this menuitem.
127 * @param mask The keyboard mask.
128 * @return item The menuitem created.
130 private JMenuItem addItem(JMenu menu, String name, int key, int mask) {
131 JMenuItem item = new JMenuItem(name);
132 item.setAccelerator(KeyStroke.getKeyStroke(key, mask));
133 item.setMnemonic(key);
134 item.addActionListener(this);
139 private boolean isMidiFile(File f) {
141 String extension = f.getName().substring(f.getName().lastIndexOf('.') + 1).toLowerCase().trim();
142 if (extension.equals("mid")) return true;
147 * Gets the users command of takes and properiate action
148 * @param e The action perfomed.
150 public void actionPerformed(ActionEvent e) {
151 String command = e.getActionCommand();
154 if(command == "New") {
155 if (!Moosique.promptOnUnsavedChanges()) Moosique.clearSequence();
156 } else if (command == "Open...") {
157 // Shows a file chooser. If shown previously, starts in the current directory.
158 if (directory != null) {
159 chooser = new JFileChooser(directory);
161 chooser = new JFileChooser();
163 chooser.addChoosableFileFilter(new MidiFileFilter());
164 int returnVal = chooser.showOpenDialog(this);
166 // Stores the current directory and loads the selected file.
167 File file = chooser.getSelectedFile();
168 if(returnVal == JFileChooser.APPROVE_OPTION && isMidiFile(file)) {
169 directory = chooser.getSelectedFile().getParentFile();
170 if (!Moosique.promptOnUnsavedChanges())
171 Moosique.load(chooser.getSelectedFile().getAbsolutePath());
173 } else if (command == "Save") {
174 if (!Moosique.save()) showSaveAsDialog();
175 } else if (command == "Save as...") {
177 } else if (command == "Exit") {
179 } else if (command == "Copy") {
181 } else if (command == "Cut") {
183 } else if (command == "Paste") {
185 } else if (command == "Select all") {
187 } else if (command == "Invert selection") {
189 } else if (command == "Preferences...") {
191 } else if (command == "Play") {
192 if (!Moosique.getSequencer().isRunning()) Moosique.play();
193 } else if (command == "Pause") {
194 if (Moosique.getSequencer().isRunning()) Moosique.pause();
195 } else if (command == "Stop") {
197 } else if (command == "Set position...") {
198 MooDialog newDialog = new MooDialog(MooDialog.SET_POSITION);
199 // Moosique.setPosition(???); Räkna ut från msr, beats, ticks, time sig.
200 } else if (command.startsWith("Octave")) {
201 MooKeyboard.setOctave(Integer.parseInt(command.substring(7,8)));
202 } else if (command == "Add track...") {
203 MooDialog newDialog = new MooDialog(MooDialog.ADD_TRACK);
204 Moosique.getSequence().createTrack();
205 } else if (command == "Delete track...") {
207 MooDialog newDialog = new MooDialog(MooDialog.DELETE_TRACK);
209 /* Let the user select a track from a list.
210 seq = Moosique.getSequence();
211 seq.deleteTrack(seq.getTracks()[NUMBER]);
213 } else if (command == "Copy track...") {
214 MooDialog newDialog = new MooDialog(MooDialog.COPY_TRACK);
215 } else if (command == "Move track...") {
216 MooDialog newDialog = new MooDialog(MooDialog.MOVE_TRACK);
217 } else if (command == "Insert measure...") {
218 MooDialog newDialog = new MooDialog(MooDialog.INSERT_MEASURE);
219 } else if (command == "Delete measure...") {
220 MooDialog newDialog = new MooDialog(MooDialog.DELETE_MEASURE);
221 } else if (command == "Set time signature...") {
223 } else if (command == "Set tempo...") {
224 MooDialog newDialog = new MooDialog(MooDialog.SET_TEMPO);
226 } else if (command == "Scale velocity...") {
228 } else if (command == "Transpose...") {
230 } else if (command == "User manual") {
231 MooDialog manual = new MooDialog(MooDialog.MANUAL);
232 } else if (command == "About") {
233 JOptionPane.showMessageDialog(null,
234 "Moosique\nversion 1.0\n\nby\n\nRoland Andersson\nMichael Andreen\nBjörn Lanneskog\nEinar Pehrson",
236 JOptionPane.INFORMATION_MESSAGE,
237 new ImageIcon(Moosique.getGUI().logo));
241 private void showSaveAsDialog() {
242 // Shows a file chooser. If shown previously, starts in the current directory.
243 if (directory != null) {
244 chooser = new JFileChooser(directory);
246 chooser = new JFileChooser();
248 chooser.addChoosableFileFilter(new MidiFileFilter());
249 int returnVal = chooser.showSaveDialog(this);
251 // Stores the current directory and loads the selected file.
252 File file = chooser.getSelectedFile();
253 if(returnVal == JFileChooser.APPROVE_OPTION && isMidiFile(file)) {
254 directory = file.getParentFile();
255 Moosique.saveAs(file.getAbsolutePath());
259 class MidiFileFilter extends javax.swing.filechooser.FileFilter {
260 public boolean accept(File f) {
262 if (f.isDirectory() || isMidiFile(f)) return true;
268 * gets the description of the filetype
269 * @return "Midifiles the only filetyp compatibel with the program
271 public String getDescription() {