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 ticksPerBeat = Moosique.getSequence().getResolution();
78 if (tickPosition == 0) {
81 // Otherwise, calculates the current song position in measures, beats and ticks.
82 long measures = tickPosition / (beatsPerMeasure * ticksPerBeat);
83 long beats = (tickPosition - measures * beatsPerMeasure * ticksPerBeat) / ticksPerBeat;
84 long ticks = tickPosition - measures * beatsPerMeasure * ticksPerBeat - beats * ticksPerBeat;
85 measuresValue.setText(Long.toString(1 + measures));
86 beatsValue.setText(Long.toString(1 + beats));
87 ticksValue.setText(Long.toString(1 + ticks));
92 * Resets the progress indicator.
94 public void resetProgInd() {
95 measuresValue.setText("1");
96 beatsValue.setText("1");
97 ticksValue.setText("1");
98 playpause.setIcon(playIcon);
99 playpause.setToolTipText("Play");
100 ticksPerBeat = Moosique.getSequence().getResolution();
104 * Creates a button with the specified image and tooltip.
106 private JButton createButton(String imagelocation, String tooltip) {
107 JButton button = new JButton (new ImageIcon(imagelocation));
108 button.setToolTipText(tooltip);
109 button.addMouseListener(mouseAdapter);
114 * Creates labels with the specified text and font size.
116 private JLabel createLabel(String title, int fontSize){
117 JLabel label = new JLabel(title,JLabel.CENTER);
118 label.setFont(new Font("Times New Roman", Font.PLAIN, fontSize));
123 * Formats the given label for the progress indicator.
125 private JLabel formatProgInd(JLabel label){
126 label.addMouseListener(mouseAdapter);
127 label.setBorder(BorderFactory.createLineBorder(Color.black));
128 label.setBackground(Color.white);
132 class MAdapter extends MouseAdapter {
133 public void mouseClicked(MouseEvent e) {
134 if (e.getSource() instanceof JButton) {
135 String toolTip = ((JButton)e.getSource()).getToolTipText();
136 if (toolTip == "Play") {
137 playpause.setIcon(pauseIcon);
138 playpause.setToolTipText("Pause");
140 } else if (toolTip == "Pause") {
141 playpause.setIcon(playIcon);
142 playpause.setToolTipText("Resume");
144 } else if (toolTip == "Resume") {
145 playpause.setIcon(pauseIcon);
146 playpause.setToolTipText("Pause");
148 } else if (toolTip == "Stop") {
151 } else if (e.getSource() instanceof JLabel) {
152 long position = Moosique.getPosition();
153 if (e.getSource().equals(measuresValue)) {
154 if (SwingUtilities.isRightMouseButton(e)) {
155 position += beatsPerMeasure * ticksPerBeat;
156 } else if (SwingUtilities.isLeftMouseButton(e) && Integer.parseInt(measuresValue.getText()) > 1) {
157 position -= beatsPerMeasure * ticksPerBeat;
159 } else if (e.getSource().equals(beatsValue)) {
160 if (SwingUtilities.isRightMouseButton(e)) {
161 position += ticksPerBeat;
162 } else if (SwingUtilities.isLeftMouseButton(e) && Integer.parseInt(beatsValue.getText()) > 1) {
163 position -= ticksPerBeat;
165 } else if (e.getSource().equals(ticksValue)) {
166 if (SwingUtilities.isRightMouseButton(e)) {
168 } else if (SwingUtilities.isLeftMouseButton(e) && Integer.parseInt(ticksValue.getText()) > 1) {
172 Moosique.setPosition(position);
173 Moosique.getGUI().update(position);
177 public void mousePressed(MouseEvent e) {
178 if (e.getSource() instanceof JButton) {
179 if (((JButton)e.getSource()).getToolTipText() == "Rewind") {
180 Moosique.rewind(beatsPerMeasure * ticksPerBeat);
181 } else if (((JButton)e.getSource()).getToolTipText() == "Fast forward") {
182 Moosique.forward(beatsPerMeasure * ticksPerBeat);
187 public void mouseReleased(MouseEvent e) {}