]> ruin.nu Git - moosique.git/blob - MooGUI.java
nu är den klar
[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
77                 InputMap im = getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
78                 KeyStroke playKey = KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0);
79                 im.put(playKey, "Play");
80
81                 // Configures window.
82                 addWindowListener(new MooGUICloser());
83                 pack();
84                 setIconImage(Toolkit.getDefaultToolkit().getImage("images/moose.gif"));
85                 Dimension bounds = Toolkit.getDefaultToolkit().getScreenSize();
86                 setSize(bounds.width,bounds.height - 40);
87                 setLocation(0, 0);
88                 // setResizable(false);
89                 setBackground(Color.white);
90                 setVisible(true);
91                 show();
92         }
93
94         private void setBackground(Container c) {
95                 c.setBackground(bgColor);
96                 Component[] comps = c.getComponents();
97                 for (int i = 0; i < comps.length; i++) {
98                         comps[i].setBackground(bgColor);
99                 }
100         }
101
102         /** 
103          * Changes the sequence of the GUI.
104          * @param sequence      the MIDI sequence to visualize
105          */
106         public void setSequence(Sequence sequence) {
107                 seq = sequence;
108                 view.setTracks(seq.getTracks());
109                 toolbar.resetProgInd();
110         }
111
112         /** 
113          * Shows the given message in the status bar.
114          * @param text  the message to show
115          */
116         public void setStatus(String text) {
117                 statusBar.setText(text);
118                 timer.schedule(new StatusResetTask(), statusResetDelay);
119         }
120
121         /**
122          * Calls on the main view to update the track views,
123          * and on the toolbar to update the progress indicator.
124          */
125         public void update(long tickPosition){
126                 view.update(tickPosition);
127                 toolbar.updateProgInd(tickPosition);
128         }
129
130         class MooGUICloser extends WindowAdapter {
131                 public void windowClosing(WindowEvent e) {
132                         Moosique.quit();
133                 }
134         }
135         
136         class StatusResetTask extends TimerTask {
137                 public void run() {
138                         setStatus(" ");
139                 }
140         }
141 }