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) {
35 Container pane = getContentPane();
36 pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
44 toolbar = new MooToolbar();
45 pane.add(toolbar, BorderLayout.NORTH);
49 view = new MooView(seq.getTracks());
50 pane.add(view, BorderLayout.CENTER);
53 statusBar = new JLabel("Welcome to Moosique!", JLabel.CENTER);
54 statusBar.setFont(FONT);
55 pane.add(statusBar, BorderLayout.SOUTH);
57 // Brings on the colors!
60 setBackground(toolbar);
62 statusBar.setBackground(bgColor);
63 view.setBackground(bgColor);
66 timer = new java.util.Timer();
68 // Sets up global key listener.
69 ActionMap am = getRootPane().getActionMap();
71 Action playAction = new AbstractAction() {
72 public void actionPerformed(ActionEvent ae) {
73 if (!Moosique.getSequencer().isRunning()) {
79 am.put("Play", playAction);
80 am.put("Octave change 2", createOctaveAction(2));
81 am.put("Octave change 4", createOctaveAction(4));
82 am.put("Octave change 6", createOctaveAction(6));
83 am.put("Octave change 8", createOctaveAction(8));
85 InputMap im = getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
86 KeyStroke playKey = KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0);
87 KeyStroke octave2Key = KeyStroke.getKeyStroke(KeyEvent.VK_F9, 0);
88 KeyStroke octave4Key = KeyStroke.getKeyStroke(KeyEvent.VK_F10, 0);
89 KeyStroke octave6Key = KeyStroke.getKeyStroke(KeyEvent.VK_F11, 0);
90 KeyStroke octave8Key = KeyStroke.getKeyStroke(KeyEvent.VK_F12, 0);
91 im.put(playKey, "Play");
92 im.put(octave2Key, "Octave change 2");
93 im.put(octave4Key, "Octave change 4");
94 im.put(octave6Key, "Octave change 6");
95 im.put(octave8Key, "Octave change 8");
99 addWindowListener(new MooGUICloser());
101 setIconImage(Toolkit.getDefaultToolkit().getImage("images/moose.gif"));
102 Dimension bounds = Toolkit.getDefaultToolkit().getScreenSize();
103 setSize(bounds.width,bounds.height - 40);
105 setBackground(Color.white);
112 * Sets the background on Containers
113 * @param c the Container that will have it's background change
115 private void setBackground(Container c) {
116 c.setBackground(bgColor);
117 Component[] comps = c.getComponents();
118 for (int i = 0; i < comps.length; i++) {
119 comps[i].setBackground(bgColor);
124 * Changes the sequence of the GUI.
125 * @param sequence the MIDI sequence to visualize
127 public void setSequence(Sequence sequence) {
129 view.setTracks(seq.getTracks(), true);
130 toolbar.resetProgInd();
134 * Shows the given message in the status bar.
135 * @param text the message to show
137 public void setStatus(String text) {
138 statusBar.setText(text);
139 timer.schedule(new StatusResetTask(), statusResetDelay);
143 * Calls on the main view to update the track views,
144 * and on the toolbar to update the progress indicator.
146 public void update(long tickPosition){
147 view.update(tickPosition);
148 toolbar.updateProgInd(tickPosition);
152 * Creates an action for a specific octave.
153 * @param octave The octave we want an action for.
155 private Action createOctaveAction(final int octave) {
156 Action octaveAction = new AbstractAction() {
157 public void actionPerformed(ActionEvent ae) {
158 MooKeyboard.setOctave(octave);
163 private void advanceStatus() {
164 System.out.print(".");
168 * Listener for closing the program
170 class MooGUICloser extends WindowAdapter {
171 public void windowClosing(WindowEvent e) {
177 * TimerTask that resets the statusbar
179 class StatusResetTask extends TimerTask {