]> ruin.nu Git - moosique.git/blob - MooGUI.java
stuff
[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
29                 this.seq = seq;
30                 
31                 Container pane = getContentPane();
32                 pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
33
34                 // Adds menu bar.
35                 menu = new MooMenu();
36                 setJMenuBar(menu);
37
38                 // Adds toolbar.
39                 toolbar = new MooToolbar();
40                 pane.add(toolbar, BorderLayout.NORTH);
41
42                 // Adds main view.
43                 view = new MooView(seq.getTracks());
44                 pane.add(view, BorderLayout.CENTER);
45
46                 // Adds status bar.
47                 statusBar = new JLabel("Welcome to Moosique!", JLabel.CENTER);
48                 statusBar.setFont(FONT);
49                 pane.add(statusBar, BorderLayout.SOUTH);
50
51                 // Brings on the colors!
52                 setBackground(pane);
53                 setBackground(menu);
54                 setBackground(toolbar);
55                 setBackground(view);
56                 statusBar.setBackground(bgColor);
57                 view.setBackground(bgColor);
58
59                 // Sets up global key listener
60                 ActionMap am = getRootPane().getActionMap();
61
62                 Action playAction = new AbstractAction() {
63                         public void actionPerformed(ActionEvent ae) {
64                                 if (!Moosique.getSequencer().isRunning()) {
65                                         Moosique.play();
66                                 } else {
67                                         Moosique.stop();
68                                 }
69                         }};
70                 am.put("Play", playAction);
71
72                 InputMap im = getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
73                 KeyStroke playKey = KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0);
74                 im.put(playKey, "Play");
75
76                 // Configures window.
77                 addWindowListener(new MooGUICloser());
78                 pack();
79                 setIconImage(Toolkit.getDefaultToolkit().getImage("images/moose.gif"));
80                 Dimension bounds = Toolkit.getDefaultToolkit().getScreenSize();
81                 setSize(bounds.width,bounds.height - 40);
82                 setLocation(0, 0);
83                 // setResizable(false);
84                 setBackground(Color.white);
85                 setVisible(true);
86                 show();
87         }
88
89         private void setBackground(Container c) {
90                 c.setBackground(bgColor);
91                 Component[] comps = c.getComponents();
92                 for (int i = 0; i < comps.length; i++) {
93                         comps[i].setBackground(bgColor);
94                 }
95         }
96
97         /** 
98          * Changes the sequence of the GUI.
99          * @param sequence      the MIDI sequence to visualize
100          */
101         public void setSequence(Sequence sequence) {
102                 seq = sequence;
103                 view.setTracks(seq.getTracks());
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 }