2 import javax.swing.filechooser.*;
3 import java.awt.event.*;
7 * Moosiques GUI representing a menubar, menus and menuitems
9 * @author Björn Lanneskog
11 public class MooMenu extends JMenuBar implements ActionListener {
13 private JMenu file, edit, playback, music, help;
16 * Creates the menu bar.
19 file = createMenu("File", KeyEvent.VK_F);
22 addItem(file, "New", KeyEvent.VK_N);
23 addItem(file, "Open...", KeyEvent.VK_O);
24 addItem(file, "Save", KeyEvent.VK_S);
25 addItem(file, "Save as...");
26 addItem(file, "Exit", KeyEvent.VK_Q);
28 edit = createMenu("Edit", KeyEvent.VK_E);
31 addItem(edit, "Copy", KeyEvent.VK_C);
32 addItem(edit, "Cut", KeyEvent.VK_X);
33 addItem(edit, "Paste", KeyEvent.VK_V);
34 addItem(edit, "Select all", KeyEvent.VK_E);
35 addItem(edit, "Invert selection", KeyEvent.VK_I);
36 addItem(edit, "Preferences...", KeyEvent.VK_P);
38 playback = createMenu("Playback", KeyEvent.VK_P);
41 addItem(playback, "Play", KeyEvent.VK_SPACE);
42 addItem(playback, "Pause");
43 addItem(playback, "Stop");
44 addItem(playback, "Jump...");
46 music = createMenu("Music", KeyEvent.VK_M);
49 addItem(music, "Add track...", KeyEvent.VK_A);
50 addItem(music, "Delete track...", KeyEvent.VK_D);
51 addItem(music, "Copy track...", KeyEvent.VK_Y);
52 addItem(music, "Move track...", KeyEvent.VK_M);
53 addItem(music, "Insert measure...");
54 addItem(music, "Delete measure...");
55 addItem(music, "Set time signature...");
56 addItem(music, "Set tempo...");
57 addItem(music, "Scale velocity...");
58 addItem(music, "Transpose...");
60 help = createMenu("Help", KeyEvent.VK_L);
63 addItem(help, "Contents");
64 addItem(help, "Getting started");
65 addItem(help, "About");
69 * creates a menu for the menubar
70 * @param name the name of the menu
71 * @param mnemonic the shortcut to activate the menu
72 * @return menu the menu to be added to the menubar
74 private JMenu createMenu(String name, int mnemonic) {
75 JMenu menu = new JMenu(name);
76 menu.setMnemonic(mnemonic);
81 * Creates a menu item.
82 * @param menu the menu to which the item is being added to
83 * @param name the name of this menuitem
84 * @return item the item to add to the menu
86 private JMenuItem addItem(JMenu menu, String name) {
87 JMenuItem item = new JMenuItem(name);
88 item.addActionListener(this);
94 * Creates a menu item with a keyboard accelerator.
95 * @param menu the menu to which the item is being added to
96 * @param name the name of this menuitem
97 * @param key the shortcut to activate the command
98 * @return item the item to add to the menu
100 private JMenuItem addItem(JMenu menu, String name, int key) {
101 JMenuItem item = new JMenuItem(name);
102 item.setAccelerator(KeyStroke.getKeyStroke(key, ActionEvent.CTRL_MASK));
103 item.addActionListener(this);
109 * checks if the fileformat is compatible with our program
110 * @param f the file to check
111 * @return true or false
113 private boolean isMidiFile(File f) {
115 String extension = f.getName().substring(f.getName().lastIndexOf('.') + 1).toLowerCase().trim();
116 if (extension.equals("mid")) return true;
121 public void actionPerformed(ActionEvent e) {
122 String command = e.getActionCommand();
124 if(command == "New") {
125 Moosique.clearSequence();
126 } else if (command == "Open...") {
127 JFileChooser chooser = new JFileChooser();
128 chooser.addChoosableFileFilter(new MidiFileFilter());
129 int returnVal = chooser.showOpenDialog(this);
130 if(returnVal == JFileChooser.APPROVE_OPTION && isMidiFile(chooser.getSelectedFile())) {
131 Moosique.load(chooser.getSelectedFile().getAbsolutePath());
133 } else if (command == "Save") {
135 } else if (command == "Save as...") {
136 JFileChooser chooser = new JFileChooser();
137 chooser.addChoosableFileFilter(new MidiFileFilter());
138 int returnVal = chooser.showSaveDialog(this);
139 if(returnVal == JFileChooser.APPROVE_OPTION && isMidiFile(chooser.getSelectedFile())) {
140 Moosique.saveAs(chooser.getSelectedFile().getAbsolutePath());
143 } else if (command == "Exit") {
145 } else if (command == "Copy") {
147 } else if (command == "Cut") {
149 } else if (command == "Paste") {
151 } else if (command == "Select all") {
153 } else if (command == "Invert selection") {
155 } else if (command == "Preferences...") {
157 } else if (command == "Play") {
158 if (Moosique.getSequencer().isRunning()) {
163 } else if (command == "Pause") {
164 if (Moosique.getSequencer().isRunning()) {
169 } else if (command == "Stop") {
171 } else if (command == "Jump...") {
173 } else if (command == "Add track...") {
175 } else if (command == "Delete track...") {
177 } else if (command == "Copy track...") {
179 } else if (command == "Move track...") {
181 } else if (command == "Insert measure...") {
183 } else if (command == "Delete measure...") {
185 } else if (command == "Set time signature...") {
187 } else if (command == "Set tempo...") {
189 } else if (command == "Scale velocity...") {
191 } else if (command == "Transpose...") {
193 } else if (command == "Contents") {
194 // contents to be filled in
195 JOptionPane.showMessageDialog(this, "här kommer contents komma");
197 } else if (command == "Getting started") {
198 // getting started to be filled in
199 JOptionPane.showMessageDialog(null, "här kommer getting started komma");
201 } else if (command == "About") {
202 // about to be filled in
203 JOptionPane.showMessageDialog(null, "här kommer about att komma");
207 class MidiFileFilter extends javax.swing.filechooser.FileFilter {
208 public boolean accept(File f) {
210 if (f.isDirectory() || isMidiFile(f)) return true;
216 * gets the description of the filetype
217 * @return "Midifiles the only filetyp compatibel with the program
219 public String getDescription() {