]> ruin.nu Git - moosique.git/blob - MooGUI.java
setting mute and solo on channel instead..
[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          */
28         public MooGUI(Sequence seq) {
29                 super("Moosique");
30
31                 this.seq = seq;
32                 
33                 Container pane = getContentPane();
34                 pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
35
36                 // Adds menu bar.
37                 menu = new MooMenu();
38                 setJMenuBar(menu);
39
40                 // Adds toolbar.
41                 toolbar = new MooToolbar();
42                 pane.add(toolbar, BorderLayout.NORTH);
43
44                 // Adds main view.
45                 view = new MooView(seq.getTracks());
46                 pane.add(view, BorderLayout.CENTER);
47
48                 // Adds status bar.
49                 statusBar = new JLabel("Welcome to Moosique!", JLabel.CENTER);
50                 statusBar.setFont(FONT);
51                 pane.add(statusBar, BorderLayout.SOUTH);
52
53                 // Brings on the colors!
54                 setBackground(pane);
55                 setBackground(menu);
56                 setBackground(toolbar);
57                 setBackground(view);
58                 statusBar.setBackground(bgColor);
59                 view.setBackground(bgColor);
60
61                 // Creates timer.
62                 timer = new java.util.Timer();
63
64                 // Sets up global key listener.
65                 ActionMap am = getRootPane().getActionMap();
66
67                 Action playAction = new AbstractAction() {
68                         public void actionPerformed(ActionEvent ae) {
69                                 if (!Moosique.getSequencer().isRunning()) {
70                                         Moosique.play();
71                                 } else {
72                                         Moosique.stop();
73                                 }
74                         }};
75                 am.put("Play", playAction);
76                 am.put("Octave change 2", createOctaveAction(2));
77                 am.put("Octave change 4", createOctaveAction(4));
78                 am.put("Octave change 6", createOctaveAction(6));
79                 am.put("Octave change 8", createOctaveAction(8));
80
81                 InputMap im = getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
82                 KeyStroke playKey = KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0);
83                 KeyStroke octave2Key = KeyStroke.getKeyStroke(KeyEvent.VK_F9, 0);
84                 KeyStroke octave4Key = KeyStroke.getKeyStroke(KeyEvent.VK_F10, 0);
85                 KeyStroke octave6Key = KeyStroke.getKeyStroke(KeyEvent.VK_F11, 0);
86                 KeyStroke octave8Key = KeyStroke.getKeyStroke(KeyEvent.VK_F12, 0);
87                 im.put(playKey, "Play");
88                 im.put(octave2Key, "Octave change 2");
89                 im.put(octave4Key, "Octave change 4");
90                 im.put(octave6Key, "Octave change 6");
91                 im.put(octave8Key, "Octave change 8");
92
93                 // Configures window.
94                 addWindowListener(new MooGUICloser());
95                 pack();
96                 setIconImage(Toolkit.getDefaultToolkit().getImage("images/moose.gif"));
97                 Dimension bounds = Toolkit.getDefaultToolkit().getScreenSize();
98                 setSize(bounds.width,bounds.height - 40);
99                 setLocation(0, 0);
100                 // setResizable(false);
101                 setBackground(Color.white);
102                 setVisible(true);
103                 show();
104         }
105
106         private void setBackground(Container c) {
107                 c.setBackground(bgColor);
108                 Component[] comps = c.getComponents();
109                 for (int i = 0; i < comps.length; i++) {
110                         comps[i].setBackground(bgColor);
111                 }
112         }
113
114         /** 
115          * Changes the sequence of the GUI.
116          * @param sequence      the MIDI sequence to visualize
117          */
118         public void setSequence(Sequence sequence) {
119                 seq = sequence;
120                 view.setTracks(seq.getTracks());
121                 toolbar.resetProgInd();
122         }
123
124         /** 
125          * Shows the given message in the status bar.
126          * @param text  the message to show
127          */
128         public void setStatus(String text) {
129                 statusBar.setText(text);
130                 timer.schedule(new StatusResetTask(), statusResetDelay);
131         }
132
133         /**
134          * Calls on the main view to update the track views,
135          * and on the toolbar to update the progress indicator.
136          */
137         public void update(long tickPosition){
138                 view.update(tickPosition);
139                 toolbar.updateProgInd(tickPosition);
140         }
141
142         private Action createOctaveAction(final int octave) {
143                 Action octaveAction = new AbstractAction() {
144                         public void actionPerformed(ActionEvent ae) {
145                                 MooKeyboard.setOctave(octave);
146                         }};
147                 return octaveAction;
148         }
149
150         class MooGUICloser extends WindowAdapter {
151                 public void windowClosing(WindowEvent e) {
152                         Moosique.quit();
153                 }
154         }
155         
156         class StatusResetTask extends TimerTask {
157                 public void run() {
158                         setStatus(" ");
159                 }
160         }
161 }