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