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