1 import javax.sound.midi.*;
4 import java.awt.event.*;
8 * Moosique's graphical user interface.
10 * @author Einar Pehrson
13 public class MooGUI extends JFrame {
17 private MooToolbar toolbar;
19 private JLabel statusBar;
20 private java.util.Timer timer;
21 public static final int statusResetDelay = 3000;
22 public static final Font FONT = new Font("Helvetica", Font.PLAIN, 10);
23 public static final Color bgColor = new Color(192, 224, 255);
28 public MooGUI(Sequence seq) {
33 Container pane = getContentPane();
34 pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
41 toolbar = new MooToolbar();
42 pane.add(toolbar, BorderLayout.NORTH);
45 view = new MooView(seq.getTracks());
46 pane.add(view, BorderLayout.CENTER);
49 statusBar = new JLabel("Welcome to Moosique!", JLabel.CENTER);
50 statusBar.setFont(FONT);
51 pane.add(statusBar, BorderLayout.SOUTH);
53 // Brings on the colors!
56 setBackground(toolbar);
58 statusBar.setBackground(bgColor);
59 view.setBackground(bgColor);
62 timer = new java.util.Timer();
64 // Sets up global key listener.
65 ActionMap am = getRootPane().getActionMap();
67 Action playAction = new AbstractAction() {
68 public void actionPerformed(ActionEvent ae) {
69 if (!Moosique.getSequencer().isRunning()) {
75 am.put("Play", playAction);
76 am.put("Octave change 2", createOctaveAction(2));
77 am.put("Octave change 4", createOctaveAction(4));
78 am.put("Octave change 6", createOctaveAction(6));
79 am.put("Octave change 8", createOctaveAction(8));
81 InputMap im = getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
82 KeyStroke playKey = KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0);
83 KeyStroke octave2Key = KeyStroke.getKeyStroke(KeyEvent.VK_F9, 0);
84 KeyStroke octave4Key = KeyStroke.getKeyStroke(KeyEvent.VK_F10, 0);
85 KeyStroke octave6Key = KeyStroke.getKeyStroke(KeyEvent.VK_F11, 0);
86 KeyStroke octave8Key = KeyStroke.getKeyStroke(KeyEvent.VK_F12, 0);
87 im.put(playKey, "Play");
88 im.put(octave2Key, "Octave change 2");
89 im.put(octave4Key, "Octave change 4");
90 im.put(octave6Key, "Octave change 6");
91 im.put(octave8Key, "Octave change 8");
94 addWindowListener(new MooGUICloser());
96 setIconImage(Toolkit.getDefaultToolkit().getImage("images/moose.gif"));
97 Dimension bounds = Toolkit.getDefaultToolkit().getScreenSize();
98 setSize(bounds.width,bounds.height - 40);
100 // setResizable(false);
101 setBackground(Color.white);
106 private void setBackground(Container c) {
107 c.setBackground(bgColor);
108 Component[] comps = c.getComponents();
109 for (int i = 0; i < comps.length; i++) {
110 comps[i].setBackground(bgColor);
115 * Changes the sequence of the GUI.
116 * @param sequence the MIDI sequence to visualize
118 public void setSequence(Sequence sequence) {
120 view.setTracks(seq.getTracks());
121 toolbar.resetProgInd();
125 * Shows the given message in the status bar.
126 * @param text the message to show
128 public void setStatus(String text) {
129 statusBar.setText(text);
130 timer.schedule(new StatusResetTask(), statusResetDelay);
134 * Calls on the main view to update the track views,
135 * and on the toolbar to update the progress indicator.
137 public void update(long tickPosition){
138 view.update(tickPosition);
139 toolbar.updateProgInd(tickPosition);
142 private Action createOctaveAction(final int octave) {
143 Action octaveAction = new AbstractAction() {
144 public void actionPerformed(ActionEvent ae) {
145 MooKeyboard.setOctave(octave);
150 class MooGUICloser extends WindowAdapter {
151 public void windowClosing(WindowEvent e) {
156 class StatusResetTask extends TimerTask {