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);
72 * Updates the progress indicator.
73 * @param tickPosition the tick position to visualize
75 public void updateProgInd(long tickPosition) {
76 int ticksPerBeat = Moosique.getSequence().getResolution();
77 int beatsPerMeasure = 4;
78 long measures = tickPosition / (beatsPerMeasure * ticksPerBeat);
79 long beats = (tickPosition - measures * beatsPerMeasure * ticksPerBeat) / ticksPerBeat;
80 long ticks = tickPosition - measures * beatsPerMeasure * ticksPerBeat - beats * ticksPerBeat;
81 measureValue.setText(Long.toString(1 + measures));
82 beatsValue.setText(Long.toString(1 + beats));
83 ticksValue.setText(Long.toString(1 + ticks));
87 * Creates a button with the specified image and tooltip.
89 private JButton createButton(String imagelocation, String tooltip) {
90 JButton button = new JButton (new ImageIcon(imagelocation));
91 button.setToolTipText(tooltip);
92 button.addMouseListener(mouseAdapter);
97 * Creates labels with the specified text and font size.
99 private JLabel createLabel(String title, int fontSize){
100 JLabel label = new JLabel(title,JLabel.CENTER);
101 label.setFont(new Font("Times New Roman", Font.PLAIN, fontSize));
106 * Formats the given label for the progress indicator.
108 private JLabel formatProgInd(JLabel label){
109 label.setBorder(BorderFactory.createLineBorder(Color.black));
110 label.setBackground(Color.white);
114 class MooMouseAdapter extends MouseAdapter {
115 public void mouseClicked(MouseEvent e) {
116 if (((JButton)e.getSource()).getToolTipText() == "Play") {
117 playpause.setIcon(pauseIcon);
118 playpause.setToolTipText("Pause");
120 } else if (((JButton)e.getSource()).getToolTipText() == "Pause") {
121 playpause.setIcon(playIcon);
122 playpause.setToolTipText("Resume");
124 } else if (((JButton)e.getSource()).getToolTipText() == "Resume") {
125 playpause.setIcon(pauseIcon);
126 playpause.setToolTipText("Pause");
128 } else if (((JButton)e.getSource()).getToolTipText() == "Stop") {
129 playpause.setIcon(playIcon);
130 playpause.setToolTipText("Play");
135 public void mousePressed(MouseEvent e) {
136 if (((JButton)e.getSource()).getToolTipText() == "Rewind") {
138 } else if (((JButton)e.getSource()).getToolTipText() == "Fast forward") {
139 Moosique.forward(96);
143 public void mouseReleased(MouseEvent e) {}