]> ruin.nu Git - moosique.git/blob - MooGUI.java
no message
[moosique.git] / MooGUI.java
1 import javax.sound.midi.*;
2 import javax.swing.*;
3 import java.awt.*;
4 import java.awt.event.*;
5 import java.util.*;
6
7 /**
8  * Moosique's graphical user interface.
9  * 
10  * @author  Einar Pehrson
11  */
12  
13 public class MooGUI extends JFrame {
14
15         private Sequence seq;
16         private MooMenu menu;
17         private MooToolbar toolbar;
18         private MooView view;
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");
25         
26         /** 
27          * Creates the GUI.
28          * @param seq The sequence that the program is operating on.
29          */
30         public MooGUI(Sequence seq) {
31                 super("Moosique");
32
33                 this.seq = seq;
34                 advanceStatus();
35                 
36                 Container pane = getContentPane();
37                 pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
38
39                 // Adds menu bar.
40                 menu = new MooMenu();
41                 setJMenuBar(menu);
42                 advanceStatus();
43
44                 // Adds toolbar.
45                 toolbar = new MooToolbar();
46                 pane.add(toolbar, BorderLayout.NORTH);
47                 advanceStatus();
48
49                 // Adds main view.
50                 view = new MooView(seq.getTracks());
51                 pane.add(view, BorderLayout.CENTER);
52
53                 // Adds status bar.
54                 statusBar = new JLabel("Welcome to Moosique!", JLabel.CENTER);
55                 statusBar.setFont(FONT);
56                 pane.add(statusBar, BorderLayout.SOUTH);
57
58                 // Brings on the colors!
59                 setBackground(pane);
60                 setBackground(menu);
61                 setBackground(toolbar);
62                 setBackground(view);
63
64                 // Creates timer.
65                 timer = new java.util.Timer();
66
67                 // Sets up global key listener.
68                 ActionMap am = getRootPane().getActionMap();
69
70                 Action playAction = new AbstractAction() {
71                         public void actionPerformed(ActionEvent ae) {
72                                 if (!Moosique.getSequencer().isRunning()) {
73                                         Moosique.play();
74                                 } else {
75                                         Moosique.stop();
76                                 }
77                         }};
78                 am.put("Play", playAction);
79                 am.put("Change octave up", createOctaveAction(1));
80                 am.put("Change octave down", createOctaveAction(-1));
81
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");
89                 advanceStatus();
90
91                 // Configures window.
92                 addWindowListener(new MooGUICloser());
93                 pack();
94                 setIconImage(logo);
95                 Dimension bounds = Toolkit.getDefaultToolkit().getScreenSize();
96                 setSize(bounds.width,bounds.height - 28);
97                 setLocation(0, 0);
98                 setBackground(Color.white);
99                 advanceStatus();
100                 setVisible(true);
101                 show();
102         }
103
104         /**
105          * Sets the background on Containers
106          * @param c the Container that will have it's background change
107          */
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);
113                 }
114         }
115
116         /** 
117          * Changes the sequence of the GUI.
118          * @param sequence      the MIDI sequence to visualize
119          */
120         public void setSequence(Sequence sequence) {
121                 seq = sequence;
122                 view.setTracks(seq.getTracks(), true);
123                 toolbar.resetProgInd();
124         }
125
126         /** 
127          * Shows the given message in the status bar.
128          * @param text  the message to show
129          */
130         public void setStatus(String text) {
131                 statusBar.setText(text);
132                 timer.schedule(new StatusResetTask(), statusResetDelay);
133         }
134
135         /**
136          * Calls on the main view to update the track views,
137          * and on the toolbar to update the progress indicator.
138          */
139         public void update(long tickPosition){
140                 view.update(tickPosition);
141                 toolbar.updateProgInd(tickPosition);
142         }
143
144         /**
145          * Creates an action for a specific octave.
146          * @param octave The octave we want an action for.
147          */
148         private Action createOctaveAction(final int octave) {
149                 Action octaveAction = new AbstractAction() {
150                         public void actionPerformed(ActionEvent ae) {
151                                 MooKeyboard.setOctave(octave);
152                         }};
153                 return octaveAction;
154         }
155
156         private void advanceStatus() {
157                 System.out.print(".");
158         }
159
160         /**
161          * Listener for closing the program
162          */
163         class MooGUICloser extends WindowAdapter {
164                 public void windowClosing(WindowEvent e) {
165                         Moosique.quit();
166                 }
167         }
168         
169         /**
170          * TimerTask that resets the statusbar
171          */
172         class StatusResetTask extends TimerTask {
173                 public void run() {
174                         setStatus(" ");
175                 }
176         }
177 }