3 import java.awt.event.*;
4 import javax.sound.midi.*;
7 * The application's toolbar, with the most frequently used commands.
9 * @author Björn Lanneskog
11 public class MooToolbar extends JToolBar {
13 private JButton rewind, playpause, stop, fastforward;
14 private JLabel measure, beats, ticks, measureValue, beatsValue, ticksValue;
15 private JPanel progIndPanel;
16 private ImageIcon playIcon, pauseIcon;
17 private MooMouseAdapter mouseAdapter;
18 public static final Color bgColor = new Color(192, 224, 255);
21 * Creates the toolbar.
25 // setAlignmentX(LEFT_ALIGNMENT);
27 mouseAdapter = new MooMouseAdapter();
29 // Creates playback buttons
30 rewind = createButton("images/rewind.gif", "Rewind");
31 playpause = createButton("images/play.gif", "Play");
32 stop = createButton("images/stop.gif", "Stop");
33 fastforward = createButton("images/forward.gif", "Fast forward");
34 playIcon = new ImageIcon("images/play.gif");
35 pauseIcon = new ImageIcon("images/pause.gif");
37 // Adds playback buttons
43 // Creates progress indicator
44 measure = createLabel("Msr", 10);
45 beats = createLabel("Beat", 10);
46 ticks = createLabel("Tick", 10);
47 measureValue = formatProgInd(createLabel("1", 16));
48 beatsValue = formatProgInd(createLabel("1", 16));
49 ticksValue = formatProgInd(createLabel("1", 16));
50 JPanel spacenorth = new JPanel();
51 spacenorth.setBackground(bgColor);
52 JPanel spacesouth = new JPanel();
53 spacesouth.setBackground(bgColor);
55 // Creates progress indicator panel and adds components
56 progIndPanel = new JPanel();
57 progIndPanel.setMaximumSize(new Dimension(120,27));
58 progIndPanel.setLayout(new GridLayout(2,4));
59 progIndPanel.add(spacenorth);
60 progIndPanel.add(measure);
61 progIndPanel.add(beats);
62 progIndPanel.add(ticks);
63 progIndPanel.add(spacesouth);
64 progIndPanel.add(measureValue);
65 progIndPanel.add(beatsValue);
66 progIndPanel.add(ticksValue);
71 * Updates the progress indicator.
72 * @param tickPosition the tick position to visualize
74 public void updateProgInd(long tickPosition) {
75 if (tickPosition == 0) {
78 // Otherwise, calculates the current song position in measures, beats and ticks.
79 int ticksPerBeat = Moosique.getSequence().getResolution();
80 int beatsPerMeasure = 4;
81 long measures = tickPosition / (beatsPerMeasure * ticksPerBeat);
82 long beats = (tickPosition - measures * beatsPerMeasure * ticksPerBeat) / ticksPerBeat;
83 long ticks = tickPosition - measures * beatsPerMeasure * ticksPerBeat - beats * ticksPerBeat;
84 measureValue.setText(Long.toString(1 + measures));
85 beatsValue.setText(Long.toString(1 + beats));
86 ticksValue.setText(Long.toString(1 + ticks));
91 * Resets the progress indicator
93 private void resetProgInd() {
94 measureValue.setText("1");
95 beatsValue.setText("1");
96 ticksValue.setText("1");
97 playpause.setIcon(playIcon);
98 playpause.setToolTipText("Play");
102 * Creates a button with the specified image and tooltip.
104 private JButton createButton(String imagelocation, String tooltip) {
105 JButton button = new JButton (new ImageIcon(imagelocation));
106 button.setToolTipText(tooltip);
107 button.addMouseListener(mouseAdapter);
112 * Creates labels with the specified text and font size.
114 private JLabel createLabel(String title, int fontSize){
115 JLabel label = new JLabel(title,JLabel.CENTER);
116 label.setFont(new Font("Times New Roman", Font.PLAIN, fontSize));
121 * Formats the given label for the progress indicator.
123 private JLabel formatProgInd(JLabel label){
124 label.setBorder(BorderFactory.createLineBorder(Color.black));
125 label.setBackground(Color.white);
129 class MooMouseAdapter extends MouseAdapter {
130 public void mouseClicked(MouseEvent e) {
131 if (((JButton)e.getSource()).getToolTipText() == "Play") {
132 playpause.setIcon(pauseIcon);
133 playpause.setToolTipText("Pause");
135 } else if (((JButton)e.getSource()).getToolTipText() == "Pause") {
136 playpause.setIcon(playIcon);
137 playpause.setToolTipText("Resume");
139 } else if (((JButton)e.getSource()).getToolTipText() == "Resume") {
140 playpause.setIcon(pauseIcon);
141 playpause.setToolTipText("Pause");
143 } else if (((JButton)e.getSource()).getToolTipText() == "Stop") {
149 public void mousePressed(MouseEvent e) {
150 if (((JButton)e.getSource()).getToolTipText() == "Rewind") {
152 } else if (((JButton)e.getSource()).getToolTipText() == "Fast forward") {
153 Moosique.forward(96);
157 public void mouseReleased(MouseEvent e) {}