]> ruin.nu Git - moosique.git/blobdiff - MooToolbar.java
no message
[moosique.git] / MooToolbar.java
index 8df4f862e702a2d388b5aad06b3937a5efebf754..de416699065e7ba60b327b2c7dddc292f483bfc6 100644 (file)
+import javax.sound.midi.*;
 import javax.swing.*;
-import java.awt.event.*;
 import java.awt.*;
-import javax.sound.midi.*;
+import java.awt.event.*;
 
 /**
- * Moosiques GUI representing a toolbar, with the most frequently used commands.
+ * The application's toolbar, with the most frequently used commands.
  * 
  * @author  Björn Lanneskog
  */
-public class MooToolbar extends JToolBar implements ActionListener     {
+public class MooToolbar extends JToolBar {
 
-       private JButton rewind, playpause, stop, fastforward;
-       private JLabel measure, beats, ticks, measurevalue, beatsvalue, ticksvalue;
-       private Image pauseimage, playimage;
+       private JButton rewind, playpause, stop, fastforward, record;
+       private JLabel measure, beats, ticks, measuresValue, beatsValue, ticksValue;
+       private JPanel progIndPanel;
+       private ImageIcon playIcon, pauseIcon;
+       private MAdapter mouseAdapter;
+       private int ticksPerBeat; 
+       public static final int beatsPerMeasure = 4;
+       public static final Color bgColor = new Color(192, 224, 255);
        
        /**
-        * Constructs a toolbar
+        * Creates the toolbar.
         */
        
-       public MooToolbar()     {
-       
-               rewind = createButton("images/rewind.gif", "rewind");
-               add(rewind);
+       public MooToolbar() {
+               setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
+               // setAlignmentX(LEFT_ALIGNMENT);
+               setFloatable(false);
+               mouseAdapter = new MAdapter();
+
+               // Creates playback buttons
+               rewind = createButton("images/rewind.gif", "Rewind");
+               record = createButton("images/record.gif", "Record");
+               playpause = createButton("images/play.gif", "Play");
+               stop = createButton("images/stop.gif", "Stop");
+               fastforward = createButton("images/forward.gif", "Fast forward");
+               playIcon = new ImageIcon("images/play.gif");
+               pauseIcon = new ImageIcon("images/pause.gif");
 
-               playpause = createButton("images/play.gif", "play/pause");
+               // Adds playback buttons
+               add(rewind);
+               add(record);
                add(playpause);
-               
-               stop = createButton("images/stop.gif", "stop");
                add(stop);
-               
-               fastforward = createButton("images/forward.gif", "fast forward");
                add(fastforward);
                
-               JPanel panel = new JPanel();
-               panel.setMaximumSize(new Dimension(100,22));
-               panel.setLayout(new GridLayout(2,4));
-               add(panel);
-               
-               panel.add(new JPanel());
-               
-               measure = createProjIndLabel("Mrs",10);
-               panel.add(measure);
-               
-               beats = createProjIndLabel("Beat",10);
-               panel.add(beats);
-               
-               ticks = createProjIndLabel("Tick",10);
-               panel.add(ticks);
-               
-               panel.add(new JPanel());
-               
-               measurevalue = createProjIndLabel("1",10);
-               panel.add(measurevalue);
-               
-               beatsvalue = createProjIndLabel("1",10);
-               panel.add(beatsvalue);
-               
-               ticksvalue = createProjIndLabel("1",10);
-               panel.add(ticksvalue);
-               
-               playimage = Toolkit.getDefaultToolkit().getImage("images/play.gif");
-               pauseimage = Toolkit.getDefaultToolkit().getImage("images/pause.gif");
-               
-               }
-               
-               /**
-                * creates a JButton for the toolbar
-                * @param imagelocation         the imagelacation of the the image displayed in the button
-                * @param tooltip               the tooltip associated with this button
-                * @return button               the button to be attached to the toolbar
-                */
-               private JButton createButton(String imagelocation, String tooltip) {
-                       JButton button = new JButton (new ImageIcon(imagelocation));
-                       button.setToolTipText(tooltip);
-                       button.addActionListener(this);
-                       return button;
-               }
-               
-               /**
-                * creates JLabels for the progressindikator, attached to the toolbar
-                * @param title         the titel displayed on the label
-                * @param fontsize      the fontzise of the text displayed on the label
-                * @param titelvalue    the label that is a part of the progressindikator
-                */
-               private JLabel createProjIndLabel(String title, int fontsize){
-                       JLabel titelvalue = new JLabel(title,JLabel.CENTER);
-                       titelvalue.setFont(new Font("Times New Roman",
-                       Font.PLAIN ,fontsize));
-                       return titelvalue;
+               // Creates progress indicator
+               measure = createLabel("Msr", 10);
+               beats = createLabel("Beat", 10);
+               ticks = createLabel("Tick", 10);
+               measuresValue = formatProgInd(createLabel("1", 16));
+               beatsValue = formatProgInd(createLabel("1", 16));
+               ticksValue = formatProgInd(createLabel("1", 16));
+               JPanel spacenorth = new JPanel();
+               spacenorth.setBackground(bgColor);
+               JPanel spacesouth = new JPanel();
+               spacesouth.setBackground(bgColor);
+
+               // Creates progress indicator panel and adds components
+               progIndPanel = new JPanel();
+               progIndPanel.setMaximumSize(new Dimension(120,27));
+               progIndPanel.setLayout(new GridLayout(2,4));
+               progIndPanel.add(spacenorth);
+               progIndPanel.add(measure);
+               progIndPanel.add(beats);
+               progIndPanel.add(ticks);
+               progIndPanel.add(spacesouth);
+               progIndPanel.add(measuresValue);
+               progIndPanel.add(beatsValue);
+               progIndPanel.add(ticksValue);
+               add(progIndPanel);
+       }
+
+       /**
+        * Updates the progress indicator.
+        * @param tickPosition  the tick position to visualize
+        */
+       public void updateProgInd(long tickPosition) {
+               System.out.print("Updating to " + tickPosition + " = ");
+               if (tickPosition == 0) {
+                       resetProgInd();
+                       System.out.println("1:1:1");
+               } else {
+                       int[] position = Moosique.getPositionForTicks(tickPosition);
+                       System.out.println("" + (position[0] + 1) + ":" + (position[1] + 1) + ":" + (position[2] + 1));
+                       measuresValue.setText(Integer.toString(position[0] + 1));
+                       beatsValue.setText(Long.toString(position[1] + 1));
+                       ticksValue.setText(Long.toString(position[2] + 1));
                }
-               
-               // int pos = Moosique.getSequencer().getTickPosition();
-               // int measures = pos / (4 * Moosique.getSequence.get)
-               // int beats = (pos - measures * 4 * 96) / 96;
-               // int ticks = (pos - measures*4*96 - beats*96);
-               
-                       
-               public void actionPerformed(ActionEvent e) {
+       }
+
+       /**
+        * Resets the progress indicator.
+        */
+       public void resetProgInd() {
+               measuresValue.setText("1");
+               beatsValue.setText("1");
+               ticksValue.setText("1");
+               updatePlayButton("Play", playIcon);
+               ticksPerBeat = Moosique.getSequence().getResolution();
+       }
+
+       /**
+        * Creates a button with the specified image and action command / tooltip.
+        */
+       private JButton createButton(String imagelocation, String command) {
+               JButton button = new JButton (new ImageIcon(imagelocation));
+               button.setToolTipText(command);
+               button.setActionCommand(command);
+               button.addMouseListener(mouseAdapter);
+               return button;
+       }
        
-                      if (((JButton)e.getSource()).getToolTipText() == "rewind") {
-                               //få in rewindmetoden
-                       }       else if (((JButton)e.getSource()).getToolTipText() == "play/pause") {
-                                       ImageIcon playpauseIcon = ((ImageIcon)playpause.getIcon());
-                                       if (Moosique.getSequencer().isRunning()) {
-                                               Moosique.pause();
-                                               playpauseIcon.setImage(playimage);
-                                       } else {
-                                               Moosique.play();
-                                               playpauseIcon.setImage(pauseimage);
-                                       }
-                       
-                       }       else if (((JButton)e.getSource()).getToolTipText() == "stop") {
+       /**
+        * Creates labels with the specified text and font size. 
+        */
+       private JLabel createLabel(String title, int fontSize){
+               JLabel label = new JLabel(title,JLabel.CENTER);
+               label.setFont(new Font("Times New Roman", Font.PLAIN, fontSize));
+               return label;
+       }
+
+       /**
+        * Formats the given label for the progress indicator.
+        */
+       private JLabel formatProgInd(JLabel label){
+               label.addMouseListener(mouseAdapter);
+               label.setBorder(BorderFactory.createLineBorder(Color.black));
+               label.setBackground(Color.white);
+               return label;
+       }
+
+       /**
+        * Updates the play button with the given command and icon.
+        */
+       private void updatePlayButton(String command, Icon icon) {
+               playpause.setIcon(icon);
+               playpause.setActionCommand(command);
+               playpause.setToolTipText(command);
+       }
+
+       class MAdapter extends MouseAdapter {
+               public void mouseClicked(MouseEvent e) {
+                       if (e.getSource() instanceof JButton) {
+                               String command = ((JButton)e.getSource()).getActionCommand();
+                               if (command == "Play") {
+                                       updatePlayButton("Pause", pauseIcon);
+                                       Moosique.play();
+                               } else if (command == "Pause") {
+                                       updatePlayButton("Resume", playIcon);
+                                       Moosique.pause();
+                               } else if (command == "Resume") {
+                                       updatePlayButton("Pause", pauseIcon);
+                                       Moosique.resume();
+                               } else if (command == "Stop") {
+                                       updatePlayButton("Play", playIcon);
                                        Moosique.stop();
-                                       
-                       }       else if (((JButton)e.getSource()).getToolTipText() == "fastforward") {
-                                       // få in fastforwardmetoden
-                       
+                               } else if (command == "Record") {
+                                       MooDialog recordDialog = new MooDialog(MooDialog.RECORD);
+                               } else if (command == "Rewind") {
+                                       updatePlayButton("Play", playIcon);
+                                       Moosique.setEditPosition(0);
+                                       Moosique.stop();
+                               } else if (command == "Fast forward") {
+
+                               }
+                       } else if (e.getSource() instanceof JLabel) {
+                               long position = Moosique.getEditPosition();
+                               if (e.getSource().equals(measuresValue)) {
+                                       if (SwingUtilities.isRightMouseButton(e)) {
+                                               position += beatsPerMeasure * ticksPerBeat;
+                                       } else if (SwingUtilities.isLeftMouseButton(e) && Integer.parseInt(measuresValue.getText()) > 1) {
+                                               position -= beatsPerMeasure * ticksPerBeat;
+                                       }
+                               } else if (e.getSource().equals(beatsValue)) {
+                                       if (SwingUtilities.isRightMouseButton(e)) {
+                                               position += ticksPerBeat;
+                                       } else if (SwingUtilities.isLeftMouseButton(e) && Integer.parseInt(beatsValue.getText()) > 1) {
+                                               position -= ticksPerBeat;
+                                       }
+                               } else if (e.getSource().equals(ticksValue)) {
+                                       if (SwingUtilities.isRightMouseButton(e)) {
+                                               position += 1;
+                                       } else if (SwingUtilities.isLeftMouseButton(e) && Integer.parseInt(ticksValue.getText()) > 1) {
+                                               position -= 1;
+                                       }
+                               }
+                               Moosique.setEditPosition(position);
+                               Moosique.getGUI().update(position);
                        }
                }
-               
+
+/*             public void mousePressed(MouseEvent e) {
+                       if (e.getSource() instanceof JButton) {
+                               if (((JButton)e.getSource()).getToolTipText() == "Rewind") {
+                                       Moosique.rewind(beatsPerMeasure * ticksPerBeat);
+                               } else if (((JButton)e.getSource()).getToolTipText() == "Fast forward") {
+                                       Moosique.forward(beatsPerMeasure * ticksPerBeat);
+                               }
+                       }
+               }
+
+               public void mouseReleased(MouseEvent e) {}
+*/     }
 }