]> ruin.nu Git - moosique.git/blob - MooMenu.java
varså god einar
[moosique.git] / MooMenu.java
1 import javax.swing.*;
2 import javax.swing.filechooser.*;
3 import java.awt.event.*;
4 import java.io.*;
5
6 /**
7  * Moosiques GUI representing a menubar, menus and menuitems
8  *
9  * @author Björn Lanneskog
10  */
11 public class MooMenu extends JMenuBar implements ActionListener {
12         
13 /*
14
15 Musikrelaterade menyer i Midisoft Recording Session:
16
17 Track           Insert New...           Har vi
18                 Delete...               Har vi
19                 Move...                 Har vi
20                 Copy...                 Har vi
21                 Combine...              Onödig
22                 Rechannel...            Kanske
23                 Split by Pitch...       Onödig
24                 
25 Music           Insert Measure...       Lagt till
26                 Delete Measure...       Lagt till
27                 Clef...                 Onödig
28                 Time Signature...       Lagt till
29                 Key Signature...        Onödig
30                 Tempo...                Lagt till
31                 Scale Velocity...       Lagt till
32                 Transpose...            Lagt till
33                 Quantize...             Onödig
34
35 Kanske också:   Reset Solo / Mute
36
37 */
38         private JMenu file, edit, playback, track, help, music;
39
40         /**
41          * contructs a menubar
42          */
43         public MooMenu() {
44                 file = makeMenu("File", KeyEvent.VK_F);
45                 add(file);
46                                 
47                 file.add(makeItem(file, "New", KeyEvent.VK_N));
48                 file.add(makeItem(file, "Open...", KeyEvent.VK_O));
49                 file.add(makeItem(file, "Save", KeyEvent.VK_S));
50                 file.add(makeItem(file, "Save as..."));
51                 file.add(makeItem(file, "Exit", KeyEvent.VK_Q));
52                 
53                 edit = makeMenu("Edit", KeyEvent.VK_E);
54                 add(edit);
55                 
56                 edit.add(makeItem(edit, "Copy", KeyEvent.VK_C));
57                 edit.add(makeItem(edit, "Cut", KeyEvent.VK_X));
58                 edit.add(makeItem(edit, "Paste", KeyEvent.VK_V));
59                 edit.add(makeItem(edit, "Select all", KeyEvent.VK_E));
60                 edit.add(makeItem(edit, "Invert selection", KeyEvent.VK_I));
61                 edit.add(makeItem(edit, "Preferences", KeyEvent.VK_P));
62                 
63                 playback = makeMenu("Playback", KeyEvent.VK_P);
64                 add(playback);
65                 
66                 playback.add(makeItem(playback, "Play", KeyEvent.VK_SPACE));
67                 playback.add(makeItem(playback, "Pause", KeyEvent.VK_SPACE));
68                 playback.add(makeItem(playback, "Stop"));
69                 playback.add(makeItem(playback, "Jump..."));
70                 
71                 track = makeMenu("Track", KeyEvent.VK_T);
72                 add(track);
73                 
74                 track.add(makeItem(track, "Add", KeyEvent.VK_A));
75                 track.add(makeItem(track, "Delete", KeyEvent.VK_D));
76                 track.add(makeItem(track, "Copy track", KeyEvent.VK_Y));
77                 track.add(makeItem(track, "Move", KeyEvent.VK_M));
78                 
79                 music = makeMenu("Music", KeyEvent.VK_M);
80                 add(music);
81                 
82                 music.add(makeItem(track, "Insert measure..."));
83                 music.add(makeItem(track, "Delete measure..."));
84                 music.add(makeItem(track, "Time signature..."));
85                 music.add(makeItem(track, "Tempo..."));
86                 music.add(makeItem(track, "Scale velocity..."));
87                 music.add(makeItem(track, "Transpose..."));
88                 
89                 help = makeMenu("Help", KeyEvent.VK_L);
90                 add(help);
91                 
92                 help.add(makeItem(help, "Contents"));
93                 help.add(makeItem(help, "Getting started"));
94                 help.add(makeItem(help, "About"));
95         }
96
97         /**
98          * creates a menu for the menubar
99          * @param name          the name of the menu
100          * @param mnemonic      the shortcut to activate the menu
101          * @return menu         the menu to be added to the menubar
102          */
103         private JMenu makeMenu(String name, int mnemonic) {
104                 JMenu menu = new JMenu(name);
105                 menu.setMnemonic(mnemonic);
106                 return menu;
107         }
108         
109         /**
110          * creates a menuitem
111          * @param menu          the menu to which the item is being added to
112          * @param name          the name of this menuitem
113          * @return item         the item to add to the menu
114          */
115         private JMenuItem makeItem(JMenu menu, String name) {
116                 JMenuItem item = new JMenuItem(name);
117                 item.addActionListener(this);
118                 menu.add(item);
119                 return item;
120         }
121         
122         /**
123          * creates a menuitem
124          * @param menu          the menu to which the item is being added to
125          * @param name          the name of this menuitem
126          * @param key           the shortcut to activate the command
127          * @return item         the item to add to the menu
128          */
129         private JMenuItem makeItem(JMenu menu, String name, int key) {
130                 JMenuItem item = new JMenuItem(name);
131                 item.setAccelerator(KeyStroke.getKeyStroke(key, ActionEvent.CTRL_MASK));
132                 item.addActionListener(this);
133                 menu.add(item);
134                 return item;
135         }
136         
137         /**
138          * checks if the fileformat is compatible with our program
139          * @param f     the file to check
140          * @return true or false
141          */
142         private boolean isMidiFile(File f) {
143                 if(f != null) {
144                         String extension = f.getName().substring(f.getName().lastIndexOf('.') + 1).toLowerCase().trim();
145                         if (extension.equals("mid")) return true;
146                 }
147                 return false;
148         }
149
150         public void actionPerformed(ActionEvent e) {
151                 String command = e.getActionCommand();
152                 
153                 if(command == "New") {
154                         Moosique.clearSequence();
155                 } else if (command == "Open...") {
156                         JFileChooser chooser = new JFileChooser();
157                         chooser.addChoosableFileFilter(new MidiFileFilter());
158                         int returnVal = chooser.showOpenDialog(this);
159                         if(returnVal == JFileChooser.APPROVE_OPTION && isMidiFile(chooser.getSelectedFile())) {
160                                 Moosique.load(chooser.getSelectedFile().getAbsolutePath());
161                         }
162                 } else if (command == "Save") {
163                         Moosique.save();
164                 } else if (command == "Save as...") {
165                         JFileChooser chooser = new JFileChooser();
166                         chooser.addChoosableFileFilter(new MidiFileFilter());
167                         int returnVal = chooser.showSaveDialog(this);
168                         if(returnVal == JFileChooser.APPROVE_OPTION && isMidiFile(chooser.getSelectedFile())) {
169                                 Moosique.saveAs(chooser.getSelectedFile().getAbsolutePath());
170                         }
171                 
172                 } else if (command == "Exit") {
173                         Moosique.quit();
174                 } else if (command == "Copy") {
175                 
176                 } else if (command == "Cut") {
177                 
178                 } else if (command == "Paste") {
179                 
180                 } else if (command == "Select all") {
181                 
182                 } else if (command == "Invert selection") {
183                 
184                 } else if (command == "Preferences") {
185
186                 } else if (command == "Play / Resume") {
187                         //if (Moosique.getSequence().isRunning()) {
188                                 //Moosique.pause();
189                         //} else Moosique.play();
190                 
191                 } else if (command == "Pause") {
192                         // koda för resume också
193                         Moosique.pause();
194                 } else if (command == "Stop") {
195                         Moosique.stop();
196                 } else if (command == "Jump...") {
197                 
198                 } else if (command == "Add") {
199                 
200                 } else if (command == "Delete") {
201                 
202                 } else if (command == "Copy track") {
203                 
204                 } else if (command == "Move") {
205                 
206                 } else if (command == "Insert Measure...") {
207                 
208                 } else if (command == "Delete Measure...") {
209                 
210                 } else if (command == "Time Signature...") {
211                 
212                 } else if (command == "Tempo...") {
213                 
214                 } else if (command == "Scale Velocity...") {
215                 
216                 } else if (command == "Transpose...") {
217                 
218                 } else if (command == "Contents") {
219                         // contents to be filled in
220                         JOptionPane.showMessageDialog(this, "här kommer contents komma");
221                 
222                 } else if (command == "Getting started") {
223                         // getting started to be filled in
224                         JOptionPane.showMessageDialog(null, "här kommer getting started komma");
225                 
226                 } else if (command == "About") {
227                         // about to be filled in
228                         JOptionPane.showMessageDialog(null, "här kommer about att komma");
229                 }
230         }
231         
232         class MidiFileFilter extends javax.swing.filechooser.FileFilter {
233                 public boolean accept(File f) {
234                         if(f != null) {
235                                 if (f.isDirectory() || isMidiFile(f)) return true;
236                         }
237                         return false;
238                 }
239                 
240                 /*
241                  * gets the description of the filetype
242                  * @return "Midifiles   the only filetyp compatibel with the program
243                  */
244                 public String getDescription() {
245                         return "MIDI files";
246                 }
247         }
248 }