]> ruin.nu Git - moosique.git/blob - MooMenu.java
Fixed some errors, updated the menu and toolbar.
[moosique.git] / MooMenu.java
1 import javax.swing.*;
2 import javax.swing.filechooser.*;
3 import java.awt.event.*;
4 import java.io.*;
5
6 /**
7  * Moosiques GUI representing a menubar, menus and menuitems
8  *
9  * @author Björn Lanneskog
10  */
11 public class MooMenu extends JMenuBar implements ActionListener {
12         
13         private JMenu file, edit, playback, music, help;
14
15         /**
16          * Creates the menu bar.
17          */
18         public MooMenu() {
19                 file = createMenu("File", KeyEvent.VK_F);
20                 add(file);
21                                 
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);
27                 
28                 edit = createMenu("Edit", KeyEvent.VK_E);
29                 add(edit);
30                 
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);
37                 
38                 playback = createMenu("Playback", KeyEvent.VK_P);
39                 add(playback);
40                 
41                 addItem(playback, "Play", KeyEvent.VK_SPACE);
42                 addItem(playback, "Pause");
43                 addItem(playback, "Stop");
44                 addItem(playback, "Jump...");
45                 
46                 music = createMenu("Music", KeyEvent.VK_M);
47                 add(music);
48                 
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...");
59                 
60                 help = createMenu("Help", KeyEvent.VK_L);
61                 add(help);
62                 
63                 addItem(help, "Contents");
64                 addItem(help, "Getting started");
65                 addItem(help, "About");
66         }
67
68         /**
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
73          */
74         private JMenu createMenu(String name, int mnemonic) {
75                 JMenu menu = new JMenu(name);
76                 menu.setMnemonic(mnemonic);
77                 return menu;
78         }
79         
80         /**
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
85          */
86         private JMenuItem addItem(JMenu menu, String name) {
87                 JMenuItem item = new JMenuItem(name);
88                 item.addActionListener(this);
89                 menu.add(item);
90                 return item;
91         }
92         
93         /**
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
99          */
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);
104                 menu.add(item);
105                 return item;
106         }
107         
108         /**
109          * checks if the fileformat is compatible with our program
110          * @param f     the file to check
111          * @return true or false
112          */
113         private boolean isMidiFile(File f) {
114                 if(f != null) {
115                         String extension = f.getName().substring(f.getName().lastIndexOf('.') + 1).toLowerCase().trim();
116                         if (extension.equals("mid")) return true;
117                 }
118                 return false;
119         }
120
121         public void actionPerformed(ActionEvent e) {
122                 String command = e.getActionCommand();
123                 
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());
132                         }
133                 } else if (command == "Save") {
134                         Moosique.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());
141                         }
142                 
143                 } else if (command == "Exit") {
144                         Moosique.quit();
145                 } else if (command == "Copy") {
146                 
147                 } else if (command == "Cut") {
148                 
149                 } else if (command == "Paste") {
150                 
151                 } else if (command == "Select all") {
152                 
153                 } else if (command == "Invert selection") {
154                 
155                 } else if (command == "Preferences...") {
156
157                 } else if (command == "Play") {
158                         if (Moosique.getSequencer().isRunning()) {
159                                 Moosique.pause();
160                         } else {
161                                 Moosique.play();
162                         }
163                 } else if (command == "Pause") {
164                         if (Moosique.getSequencer().isRunning()) {
165                                 Moosique.resume();
166                         } else {
167                                 Moosique.pause();
168                         }
169                 } else if (command == "Stop") {
170                         Moosique.stop();
171                 } else if (command == "Jump...") {
172                 
173                 } else if (command == "Add track...") {
174                 
175                 } else if (command == "Delete track...") {
176                 
177                 } else if (command == "Copy track...") {
178                 
179                 } else if (command == "Move track...") {
180                 
181                 } else if (command == "Insert measure...") {
182                 
183                 } else if (command == "Delete measure...") {
184                 
185                 } else if (command == "Set time signature...") {
186                 
187                 } else if (command == "Set tempo...") {
188                 
189                 } else if (command == "Scale velocity...") {
190                 
191                 } else if (command == "Transpose...") {
192                 
193                 } else if (command == "Contents") {
194                         // contents to be filled in
195                         JOptionPane.showMessageDialog(this, "här kommer contents komma");
196                 
197                 } else if (command == "Getting started") {
198                         // getting started to be filled in
199                         JOptionPane.showMessageDialog(null, "här kommer getting started komma");
200                 
201                 } else if (command == "About") {
202                         // about to be filled in
203                         JOptionPane.showMessageDialog(null, "här kommer about att komma");
204                 }
205         }
206         
207         class MidiFileFilter extends javax.swing.filechooser.FileFilter {
208                 public boolean accept(File f) {
209                         if(f != null) {
210                                 if (f.isDirectory() || isMidiFile(f)) return true;
211                         }
212                         return false;
213                 }
214                 
215                 /*
216                  * gets the description of the filetype
217                  * @return "Midifiles   the only filetyp compatibel with the program
218                  */
219                 public String getDescription() {
220                         return "MIDI files";
221                 }
222         }
223 }