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