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 updatePlayButton("Play", playIcon);
100 ticksPerBeat = Moosique.getSequence().getResolution();
104 * Creates a button with the specified image and action command / tooltip.
106 private JButton createButton(String imagelocation, String command) {
107 JButton button = new JButton (new ImageIcon(imagelocation));
108 button.setToolTipText(command);
109 button.setActionCommand(command);
110 button.addMouseListener(mouseAdapter);
115 * Creates labels with the specified text and font size.
117 private JLabel createLabel(String title, int fontSize){
118 JLabel label = new JLabel(title,JLabel.CENTER);
119 label.setFont(new Font("Times New Roman", Font.PLAIN, fontSize));
124 * Formats the given label for the progress indicator.
126 private JLabel formatProgInd(JLabel label){
127 label.addMouseListener(mouseAdapter);
128 label.setBorder(BorderFactory.createLineBorder(Color.black));
129 label.setBackground(Color.white);
134 * Updates the play button with the given command and icon.
136 private void updatePlayButton(String command, Icon icon) {
137 playpause.setIcon(icon);
138 playpause.setActionCommand(command);
139 playpause.setToolTipText(command);
142 class MAdapter extends MouseAdapter {
143 public void mouseClicked(MouseEvent e) {
144 if (e.getSource() instanceof JButton) {
145 String command = ((JButton)e.getSource()).getActionCommand();
146 if (command == "Play") {
147 updatePlayButton("Pause", pauseIcon);
149 } else if (command == "Pause") {
150 updatePlayButton("Resume", playIcon);
152 } else if (command == "Resume") {
153 updatePlayButton("Pause", pauseIcon);
155 } else if (command == "Rewind") {
156 System.out.println("Rewound!");
157 // Different implementation, perhaps?
158 updatePlayButton("Play", playIcon);
159 Moosique.setEditPosition(0);
161 } else if (command == "Fast forward") {
162 } else if (command == "Stop") {
163 updatePlayButton("Play", playIcon);
166 } else if (e.getSource() instanceof JLabel) {
167 long position = Moosique.getEditPosition();
168 if (e.getSource().equals(measuresValue)) {
169 if (SwingUtilities.isRightMouseButton(e)) {
170 position += beatsPerMeasure * ticksPerBeat;
171 } else if (SwingUtilities.isLeftMouseButton(e) && Integer.parseInt(measuresValue.getText()) > 1) {
172 position -= beatsPerMeasure * ticksPerBeat;
174 } else if (e.getSource().equals(beatsValue)) {
175 if (SwingUtilities.isRightMouseButton(e)) {
176 position += ticksPerBeat;
177 } else if (SwingUtilities.isLeftMouseButton(e) && Integer.parseInt(beatsValue.getText()) > 1) {
178 position -= ticksPerBeat;
180 } else if (e.getSource().equals(ticksValue)) {
181 if (SwingUtilities.isRightMouseButton(e)) {
183 } else if (SwingUtilities.isLeftMouseButton(e) && Integer.parseInt(ticksValue.getText()) > 1) {
187 Moosique.setEditPosition(position);
188 Moosique.getGUI().update(position);
192 /* public void mousePressed(MouseEvent e) {
193 if (e.getSource() instanceof JButton) {
194 if (((JButton)e.getSource()).getToolTipText() == "Rewind") {
195 Moosique.rewind(beatsPerMeasure * ticksPerBeat);
196 } else if (((JButton)e.getSource()).getToolTipText() == "Fast forward") {
197 Moosique.forward(beatsPerMeasure * ticksPerBeat);
202 public void mouseReleased(MouseEvent e) {}