]> 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
6 /**
7  * Moosique's graphical user interface.
8  * 
9  * @author  Einar Pehrson
10  */
11  
12 public class MooGUI extends JFrame {
13
14         private Sequence seq;
15         private MooMenu menu;
16         private MooToolbar toolbar;
17         private MooView view;
18         private JLabel statusBar;
19         public static final Font FONT = new Font("Helvetica", Font.PLAIN, 10);
20         public static final Color bgColor = new Color(192, 224, 255);
21         
22         /** 
23          * Creates the GUI.
24          */
25         public MooGUI(Sequence seq) {
26                 super("Moosique");
27
28                 this.seq = seq;
29                 
30                 Container pane = getContentPane();
31                 pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
32
33                 // Adds menu bar.
34                 menu = new MooMenu();
35                 setJMenuBar(menu);
36
37                 // Adds toolbar.
38                 toolbar = new MooToolbar();
39                 pane.add(toolbar, BorderLayout.NORTH);
40
41                 // Adds main view.
42                 view = new MooView(seq.getTracks());
43                 pane.add(view, BorderLayout.CENTER);
44
45                 // Adds status bar.
46                 statusBar = new JLabel("Welcome to Moosique!", JLabel.CENTER);
47                 statusBar.setFont(FONT);
48                 pane.add(statusBar, BorderLayout.SOUTH);
49
50                 // Brings on the colors!
51                 setBackground(pane);
52                 setBackground(menu);
53                 setBackground(toolbar);
54                 setBackground(view);
55                 statusBar.setBackground(bgColor);
56                 view.setBackground(bgColor);
57
58                 // Sets up global key listener
59                 ActionMap am = getRootPane().getActionMap();
60
61                 Action playAction = new AbstractAction() {
62                         public void actionPerformed(ActionEvent ae) {
63                                 if (!Moosique.getSequencer().isRunning()) {
64                                         Moosique.play();
65                                 } else {
66                                         Moosique.stop();
67                                 }
68                         }};
69                 am.put("Play", playAction);
70
71                 InputMap im = getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
72                 KeyStroke playKey = KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0);
73                 im.put(playKey, "Play");
74
75                 // Configures window.
76                 addWindowListener(new MooGUICloser());
77                 pack();
78                 setIconImage(Toolkit.getDefaultToolkit().getImage("images/moose.gif"));
79                 Dimension bounds = Toolkit.getDefaultToolkit().getScreenSize();
80                 setSize(bounds.width,bounds.height - 40);
81                 setLocation(0, 0);
82                 // setResizable(false);
83                 setBackground(Color.white);
84                 setVisible(true);
85                 show();
86         }
87
88         private void setBackground(Container c) {
89                 c.setBackground(bgColor);
90                 Component[] comps = c.getComponents();
91                 for (int i = 0; i < comps.length; i++) {
92                         comps[i].setBackground(bgColor);
93                 }
94         }
95
96         /** 
97          * Changes the sequence of the GUI.
98          * @param sequence      the MIDI sequence to visualize
99          */
100         public void setSequence(Sequence sequence) {
101                 seq = sequence;
102                 view.setTracks(seq.getTracks());
103                 toolbar.resetProgInd();
104         }
105
106         /** 
107          * Shows the given message in the status bar.
108          * @param text  the message to show
109          */
110         public void setStatus(String text) {
111                 statusBar.setText(text);
112         }
113
114         /**
115          * Calls on the main view to update the track views,
116          * and on the toolbar to update the progress indicator.
117          */
118         public void update(long tickPosition){
119                 view.update(tickPosition);
120                 toolbar.updateProgInd(tickPosition);
121         }
122
123         class MooGUICloser extends WindowAdapter {
124                 public void windowClosing(WindowEvent e) {
125                         Moosique.quit();
126                 }
127         }
128 }