]> 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.*;
5 import java.awt.event.*;
6 import java.io.*;
7 import java.util.*;
8
9 /**
10  * Moosiques GUI representing a menubar, menus and menuitems
11  *
12  * @author Björn Lanneskog
13  */
14 public class MooMenu extends JMenuBar implements ActionListener {
15         
16         private JMenu file, reopen, edit, keyboard, playback, music, help;
17         private JFileChooser chooser;
18         private File directory;
19
20         /**
21          * Creates the menu bar.
22          */
23         public MooMenu() {
24                 // Fetches last work directory, and recent files.
25                 // directory = Moosique.getRecentDirectory();
26                 ArrayList recentFiles = new ArrayList(); // ... = Moosique.getRecentFiles();
27
28                 // Creates the sub-menus and their items.
29                 file = createMenu("File", KeyEvent.VK_F);
30                 add(file);
31                                 
32                 addItem(file, "New", KeyEvent.VK_N, true);
33                 addItem(file, "Open...", KeyEvent.VK_O, true);
34                 reopen = createMenu("Reopen", KeyEvent.VK_R);
35                 Iterator it = recentFiles.iterator();
36                 while (it.hasNext()) addReopenItem((String)it.next());
37                 file.add(reopen);
38                 addItem(file, "Save", KeyEvent.VK_S, true);
39                 addItem(file, "Save as...", KeyEvent.VK_A, false);
40                 file.addSeparator();
41                 addItem(file, "Exit", KeyEvent.VK_Q, true);
42                 
43                 edit = createMenu("Edit", KeyEvent.VK_E);
44                 add(edit);
45                 
46                 addItem(edit, "Copy", KeyEvent.VK_C, true);
47                 addItem(edit, "Cut", KeyEvent.VK_X, true);
48                 addItem(edit, "Paste", KeyEvent.VK_V, true);
49                 edit.addSeparator();
50                 addItem(edit, "Select all", KeyEvent.VK_E, true);
51                 addItem(edit, "Invert selection", KeyEvent.VK_I, true);
52                 edit.addSeparator();
53                 addItem(edit, "Preferences...", KeyEvent.VK_P, true);
54                 
55                 playback = createMenu("Playback", KeyEvent.VK_P);
56                 add(playback);
57                 
58                 addItem(playback, "Play", "F5", KeyEvent.VK_P);
59                 addItem(playback, "Pause", "F7", KeyEvent.VK_A);
60                 addItem(playback, "Stop", "F6", KeyEvent.VK_S);
61                 playback.addSeparator();
62                 addItem(playback, "Set position...", KeyEvent.VK_E, false);
63                 playback.addSeparator();
64                 keyboard = createMenu("Set keyboard octave", KeyEvent.VK_K);
65                 edit.add(keyboard);
66                 for (int i = 9; i >= 0; i--) addItem(keyboard, "" + i, i + 48, false);
67                 
68                 music = createMenu("Music", KeyEvent.VK_M);
69                 add(music);
70                 
71                 addItem(music, "Add track...", KeyEvent.VK_A, true);
72                 addItem(music, "Delete track...", KeyEvent.VK_D, true);
73                 addItem(music, "Copy track...", KeyEvent.VK_Y, true);
74                 addItem(music, "Move track...", KeyEvent.VK_M, true);
75                 music.addSeparator();
76                 addItem(music, "Insert measure...", KeyEvent.VK_I, false);
77                 addItem(music, "Delete measure...", KeyEvent.VK_E, false);
78                 music.addSeparator();
79                 addItem(music, "Set time signature...", KeyEvent.VK_S, false);
80                 addItem(music, "Set tempo...", KeyEvent.VK_M, false);
81                 addItem(music, "Scale velocity...", KeyEvent.VK_V, false);
82                 addItem(music, "Transpose...", KeyEvent.VK_T, false);
83                 
84                 help = createMenu("Help", KeyEvent.VK_L);
85                 add(help);
86
87                 addItem(help, "User manual", "F1", KeyEvent.VK_M);
88                 help.addSeparator();
89                 addItem(help, "About", KeyEvent.VK_A, false);
90         }
91         /**
92          * Creats a menu in the menubar.
93          * @param name          The name of the menu.
94          * @param mnemnic       The shortcut-key to access the menu.
95          * @return menu         The menu created.
96          */
97         private JMenu createMenu(String name, int mnemonic) {
98                 JMenu menu = new JMenu(name);
99                 menu.setMnemonic(mnemonic);
100                 return menu;
101         }
102         
103         /**
104          * Creats a menuitem in the menu.
105          * @param menu          The menu where to add the menuitem.
106          * @param name          The name of the menuitem.
107          * @param mnemonic      The keyboard mnemonic used to access this menuitem
108          * @param accelerate    whether to create a keyboard accelerator for this item
109          * @return item         The menuitem created.
110          */
111         private JMenuItem addItem(JMenu menu, String name, int mnemonic, boolean accelerate) {
112                 JMenuItem item = new JMenuItem(name);
113                 item.addActionListener(this);
114                 item.setMnemonic(mnemonic);
115                 if (accelerate) item.setAccelerator(KeyStroke.getKeyStroke(mnemonic, ActionEvent.CTRL_MASK));
116                 menu.add(item);
117                 return item;
118         }
119         
120         /**
121          * Creats a menuitem in the menu.
122          * @param menu          The menu to where to add the menuitem.
123          * @param name          The name of the menuitem.
124          * @param key           The keystroke to access this menuitem.
125          * @param mnemonic      The keyboard mnemonic used to access this menuitem
126          * @return item         The menuitem created.
127          */
128         private JMenuItem addItem(JMenu menu, String name, String key, int mnemonic) {
129                 JMenuItem item = new JMenuItem(name);
130                 item.setMnemonic(mnemonic);
131                 item.setAccelerator(KeyStroke.getKeyStroke(key));
132                 item.addActionListener(this);
133                 menu.add(item);
134                 return item;
135         }
136
137         /**
138         * Adds an item for this file to the top of the reopen menu.
139         */
140         private void addReopenItem(String file) {
141                 for (int i = 0; i < reopen.getMenuComponentCount(); i++) {
142                         if (file.equals(((JMenuItem)reopen.getMenuComponent(i)).getText()))
143                                 reopen.remove(i);
144                 }
145                 JMenuItem recentFile = new JMenuItem(file);
146                 recentFile.addActionListener(this);
147                 reopen.insert(recentFile, 0);
148                 if (reopen.getMenuComponentCount() > 5) reopen.remove(5);
149         }
150
151         /**
152         * Checks if the given file has the extension ".mid".
153         */
154         private boolean isMidiFile(File f) {
155                 if(f != null) {
156                         String extension = f.getName().substring(f.getName().lastIndexOf('.') + 1).toLowerCase().trim();
157                         if (extension.equals("mid")) return true;
158                 }
159                 return false;
160         }
161         /**
162         * Gets the users command of takes and properiate action
163         * @param e      The action perfomed.
164         */
165         public void actionPerformed(ActionEvent e) {
166                 String command = e.getActionCommand();
167                 Sequence seq;
168                 
169                 if(command == "New") {
170                         if (!Moosique.promptOnUnsavedChanges()) Moosique.clearSequence();
171                 } else if (command == "Open...") {
172                         // Shows a file chooser. If shown previously, starts in the current directory.
173                         if (directory != null) {
174                                 chooser = new JFileChooser(directory);
175                         } else {
176                                 chooser = new JFileChooser();
177                         }
178                         chooser.addChoosableFileFilter(new MidiFileFilter());
179                         int returnVal = chooser.showOpenDialog(this);
180
181                         // Stores the current directory and loads the selected file.
182                         File file = chooser.getSelectedFile();
183                         if(returnVal == JFileChooser.APPROVE_OPTION && isMidiFile(file)) {
184                                 directory = file.getParentFile();
185                                 if (!Moosique.promptOnUnsavedChanges()) {
186                                         if (Moosique.load(file)) addReopenItem(file.getAbsolutePath());
187                                 }
188                         }
189                 } else if (command == "Save") {
190                         if (!Moosique.save()) showSaveAsDialog();
191                 } else if (command == "Save as...") {
192                         showSaveAsDialog();
193                 } else if (command == "Exit") {
194                         Moosique.quit();
195                 } else if (command == "Copy") {
196                 
197                 } else if (command == "Cut") {
198                 
199                 } else if (command == "Paste") {
200                 
201                 } else if (command == "Select all") {
202                 
203                 } else if (command == "Invert selection") {
204                 
205                 } else if (command == "Preferences...") {
206
207                 } else if (command == "Play") {
208                         if (!Moosique.getSequencer().isRunning()) Moosique.play();
209                 } else if (command == "Pause") {
210                         if (Moosique.getSequencer().isRunning()) Moosique.pause();
211                 } else if (command == "Stop") {
212                         Moosique.stop();
213                 } else if (command == "Set position...") {
214                         MooDialog newDialog = new MooDialog(MooDialog.SET_POSITION);
215                         // Moosique.setPosition(???); Räkna ut från msr, beats, ticks, time sig.
216                 } else if (keyboard.isMenuComponent((JMenuItem)e.getSource())) {
217                         MooKeyboard.setOctave(Integer.parseInt(command));
218                 } else if (command == "Add track...") {
219                         MooDialog newDialog = new MooDialog(MooDialog.ADD_TRACK);
220                         Moosique.getSequence().createTrack();
221                 } else if (command == "Delete track...") {
222                         MooDialog newDialog = new MooDialog(MooDialog.DELETE_TRACK);
223                         /* Let the user select a track from a list.
224                         seq = Moosique.getSequence();
225                         seq.deleteTrack(seq.getTracks()[NUMBER]);
226                         */
227                 } else if (command == "Copy track...") {
228                         MooDialog newDialog = new MooDialog(MooDialog.COPY_TRACK);
229                 } else if (command == "Move track...") {
230                         MooDialog newDialog = new MooDialog(MooDialog.MOVE_TRACK);
231                 } else if (command == "Insert measure...") {
232                         MooDialog newDialog = new MooDialog(MooDialog.INSERT_MEASURE);
233                 } else if (command == "Delete measure...") {
234                         MooDialog newDialog = new MooDialog(MooDialog.DELETE_MEASURE);
235                 } else if (command == "Set time signature...") {
236                 
237                 } else if (command == "Set tempo...") {
238                         MooDialog newDialog = new MooDialog(MooDialog.SET_TEMPO);
239                 } else if (command == "Scale velocity...") {
240                         MooDialog newDialog = new MooDialog(MooDialog.SCALE_VELOCITY);
241                 } else if (command == "Transpose...") {
242                         MooDialog newDialog = new MooDialog(MooDialog.TRANSPOSE);
243                 } else if (command == "User manual") {
244                         MooDialog manual = new MooDialog(MooDialog.MANUAL);
245                 } else if (command == "About") {
246                         JOptionPane.showMessageDialog(null,
247                                 "Moosique\nversion 1.0\n\n© 2003\nRoland Andersson\nMichael Andreen\nBjörn Lanneskog\nEinar Pehrson",
248                                 "About Moosique",
249                                 JOptionPane.INFORMATION_MESSAGE,
250                                 new ImageIcon(Moosique.getGUI().logo));
251                 } else if (reopen.isMenuComponent((JMenuItem)e.getSource())) {
252                         String recentFile = ((JMenuItem)e.getSource()).getText();
253                         if (!Moosique.promptOnUnsavedChanges()) {
254                                 if (Moosique.load(new File(recentFile))) addReopenItem(recentFile);
255                         }
256                 }
257         }
258
259         private void showSaveAsDialog() {
260                         // Shows a file chooser. If shown previously, starts in the current directory.
261                         if (directory != null) {
262                                 chooser = new JFileChooser(directory);
263                         } else {
264                                 chooser = new JFileChooser();
265                         }
266                         chooser.addChoosableFileFilter(new MidiFileFilter());
267                         int returnVal = chooser.showSaveDialog(this);
268
269                         // Stores the current directory and loads the selected file.
270                         File file = chooser.getSelectedFile();
271                         if(returnVal == JFileChooser.APPROVE_OPTION && isMidiFile(file)) {
272                                 directory = file.getParentFile();
273                                 if (Moosique.saveAs(file)) addReopenItem(file.getAbsolutePath());
274                         }
275         }
276
277         class MidiFileFilter extends javax.swing.filechooser.FileFilter {
278                 public boolean accept(File f) {
279                         if(f != null) {
280                                 if (f.isDirectory() || isMidiFile(f)) return true;
281                         }
282                         return false;
283                 }
284                 
285                 /**
286                  * gets the description of the filetype
287                  * @return "Midifiles   the only filetyp compatibel with the program
288                  */
289                 public String getDescription() {
290                         return "MIDI files";
291                 }
292         }
293 }