1 import javax.sound.midi.*;
4 import java.awt.event.*;
9 * Moosique's graphical user interface.
11 * @author Einar Pehrson
14 public class MooGUI extends JFrame {
18 private MooToolbar toolbar;
20 private JLabel statusBar;
21 private java.util.Timer timer;
22 public static final int statusResetDelay = 3000;
23 public static final Font FONT = new Font("Helvetica", Font.PLAIN, 10);
24 public static final Color bgColor = new Color(192, 224, 255);
25 public static final Image logo = Toolkit.getDefaultToolkit().getImage("images/moose.gif");
29 * @param seq The sequence that the program is operating on.
31 public MooGUI(Sequence seq, File file) {
33 if (file != null) setTitle("Moosique - " + file.getName());
38 Container pane = getContentPane();
39 pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
47 toolbar = new MooToolbar();
48 pane.add(toolbar, BorderLayout.NORTH);
52 view = new MooView(seq.getTracks());
53 pane.add(view, BorderLayout.CENTER);
56 statusBar = new JLabel("Welcome to Moosique!", JLabel.CENTER);
57 statusBar.setFont(FONT);
58 pane.add(statusBar, BorderLayout.SOUTH);
60 // Brings on the colors!
63 setBackground(toolbar);
67 timer = new java.util.Timer();
69 // Sets up global key listener.
70 ActionMap am = getRootPane().getActionMap();
72 Action playAction = new AbstractAction() {
73 public void actionPerformed(ActionEvent ae) {
74 if (!Moosique.getSequencer().isRunning()) {
80 am.put("Play", playAction);
81 am.put("Change octave up", createOctaveAction(true));
82 am.put("Change octave down", createOctaveAction(false));
84 InputMap im = getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
85 KeyStroke playKey = KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0);
86 KeyStroke octaveUpKey = KeyStroke.getKeyStroke(KeyEvent.VK_F9, 0);
87 KeyStroke octaveDownKey = KeyStroke.getKeyStroke(KeyEvent.VK_F10, 0);
88 im.put(playKey, "Play");
89 im.put(octaveUpKey, "Change octave up");
90 im.put(octaveDownKey, "Change octave down");
94 addWindowListener(new MooGUICloser());
97 Dimension bounds = Toolkit.getDefaultToolkit().getScreenSize();
98 setSize(bounds.width,bounds.height - 28);
100 setBackground(Color.white);
107 * Sets the background on Containers
108 * @param c the Container that will have it's background change
110 private void setBackground(Container c) {
111 c.setBackground(bgColor);
112 Component[] comps = c.getComponents();
113 for (int i = 0; i < comps.length; i++) {
114 comps[i].setBackground(bgColor);
119 * Changes the sequence of the GUI.
120 * @param sequence the MIDI sequence to visualize
122 public void setSequence(Sequence sequence) {
124 view.setTracks(seq.getTracks(), true);
125 toolbar.resetProgInd();
129 * Shows the given message in the status bar.
130 * @param text the message to show
132 public void setStatus(String text) {
133 statusBar.setText(text);
134 timer.schedule(new StatusResetTask(), statusResetDelay);
138 * Calls on the main view to update the track views,
139 * and on the toolbar to update the progress indicator.
141 public void update(long tickPosition){
142 view.update(tickPosition);
143 toolbar.updateProgInd(tickPosition);
147 * Creates an action for a specific octave.
148 * @param increase true for increase, false for decrease
150 private Action createOctaveAction(final boolean increase) {
151 Action octaveAction = new AbstractAction() {
152 public void actionPerformed(ActionEvent ae) {
153 MooKeyboard.setRelativeOctave(increase);
158 private void advanceStatus() {
159 System.out.print(".");
163 * Listener for closing the program
165 class MooGUICloser extends WindowAdapter {
166 public void windowClosing(WindowEvent e) {
172 * TimerTask that resets the statusbar
174 class StatusResetTask extends TimerTask {