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 setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
28 // setAlignmentX(LEFT_ALIGNMENT);
30 mouseAdapter = new MAdapter();
32 // Creates playback buttons
33 rewind = createButton("images/rewind.gif", "Rewind");
34 playpause = createButton("images/play.gif", "Play");
35 stop = createButton("images/stop.gif", "Stop");
36 fastforward = createButton("images/forward.gif", "Fast forward");
37 playIcon = new ImageIcon("images/play.gif");
38 pauseIcon = new ImageIcon("images/pause.gif");
40 // Adds playback buttons
46 // Creates progress indicator
47 measure = createLabel("Msr", 10);
48 beats = createLabel("Beat", 10);
49 ticks = createLabel("Tick", 10);
50 measuresValue = formatProgInd(createLabel("1", 16));
51 beatsValue = formatProgInd(createLabel("1", 16));
52 ticksValue = formatProgInd(createLabel("1", 16));
53 JPanel spacenorth = new JPanel();
54 spacenorth.setBackground(bgColor);
55 JPanel spacesouth = new JPanel();
56 spacesouth.setBackground(bgColor);
58 // Creates progress indicator panel and adds components
59 progIndPanel = new JPanel();
60 progIndPanel.setMaximumSize(new Dimension(120,27));
61 progIndPanel.setLayout(new GridLayout(2,4));
62 progIndPanel.add(spacenorth);
63 progIndPanel.add(measure);
64 progIndPanel.add(beats);
65 progIndPanel.add(ticks);
66 progIndPanel.add(spacesouth);
67 progIndPanel.add(measuresValue);
68 progIndPanel.add(beatsValue);
69 progIndPanel.add(ticksValue);
74 * Updates the progress indicator.
75 * @param tickPosition the tick position to visualize
77 public void updateProgInd(long tickPosition) {
78 ticksPerBeat = Moosique.getSequence().getResolution();
79 if (tickPosition == 0) {
82 // Otherwise, calculates the current song position in measures, beats and ticks.
83 long measures = tickPosition / (beatsPerMeasure * ticksPerBeat);
84 long beats = (tickPosition - measures * beatsPerMeasure * ticksPerBeat) / ticksPerBeat;
85 long ticks = tickPosition - measures * beatsPerMeasure * ticksPerBeat - beats * ticksPerBeat;
86 measuresValue.setText(Long.toString(1 + measures));
87 beatsValue.setText(Long.toString(1 + beats));
88 ticksValue.setText(Long.toString(1 + ticks));
93 * Resets the progress indicator.
95 public void resetProgInd() {
96 measuresValue.setText("1");
97 beatsValue.setText("1");
98 ticksValue.setText("1");
99 playpause.setIcon(playIcon);
100 playpause.setToolTipText("Play");
101 ticksPerBeat = Moosique.getSequence().getResolution();
105 * Creates a button with the specified image and action command / tooltip.
107 private JButton createButton(String imagelocation, String command) {
108 JButton button = new JButton (new ImageIcon(imagelocation));
109 button.setToolTipText(command);
110 button.setActionCommand(command);
111 button.addMouseListener(mouseAdapter);
116 * Creates labels with the specified text and font size.
118 private JLabel createLabel(String title, int fontSize){
119 JLabel label = new JLabel(title,JLabel.CENTER);
120 label.setFont(new Font("Times New Roman", Font.PLAIN, fontSize));
125 * Formats the given label for the progress indicator.
127 private JLabel formatProgInd(JLabel label){
128 label.addMouseListener(mouseAdapter);
129 label.setBorder(BorderFactory.createLineBorder(Color.black));
130 label.setBackground(Color.white);
134 class MAdapter extends MouseAdapter {
135 public void mouseClicked(MouseEvent e) {
136 if (e.getSource() instanceof JButton) {
137 String command = ((JButton)e.getSource()).getActionCommand();
138 if (command == "Play") {
139 playpause.setIcon(pauseIcon);
140 playpause.setActionCommand("Pause");
141 playpause.setToolTipText("Pause");
143 } else if (command == "Pause") {
144 playpause.setIcon(playIcon);
145 playpause.setActionCommand("Resume");
146 playpause.setToolTipText("Resume");
148 } else if (command == "Resume") {
149 playpause.setIcon(pauseIcon);
150 playpause.setActionCommand("Pause");
151 playpause.setToolTipText("Pause");
153 } else if (command == "Rewind") {
154 // Different implementation, perhaps?
155 Moosique.setEditPosition(0);
157 } else if (command == "Fast forward") {
159 } else if (command == "Stop") {
162 } else if (e.getSource() instanceof JLabel) {
163 long position = Moosique.getEditPosition();
164 if (e.getSource().equals(measuresValue)) {
165 if (SwingUtilities.isRightMouseButton(e)) {
166 position += beatsPerMeasure * ticksPerBeat;
167 } else if (SwingUtilities.isLeftMouseButton(e) && Integer.parseInt(measuresValue.getText()) > 1) {
168 position -= beatsPerMeasure * ticksPerBeat;
170 } else if (e.getSource().equals(beatsValue)) {
171 if (SwingUtilities.isRightMouseButton(e)) {
172 position += ticksPerBeat;
173 } else if (SwingUtilities.isLeftMouseButton(e) && Integer.parseInt(beatsValue.getText()) > 1) {
174 position -= ticksPerBeat;
176 } else if (e.getSource().equals(ticksValue)) {
177 if (SwingUtilities.isRightMouseButton(e)) {
179 } else if (SwingUtilities.isLeftMouseButton(e) && Integer.parseInt(ticksValue.getText()) > 1) {
183 Moosique.setEditPosition(position);
184 Moosique.getGUI().update(position);
188 public void mousePressed(MouseEvent e) {
189 if (e.getSource() instanceof JButton) {
190 if (((JButton)e.getSource()).getToolTipText() == "Rewind") {
191 Moosique.rewind(beatsPerMeasure * ticksPerBeat);
192 } else if (((JButton)e.getSource()).getToolTipText() == "Fast forward") {
193 Moosique.forward(beatsPerMeasure * ticksPerBeat);
198 public void mouseReleased(MouseEvent e) {}