]> 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, 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 JFrame popupmenu, containing diffrent choices
114         * @param title  the title of the JFrame
115         * @param labelone       the first label of two
116         * @param labeltwo       the second label of two
117         * @return trackframe    the JFrame to popup....ffaaaaaaaaaaaaaaaan!
118         */
119         
120                 
121         
122         /**
123          * checks if the fileformat is compatible with our program
124          * @param f     the file to check
125          * @return true or false
126          */
127         private boolean isMidiFile(File f) {
128                 if(f != null) {
129                         String extension = f.getName().substring(f.getName().lastIndexOf('.') + 1).toLowerCase().trim();
130                         if (extension.equals("mid")) return true;
131                 }
132                 return false;
133         }
134
135         public void actionPerformed(ActionEvent e) {
136                 String command = e.getActionCommand();
137                 Sequence seq;
138                 
139                 if(command == "New") {
140                         Moosique.clearSequence();
141                 } else if (command == "Open...") {
142                         if (directory != null) {
143                                 chooser = new JFileChooser(directory);
144                         } else {
145                                 chooser = new JFileChooser();
146                         }
147                         chooser.addChoosableFileFilter(new MidiFileFilter());
148                         int returnVal = chooser.showOpenDialog(this);
149                         if(returnVal == JFileChooser.APPROVE_OPTION && isMidiFile(chooser.getSelectedFile())) {
150                                 directory = chooser.getSelectedFile().getParentFile();
151                                 Moosique.load(chooser.getSelectedFile().getAbsolutePath());
152                         }
153                 } else if (command == "Save") {
154                         Moosique.save();
155                 } else if (command == "Save as...") {
156                         if (directory != null) {
157                                 chooser = new JFileChooser(directory);
158                         } else {
159                                 chooser = new JFileChooser();
160                         }
161                         chooser.addChoosableFileFilter(new MidiFileFilter());
162                         int returnVal = chooser.showSaveDialog(this);
163                         if(returnVal == JFileChooser.APPROVE_OPTION && isMidiFile(chooser.getSelectedFile())) {
164                                 directory = chooser.getSelectedFile().getParentFile();
165                                 Moosique.saveAs(chooser.getSelectedFile().getAbsolutePath());
166                         }
167                 } else if (command == "Exit") {
168                         Moosique.quit();
169                 } else if (command == "Copy") {
170                 
171                 } else if (command == "Cut") {
172                 
173                 } else if (command == "Paste") {
174                 
175                 } else if (command == "Select all") {
176                 
177                 } else if (command == "Invert selection") {
178                 
179                 } else if (command == "Preferences...") {
180
181                 } else if (command == "Play") {
182                         if (!Moosique.getSequencer().isRunning()) Moosique.play();
183                 } else if (command == "Pause") {
184                         if (Moosique.getSequencer().isRunning()) Moosique.pause();
185                 } else if (command == "Stop") {
186                         Moosique.stop();
187                 } else if (command == "Jump...") {
188                         
189                 } else if (command == "Add track...") {
190                 
191                         JFrame frame = new JFrame("Add track");
192                         JPanel panel = new JPanel();
193                         //panel.setPreferredSize(new Dimension(250,400));
194                         panel.setLayout(new GridLayout(2,2));
195                         frame.setContentPane(panel);
196                         
197                         JLabel top = new JLabel("Name of track", JLabel.CENTER);
198                         top.setFont(new Font("Times new Roman", Font.BOLD, 10));
199                         panel.add(top);
200                         
201                         JTextField field = new JTextField(5);
202                         panel.add(field);
203                        
204                         JLabel bottom = new JLabel("Add it after",JLabel.CENTER);
205                         bottom.setFont(new Font("Times new Roman", Font.BOLD, 10));
206                         panel.add(bottom);
207                         
208                         JComboBox tracklist = new JComboBox();
209                         panel.add(tracklist);
210                         
211                         
212                         frame.pack();
213                         frame.show();
214                         
215                         Moosique.getSequence().createTrack();
216                         
217                 } else if (command == "Delete track...") {
218                         
219                         JFrame frame = new JFrame("Delete track");
220                         JPanel panel = new JPanel();
221                         panel.setPreferredSize(new Dimension(250,400));
222                         frame.setContentPane(panel);
223                         
224                         frame.pack();
225                         frame.show();
226                         
227                         /* Let the user select a track from a list.
228                         seq = Moosique.getSequence();
229                         seq.deleteTrack(seq.getTracks()[NUMBER]);
230                         */
231                 } else if (command == "Copy track...") {
232                 
233                         JFrame frame = new JFrame("Copy track");
234                         JPanel panel = new JPanel();
235                         panel.setPreferredSize(new Dimension(250,400));
236                         frame.setContentPane(panel);
237                         
238                         frame.pack();
239                         frame.show();
240                 
241                 } else if (command == "Move track...") {
242                 
243                         JFrame frame = new JFrame("Move track");
244                         JPanel panel = new JPanel();
245                         panel.setPreferredSize(new Dimension(250,400));
246                         panel.setLayout(new GridLayout(2,2));
247                         frame.setContentPane(panel);
248                         
249                         frame.pack();
250                         frame.show();
251                 
252
253                 } else if (command == "Insert measure...") {
254                 
255                 } else if (command == "Delete measure...") {
256                 
257                 } else if (command == "Set time signature...") {
258                 
259                 } else if (command == "Set tempo...") {
260                 
261                 } else if (command == "Scale velocity...") {
262                 
263                 } else if (command == "Transpose...") {
264                 
265                 } else if (command == "Contents") {
266                         // contents to be filled in
267                         JOptionPane.showMessageDialog(this, "här kommer contents komma");
268                 
269                 } else if (command == "Getting started") {
270                         // getting started to be filled in
271                         JOptionPane.showMessageDialog(null, "här kommer getting started komma");
272                 
273                 } else if (command == "About") {
274                         // about to be filled in
275                         JOptionPane.showMessageDialog(null, "här kommer about att komma");
276                 }
277         }
278
279         class MidiFileFilter extends javax.swing.filechooser.FileFilter {
280                 public boolean accept(File f) {
281                         if(f != null) {
282                                 if (f.isDirectory() || isMidiFile(f)) return true;
283                         }
284                         return false;
285                 }
286                 
287                 /*
288                  * gets the description of the filetype
289                  * @return "Midifiles   the only filetyp compatibel with the program
290                  */
291                 public String getDescription() {
292                         return "MIDI files";
293                 }
294         }
295         
296         
297 }