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