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);
27 addItem(file, "Open...", KeyEvent.VK_O);
28 addItem(file, "Save", KeyEvent.VK_S);
29 addItem(file, "Save as...");
30 addItem(file, "Exit", KeyEvent.VK_Q);
32 edit = createMenu("Edit", KeyEvent.VK_E);
35 addItem(edit, "Copy", KeyEvent.VK_C);
36 addItem(edit, "Cut", KeyEvent.VK_X);
37 addItem(edit, "Paste", KeyEvent.VK_V);
38 addItem(edit, "Select all", KeyEvent.VK_E);
39 addItem(edit, "Invert selection", KeyEvent.VK_I);
40 addItem(edit, "Preferences...", KeyEvent.VK_P);
42 playback = createMenu("Playback", KeyEvent.VK_P);
45 addItem(playback, "Play", KeyEvent.VK_SPACE);
46 addItem(playback, "Pause");
47 addItem(playback, "Stop");
48 addItem(playback, "Jump...");
50 music = createMenu("Music", KeyEvent.VK_M);
53 addItem(music, "Add track...", KeyEvent.VK_A);
54 addItem(music, "Delete track...", KeyEvent.VK_D);
55 addItem(music, "Copy track...", KeyEvent.VK_Y);
56 addItem(music, "Move track...", KeyEvent.VK_M);
57 addItem(music, "Insert measure...");
58 addItem(music, "Delete measure...");
59 addItem(music, "Set time signature...");
60 addItem(music, "Set tempo...");
61 addItem(music, "Scale velocity...");
62 addItem(music, "Transpose...");
64 help = createMenu("Help", KeyEvent.VK_L);
67 addItem(help, "Contents");
68 addItem(help, "Getting started");
69 addItem(help, "About");
73 * creates a menu for the menubar
74 * @param name the name of the menu
75 * @param mnemonic the shortcut to activate the menu
76 * @return menu the menu to be added to the menubar
78 private JMenu createMenu(String name, int mnemonic) {
79 JMenu menu = new JMenu(name);
80 menu.setMnemonic(mnemonic);
85 * Creates a menu item.
86 * @param menu the menu to which the item is being added to
87 * @param name the name of this menuitem
88 * @return item the item to add to the menu
90 private JMenuItem addItem(JMenu menu, String name) {
91 JMenuItem item = new JMenuItem(name);
92 item.addActionListener(this);
98 * Creates a menu item with a keyboard accelerator.
99 * @param menu the menu to which the item is being added to
100 * @param name the name of this menuitem
101 * @param key the shortcut to activate the command
102 * @return item the item to add to the menu
104 private JMenuItem addItem(JMenu menu, String name, int key) {
105 JMenuItem item = new JMenuItem(name);
106 item.setAccelerator(KeyStroke.getKeyStroke(key, ActionEvent.CTRL_MASK));
107 item.addActionListener(this);
113 * creates a JFrame popupmenu, containing diffrent choices
114 * @param title the title of the JFrame
115 * @param labelone the first label of two
116 * @param labeltwo the second label of two
117 * @return trackframe the JFrame to popup....ffaaaaaaaaaaaaaaaan!
123 * checks if the fileformat is compatible with our program
124 * @param f the file to check
125 * @return true or false
127 private boolean isMidiFile(File f) {
129 String extension = f.getName().substring(f.getName().lastIndexOf('.') + 1).toLowerCase().trim();
130 if (extension.equals("mid")) return true;
135 public void actionPerformed(ActionEvent e) {
136 String command = e.getActionCommand();
139 if(command == "New") {
140 Moosique.clearSequence();
141 } else if (command == "Open...") {
142 // Shows a file chooser. If shown previously, starts in the current directory.
143 if (directory != null) {
144 chooser = new JFileChooser(directory);
146 chooser = new JFileChooser();
148 chooser.addChoosableFileFilter(new MidiFileFilter());
149 int returnVal = chooser.showOpenDialog(this);
151 // Stores the current directory and loads the selected file.
152 File file = chooser.getSelectedFile();
153 if(returnVal == JFileChooser.APPROVE_OPTION && isMidiFile(file)) {
154 directory = chooser.getSelectedFile().getParentFile();
155 Moosique.load(chooser.getSelectedFile().getAbsolutePath());
157 } else if (command == "Save") {
159 } else if (command == "Save as...") {
160 // Shows a file chooser. If shown previously, starts in the current directory.
161 if (directory != null) {
162 chooser = new JFileChooser(directory);
164 chooser = new JFileChooser();
166 chooser.addChoosableFileFilter(new MidiFileFilter());
167 int returnVal = chooser.showSaveDialog(this);
169 // Stores the current directory and loads the selected file.
170 File file = chooser.getSelectedFile();
171 if(returnVal == JFileChooser.APPROVE_OPTION && isMidiFile(file)) {
172 directory = file.getParentFile();
173 Moosique.saveAs(file.getAbsolutePath());
175 } else if (command == "Exit") {
177 } else if (command == "Copy") {
179 } else if (command == "Cut") {
181 } else if (command == "Paste") {
183 } else if (command == "Select all") {
185 } else if (command == "Invert selection") {
187 } else if (command == "Preferences...") {
189 } else if (command == "Play") {
190 if (!Moosique.getSequencer().isRunning()) Moosique.play();
191 } else if (command == "Pause") {
192 if (Moosique.getSequencer().isRunning()) Moosique.pause();
193 } else if (command == "Stop") {
195 } else if (command == "Jump...") {
197 } else if (command == "Add track...") {
199 JFrame frame = new JFrame("Add track");
200 JPanel panel = new JPanel();
201 //panel.setPreferredSize(new Dimension(250,400));
202 panel.setLayout(new GridLayout(2,2));
203 frame.setContentPane(panel);
205 JLabel top = new JLabel("Name of track", JLabel.CENTER);
206 top.setFont(new Font("Times new Roman", Font.BOLD, 10));
209 JTextField field = new JTextField(5);
212 JLabel bottom = new JLabel("Add it after",JLabel.CENTER);
213 bottom.setFont(new Font("Times new Roman", Font.BOLD, 10));
216 JComboBox tracklist = new JComboBox();
217 panel.add(tracklist);
223 Moosique.getSequence().createTrack();
225 } else if (command == "Delete track...") {
227 JFrame frame = new JFrame("Delete track");
228 JPanel panel = new JPanel();
229 panel.setPreferredSize(new Dimension(250,400));
230 frame.setContentPane(panel);
235 /* Let the user select a track from a list.
236 seq = Moosique.getSequence();
237 seq.deleteTrack(seq.getTracks()[NUMBER]);
239 } else if (command == "Copy track...") {
241 JFrame frame = new JFrame("Copy track");
242 JPanel panel = new JPanel();
243 panel.setPreferredSize(new Dimension(250,400));
244 frame.setContentPane(panel);
249 } else if (command == "Move track...") {
251 JFrame frame = new JFrame("Move track");
252 JPanel panel = new JPanel();
253 panel.setPreferredSize(new Dimension(250,400));
254 panel.setLayout(new GridLayout(2,2));
255 frame.setContentPane(panel);
261 } else if (command == "Insert measure...") {
263 } else if (command == "Delete measure...") {
265 } else if (command == "Set time signature...") {
267 } else if (command == "Set tempo...") {
269 } else if (command == "Scale velocity...") {
271 } else if (command == "Transpose...") {
273 } else if (command == "Contents") {
274 // contents to be filled in
275 JOptionPane.showMessageDialog(this, "här kommer contents komma");
277 } else if (command == "Getting started") {
278 // getting started to be filled in
279 JOptionPane.showMessageDialog(null, "här kommer getting started komma");
281 } else if (command == "About") {
282 // about to be filled in
283 JOptionPane.showMessageDialog(null, "här kommer about att komma");
287 class MidiFileFilter extends javax.swing.filechooser.FileFilter {
288 public boolean accept(File f) {
290 if (f.isDirectory() || isMidiFile(f)) return true;
296 * gets the description of the filetype
297 * @return "Midifiles the only filetyp compatibel with the program
299 public String getDescription() {