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 {
15 Musikrelaterade menyer i Midisoft Recording Session:
17 Track Insert New... Har vi
23 Split by Pitch... Onödig
25 Music Insert Measure... Lagt till
26 Delete Measure... Lagt till
28 Time Signature... Lagt till
29 Key Signature... Onödig
31 Scale Velocity... Lagt till
32 Transpose... Lagt till
35 Kanske också: Reset Solo / Mute
38 private JMenu file, edit, playback, track, help, music;
44 file = makeMenu("File", KeyEvent.VK_F);
47 file.add(makeItem(file, "New", KeyEvent.VK_N));
48 file.add(makeItem(file, "Open...", KeyEvent.VK_O));
49 file.add(makeItem(file, "Save", KeyEvent.VK_S));
50 file.add(makeItem(file, "Save as..."));
51 file.add(makeItem(file, "Exit", KeyEvent.VK_Q));
53 edit = makeMenu("Edit", KeyEvent.VK_E);
56 edit.add(makeItem(edit, "Copy", KeyEvent.VK_C));
57 edit.add(makeItem(edit, "Cut", KeyEvent.VK_X));
58 edit.add(makeItem(edit, "Paste", KeyEvent.VK_V));
59 edit.add(makeItem(edit, "Select all", KeyEvent.VK_E));
60 edit.add(makeItem(edit, "Invert selection", KeyEvent.VK_I));
61 edit.add(makeItem(edit, "Preferences", KeyEvent.VK_P));
63 playback = makeMenu("Playback", KeyEvent.VK_P);
66 playback.add(makeItem(playback, "Play", KeyEvent.VK_SPACE));
67 playback.add(makeItem(playback, "Pause", KeyEvent.VK_SPACE));
68 playback.add(makeItem(playback, "Stop"));
69 playback.add(makeItem(playback, "Jump..."));
71 track = makeMenu("Track", KeyEvent.VK_T);
74 track.add(makeItem(track, "Add", KeyEvent.VK_A));
75 track.add(makeItem(track, "Delete", KeyEvent.VK_D));
76 track.add(makeItem(track, "Copy track", KeyEvent.VK_Y));
77 track.add(makeItem(track, "Move", KeyEvent.VK_M));
79 music = makeMenu("Music", KeyEvent.VK_M);
82 music.add(makeItem(track, "Insert measure..."));
83 music.add(makeItem(track, "Delete measure..."));
84 music.add(makeItem(track, "Time signature..."));
85 music.add(makeItem(track, "Tempo..."));
86 music.add(makeItem(track, "Scale velocity..."));
87 music.add(makeItem(track, "Transpose..."));
89 help = makeMenu("Help", KeyEvent.VK_L);
92 help.add(makeItem(help, "Contents"));
93 help.add(makeItem(help, "Getting started"));
94 help.add(makeItem(help, "About"));
98 * creates a menu for the menubar
99 * @param name the name of the menu
100 * @param mnemonic the shortcut to activate the menu
101 * @return menu the menu to be added to the menubar
103 private JMenu makeMenu(String name, int mnemonic) {
104 JMenu menu = new JMenu(name);
105 menu.setMnemonic(mnemonic);
111 * @param menu the menu to which the item is being added to
112 * @param name the name of this menuitem
113 * @return item the item to add to the menu
115 private JMenuItem makeItem(JMenu menu, String name) {
116 JMenuItem item = new JMenuItem(name);
117 item.addActionListener(this);
124 * @param menu the menu to which the item is being added to
125 * @param name the name of this menuitem
126 * @param key the shortcut to activate the command
127 * @return item the item to add to the menu
129 private JMenuItem makeItem(JMenu menu, String name, int key) {
130 JMenuItem item = new JMenuItem(name);
131 item.setAccelerator(KeyStroke.getKeyStroke(key, ActionEvent.CTRL_MASK));
132 item.addActionListener(this);
138 * checks if the fileformat is compatible with our program
139 * @param f the file to check
140 * @return true or false
142 private boolean isMidiFile(File f) {
144 String extension = f.getName().substring(f.getName().lastIndexOf('.') + 1).toLowerCase().trim();
145 if (extension.equals("mid")) return true;
150 public void actionPerformed(ActionEvent e) {
151 String command = e.getActionCommand();
153 if(command == "New") {
154 Moosique.clearSequence();
155 } else if (command == "Open...") {
156 JFileChooser chooser = new JFileChooser();
157 chooser.addChoosableFileFilter(new MidiFileFilter());
158 int returnVal = chooser.showOpenDialog(this);
159 if(returnVal == JFileChooser.APPROVE_OPTION && isMidiFile(chooser.getSelectedFile())) {
160 Moosique.load(chooser.getSelectedFile().getAbsolutePath());
162 } else if (command == "Save") {
164 } else if (command == "Save as...") {
165 JFileChooser chooser = new JFileChooser();
166 chooser.addChoosableFileFilter(new MidiFileFilter());
167 int returnVal = chooser.showSaveDialog(this);
168 if(returnVal == JFileChooser.APPROVE_OPTION && isMidiFile(chooser.getSelectedFile())) {
169 Moosique.saveAs(chooser.getSelectedFile().getAbsolutePath());
172 } else if (command == "Exit") {
174 } else if (command == "Copy") {
176 } else if (command == "Cut") {
178 } else if (command == "Paste") {
180 } else if (command == "Select all") {
182 } else if (command == "Invert selection") {
184 } else if (command == "Preferences") {
186 } else if (command == "Play / Resume") {
187 //if (Moosique.getSequence().isRunning()) {
189 //} else Moosique.play();
191 } else if (command == "Pause") {
192 // koda för resume också
194 } else if (command == "Stop") {
196 } else if (command == "Jump...") {
198 } else if (command == "Add") {
200 } else if (command == "Delete") {
202 } else if (command == "Copy track") {
204 } else if (command == "Move") {
206 } else if (command == "Insert Measure...") {
208 } else if (command == "Delete Measure...") {
210 } else if (command == "Time Signature...") {
212 } else if (command == "Tempo...") {
214 } else if (command == "Scale Velocity...") {
216 } else if (command == "Transpose...") {
218 } else if (command == "Contents") {
219 // contents to be filled in
220 JOptionPane.showMessageDialog(this, "här kommer contents komma");
222 } else if (command == "Getting started") {
223 // getting started to be filled in
224 JOptionPane.showMessageDialog(null, "här kommer getting started komma");
226 } else if (command == "About") {
227 // about to be filled in
228 JOptionPane.showMessageDialog(null, "här kommer about att komma");
232 class MidiFileFilter extends javax.swing.filechooser.FileFilter {
233 public boolean accept(File f) {
235 if (f.isDirectory() || isMidiFile(f)) return true;
241 * gets the description of the filetype
242 * @return "Midifiles the only filetyp compatibel with the program
244 public String getDescription() {