]> ruin.nu Git - moosique.git/blob - MooMenu.java
Fixed drag select in track view.
[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                         if (!Moosique.promptOnUnsavedChanges()) 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                                 if (!Moosique.promptOnUnsavedChanges())
171                                         Moosique.load(chooser.getSelectedFile().getAbsolutePath());
172                         }
173                 } else if (command == "Save") {
174                         if (!Moosique.save()) showSaveAsDialog();
175                 } else if (command == "Save as...") {
176                         showSaveAsDialog();
177                 } else if (command == "Exit") {
178                         Moosique.quit();
179                 } else if (command == "Copy") {
180                 
181                 } else if (command == "Cut") {
182                 
183                 } else if (command == "Paste") {
184                 
185                 } else if (command == "Select all") {
186                 
187                 } else if (command == "Invert selection") {
188                 
189                 } else if (command == "Preferences...") {
190
191                 } else if (command == "Play") {
192                         if (!Moosique.getSequencer().isRunning()) Moosique.play();
193                 } else if (command == "Pause") {
194                         if (Moosique.getSequencer().isRunning()) Moosique.pause();
195                 } else if (command == "Stop") {
196                         Moosique.stop();
197                 } else if (command == "Set position...") {
198                         MooDialog newDialog = new MooDialog(MooDialog.SET_POSITION);
199                         // Moosique.setPosition(???); Räkna ut från msr, beats, ticks, time sig.
200                 } else if (command.startsWith("Octave")) {
201                         MooKeyboard.setOctave(Integer.parseInt(command.substring(7,8)));
202                 } else if (command == "Add track...") {
203                         MooDialog newDialog = new MooDialog(MooDialog.ADD_TRACK);
204                         Moosique.getSequence().createTrack();
205                 } else if (command == "Delete track...") {
206                 
207                         MooDialog newDialog = new MooDialog(MooDialog.DELETE_TRACK);
208                         
209                         /* Let the user select a track from a list.
210                         seq = Moosique.getSequence();
211                         seq.deleteTrack(seq.getTracks()[NUMBER]);
212                         */
213                 } else if (command == "Copy track...") {
214                         MooDialog newDialog = new MooDialog(MooDialog.COPY_TRACK);
215                 } else if (command == "Move track...") {
216                         MooDialog newDialog = new MooDialog(MooDialog.MOVE_TRACK);
217                 } else if (command == "Insert measure...") {
218                         MooDialog newDialog = new MooDialog(MooDialog.INSERT_MEASURE);
219                 } else if (command == "Delete measure...") {
220                         MooDialog newDialog = new MooDialog(MooDialog.DELETE_MEASURE);
221                 } else if (command == "Set time signature...") {
222                 
223                 } else if (command == "Set tempo...") {
224                         MooDialog newDialog = new MooDialog(MooDialog.SET_TEMPO);
225                 
226                 } else if (command == "Scale velocity...") {
227                 
228                 } else if (command == "Transpose...") {
229                 
230                 } else if (command == "User manual") {
231                         MooDialog manual = new MooDialog(MooDialog.MANUAL);
232                 } else if (command == "About") {
233                         JOptionPane.showMessageDialog(null,
234                                 "Moosique\nversion 1.0\n\nby\n\nRoland Andersson\nMichael Andreen\nBjörn Lanneskog\nEinar Pehrson",
235                                 "About Moosique",
236                                 JOptionPane.INFORMATION_MESSAGE,
237                                 new ImageIcon(Moosique.getGUI().logo));
238                 }
239         }
240
241         private void showSaveAsDialog() {
242                         // Shows a file chooser. If shown previously, starts in the current directory.
243                         if (directory != null) {
244                                 chooser = new JFileChooser(directory);
245                         } else {
246                                 chooser = new JFileChooser();
247                         }
248                         chooser.addChoosableFileFilter(new MidiFileFilter());
249                         int returnVal = chooser.showSaveDialog(this);
250
251                         // Stores the current directory and loads the selected file.
252                         File file = chooser.getSelectedFile();
253                         if(returnVal == JFileChooser.APPROVE_OPTION && isMidiFile(file)) {
254                                 directory = file.getParentFile();
255                                 Moosique.saveAs(file.getAbsolutePath());
256                         }
257         }
258
259         class MidiFileFilter extends javax.swing.filechooser.FileFilter {
260                 public boolean accept(File f) {
261                         if(f != null) {
262                                 if (f.isDirectory() || isMidiFile(f)) return true;
263                         }
264                         return false;
265                 }
266                 
267                 /**
268                  * gets the description of the filetype
269                  * @return "Midifiles   the only filetyp compatibel with the program
270                  */
271                 public String getDescription() {
272                         return "MIDI files";
273                 }
274         }
275         
276         
277 }