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 ImageIcon playIcon, pauseIcon;
16 private MooMouseAdapter mouseAdapter;
17 public static final Color bgColor = new Color(192, 224, 255);
20 * Creates the toolbar.
24 // setAlignmentX(LEFT_ALIGNMENT);
26 mouseAdapter = new MooMouseAdapter();
28 // Creates playback buttons
29 rewind = createButton("images/rewind.gif", "Rewind");
30 playpause = createButton("images/play.gif", "Play");
31 stop = createButton("images/stop.gif", "Stop");
32 fastforward = createButton("images/forward.gif", "Fast forward");
33 playIcon = new ImageIcon("images/play.gif");
34 pauseIcon = new ImageIcon("images/pause.gif");
36 // Adds playback buttons
42 // Creates progress indicator
43 measure = createLabel("Msr", 10);
44 beats = createLabel("Beat", 10);
45 ticks = createLabel("Tick", 10);
46 measureValue = createLabel("1", 16);
47 measureValue.setBorder(BorderFactory.createLineBorder(Color.black));
48 beatsValue = createLabel("1", 16);
49 beatsValue.setBorder(BorderFactory.createLineBorder(Color.black));
50 ticksValue = createLabel("1", 16);
51 ticksValue.setBorder(BorderFactory.createLineBorder(Color.black));
52 JPanel spacenorth = new JPanel();
53 spacenorth.setBackground(bgColor);
54 JPanel spacesouth = new JPanel();
55 spacesouth.setBackground(bgColor);
57 // Creates progress indicator panel and adds components
58 JPanel progIndPanel = new JPanel();
59 progIndPanel.setMaximumSize(new Dimension(120,27));
60 progIndPanel.setLayout(new GridLayout(2,4));
61 progIndPanel.add(spacenorth);
62 progIndPanel.add(measure);
63 progIndPanel.add(beats);
64 progIndPanel.add(ticks);
65 progIndPanel.add(spacesouth);
66 progIndPanel.add(measureValue);
67 progIndPanel.add(beatsValue);
68 progIndPanel.add(ticksValue);
74 * Updates the progress indicator.
76 public void updateProgInd() {
77 long pos = Moosique.getSequencer().getTickPosition();
78 int ticksPerBeat = Moosique.getSequence().getResolution();
79 int beatsPerMeasure = 4;
80 long measures = pos / (beatsPerMeasure * ticksPerBeat);
81 long beats = (pos - measures * beatsPerMeasure * ticksPerBeat) / ticksPerBeat;
82 long ticks = pos - measures * beatsPerMeasure * ticksPerBeat - beats * ticksPerBeat;
83 measureValue.setText(Long.toString(measures));
84 beatsValue.setText(Long.toString(beats));
85 ticksValue.setText(Long.toString(ticks));
89 * Creates a button with the specified image and tooltip.
90 * @param imagelocation the imagelacation of the the image displayed in the button
91 * @param tooltip the tooltip associated with this button
92 * @return button the button to be attached to the toolbar
94 private JButton createButton(String imagelocation, String tooltip) {
95 JButton button = new JButton (new ImageIcon(imagelocation));
96 button.setToolTipText(tooltip);
97 button.addMouseListener(mouseAdapter);
102 * Creates labels with the specified text and font size.
103 * @param title the titel displayed on the label
104 * @param fontsize the fontzise of the text displayed on the label
106 private JLabel createLabel(String title, int fontSize){
107 JLabel label = new JLabel(title,JLabel.CENTER);
108 label.setFont(new Font("Times New Roman", Font.PLAIN, fontSize));
112 class MooMouseAdapter extends MouseAdapter {
113 public void mouseClicked(MouseEvent e) {
114 if (((JButton)e.getSource()).getToolTipText() == "Play") {
116 playpause.setIcon(pauseIcon);
117 playpause.setToolTipText("Pause");
118 while(Moosique.getSequencer().isRunning()) {
119 Moosique.getGUI().update();
121 } else if (((JButton)e.getSource()).getToolTipText() == "Pause") {
123 playpause.setIcon(playIcon);
124 playpause.setToolTipText("Resume");
125 } else if (((JButton)e.getSource()).getToolTipText() == "Resume") {
127 playpause.setIcon(pauseIcon);
128 playpause.setToolTipText("Pause");
129 while(Moosique.getSequencer().isRunning()) {
130 Moosique.getGUI().update();
132 } else if (((JButton)e.getSource()).getToolTipText() == "Stop") {
134 playpause.setIcon(playIcon);
135 playpause.setToolTipText("Play");
139 public void mousePressed(MouseEvent e) {
140 if (((JButton)e.getSource()).getToolTipText() == "Rewind") {
142 } else if (((JButton)e.getSource()).getToolTipText() == "Fast forward") {
143 Moosique.forward(96);
147 public void mouseReleased(MouseEvent e) {}