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, measuresValue, beatsValue, ticksValue;
15 private JPanel progIndPanel;
16 private ImageIcon playIcon, pauseIcon;
17 private MAdapter mouseAdapter;
18 private int ticksPerBeat;
19 public static final int beatsPerMeasure = 4;
20 public static final Color bgColor = new Color(192, 224, 255);
23 * Creates the toolbar.
27 // setAlignmentX(LEFT_ALIGNMENT);
29 mouseAdapter = new MAdapter();
31 // Creates playback buttons
32 rewind = createButton("images/rewind.gif", "Rewind");
33 playpause = createButton("images/play.gif", "Play");
34 stop = createButton("images/stop.gif", "Stop");
35 fastforward = createButton("images/forward.gif", "Fast forward");
36 playIcon = new ImageIcon("images/play.gif");
37 pauseIcon = new ImageIcon("images/pause.gif");
39 // Adds playback buttons
45 // Creates progress indicator
46 measure = createLabel("Msr", 10);
47 beats = createLabel("Beat", 10);
48 ticks = createLabel("Tick", 10);
49 measuresValue = formatProgInd(createLabel("1", 16));
50 beatsValue = formatProgInd(createLabel("1", 16));
51 ticksValue = formatProgInd(createLabel("1", 16));
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 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(measuresValue);
67 progIndPanel.add(beatsValue);
68 progIndPanel.add(ticksValue);
73 * Updates the progress indicator.
74 * @param tickPosition the tick position to visualize
76 public void updateProgInd(long tickPosition) {
77 if (tickPosition == 0) {
80 // Otherwise, calculates the current song position in measures, beats and ticks.
81 long measures = tickPosition / (beatsPerMeasure * ticksPerBeat);
82 long beats = (tickPosition - measures * beatsPerMeasure * ticksPerBeat) / ticksPerBeat;
83 long ticks = tickPosition - measures * beatsPerMeasure * ticksPerBeat - beats * ticksPerBeat;
84 measuresValue.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 public void resetProgInd() {
94 measuresValue.setText("1");
95 beatsValue.setText("1");
96 ticksValue.setText("1");
97 playpause.setIcon(playIcon);
98 playpause.setToolTipText("Play");
99 ticksPerBeat = Moosique.getSequence().getResolution();
103 * Creates a button with the specified image and tooltip.
105 private JButton createButton(String imagelocation, String tooltip) {
106 JButton button = new JButton (new ImageIcon(imagelocation));
107 button.setToolTipText(tooltip);
108 button.addMouseListener(mouseAdapter);
113 * Creates labels with the specified text and font size.
115 private JLabel createLabel(String title, int fontSize){
116 JLabel label = new JLabel(title,JLabel.CENTER);
117 label.setFont(new Font("Times New Roman", Font.PLAIN, fontSize));
122 * Formats the given label for the progress indicator.
124 private JLabel formatProgInd(JLabel label){
125 label.addMouseListener(mouseAdapter);
126 label.setBorder(BorderFactory.createLineBorder(Color.black));
127 label.setBackground(Color.white);
131 class MAdapter extends MouseAdapter {
132 public void mouseClicked(MouseEvent e) {
133 if (e.getSource() instanceof JButton) {
134 String toolTip = ((JButton)e.getSource()).getToolTipText();
135 if (toolTip == "Play") {
136 playpause.setIcon(pauseIcon);
137 playpause.setToolTipText("Pause");
139 } else if (toolTip == "Pause") {
140 playpause.setIcon(playIcon);
141 playpause.setToolTipText("Resume");
143 } else if (toolTip == "Resume") {
144 playpause.setIcon(pauseIcon);
145 playpause.setToolTipText("Pause");
147 } else if (toolTip == "Stop") {
150 } else if (e.getSource() instanceof JLabel) {
151 long position = Moosique.getPosition();
152 if (e.getSource().equals(measuresValue)) {
153 if (SwingUtilities.isRightMouseButton(e)) {
154 position += beatsPerMeasure * ticksPerBeat;
155 } else if (SwingUtilities.isLeftMouseButton(e) && Integer.parseInt(measuresValue.getText()) > 1) {
156 position -= beatsPerMeasure * ticksPerBeat;
158 } else if (e.getSource().equals(beatsValue)) {
159 if (SwingUtilities.isRightMouseButton(e)) {
160 position += ticksPerBeat;
161 } else if (SwingUtilities.isLeftMouseButton(e) && Integer.parseInt(beatsValue.getText()) > 1) {
162 position -= ticksPerBeat;
164 } else if (e.getSource().equals(ticksValue)) {
165 if (SwingUtilities.isRightMouseButton(e)) {
167 } else if (SwingUtilities.isLeftMouseButton(e) && Integer.parseInt(ticksValue.getText()) > 1) {
171 Moosique.setPosition(position);
172 Moosique.getGUI().update(position);
176 public void mousePressed(MouseEvent e) {
177 if (e.getSource() instanceof JButton) {
178 if (((JButton)e.getSource()).getToolTipText() == "Rewind") {
179 Moosique.rewind(beatsPerMeasure * ticksPerBeat);
180 } else if (((JButton)e.getSource()).getToolTipText() == "Fast forward") {
181 Moosique.forward(beatsPerMeasure * ticksPerBeat);
186 public void mouseReleased(MouseEvent e) {}