]> ruin.nu Git - moosique.git/blob - MooMenu.java
inte helt klar
[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);
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);
31                 
32                 edit = createMenu("Edit", KeyEvent.VK_E);
33                 add(edit);
34                 
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);
41                 
42                 playback = createMenu("Playback", KeyEvent.VK_P);
43                 add(playback);
44                 
45                 addItem(playback, "Play", KeyEvent.VK_SPACE);
46                 addItem(playback, "Pause");
47                 addItem(playback, "Stop");
48                 addItem(playback, "Jump...");
49                 
50                 music = createMenu("Music", KeyEvent.VK_M);
51                 add(music);
52                 
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...");
63                 
64                 help = createMenu("Help", KeyEvent.VK_L);
65                 add(help);
66                 
67                 addItem(help, "Contents");
68                 addItem(help, "Getting started");
69                 addItem(help, "About");
70         }
71
72         /**
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
77          */
78         private JMenu createMenu(String name, int mnemonic) {
79                 JMenu menu = new JMenu(name);
80                 menu.setMnemonic(mnemonic);
81                 return menu;
82         }
83         
84         /**
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
89          */
90         private JMenuItem addItem(JMenu menu, String name) {
91                 JMenuItem item = new JMenuItem(name);
92                 item.addActionListener(this);
93                 menu.add(item);
94                 return item;
95         }
96         
97         /**
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
103          */
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);
108                 menu.add(item);
109                 return item;
110         }
111         
112         /**
113         * creates a JDialog popupmenu, containing diffrent choices
114         * @param title          the title of the dialog
115         * @return trackframe    the JDialog....ffaaaaaaaaaaaaaaaan!
116         */
117         //private JDialog makeDialog(String title){
118         //      
119         //}
120         /**
121          * checks if the fileformat is compatible with our program
122          * @param f     the file to check
123          * @return true or false
124          */
125         private boolean isMidiFile(File f) {
126                 if(f != null) {
127                         String extension = f.getName().substring(f.getName().lastIndexOf('.') + 1).toLowerCase().trim();
128                         if (extension.equals("mid")) return true;
129                 }
130                 return false;
131         }
132
133         public void actionPerformed(ActionEvent e) {
134                 String command = e.getActionCommand();
135                 Sequence seq;
136                 
137                 if(command == "New") {
138                         Moosique.clearSequence();
139                 } else if (command == "Open...") {
140                         // Shows a file chooser. If shown previously, starts in the current directory.
141                         if (directory != null) {
142                                 chooser = new JFileChooser(directory);
143                         } else {
144                                 chooser = new JFileChooser();
145                         }
146                         chooser.addChoosableFileFilter(new MidiFileFilter());
147                         int returnVal = chooser.showOpenDialog(this);
148
149                         // Stores the current directory and loads the selected file.
150                         File file = chooser.getSelectedFile();
151                         if(returnVal == JFileChooser.APPROVE_OPTION && isMidiFile(file)) {
152                                 directory = chooser.getSelectedFile().getParentFile();
153                                 Moosique.load(chooser.getSelectedFile().getAbsolutePath());
154                         }
155                 } else if (command == "Save") {
156                         Moosique.save();
157                 } else if (command == "Save as...") {
158                         // Shows a file chooser. If shown previously, starts in the current directory.
159                         if (directory != null) {
160                                 chooser = new JFileChooser(directory);
161                         } else {
162                                 chooser = new JFileChooser();
163                         }
164                         chooser.addChoosableFileFilter(new MidiFileFilter());
165                         int returnVal = chooser.showSaveDialog(this);
166
167                         // Stores the current directory and loads the selected file.
168                         File file = chooser.getSelectedFile();
169                         if(returnVal == JFileChooser.APPROVE_OPTION && isMidiFile(file)) {
170                                 directory = file.getParentFile();
171                                 Moosique.saveAs(file.getAbsolutePath());
172                         }
173                 } else if (command == "Exit") {
174                         Moosique.quit();
175                 } else if (command == "Copy") {
176                 
177                 } else if (command == "Cut") {
178                 
179                 } else if (command == "Paste") {
180                 
181                 } else if (command == "Select all") {
182                 
183                 } else if (command == "Invert selection") {
184                 
185                 } else if (command == "Preferences...") {
186
187                 } else if (command == "Play") {
188                         if (!Moosique.getSequencer().isRunning()) Moosique.play();
189                 } else if (command == "Pause") {
190                         if (Moosique.getSequencer().isRunning()) Moosique.pause();
191                 } else if (command == "Stop") {
192                         Moosique.stop();
193                 } else if (command == "Jump...") {
194                         
195                 } else if (command == "Add track...") {
196                 
197                         MooDialog what = new MooDialog(MooDialog.ADD_TRACK);
198                         
199                         Moosique.getSequence().createTrack();
200                         
201                 } else if (command == "Delete track...") {
202                 
203                         MooDialog what = new MooDialog(MooDialog.DELETE_TRACK);
204                         
205                         /* Let the user select a track from a list.
206                         seq = Moosique.getSequence();
207                         seq.deleteTrack(seq.getTracks()[NUMBER]);
208                         */
209                 } else if (command == "Copy track...") {
210                 
211                         MooDialog what = new MooDialog(MooDialog.COPY_TRACK);
212                         
213                 } else if (command == "Move track...") {
214                 
215                         MooDialog what = new MooDialog(MooDialog.MOVE_TRACK);
216
217                 } else if (command == "Insert measure...") {
218                 
219                 } else if (command == "Delete measure...") {
220                 
221                 } else if (command == "Set time signature...") {
222                 
223                 } else if (command == "Set tempo...") {
224                 
225                 } else if (command == "Scale velocity...") {
226                 
227                 } else if (command == "Transpose...") {
228                 
229                 } else if (command == "Contents") {
230                         // contents to be filled in
231                         JOptionPane.showMessageDialog(this, "här kommer contents komma");
232                 
233                 } else if (command == "Getting started") {
234                         // getting started to be filled in
235                         JOptionPane.showMessageDialog(null, "här kommer getting started komma");
236                 
237                 } else if (command == "About") {
238                         // about to be filled in
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 }