]> ruin.nu Git - moosique.git/blob - MooMenu.java
*** empty log message ***
[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 import java.awt.*;
7
8 /**
9  * Moosiques GUI representing a menubar, menus and menuitems
10  *
11  * @author Björn Lanneskog
12  */
13 public class MooMenu extends JMenuBar implements ActionListener {
14         
15         private JMenu file, edit, keyboard, playback, music, help;
16         private JFileChooser chooser;
17         private File directory;
18
19         /**
20          * Creates the menu bar.
21          */
22         public MooMenu() {
23                 file = createMenu("File", KeyEvent.VK_F);
24                 add(file);
25                                 
26                 addItem(file, "New", KeyEvent.VK_N, ActionEvent.CTRL_MASK);
27                 addItem(file, "Open...", KeyEvent.VK_O, ActionEvent.CTRL_MASK);
28                 addItem(file, "Save", KeyEvent.VK_S, ActionEvent.CTRL_MASK);
29                 addItem(file, "Save as...", KeyEvent.VK_A);
30                 file.addSeparator();
31                 addItem(file, "Exit", KeyEvent.VK_Q, ActionEvent.CTRL_MASK);
32                 
33                 edit = createMenu("Edit", KeyEvent.VK_E);
34                 add(edit);
35                 
36                 addItem(edit, "Copy", KeyEvent.VK_C, ActionEvent.CTRL_MASK);
37                 addItem(edit, "Cut", KeyEvent.VK_X, ActionEvent.CTRL_MASK);
38                 addItem(edit, "Paste", KeyEvent.VK_V, ActionEvent.CTRL_MASK);
39                 edit.addSeparator();
40                 addItem(edit, "Select all", KeyEvent.VK_E, ActionEvent.CTRL_MASK);
41                 addItem(edit, "Invert selection", KeyEvent.VK_I, ActionEvent.CTRL_MASK);
42                 edit.addSeparator();
43                 addItem(edit, "Preferences...", KeyEvent.VK_P, ActionEvent.CTRL_MASK);
44                 
45                 playback = createMenu("Playback", KeyEvent.VK_P);
46                 add(playback);
47                 
48                 addItem(playback, "Play", "F5", KeyEvent.VK_P);
49                 addItem(playback, "Pause", "F7", KeyEvent.VK_A);
50                 addItem(playback, "Stop", "F6", KeyEvent.VK_S);
51                 playback.addSeparator();
52                 addItem(playback, "Set position...", KeyEvent.VK_E);
53                 playback.addSeparator();
54                 keyboard = createMenu("Set keyboard octave", KeyEvent.VK_K);
55                 edit.add(keyboard);
56                 for (int i = 9; i >= 0; i--) addItem(keyboard, "Octave " + i, i + 48);
57                 
58                 music = createMenu("Music", KeyEvent.VK_M);
59                 add(music);
60                 
61                 addItem(music, "Add track...", KeyEvent.VK_A, ActionEvent.CTRL_MASK);
62                 addItem(music, "Delete track...", KeyEvent.VK_D, ActionEvent.CTRL_MASK);
63                 addItem(music, "Copy track...", KeyEvent.VK_Y, ActionEvent.CTRL_MASK);
64                 addItem(music, "Move track...", KeyEvent.VK_M, ActionEvent.CTRL_MASK);
65                 music.addSeparator();
66                 addItem(music, "Insert measure...", KeyEvent.VK_I);
67                 addItem(music, "Delete measure...", KeyEvent.VK_E);
68                 music.addSeparator();
69                 addItem(music, "Set time signature...", KeyEvent.VK_S);
70                 addItem(music, "Set tempo...", KeyEvent.VK_M);
71                 addItem(music, "Scale velocity...", KeyEvent.VK_V);
72                 addItem(music, "Transpose...", KeyEvent.VK_T);
73                 
74                 help = createMenu("Help", KeyEvent.VK_L);
75                 add(help);
76                 
77                 addItem(help, "User manual", "F1", KeyEvent.VK_M);
78                 help.addSeparator();
79                 addItem(help, "About", KeyEvent.VK_A);
80         }
81         /**
82          * Creats a menu in the menubar.
83          * @param name          The name of the menu.
84          * @param mnemnic       The shortcut-key to access the menu.
85          * @return menu         The menu created.
86          */
87         private JMenu createMenu(String name, int mnemonic) {
88                 JMenu menu = new JMenu(name);
89                 menu.setMnemonic(mnemonic);
90                 return menu;
91         }
92         
93         /**
94          * Creats a menuitem in the menu.
95          * @param menu          The menu where to add the menuitem.
96          * @param name          The name of the menuitem.
97          * @return item         The menuitem created.
98          */
99         private JMenuItem addItem(JMenu menu, String name, int mnemonic) {
100                 JMenuItem item = new JMenuItem(name);
101                 item.addActionListener(this);
102                 menu.add(item);
103                 return item;
104         }
105         
106         /**
107          * Creats a menuitem in the menu.
108          * @param menu          The menu to where to add the menuitem.
109          * @param name          The name of the menuitem.
110          * @param key           The keystroke to access this menuitem.
111          * @return item         The menuitem created.
112          */
113         private JMenuItem addItem(JMenu menu, String name, String key, int mnemonic) {
114                 JMenuItem item = new JMenuItem(name);
115                 item.setAccelerator(KeyStroke.getKeyStroke(key));
116                 item.setMnemonic(mnemonic);
117                 item.addActionListener(this);
118                 menu.add(item);
119                 return item;
120         }
121         
122         /**
123          * Creats a menuitem in the menu.
124          * @param menu          The menu to where to add the menuitem.
125          * @param name          The name of the menuitem.
126          * @param key           The keystroke to access this menuitem.
127          * @param mask          The keyboard mask.
128          * @return item         The menuitem created.
129          */
130         private JMenuItem addItem(JMenu menu, String name, int key, int mask) {
131                 JMenuItem item = new JMenuItem(name);
132                 item.setAccelerator(KeyStroke.getKeyStroke(key, mask));
133                 item.setMnemonic(key);
134                 item.addActionListener(this);
135                 menu.add(item);
136                 return item;
137         }
138         
139         private boolean isMidiFile(File f) {
140                 if(f != null) {
141                         String extension = f.getName().substring(f.getName().lastIndexOf('.') + 1).toLowerCase().trim();
142                         if (extension.equals("mid")) return true;
143                 }
144                 return false;
145         }
146         /**
147         * Gets the users command of takes and properiate action
148         * @param e      The action perfomed.
149         */
150         public void actionPerformed(ActionEvent e) {
151                 String command = e.getActionCommand();
152                 Sequence seq;
153                 
154                 if(command == "New") {
155                         Moosique.clearSequence();
156                 } else if (command == "Open...") {
157                         // Shows a file chooser. If shown previously, starts in the current directory.
158                         if (directory != null) {
159                                 chooser = new JFileChooser(directory);
160                         } else {
161                                 chooser = new JFileChooser();
162                         }
163                         chooser.addChoosableFileFilter(new MidiFileFilter());
164                         int returnVal = chooser.showOpenDialog(this);
165
166                         // Stores the current directory and loads the selected file.
167                         File file = chooser.getSelectedFile();
168                         if(returnVal == JFileChooser.APPROVE_OPTION && isMidiFile(file)) {
169                                 directory = chooser.getSelectedFile().getParentFile();
170                                 Moosique.load(chooser.getSelectedFile().getAbsolutePath());
171                         }
172                 } else if (command == "Save") {
173                         if (!Moosique.save()) showSaveAsDialog();
174                 } else if (command == "Save as...") {
175                         showSaveAsDialog();
176                 } else if (command == "Exit") {
177                         Moosique.quit();
178                 } else if (command == "Copy") {
179                 
180                 } else if (command == "Cut") {
181                 
182                 } else if (command == "Paste") {
183                 
184                 } else if (command == "Select all") {
185                 
186                 } else if (command == "Invert selection") {
187                 
188                 } else if (command == "Preferences...") {
189
190                 } else if (command == "Play") {
191                         if (!Moosique.getSequencer().isRunning()) Moosique.play();
192                 } else if (command == "Pause") {
193                         if (Moosique.getSequencer().isRunning()) Moosique.pause();
194                 } else if (command == "Stop") {
195                         Moosique.stop();
196                 } else if (command == "Set position...") {
197                         MooDialog newDialog = new MooDialog(MooDialog.SET_POSITION);
198                         // Moosique.setPosition(???); Räkna ut från msr, beats, ticks, time sig.
199                 } else if (command.startsWith("Octave")) {
200                         MooKeyboard.setOctave(Integer.parseInt(command.substring(7,8)));
201                 } else if (command == "Add track...") {
202                         MooDialog newDialog = new MooDialog(MooDialog.ADD_TRACK);
203                         Moosique.getSequence().createTrack();
204                 } else if (command == "Delete track...") {
205                 
206                         MooDialog newDialog = new MooDialog(MooDialog.DELETE_TRACK);
207                         
208                         /* Let the user select a track from a list.
209                         seq = Moosique.getSequence();
210                         seq.deleteTrack(seq.getTracks()[NUMBER]);
211                         */
212                 } else if (command == "Copy track...") {
213                         MooDialog newDialog = new MooDialog(MooDialog.COPY_TRACK);
214                 } else if (command == "Move track...") {
215                         MooDialog newDialog = new MooDialog(MooDialog.MOVE_TRACK);
216                 } else if (command == "Insert measure...") {
217                         MooDialog newDialog = new MooDialog(MooDialog.INSERT_MEASURE);
218                 } else if (command == "Delete measure...") {
219                         MooDialog newDialog = new MooDialog(MooDialog.DELETE_MEASURE);
220                 } else if (command == "Set time signature...") {
221                 
222                 } else if (command == "Set tempo...") {
223                         MooDialog newDialog = new MooDialog(MooDialog.SET_TEMPO);
224                 
225                 } else if (command == "Scale velocity...") {
226                 
227                 } else if (command == "Transpose...") {
228                 
229                 } else if (command == "User manual") {
230                         MooDialog manual = new MooDialog(MooDialog.MANUAL);
231                 } else if (command == "About") {
232                         JOptionPane.showMessageDialog(null,
233                                 "Moosique\nversion 1.0\n\nby\n\nRoland Andersson\nMichael Andreen\nBjörn Lanneskog\nEinar Pehrson",
234                                 "About Moosique",
235                                 JOptionPane.INFORMATION_MESSAGE,
236                                 new ImageIcon(Moosique.getGUI().logo));
237                 }
238         }
239
240         private void showSaveAsDialog() {
241                         // Shows a file chooser. If shown previously, starts in the current directory.
242                         if (directory != null) {
243                                 chooser = new JFileChooser(directory);
244                         } else {
245                                 chooser = new JFileChooser();
246                         }
247                         chooser.addChoosableFileFilter(new MidiFileFilter());
248                         int returnVal = chooser.showSaveDialog(this);
249
250                         // Stores the current directory and loads the selected file.
251                         File file = chooser.getSelectedFile();
252                         if(returnVal == JFileChooser.APPROVE_OPTION && isMidiFile(file)) {
253                                 directory = file.getParentFile();
254                                 Moosique.saveAs(file.getAbsolutePath());
255                         }
256         }
257
258         class MidiFileFilter extends javax.swing.filechooser.FileFilter {
259                 public boolean accept(File f) {
260                         if(f != null) {
261                                 if (f.isDirectory() || isMidiFile(f)) return true;
262                         }
263                         return false;
264                 }
265                 
266                 /**
267                  * gets the description of the filetype
268                  * @return "Midifiles   the only filetyp compatibel with the program
269                  */
270                 public String getDescription() {
271                         return "MIDI files";
272                 }
273         }
274         
275         
276 }