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);
27 * @param seq The sequence that the program is operating on.
29 public MooGUI(Sequence seq) {
34 Container pane = getContentPane();
35 pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
42 toolbar = new MooToolbar();
43 pane.add(toolbar, BorderLayout.NORTH);
46 view = new MooView(seq.getTracks());
47 pane.add(view, BorderLayout.CENTER);
50 statusBar = new JLabel("Welcome to Moosique!", JLabel.CENTER);
51 statusBar.setFont(FONT);
52 pane.add(statusBar, BorderLayout.SOUTH);
54 // Brings on the colors!
57 setBackground(toolbar);
59 statusBar.setBackground(bgColor);
60 view.setBackground(bgColor);
63 timer = new java.util.Timer();
65 // Sets up global key listener.
66 ActionMap am = getRootPane().getActionMap();
68 Action playAction = new AbstractAction() {
69 public void actionPerformed(ActionEvent ae) {
70 if (!Moosique.getSequencer().isRunning()) {
76 am.put("Play", playAction);
77 am.put("Octave change 2", createOctaveAction(2));
78 am.put("Octave change 4", createOctaveAction(4));
79 am.put("Octave change 6", createOctaveAction(6));
80 am.put("Octave change 8", createOctaveAction(8));
82 InputMap im = getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
83 KeyStroke playKey = KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0);
84 KeyStroke octave2Key = KeyStroke.getKeyStroke(KeyEvent.VK_F9, 0);
85 KeyStroke octave4Key = KeyStroke.getKeyStroke(KeyEvent.VK_F10, 0);
86 KeyStroke octave6Key = KeyStroke.getKeyStroke(KeyEvent.VK_F11, 0);
87 KeyStroke octave8Key = KeyStroke.getKeyStroke(KeyEvent.VK_F12, 0);
88 im.put(playKey, "Play");
89 im.put(octave2Key, "Octave change 2");
90 im.put(octave4Key, "Octave change 4");
91 im.put(octave6Key, "Octave change 6");
92 im.put(octave8Key, "Octave change 8");
95 addWindowListener(new MooGUICloser());
97 setIconImage(Toolkit.getDefaultToolkit().getImage("images/moose.gif"));
98 Dimension bounds = Toolkit.getDefaultToolkit().getScreenSize();
99 setSize(bounds.width,bounds.height - 40);
101 // setResizable(false);
102 setBackground(Color.white);
108 * Sets the background on Containers
109 * @param c the Container that will have it's background change
111 private void setBackground(Container c) {
112 c.setBackground(bgColor);
113 Component[] comps = c.getComponents();
114 for (int i = 0; i < comps.length; i++) {
115 comps[i].setBackground(bgColor);
120 * Changes the sequence of the GUI.
121 * @param sequence the MIDI sequence to visualize
123 public void setSequence(Sequence sequence) {
125 view.setTracks(seq.getTracks(), true);
126 toolbar.resetProgInd();
130 * Shows the given message in the status bar.
131 * @param text the message to show
133 public void setStatus(String text) {
134 statusBar.setText(text);
135 timer.schedule(new StatusResetTask(), statusResetDelay);
139 * Calls on the main view to update the track views,
140 * and on the toolbar to update the progress indicator.
142 public void update(long tickPosition){
143 view.update(tickPosition);
144 toolbar.updateProgInd(tickPosition);
148 * Creates an action for a specific octave.
149 * @param octave The octave we want an action for.
151 private Action createOctaveAction(final int octave) {
152 Action octaveAction = new AbstractAction() {
153 public void actionPerformed(ActionEvent ae) {
154 MooKeyboard.setOctave(octave);
160 * Listener for closing the program
162 class MooGUICloser extends WindowAdapter {
163 public void windowClosing(WindowEvent e) {
169 * TimerTask that resets the statusbar
171 class StatusResetTask extends TimerTask {