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 private boolean drawEmptyTracks = false;
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);
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);
107 private void setBackground(Container c) {
108 c.setBackground(bgColor);
109 Component[] comps = c.getComponents();
110 for (int i = 0; i < comps.length; i++) {
111 comps[i].setBackground(bgColor);
116 * Changes the sequence of the GUI.
117 * @param sequence the MIDI sequence to visualize
119 public void setSequence(Sequence sequence) {
121 view.setTracks(seq.getTracks());
122 toolbar.resetProgInd();
126 * Shows the given message in the status bar.
127 * @param text the message to show
129 public void setStatus(String text) {
130 statusBar.setText(text);
131 timer.schedule(new StatusResetTask(), statusResetDelay);
135 * Calls on the main view to update the track views,
136 * and on the toolbar to update the progress indicator.
138 public void update(long tickPosition){
139 view.update(tickPosition);
140 toolbar.updateProgInd(tickPosition);
144 * Shows the given message in the status bar.
145 * @param text the message to show
147 public boolean drawEmptyTracks() {
148 return drawEmptyTracks;
152 * Shows the given message in the status bar.
153 * @param text the message to show
155 public void setDrawEmptyTracks(boolean state) {
156 drawEmptyTracks = state;
159 private Action createOctaveAction(final int octave) {
160 Action octaveAction = new AbstractAction() {
161 public void actionPerformed(ActionEvent ae) {
162 MooKeyboard.setOctave(octave);
167 class MooGUICloser extends WindowAdapter {
168 public void windowClosing(WindowEvent e) {
173 class StatusResetTask extends TimerTask {