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);
24 public static final Image logo = Toolkit.getDefaultToolkit().getImage("images/moose.gif");
28 * @param seq The sequence that the program is operating on.
30 public MooGUI(Sequence seq) {
36 Container pane = getContentPane();
37 pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
45 toolbar = new MooToolbar();
46 pane.add(toolbar, BorderLayout.NORTH);
50 view = new MooView(seq.getTracks());
51 pane.add(view, BorderLayout.CENTER);
54 statusBar = new JLabel("Welcome to Moosique!", JLabel.CENTER);
55 statusBar.setFont(FONT);
56 pane.add(statusBar, BorderLayout.SOUTH);
58 // Brings on the colors!
61 setBackground(toolbar);
65 timer = new java.util.Timer();
67 // Sets up global key listener.
68 ActionMap am = getRootPane().getActionMap();
70 Action playAction = new AbstractAction() {
71 public void actionPerformed(ActionEvent ae) {
72 if (!Moosique.getSequencer().isRunning()) {
78 am.put("Play", playAction);
79 am.put("Change octave up", createOctaveAction(1));
80 am.put("Change octave down", createOctaveAction(-1));
82 InputMap im = getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
83 KeyStroke playKey = KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0);
84 KeyStroke octaveUpKey = KeyStroke.getKeyStroke(KeyEvent.VK_F9, 0);
85 KeyStroke octaveDownKey = KeyStroke.getKeyStroke(KeyEvent.VK_F10, 0);
86 im.put(playKey, "Play");
87 im.put(octaveUpKey, "Change octave up");
88 im.put(octaveDownKey, "Change octave down");
92 addWindowListener(new MooGUICloser());
95 Dimension bounds = Toolkit.getDefaultToolkit().getScreenSize();
96 setSize(bounds.width,bounds.height - 28);
98 setBackground(Color.white);
105 * Sets the background on Containers
106 * @param c the Container that will have it's background change
108 private void setBackground(Container c) {
109 c.setBackground(bgColor);
110 Component[] comps = c.getComponents();
111 for (int i = 0; i < comps.length; i++) {
112 comps[i].setBackground(bgColor);
117 * Changes the sequence of the GUI.
118 * @param sequence the MIDI sequence to visualize
120 public void setSequence(Sequence sequence) {
122 view.setTracks(seq.getTracks(), true);
123 toolbar.resetProgInd();
127 * Shows the given message in the status bar.
128 * @param text the message to show
130 public void setStatus(String text) {
131 statusBar.setText(text);
132 timer.schedule(new StatusResetTask(), statusResetDelay);
136 * Calls on the main view to update the track views,
137 * and on the toolbar to update the progress indicator.
139 public void update(long tickPosition){
140 view.update(tickPosition);
141 toolbar.updateProgInd(tickPosition);
145 * Creates an action for a specific octave.
146 * @param octave The octave we want an action for.
148 private Action createOctaveAction(final int octave) {
149 Action octaveAction = new AbstractAction() {
150 public void actionPerformed(ActionEvent ae) {
151 MooKeyboard.setOctave(octave);
156 private void advanceStatus() {
157 System.out.print(".");
161 * Listener for closing the program
163 class MooGUICloser extends WindowAdapter {
164 public void windowClosing(WindowEvent e) {
170 * TimerTask that resets the statusbar
172 class StatusResetTask extends TimerTask {