import javax.swing.*;
-import java.awt.event.*;
import java.awt.*;
-import java.awt.Color;
+import java.awt.event.*;
+import javax.sound.midi.*;
-public class MooToolbar extends JToolBar implements ActionListener {
+/**
+ * The application's toolbar, with the most frequently used commands.
+ *
+ * @author Björn Lanneskog
+ */
+public class MooToolbar extends JToolBar {
- public MooToolbar() {
+ 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);
- rewind = createButton("images/rewind.gif", "rewind");
- add(rewind);
+ /**
+ * Creates the toolbar.
+ */
+
+ public MooToolbar() {
+ // setAlignmentX(LEFT_ALIGNMENT);
+ setFloatable(false);
+ mouseAdapter = new MooMouseAdapter();
+
+ // Creates playback buttons
+ rewind = createButton("images/rewind.gif", "Rewind");
+ 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");
+ // Adds playback buttons
+ add(rewind);
add(playpause);
-
- stop = createButton("images/stop.gif", "stop");
add(stop);
-
- fastforward = createButton("images/forward.gif", "fast forward");
add(fastforward);
- meas = createJump("Measure:", "1");
- add(meas);
-
- beat = createJump("Beats:", "1");
- add(beat);
-
- ticks = createJump("Ticks:", "1");
- add(ticks);
+ // Creates progress indicator
+ measure = createLabel("Msr", 10);
+ beats = createLabel("Beat", 10);
+ ticks = createLabel("Tick", 10);
+ 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
+ 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(measureValue);
+ progIndPanel.add(beatsValue);
+ progIndPanel.add(ticksValue);
+ add(progIndPanel);
+
+ }
+
+ /**
+ * Updates the progress indicator.
+ * @param tickPosition the tick position to visualize
+ */
+ public void updateProgInd(long tickPosition) {
+ 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));
+ }
+
+ /*
+ * Creates a button with the specified image and tooltip.
+ */
+ private JButton createButton(String imagelocation, String tooltip) {
+ JButton button = new JButton (new ImageIcon(imagelocation));
+ button.setToolTipText(tooltip);
+ button.addMouseListener(mouseAdapter);
+ return button;
+ }
+
+ /*
+ * 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.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") {
+ playpause.setIcon(pauseIcon);
+ playpause.setToolTipText("Pause");
+ Moosique.play();
+ } else if (((JButton)e.getSource()).getToolTipText() == "Pause") {
+ playpause.setIcon(playIcon);
+ playpause.setToolTipText("Resume");
+ Moosique.pause();
+ } else if (((JButton)e.getSource()).getToolTipText() == "Resume") {
+ playpause.setIcon(pauseIcon);
+ playpause.setToolTipText("Pause");
+ Moosique.resume();
+ } else if (((JButton)e.getSource()).getToolTipText() == "Stop") {
+ playpause.setIcon(playIcon);
+ playpause.setToolTipText("Play");
+ Moosique.stop();
+ }
}
-
- private JButton createButton(String imageLocation, String toolTip) {
- JButton button = new JButton (new ImageIcon(imageLocation));
- button.setToolTipText(toolTip);
- button.addActionListener(this);
- return button;
- }
-
- private JPanel createJump(String labelname, String inivalu) {
- JLabel label = new JLabel(labelname);
- JTextField field = new JTextField(inivalu);
- JPanel panel = new JPanel();
- panel.setLayout(new BorderLayout());
- panel.add(label, BorderLayout.NORTH);
- panel.add(field, BorderLayout.SOUTH);
- return panel;
+
+ public void mousePressed(MouseEvent e) {
+ if (((JButton)e.getSource()).getToolTipText() == "Rewind") {
+ Moosique.rewind(96);
+ } else if (((JButton)e.getSource()).getToolTipText() == "Fast forward") {
+ Moosique.forward(96);
+ }
}
-
- public void actionPerformed(ActionEvent e) {}
-
- /*if () {
-
- } else if () {
-
-
- } else if () {
-
- } else if () {
-
- }*/
- private JButton rewind;
- private JButton playpause;
- private JButton stop;
- private JButton fastforward;
- private JPanel meas;
- private JPanel beat;
- private JPanel ticks;
-}
+ public void mouseReleased(MouseEvent e) {}
+ }
+}
\ No newline at end of file