]> ruin.nu Git - moosique.git/blob - MooMenu.java
Fixed some bugs
[moosique.git] / MooMenu.java
1 import javax.sound.midi.*;
2 import javax.swing.*;
3 import javax.swing.filechooser.*;
4 import java.awt.event.*;
5 import java.io.*;
6
7 /**
8  * Moosiques GUI representing a menubar, menus and menuitems
9  *
10  * @author Björn Lanneskog
11  */
12 public class MooMenu extends JMenuBar implements ActionListener {
13         
14         private JMenu file, edit, playback, music, help;
15
16         /**
17          * Creates the menu bar.
18          */
19         public MooMenu() {
20                 file = createMenu("File", KeyEvent.VK_F);
21                 add(file);
22                                 
23                 addItem(file, "New", KeyEvent.VK_N);
24                 addItem(file, "Open...", KeyEvent.VK_O);
25                 addItem(file, "Save", KeyEvent.VK_S);
26                 addItem(file, "Save as...");
27                 addItem(file, "Exit", KeyEvent.VK_Q);
28                 
29                 edit = createMenu("Edit", KeyEvent.VK_E);
30                 add(edit);
31                 
32                 addItem(edit, "Copy", KeyEvent.VK_C);
33                 addItem(edit, "Cut", KeyEvent.VK_X);
34                 addItem(edit, "Paste", KeyEvent.VK_V);
35                 addItem(edit, "Select all", KeyEvent.VK_E);
36                 addItem(edit, "Invert selection", KeyEvent.VK_I);
37                 addItem(edit, "Preferences...", KeyEvent.VK_P);
38                 
39                 playback = createMenu("Playback", KeyEvent.VK_P);
40                 add(playback);
41                 
42                 addItem(playback, "Play", KeyEvent.VK_SPACE);
43                 addItem(playback, "Pause");
44                 addItem(playback, "Stop");
45                 addItem(playback, "Jump...");
46                 
47                 music = createMenu("Music", KeyEvent.VK_M);
48                 add(music);
49                 
50                 addItem(music, "Add track...", KeyEvent.VK_A);
51                 addItem(music, "Delete track...", KeyEvent.VK_D);
52                 addItem(music, "Copy track...", KeyEvent.VK_Y);
53                 addItem(music, "Move track...", KeyEvent.VK_M);
54                 addItem(music, "Insert measure...");
55                 addItem(music, "Delete measure...");
56                 addItem(music, "Set time signature...");
57                 addItem(music, "Set tempo...");
58                 addItem(music, "Scale velocity...");
59                 addItem(music, "Transpose...");
60                 
61                 help = createMenu("Help", KeyEvent.VK_L);
62                 add(help);
63                 
64                 addItem(help, "Contents");
65                 addItem(help, "Getting started");
66                 addItem(help, "About");
67         }
68
69         /**
70          * creates a menu for the menubar
71          * @param name          the name of the menu
72          * @param mnemonic      the shortcut to activate the menu
73          * @return menu         the menu to be added to the menubar
74          */
75         private JMenu createMenu(String name, int mnemonic) {
76                 JMenu menu = new JMenu(name);
77                 menu.setMnemonic(mnemonic);
78                 return menu;
79         }
80         
81         /**
82          * Creates a menu item.
83          * @param menu          the menu to which the item is being added to
84          * @param name          the name of this menuitem
85          * @return item         the item to add to the menu
86          */
87         private JMenuItem addItem(JMenu menu, String name) {
88                 JMenuItem item = new JMenuItem(name);
89                 item.addActionListener(this);
90                 menu.add(item);
91                 return item;
92         }
93         
94         /**
95          * Creates a menu item with a keyboard accelerator.
96          * @param menu          the menu to which the item is being added to
97          * @param name          the name of this menuitem
98          * @param key           the shortcut to activate the command
99          * @return item         the item to add to the menu
100          */
101         private JMenuItem addItem(JMenu menu, String name, int key) {
102                 JMenuItem item = new JMenuItem(name);
103                 item.setAccelerator(KeyStroke.getKeyStroke(key, ActionEvent.CTRL_MASK));
104                 item.addActionListener(this);
105                 menu.add(item);
106                 return item;
107         }
108         
109         /**
110          * checks if the fileformat is compatible with our program
111          * @param f     the file to check
112          * @return true or false
113          */
114         private boolean isMidiFile(File f) {
115                 if(f != null) {
116                         String extension = f.getName().substring(f.getName().lastIndexOf('.') + 1).toLowerCase().trim();
117                         if (extension.equals("mid")) return true;
118                 }
119                 return false;
120         }
121
122         public void actionPerformed(ActionEvent e) {
123                 String command = e.getActionCommand();
124                 Sequence seq;
125                 
126                 if(command == "New") {
127                         Moosique.clearSequence();
128                 } else if (command == "Open...") {
129                         JFileChooser chooser = new JFileChooser();
130                         chooser.addChoosableFileFilter(new MidiFileFilter());
131                         int returnVal = chooser.showOpenDialog(this);
132                         if(returnVal == JFileChooser.APPROVE_OPTION && isMidiFile(chooser.getSelectedFile())) {
133                                 Moosique.load(chooser.getSelectedFile().getAbsolutePath());
134                         }
135                 } else if (command == "Save") {
136                         Moosique.save();
137                 } else if (command == "Save as...") {
138                         JFileChooser chooser = new JFileChooser();
139                         chooser.addChoosableFileFilter(new MidiFileFilter());
140                         int returnVal = chooser.showSaveDialog(this);
141                         if(returnVal == JFileChooser.APPROVE_OPTION && isMidiFile(chooser.getSelectedFile())) {
142                                 Moosique.saveAs(chooser.getSelectedFile().getAbsolutePath());
143                         }
144                 } else if (command == "Exit") {
145                         Moosique.quit();
146                 } else if (command == "Copy") {
147                 
148                 } else if (command == "Cut") {
149                 
150                 } else if (command == "Paste") {
151                 
152                 } else if (command == "Select all") {
153                 
154                 } else if (command == "Invert selection") {
155                 
156                 } else if (command == "Preferences...") {
157
158                 } else if (command == "Play") {
159                         if (!Moosique.getSequencer().isRunning()) Moosique.play();
160                 } else if (command == "Pause") {
161                         if (Moosique.getSequencer().isRunning()) Moosique.pause();
162                 } else if (command == "Stop") {
163                         Moosique.stop();
164                 } else if (command == "Jump...") {
165                         
166                 } else if (command == "Add track...") {
167                         Moosique.getSequence().createTrack();
168                 } else if (command == "Delete track...") {
169                         /* Let the user select a track from a list.
170                         seq = Moosique.getSequence();
171                         seq.deleteTrack(seq.getTracks()[NUMBER]);
172                         */
173                 } else if (command == "Copy track...") {
174                 
175                 } else if (command == "Move track...") {
176                 
177                 } else if (command == "Insert measure...") {
178                 
179                 } else if (command == "Delete measure...") {
180                 
181                 } else if (command == "Set time signature...") {
182                 
183                 } else if (command == "Set tempo...") {
184                 
185                 } else if (command == "Scale velocity...") {
186                 
187                 } else if (command == "Transpose...") {
188                 
189                 } else if (command == "Contents") {
190                         // contents to be filled in
191                         JOptionPane.showMessageDialog(this, "här kommer contents komma");
192                 
193                 } else if (command == "Getting started") {
194                         // getting started to be filled in
195                         JOptionPane.showMessageDialog(null, "här kommer getting started komma");
196                 
197                 } else if (command == "About") {
198                         // about to be filled in
199                         JOptionPane.showMessageDialog(null, "här kommer about att komma");
200                 }
201         }
202
203         class MidiFileFilter extends javax.swing.filechooser.FileFilter {
204                 public boolean accept(File f) {
205                         if(f != null) {
206                                 if (f.isDirectory() || isMidiFile(f)) return true;
207                         }
208                         return false;
209                 }
210                 
211                 /*
212                  * gets the description of the filetype
213                  * @return "Midifiles   the only filetyp compatibel with the program
214                  */
215                 public String getDescription() {
216                         return "MIDI files";
217                 }
218         }
219 }