]> 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
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                 
145                 } else if (command == "Exit") {
146                         Moosique.quit();
147                 } else if (command == "Copy") {
148                 
149                 } else if (command == "Cut") {
150                 
151                 } else if (command == "Paste") {
152                 
153                 } else if (command == "Select all") {
154                 
155                 } else if (command == "Invert selection") {
156                 
157                 } else if (command == "Preferences...") {
158
159                 } else if (command == "Play") {
160                         if (Moosique.getSequencer().isRunning()) {
161                                 Moosique.pause();
162                         } else {
163                                 Moosique.play();
164                         }
165                 } else if (command == "Pause") {
166                         if (Moosique.getSequencer().isRunning()) {
167                                 Moosique.resume();
168                         } else {
169                                 Moosique.pause();
170                         }
171                 } else if (command == "Stop") {
172                         Moosique.stop();
173                 } else if (command == "Jump...") {
174                 
175                 } else if (command == "Add track...") {
176                         Moosique.getSequence().createTrack();
177                 } else if (command == "Delete track...") {
178                         /* Let the user select a track from a list.
179                         seq = Moosique.getSequence();
180                         seq.deleteTrack(seq.getTracks()[NUMBER]);
181                         */
182                 } else if (command == "Copy track...") {
183                 
184                 } else if (command == "Move track...") {
185                 
186                 } else if (command == "Insert measure...") {
187                 
188                 } else if (command == "Delete measure...") {
189                 
190                 } else if (command == "Set time signature...") {
191                 
192                 } else if (command == "Set tempo...") {
193                 
194                 } else if (command == "Scale velocity...") {
195                 
196                 } else if (command == "Transpose...") {
197                 
198                 } else if (command == "Contents") {
199                         // contents to be filled in
200                         JOptionPane.showMessageDialog(this, "här kommer contents komma");
201                 
202                 } else if (command == "Getting started") {
203                         // getting started to be filled in
204                         JOptionPane.showMessageDialog(null, "här kommer getting started komma");
205                 
206                 } else if (command == "About") {
207                         // about to be filled in
208                         JOptionPane.showMessageDialog(null, "här kommer about att komma");
209                 }
210         }
211
212         class MidiFileFilter extends javax.swing.filechooser.FileFilter {
213                 public boolean accept(File f) {
214                         if(f != null) {
215                                 if (f.isDirectory() || isMidiFile(f)) return true;
216                         }
217                         return false;
218                 }
219                 
220                 /*
221                  * gets the description of the filetype
222                  * @return "Midifiles   the only filetyp compatibel with the program
223                  */
224                 public String getDescription() {
225                         return "MIDI files";
226                 }
227         }
228 }