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