]> ruin.nu Git - moosique.git/blobdiff - MooToolbar.java
stuff
[moosique.git] / MooToolbar.java
index a9ec10bd09b9a79d53e33b6689fb8c0d80cd93de..a3b250a42bf3db7273b7f3a068fcad2cf6845717 100644 (file)
@@ -12,6 +12,7 @@ public class MooToolbar extends JToolBar {
 
        private JButton rewind, playpause, stop, fastforward;
        private JLabel measure, beats, ticks, measureValue, beatsValue, ticksValue;
+       private JPanel progIndPanel;
        private ImageIcon playIcon, pauseIcon;
        private MooMouseAdapter mouseAdapter;
        public static final Color bgColor = new Color(192, 224, 255);
@@ -43,19 +44,16 @@ public class MooToolbar extends JToolBar {
                measure = createLabel("Msr", 10);
                beats = createLabel("Beat", 10);
                ticks = createLabel("Tick", 10);
-               measureValue = createLabel("1", 16);
-               measureValue.setBorder(BorderFactory.createLineBorder(Color.black));
-               beatsValue = createLabel("1", 16);
-               beatsValue.setBorder(BorderFactory.createLineBorder(Color.black));
-               ticksValue = createLabel("1", 16);
-               ticksValue.setBorder(BorderFactory.createLineBorder(Color.black));
+               measureValue = 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
-               JPanel progIndPanel = new JPanel();
+               progIndPanel = new JPanel();
                progIndPanel.setMaximumSize(new Dimension(120,27));
                progIndPanel.setLayout(new GridLayout(2,4));
                progIndPanel.add(spacenorth);
@@ -67,29 +65,41 @@ public class MooToolbar extends JToolBar {
                progIndPanel.add(beatsValue);
                progIndPanel.add(ticksValue);
                add(progIndPanel);
-
        }
 
        /**
         * Updates the progress indicator.
+        * @param tickPosition  the tick position to visualize
         */
-       public void updateProgInd() {
-               long pos = Moosique.getSequencer().getTickPosition();
-               int ticksPerBeat = Moosique.getSequence().getResolution();
-               int beatsPerMeasure = 4;
-               long measures = pos / (beatsPerMeasure * ticksPerBeat);
-               long beats = (pos - measures * beatsPerMeasure * ticksPerBeat) / ticksPerBeat;
-               long ticks = pos - measures * beatsPerMeasure * ticksPerBeat - beats * ticksPerBeat;
-               measureValue.setText(Long.toString(measures));
-               beatsValue.setText(Long.toString(beats));
-               ticksValue.setText(Long.toString(ticks));
+       public void updateProgInd(long tickPosition) {
+               if (tickPosition == 0) {
+                       resetProgInd();
+               } else {
+                       // Otherwise, calculates the current song position in measures, beats and ticks.
+                       int ticksPerBeat = Moosique.getSequence().getResolution();
+                       int beatsPerMeasure = 4;
+                       long measures = tickPosition / (beatsPerMeasure * ticksPerBeat);
+                       long beats = (tickPosition - measures * beatsPerMeasure * ticksPerBeat) / ticksPerBeat;
+                       long ticks = tickPosition - measures * beatsPerMeasure * ticksPerBeat - beats * ticksPerBeat;
+                       measureValue.setText(Long.toString(1 + measures));
+                       beatsValue.setText(Long.toString(1 + beats));
+                       ticksValue.setText(Long.toString(1 + ticks));
+               }
        }
 
-       /**
+       /*
+        * Resets the progress indicator
+        */
+       private void resetProgInd() {
+                       measureValue.setText("1");
+                       beatsValue.setText("1");
+                       ticksValue.setText("1");
+                       playpause.setIcon(playIcon);
+                       playpause.setToolTipText("Play");
+       }
+
+       /*
         * Creates a button with the specified image and tooltip.
-        * @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));
@@ -98,10 +108,8 @@ public class MooToolbar extends JToolBar {
                return button;
        }
        
-       /**
+       /*
         * Creates labels with the specified text and font size. 
-        * @param title         the titel displayed on the label
-        * @param fontsize      the fontzise of the text displayed on the label
         */
        private JLabel createLabel(String title, int fontSize){
                JLabel label = new JLabel(title,JLabel.CENTER);
@@ -109,30 +117,32 @@ public class MooToolbar extends JToolBar {
                return label;
        }
 
+       /*
+        * Formats the given label for the progress indicator.
+        */
+       private JLabel formatProgInd(JLabel label){
+               label.setBorder(BorderFactory.createLineBorder(Color.black));
+               label.setBackground(Color.white);
+               return label;
+       }
+
        class MooMouseAdapter extends MouseAdapter {
                public void mouseClicked(MouseEvent e) {
                        if (((JButton)e.getSource()).getToolTipText() == "Play") {
-                                       Moosique.play();
-                                       playpause.setIcon(pauseIcon);
-                                       playpause.setToolTipText("Pause");
-                                       while(Moosique.getSequencer().isRunning()) {
-                                               Moosique.getGUI().update();
-                                       }
+                               playpause.setIcon(pauseIcon);
+                               playpause.setToolTipText("Pause");
+                               Moosique.play();
                        } else if (((JButton)e.getSource()).getToolTipText() == "Pause") {
-                                       Moosique.pause();
-                                       playpause.setIcon(playIcon);
-                                       playpause.setToolTipText("Resume");
+                               playpause.setIcon(playIcon);
+                               playpause.setToolTipText("Resume");
+                               Moosique.pause();
                        } else if (((JButton)e.getSource()).getToolTipText() == "Resume") {
-                                       Moosique.resume();
-                                       playpause.setIcon(pauseIcon);
-                                       playpause.setToolTipText("Pause");
-                                       while(Moosique.getSequencer().isRunning()) {
-                                               Moosique.getGUI().update();
-                                       }
+                               playpause.setIcon(pauseIcon);
+                               playpause.setToolTipText("Pause");
+                               Moosique.resume();
                        } else if (((JButton)e.getSource()).getToolTipText() == "Stop") {
+                               resetProgInd();
                                Moosique.stop();
-                               playpause.setIcon(playIcon);
-                               playpause.setToolTipText("Play");
                        }
                }