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 private boolean updateView = true;
23 public static final int statusResetDelay = 3000;
24 public static final Font FONT = new Font("Helvetica", Font.PLAIN, 10);
25 public static final Color bgColor = new Color(192, 224, 255);
26 public static final Image logo = Toolkit.getDefaultToolkit().getImage("images/moose.gif");
30 * @param seq The sequence that the program is operating on.
32 public MooGUI(Sequence seq, File file) {
34 if (file != null) setTitle("Moosique - " + file.getName());
39 Container pane = getContentPane();
40 pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
48 toolbar = new MooToolbar();
49 pane.add(toolbar, BorderLayout.NORTH);
53 view = new MooView(seq.getTracks());
54 pane.add(view, BorderLayout.CENTER);
57 statusBar = new JLabel("Welcome to Moosique!", JLabel.CENTER);
58 statusBar.setFont(FONT);
59 pane.add(statusBar, BorderLayout.SOUTH);
61 // Brings on the colors!
64 setBackground(toolbar);
68 timer = new java.util.Timer();
70 // Sets up global key listener.
71 ActionMap am = getRootPane().getActionMap();
73 Action playAction = new AbstractAction() {
74 public void actionPerformed(ActionEvent ae) {
75 if (!Moosique.getSequencer().isRunning()) {
81 am.put("Play", playAction);
82 am.put("Change octave up", createOctaveAction(true));
83 am.put("Change octave down", createOctaveAction(false));
85 InputMap im = getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
86 KeyStroke playKey = KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0);
87 KeyStroke octaveUpKey = KeyStroke.getKeyStroke(KeyEvent.VK_F9, 0);
88 KeyStroke octaveDownKey = KeyStroke.getKeyStroke(KeyEvent.VK_F10, 0);
89 im.put(playKey, "Play");
90 im.put(octaveUpKey, "Change octave up");
91 im.put(octaveDownKey, "Change octave down");
95 addWindowListener(new MooGUICloser());
98 Dimension bounds = Toolkit.getDefaultToolkit().getScreenSize();
99 setSize(bounds.width,bounds.height - 28);
101 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, File file) {
125 if (file != null) setTitle("Moosique - " + file.getName());
126 else setTitle("Moosique");
127 view.setTracks(seq.getTracks(), true);
128 toolbar.resetProgInd();
132 * Shows the given message in the status bar.
133 * @param text the message to show
135 public void setStatus(String text) {
136 statusBar.setText(text);
137 timer.schedule(new StatusResetTask(), statusResetDelay);
141 * Calls on the main view to update the track views,
142 * and on the toolbar to update the progress indicator.
144 public synchronized void update(long tickPosition){
145 if (updateView) view.update(tickPosition);
146 toolbar.updateProgInd(tickPosition);
150 * Creates an action for a specific octave.
151 * @param increase true for increase, false for decrease
153 private Action createOctaveAction(final boolean increase) {
154 Action octaveAction = new AbstractAction() {
155 public void actionPerformed(ActionEvent ae) {
156 MooKeyboard.setRelativeOctave(increase);
162 * Advances the current progress counter by printing a "." to the System output.
164 private void advanceStatus() {
165 System.out.print(".");
169 * Listener for closing the program
171 class MooGUICloser extends WindowAdapter {
172 public void windowClosing(WindowEvent e) {
178 * TimerTask that resets the statusbar
180 class StatusResetTask extends TimerTask {