]> ruin.nu Git - moosique.git/blob - MooGUI.java
*** empty log 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         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);
25         
26         /** 
27          * Creates the GUI.
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         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);
112                 }
113         }
114
115         /** 
116          * Changes the sequence of the GUI.
117          * @param sequence      the MIDI sequence to visualize
118          */
119         public void setSequence(Sequence sequence) {
120                 seq = sequence;
121                 view.setTracks(seq.getTracks());
122                 toolbar.resetProgInd();
123         }
124
125         /** 
126          * Shows the given message in the status bar.
127          * @param text  the message to show
128          */
129         public void setStatus(String text) {
130                 statusBar.setText(text);
131                 timer.schedule(new StatusResetTask(), statusResetDelay);
132         }
133
134         /**
135          * Calls on the main view to update the track views,
136          * and on the toolbar to update the progress indicator.
137          */
138         public void update(long tickPosition){
139                 view.update(tickPosition);
140                 toolbar.updateProgInd(tickPosition);
141         }
142
143         /** 
144          * Shows the given message in the status bar.
145          * @param text  the message to show
146          */
147         public boolean drawEmptyTracks() {
148                 return drawEmptyTracks;
149         }
150
151         /** 
152          * Shows the given message in the status bar.
153          * @param text  the message to show
154          */
155         public void setDrawEmptyTracks(boolean state) {
156                 drawEmptyTracks = state;
157         }
158
159         private Action createOctaveAction(final int octave) {
160                 Action octaveAction = new AbstractAction() {
161                         public void actionPerformed(ActionEvent ae) {
162                                 MooKeyboard.setOctave(octave);
163                         }};
164                 return octaveAction;
165         }
166
167         class MooGUICloser extends WindowAdapter {
168                 public void windowClosing(WindowEvent e) {
169                         Moosique.quit();
170                 }
171         }
172         
173         class StatusResetTask extends TimerTask {
174                 public void run() {
175                         setStatus(" ");
176                 }
177         }
178 }