X-Git-Url: https://ruin.nu/git/?p=moosique.git;a=blobdiff_plain;f=MooMenu.java;h=ae1672d083508740a9ec0becedb6b9234ff67fba;hp=1315bfbd504b00155a5f49d69a69c63973b64b9b;hb=HEAD;hpb=a5f89d87c8559d170c61ecc9516c4adba7f71083 diff --git a/MooMenu.java b/MooMenu.java index 1315bfb..ae1672d 100644 --- a/MooMenu.java +++ b/MooMenu.java @@ -1,92 +1,293 @@ +import javax.sound.midi.*; import javax.swing.*; +import javax.swing.filechooser.*; +import java.awt.*; +import java.awt.event.*; +import java.io.*; +import java.util.*; -public class MooMenu extends JMenuBar -{ - // empty contructor that anropar the createMooMenu in the mainmethod - public MooMenu() - { - JMenuBar menubar = new JMenuBar(); - menubar.add(createFileMenu()); - menubar.add(createEditMenu()); - menubar.add(createSequenceMenu()); - menubar.add(createTrackMenu()); - menubar.add(createNoteMenu()); - menubar.add(createHelpMenu()); - } +/** + * Moosiques GUI representing a menubar, menus and menuitems + * + * @author Björn Lanneskog + */ +public class MooMenu extends JMenuBar implements ActionListener { + + private JMenu file, reopen, edit, keyboard, playback, music, help; + private JFileChooser chooser; + private File directory; - // creates the filemenu - public JMenu createFileMenu() - { - JMenu file = new JMenu("File"); + /** + * Creates the menu bar. + */ + public MooMenu() { + // Fetches last work directory, and recent files. + // directory = Moosique.getRecentDirectory(); + ArrayList recentFiles = new ArrayList(); // ... = Moosique.getRecentFiles(); + + // Creates the sub-menus and their items. + file = createMenu("File", KeyEvent.VK_F); + add(file); + + addItem(file, "New", KeyEvent.VK_N, true); + addItem(file, "Open...", KeyEvent.VK_O, true); + reopen = createMenu("Reopen", KeyEvent.VK_R); + Iterator it = recentFiles.iterator(); + while (it.hasNext()) addReopenItem((String)it.next()); + file.add(reopen); + addItem(file, "Save", KeyEvent.VK_S, true); + addItem(file, "Save as...", KeyEvent.VK_A, false); + file.addSeparator(); + addItem(file, "Exit", KeyEvent.VK_Q, true); + + edit = createMenu("Edit", KeyEvent.VK_E); + add(edit); + + addItem(edit, "Copy", KeyEvent.VK_C, true); + addItem(edit, "Cut", KeyEvent.VK_X, true); + addItem(edit, "Paste", KeyEvent.VK_V, true); + edit.addSeparator(); + addItem(edit, "Select all", KeyEvent.VK_E, true); + addItem(edit, "Invert selection", KeyEvent.VK_I, true); + edit.addSeparator(); + addItem(edit, "Preferences...", KeyEvent.VK_P, true); + + playback = createMenu("Playback", KeyEvent.VK_P); + add(playback); + + addItem(playback, "Play", "F5", KeyEvent.VK_P); + addItem(playback, "Pause", "F7", KeyEvent.VK_A); + addItem(playback, "Stop", "F6", KeyEvent.VK_S); + playback.addSeparator(); + addItem(playback, "Set position...", KeyEvent.VK_E, false); + playback.addSeparator(); + keyboard = createMenu("Set keyboard octave", KeyEvent.VK_K); + edit.add(keyboard); + for (int i = 9; i >= 0; i--) addItem(keyboard, "" + i, i + 48, false); - JMenuItem neww = new JMenuItem("New"); - file.add(neww); - JMenuItem open = new JMenuItem("Open"); - file.add(open); - JMenuItem saveas = new JMenuItem("Save as..."); - file.add(saveas); - JMenuItem save = new JMenuItem("Save"); - file.add(save); - JMenuItem exporttomidi = new JMenuItem("Export to MIDI..."); - file.add(exporttomidi); - JMenuItem exit = new JMenuItem("Exit"); - file.add(exporttomidi); - return file; + music = createMenu("Music", KeyEvent.VK_M); + add(music); + + addItem(music, "Add track...", KeyEvent.VK_A, true); + addItem(music, "Delete track...", KeyEvent.VK_D, true); + addItem(music, "Copy track...", KeyEvent.VK_Y, true); + addItem(music, "Move track...", KeyEvent.VK_M, true); + music.addSeparator(); + addItem(music, "Insert measure...", KeyEvent.VK_I, false); + addItem(music, "Delete measure...", KeyEvent.VK_E, false); + music.addSeparator(); + addItem(music, "Set time signature...", KeyEvent.VK_S, false); + addItem(music, "Set tempo...", KeyEvent.VK_M, false); + addItem(music, "Scale velocity...", KeyEvent.VK_V, false); + addItem(music, "Transpose...", KeyEvent.VK_T, false); + + help = createMenu("Help", KeyEvent.VK_L); + add(help); + + addItem(help, "User manual", "F1", KeyEvent.VK_M); + help.addSeparator(); + addItem(help, "About", KeyEvent.VK_A, false); + } + /** + * 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; } - // creates the editmenu - public JMenu createEditMenu() - { - JMenu edit = new JMenu("Edit"); - - JMenuItem copy = new JMenuItem("Copy"); - edit.add(copy); - JMenuItem cut = new JMenuItem("Cut"); - edit.add(cut); - JMenuItem paste = new JMenuItem("Paste"); - edit.add(paste); - JMenuItem selectall = new JMenuItem("Select All"); - edit.add(selectall); - JMenuItem invertselection = new JMenuItem("Invert selection"); - edit.add(invertselection); - return edit; + /** + * Creats a menuitem in the menu. + * @param menu The menu where to add the menuitem. + * @param name The name of the menuitem. + * @param mnemonic The keyboard mnemonic used to access this menuitem + * @param accelerate whether to create a keyboard accelerator for this item + * @return item The menuitem created. + */ + private JMenuItem addItem(JMenu menu, String name, int mnemonic, boolean accelerate) { + JMenuItem item = new JMenuItem(name); + item.addActionListener(this); + item.setMnemonic(mnemonic); + if (accelerate) item.setAccelerator(KeyStroke.getKeyStroke(mnemonic, ActionEvent.CTRL_MASK)); + menu.add(item); + return item; } - - //creates the sequencemenu - public JMenu createSequenceMenu() - { - - JMenu sequence = new JMenu("Sequence"); - return sequence; + + /** + * 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 mnemonic The keyboard mnemonic used to access this menuitem + * @return item The menuitem created. + */ + private JMenuItem addItem(JMenu menu, String name, String key, int mnemonic) { + JMenuItem item = new JMenuItem(name); + item.setMnemonic(mnemonic); + item.setAccelerator(KeyStroke.getKeyStroke(key)); + item.addActionListener(this); + menu.add(item); + return item; } - // creates the trackmenu - public JMenu createTrackMenu() - { - JMenu track = new JMenu("Track"); - return track; + /** + * Adds an item for this file to the top of the reopen menu. + */ + private void addReopenItem(String file) { + for (int i = 0; i < reopen.getMenuComponentCount(); i++) { + if (file.equals(((JMenuItem)reopen.getMenuComponent(i)).getText())) + reopen.remove(i); + } + JMenuItem recentFile = new JMenuItem(file); + recentFile.addActionListener(this); + reopen.insert(recentFile, 0); + if (reopen.getMenuComponentCount() > 5) reopen.remove(5); } - // creates the notemenu - public JMenu createNoteMenu() - { - JMenu note = new JMenu("Note"); - return note; + /** + * Checks if the given file has the extension ".mid". + */ + 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") { + if (!Moosique.promptOnUnsavedChanges()) Moosique.clearSequence(); + } else if (command == "Open...") { + // 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); - // creates the aboutmenu - public JMenu createHelpMenu() - { - JMenu help = new JMenu("Help"); + // Stores the current directory and loads the selected file. + File file = chooser.getSelectedFile(); + if(returnVal == JFileChooser.APPROVE_OPTION && isMidiFile(file)) { + directory = file.getParentFile(); + if (!Moosique.promptOnUnsavedChanges()) { + if (Moosique.load(file)) addReopenItem(file.getAbsolutePath()); + } + } + } else if (command == "Save") { + if (!Moosique.save()) showSaveAsDialog(); + } else if (command == "Save as...") { + showSaveAsDialog(); + } else if (command == "Exit") { + Moosique.quit(); + } else if (command == "Copy") { + + } else if (command == "Cut") { + + } else if (command == "Paste") { + + } else if (command == "Select all") { + + } else if (command == "Invert selection") { - JMenuItem contents = new JMenuItem("Contents"); - help.add(contents); - JMenuItem gettingstarted = new JMenuItem("Getting started"); - help.add(gettingstarted); - JMenuItem about = new JMenuItem("About"); - help.add(about); + } else if (command == "Preferences...") { - return help; + } else if (command == "Play") { + 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 == "Set position...") { + MooDialog newDialog = new MooDialog(MooDialog.SET_POSITION); + // Moosique.setPosition(???); Räkna ut från msr, beats, ticks, time sig. + } else if (keyboard.isMenuComponent((JMenuItem)e.getSource())) { + MooKeyboard.setOctave(Integer.parseInt(command)); + } else if (command == "Add track...") { + MooDialog newDialog = new MooDialog(MooDialog.ADD_TRACK); + Moosique.getSequence().createTrack(); + } else if (command == "Delete track...") { + 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 == "Set tempo...") { + MooDialog newDialog = new MooDialog(MooDialog.SET_TEMPO); + } else if (command == "Scale velocity...") { + MooDialog newDialog = new MooDialog(MooDialog.SCALE_VELOCITY); + } else if (command == "Transpose...") { + MooDialog newDialog = new MooDialog(MooDialog.TRANSPOSE); + } else if (command == "User manual") { + MooDialog manual = new MooDialog(MooDialog.MANUAL); + } else if (command == "About") { + JOptionPane.showMessageDialog(null, + "Moosique\nversion 1.0\n\n© 2003\nRoland Andersson\nMichael Andreen\nBjörn Lanneskog\nEinar Pehrson", + "About Moosique", + JOptionPane.INFORMATION_MESSAGE, + new ImageIcon(Moosique.getGUI().logo)); + } else if (reopen.isMenuComponent((JMenuItem)e.getSource())) { + String recentFile = ((JMenuItem)e.getSource()).getText(); + if (!Moosique.promptOnUnsavedChanges()) { + if (Moosique.load(new File(recentFile))) addReopenItem(recentFile); + } + } + } + + 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(); + if (Moosique.saveAs(file)) addReopenItem(file.getAbsolutePath()); + } } + 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"; + } + } }