]> ruin.nu Git - moosique.git/blobdiff - MooGUI.java
no message
[moosique.git] / MooGUI.java
index f0c108ae7a800831ecc371584081433c34fc29d6..e1ee025a60476934fcabe2a2fc03836e4e891748 100644 (file)
@@ -2,25 +2,29 @@ import javax.sound.midi.*;
 import javax.swing.*;
 import java.awt.*;
 import java.awt.event.*;
+import java.util.*;
 
 /**
  * Moosique's graphical user interface.
  * 
- * @author  Mikael Andreen
+ * @author  Einar Pehrson
  */
  
-public class MooGUI extends JFrame implements WindowListener {
+public class MooGUI extends JFrame {
 
        private Sequence seq;
-
-       private JPanel trackPanel;
-       private MooTrackView[] trackViews;
+       private MooMenu menu;
+       private MooToolbar toolbar;
+       private MooView view;
        private JLabel statusBar;
-
-       public static Font standardFont = new Font("Helvetica", Font.PLAIN, 10);
+       private java.util.Timer timer;
+       public static final int statusResetDelay = 3000;
+       public static final Font FONT = new Font("Helvetica", Font.PLAIN, 10);
+       public static final Color bgColor = new Color(192, 224, 255);
        
        /** 
         * Creates the GUI.
+        * @param seq The sequence that the program is operating on.
         */
        public MooGUI(Sequence seq) {
                super("Moosique");
@@ -28,42 +32,98 @@ public class MooGUI extends JFrame implements WindowListener {
                this.seq = seq;
                
                Container pane = getContentPane();
-               pane.setLayout(new BorderLayout());
-               
+               pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
+
                // Adds menu bar.
-               setJMenuBar(new MooMenu());
+               menu = new MooMenu();
+               setJMenuBar(menu);
 
                // Adds toolbar.
-               pane.add(new MooToolbar(), BorderLayout.NORTH);
-               addWindowListener(this);
+               toolbar = new MooToolbar();
+               pane.add(toolbar, BorderLayout.NORTH);
 
-               // Adds tracks.
-               trackPanel = new JPanel(true);
-               createTrackViews();
-               pane.add(trackPanel, BorderLayout.CENTER);
+               // Adds main view.
+               view = new MooView(seq.getTracks());
+               pane.add(view, BorderLayout.CENTER);
 
                // Adds status bar.
                statusBar = new JLabel("Welcome to Moosique!", JLabel.CENTER);
+               statusBar.setFont(FONT);
                pane.add(statusBar, BorderLayout.SOUTH);
 
-               addWindowListener(this);
+               // Brings on the colors!
+               setBackground(pane);
+               setBackground(menu);
+               setBackground(toolbar);
+               setBackground(view);
+               statusBar.setBackground(bgColor);
+               view.setBackground(bgColor);
+
+               // Creates timer.
+               timer = new java.util.Timer();
+
+               // Sets up global key listener.
+               ActionMap am = getRootPane().getActionMap();
+
+               Action playAction = new AbstractAction() {
+                       public void actionPerformed(ActionEvent ae) {
+                               if (!Moosique.getSequencer().isRunning()) {
+                                       Moosique.play();
+                               } else {
+                                       Moosique.stop();
+                               }
+                       }};
+               am.put("Play", playAction);
+               am.put("Octave change 2", createOctaveAction(2));
+               am.put("Octave change 4", createOctaveAction(4));
+               am.put("Octave change 6", createOctaveAction(6));
+               am.put("Octave change 8", createOctaveAction(8));
+
+               InputMap im = getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
+               KeyStroke playKey = KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0);
+               KeyStroke octave2Key = KeyStroke.getKeyStroke(KeyEvent.VK_F9, 0);
+               KeyStroke octave4Key = KeyStroke.getKeyStroke(KeyEvent.VK_F10, 0);
+               KeyStroke octave6Key = KeyStroke.getKeyStroke(KeyEvent.VK_F11, 0);
+               KeyStroke octave8Key = KeyStroke.getKeyStroke(KeyEvent.VK_F12, 0);
+               im.put(playKey, "Play");
+               im.put(octave2Key, "Octave change 2");
+               im.put(octave4Key, "Octave change 4");
+               im.put(octave6Key, "Octave change 6");
+               im.put(octave8Key, "Octave change 8");
+
+               // Configures window.
+               addWindowListener(new MooGUICloser());
                pack();
-               Dimension bounds = new Dimension(400,300);
-               setSize(bounds.width,bounds.height);
-               setLocation((Toolkit.getDefaultToolkit().getScreenSize().width / 2) - (bounds.width / 2), (Toolkit.getDefaultToolkit().getScreenSize().height / 2) - (bounds.height / 2));
+               setIconImage(Toolkit.getDefaultToolkit().getImage("images/moose.gif"));
+               Dimension bounds = Toolkit.getDefaultToolkit().getScreenSize();
+               setSize(bounds.width,bounds.height - 40);
+               setLocation(0, 0);
                // setResizable(false);
-               // setBackground(Color.white);
+               setBackground(Color.white);
                setVisible(true);
                show();
        }
-       
+
+       /**
+        * Sets the background on Containers
+        * @param c the Container that will have it's background change
+        */
+       private void setBackground(Container c) {
+               c.setBackground(bgColor);
+               Component[] comps = c.getComponents();
+               for (int i = 0; i < comps.length; i++) {
+                       comps[i].setBackground(bgColor);
+               }
+       }
+
        /** 
         * Changes the sequence of the GUI.
         * @param sequence      the MIDI sequence to visualize
         */
        public void setSequence(Sequence sequence) {
                seq = sequence;
-               createTrackViews();
+               view.setTracks(seq.getTracks(), true);
+               toolbar.resetProgInd();
        }
 
        /** 
@@ -72,23 +132,45 @@ public class MooGUI extends JFrame implements WindowListener {
         */
        public void setStatus(String text) {
                statusBar.setText(text);
+               timer.schedule(new StatusResetTask(), statusResetDelay);
+       }
+
+       /**
+        * Calls on the main view to update the track views,
+        * and on the toolbar to update the progress indicator.
+        */
+       public void update(long tickPosition){
+               view.update(tickPosition);
+               toolbar.updateProgInd(tickPosition);
+       }
+
+       /**
+        * Creates an action for a specific octave.
+        * @param octave The octave we want an action for.
+        */
+       private Action createOctaveAction(final int octave) {
+               Action octaveAction = new AbstractAction() {
+                       public void actionPerformed(ActionEvent ae) {
+                               MooKeyboard.setOctave(octave);
+                       }};
+               return octaveAction;
+       }
+
+       /**
+        * Listener for closing the program
+        */
+       class MooGUICloser extends WindowAdapter {
+               public void windowClosing(WindowEvent e) {
+                       Moosique.quit();
+               }
        }
        
-       private void createTrackViews() {
-               trackPanel.removeAll();
-               Track[] tracks = seq.getTracks();
-               trackViews = new MooTrackView[tracks.length];
-               for (int i = 0; i < tracks.length; i++) {
-                       trackViews[i] = new MooTrackView(tracks[i]);
-                       trackPanel.add(new MooTrackView(tracks[i]));
+       /**
+        * TimerTask that resets the statusbar
+        */
+       class StatusResetTask extends TimerTask {
+               public void run() {
+                       setStatus(" ");
                }
        }
-
-       public void windowOpened(WindowEvent e) {}
-       public void windowClosing(WindowEvent e) {Moosique.quit();}
-       public void windowClosed(WindowEvent e) {}
-       public void windowIconified(WindowEvent e) {}
-       public void windowDeiconified(WindowEvent e) {}
-       public void windowActivated(WindowEvent e) {}
-       public void windowDeactivated(WindowEvent e) {}
 }