X-Git-Url: https://ruin.nu/git/?p=moosique.git;a=blobdiff_plain;f=MooMenu.java;h=f4090bdc5d70ef1fabffafbe71c45a8dba24357a;hp=65d7b4f48ecdefd8812e4adb54f2bd678a41312f;hb=aae2d0b4428236b4147f466b3858a34bb7ed174f;hpb=be09b535cfe4cb14ea261bcbf12553e9d6c2a3b0 diff --git a/MooMenu.java b/MooMenu.java index 65d7b4f..f4090bd 100644 --- a/MooMenu.java +++ b/MooMenu.java @@ -1,109 +1,171 @@ +import javax.sound.midi.*; import javax.swing.*; +import javax.swing.filechooser.*; import java.awt.event.*; -import javax.swing.JComponent.*; -import javax.swing.filechooser.FileFilter; +import java.io.*; +import java.awt.*; -public class MooMenu extends JMenuBar implements ActionListener{ - - // Ett något smidigare sätt, kanske.. / EP +/** + * Moosiques GUI representing a menubar, menus and menuitems + * + * @author Björn Lanneskog + */ +public class MooMenu extends JMenuBar implements ActionListener { + private JMenu file, edit, playback, music, help; + private JFileChooser chooser; + private File directory; + + /** + * Creates the menu bar. + */ public MooMenu() { - file = makeMenu("File", KeyEvent.VK_F); + file = createMenu("File", KeyEvent.VK_F); add(file); - file.add(makeAccItem(file, "New", KeyEvent.VK_N)); - file.add(makeAccItem(file, "Open...", KeyEvent.VK_O)); - file.add(makeAccItem(file, "Save", KeyEvent.VK_S)); - file.add(makeItem(file, "Save as...")); - file.add(makeAccItem(file, "Exit", KeyEvent.VK_Q)); + addItem(file, "New", KeyEvent.VK_N, ActionEvent.CTRL_MASK); + addItem(file, "Open...", KeyEvent.VK_O, ActionEvent.CTRL_MASK); + addItem(file, "Save", KeyEvent.VK_S, ActionEvent.CTRL_MASK); + addItem(file, "Save as..."); + addItem(file, "Exit", KeyEvent.VK_Q, ActionEvent.CTRL_MASK); - edit = makeMenu("Edit", KeyEvent.VK_E); + edit = createMenu("Edit", KeyEvent.VK_E); add(edit); - edit.add(makeAccItem(edit, "Copy", KeyEvent.VK_C)); - edit.add(makeAccItem(edit, "Cut", KeyEvent.VK_X)); - edit.add(makeAccItem(edit, "Paste", KeyEvent.VK_V)); - edit.add(makeAccItem(edit, "Select all", KeyEvent.VK_E)); - edit.add(makeAccItem(edit, "Invert selection", KeyEvent.VK_I)); + addItem(edit, "Copy", KeyEvent.VK_C, ActionEvent.CTRL_MASK); + addItem(edit, "Cut", KeyEvent.VK_X, ActionEvent.CTRL_MASK); + addItem(edit, "Paste", KeyEvent.VK_V, ActionEvent.CTRL_MASK); + addItem(edit, "Select all", KeyEvent.VK_E, ActionEvent.CTRL_MASK); + addItem(edit, "Invert selection", KeyEvent.VK_I, ActionEvent.CTRL_MASK); + edit.addSeparator(); + addItem(edit, "Preferences...", KeyEvent.VK_P, ActionEvent.CTRL_MASK); - playback = makeMenu("Playback", KeyEvent.VK_P); + playback = createMenu("Playback", KeyEvent.VK_P); add(playback); - playback.add(makeAccItem(playback, "Play", KeyEvent.VK_SPACE)); - playback.add(makeAccItem(playback, "Pause", KeyEvent.VK_SPACE)); - playback.add(makeItem(playback, "Stop")); - playback.add(makeItem(playback, "Jump...")); + addItem(playback, "Play", "F5"); + addItem(playback, "Pause", "F7"); + addItem(playback, "Stop", "F6"); + addItem(playback, "Jump..."); - track = makeMenu("Track", KeyEvent.VK_T); - add(track); + music = createMenu("Music", KeyEvent.VK_M); + add(music); - track.add(makeAccItem(track, "Add", KeyEvent.VK_A)); - track.add(makeAccItem(track, "Delete", KeyEvent.VK_D)); - track.add(makeAccItem(track, "Copy track", KeyEvent.VK_Y)); - track.add(makeAccItem(track, "Move", KeyEvent.VK_M)); + addItem(music, "Add track...", KeyEvent.VK_A, ActionEvent.CTRL_MASK); + addItem(music, "Delete track...", KeyEvent.VK_D, ActionEvent.CTRL_MASK); + addItem(music, "Copy track...", KeyEvent.VK_Y, ActionEvent.CTRL_MASK); + addItem(music, "Move track...", KeyEvent.VK_M, ActionEvent.CTRL_MASK); + music.addSeparator(); + addItem(music, "Insert measure..."); + addItem(music, "Delete measure..."); + music.addSeparator(); + addItem(music, "Set time signature..."); + addItem(music, "Set tempo..."); + addItem(music, "Scale velocity..."); + addItem(music, "Transpose..."); - help = makeMenu("Help", KeyEvent.VK_P); + help = createMenu("Help", KeyEvent.VK_L); add(help); - help.add(makeItem(help, "Contents")); - help.add(makeItem(help, "Getting started")); - help.add(makeItem(help, "About")); + addItem(help, "Contents", "F1"); + addItem(help, "Getting started"); + addItem(help, "About"); } - - private JMenu makeMenu(String name, int mnemonic) { + /** + * Creats a menu in the menubar. + * @param name The name of the menu. + * @param mnemnic The shortcut-key to access the menu. + * @return menu The menu created. + */ + private JMenu createMenu(String name, int mnemonic) { JMenu menu = new JMenu(name); menu.setMnemonic(mnemonic); return menu; } - - private JMenuItem makeItem(JMenu menu, String name) { + + /** + * Creats a menuitem in the menu. + * @param menu The menu where to add the menuitem. + * @param name The name of the menuitem. + * @return item The menuitem created. + */ + private JMenuItem addItem(JMenu menu, String name) { JMenuItem item = new JMenuItem(name); item.addActionListener(this); menu.add(item); return item; } - - private JMenuItem makeAccItem(JMenu menu, String name, int key) { + + /** + * Creats a menuitem in the menu. + * @param menu The menu to where to add the menuitem. + * @param name The name of the menuitem. + * @param key The keystroke to access this menuitem. + * @return item The menuitem created. + */ + private JMenuItem addItem(JMenu menu, String name, String key) { + JMenuItem item = new JMenuItem(name); + item.setAccelerator(KeyStroke.getKeyStroke(key)); + item.addActionListener(this); + menu.add(item); + return item; + } + + /** + * Creats a menuitem in the menu. + * @param menu The menu to where to add the menuitem. + * @param name The name of the menuitem. + * @param key The keystroke to access this menuitem. + * @param mask The keyboard mask. + * @return item The menuitem created. + */ + private JMenuItem addItem(JMenu menu, String name, int key, int mask) { JMenuItem item = new JMenuItem(name); - item.setAccelerator(KeyStroke.getKeyStroke(key, ActionEvent.CTRL_MASK)); + item.setAccelerator(KeyStroke.getKeyStroke(key, mask)); item.addActionListener(this); menu.add(item); return item; } + private boolean isMidiFile(File f) { + if(f != null) { + String extension = f.getName().substring(f.getName().lastIndexOf('.') + 1).toLowerCase().trim(); + if (extension.equals("mid")) return true; + } + return false; + } + /** + * Gets the users command of takes and properiate action + * @param e The action perfomed. + */ public void actionPerformed(ActionEvent e) { String command = e.getActionCommand(); + Sequence seq; if(command == "New") { - + Moosique.clearSequence(); } else if (command == "Open...") { - - JFileChooser chooser = new JFileChooser(); - // här måste jag lägga till en filefilter - // som bestämmer vilka filtyper som får öppnas - + // Shows a file chooser. If shown previously, starts in the current directory. + if (directory != null) { + chooser = new JFileChooser(directory); + } else { + chooser = new JFileChooser(); + } + chooser.addChoosableFileFilter(new MidiFileFilter()); int returnVal = chooser.showOpenDialog(this); - if(returnVal == JFileChooser.APPROVE_OPTION) { - System.out.println("You chose to open this file: " + - chooser.getSelectedFile().getName()); + + // Stores the current directory and loads the selected file. + File file = chooser.getSelectedFile(); + if(returnVal == JFileChooser.APPROVE_OPTION && isMidiFile(file)) { + directory = chooser.getSelectedFile().getParentFile(); + Moosique.load(chooser.getSelectedFile().getAbsolutePath()); } - } else if (command == "Save") { - + if (!Moosique.save()) showSaveAsDialog(); } else if (command == "Save as...") { - - JFileChooser chooser = new JFileChooser(); - // här måste jag lägga till en filefilter - // som bestämmer vilka filtyper som får sparas - - int returnVal = chooser.showSaveDialog(this); - if(returnVal == JFileChooser.APPROVE_OPTION) { - System.out.println("You chose to open this file: " + - chooser.getSelectedFile().getName()); - } - + showSaveAsDialog(); } else if (command == "Exit") { - + Moosique.quit(); } else if (command == "Copy") { } else if (command == "Cut") { @@ -114,42 +176,90 @@ public class MooMenu extends JMenuBar implements ActionListener{ } else if (command == "Invert selection") { + } else if (command == "Preferences...") { + } else if (command == "Play") { - JOptionPane.showMessageDialog(null, "playing"); - + if (!Moosique.getSequencer().isRunning()) Moosique.play(); } else if (command == "Pause") { - + if (Moosique.getSequencer().isRunning()) Moosique.pause(); } else if (command == "Stop") { - + Moosique.stop(); } else if (command == "Jump...") { + MooDialog newDialog = new MooDialog(MooDialog.JUMP); + // Moosique.setPosition(???); Räkna ut från msr, beats, ticks, time sig. + } else if (command == "Add track...") { + MooDialog newDialog = new MooDialog(MooDialog.ADD_TRACK); + Moosique.getSequence().createTrack(); + } else if (command == "Delete track...") { - } else if (command == "Add") { + MooDialog newDialog = new MooDialog(MooDialog.DELETE_TRACK); + + /* Let the user select a track from a list. + seq = Moosique.getSequence(); + seq.deleteTrack(seq.getTracks()[NUMBER]); + */ + } else if (command == "Copy track...") { + MooDialog newDialog = new MooDialog(MooDialog.COPY_TRACK); + } else if (command == "Move track...") { + MooDialog newDialog = new MooDialog(MooDialog.MOVE_TRACK); + } else if (command == "Insert measure...") { + MooDialog newDialog = new MooDialog(MooDialog.INSERT_MEASURE); + } else if (command == "Delete measure...") { + MooDialog newDialog = new MooDialog(MooDialog.DELETE_MEASURE); + } else if (command == "Set time signature...") { - } else if (command == "Delete") { + } else if (command == "Set tempo...") { - } else if (command == "Copy track") { + } else if (command == "Scale velocity...") { - } else if (command == "Move") { + } else if (command == "Transpose...") { } else if (command == "Contents") { - // contents to be filled in - JOptionPane.showMessageDialog(this, "här kommer contents komma"); - + MooDialog contents = new MooDialog(MooDialog.CONTENTS); } else if (command == "Getting started") { - // getting started to be filled in + JOptionPane.showMessageDialog(null, "här kommer getting started komma"); } else if (command == "About") { - // about to be filled in + JOptionPane.showMessageDialog(null, "här kommer about att komma"); - } } + private void showSaveAsDialog() { + // Shows a file chooser. If shown previously, starts in the current directory. + if (directory != null) { + chooser = new JFileChooser(directory); + } else { + chooser = new JFileChooser(); + } + chooser.addChoosableFileFilter(new MidiFileFilter()); + int returnVal = chooser.showSaveDialog(this); + + // Stores the current directory and loads the selected file. + File file = chooser.getSelectedFile(); + if(returnVal == JFileChooser.APPROVE_OPTION && isMidiFile(file)) { + directory = file.getParentFile(); + Moosique.saveAs(file.getAbsolutePath()); + } + } - private JMenu file; - private JMenu edit; - private JMenu playback; - private JMenu track; - private JMenu help; + class MidiFileFilter extends javax.swing.filechooser.FileFilter { + public boolean accept(File f) { + if(f != null) { + if (f.isDirectory() || isMidiFile(f)) return true; + } + return false; + } + + /** + * gets the description of the filetype + * @return "Midifiles the only filetyp compatibel with the program + */ + public String getDescription() { + return "MIDI files"; + } + } + + }