1 import javax.sound.midi.*;
4 import java.awt.event.*;
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, record;
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 record = createButton("images/record.gif", "Record");
35 playpause = createButton("images/play.gif", "Play");
36 stop = createButton("images/stop.gif", "Stop");
37 fastforward = createButton("images/forward.gif", "Fast forward");
38 playIcon = new ImageIcon("images/play.gif");
39 pauseIcon = new ImageIcon("images/pause.gif");
41 // Adds playback buttons
48 // Creates progress indicator
49 measure = createLabel("Msr", 10);
50 beats = createLabel("Beat", 10);
51 ticks = createLabel("Tick", 10);
52 measuresValue = formatProgInd(createLabel("1", 16));
53 beatsValue = formatProgInd(createLabel("1", 16));
54 ticksValue = formatProgInd(createLabel("1", 16));
55 JPanel spacenorth = new JPanel();
56 spacenorth.setBackground(bgColor);
57 JPanel spacesouth = new JPanel();
58 spacesouth.setBackground(bgColor);
60 // Creates progress indicator panel and adds components
61 progIndPanel = new JPanel();
62 progIndPanel.setMaximumSize(new Dimension(120,27));
63 progIndPanel.setLayout(new GridLayout(2,4));
64 progIndPanel.add(spacenorth);
65 progIndPanel.add(measure);
66 progIndPanel.add(beats);
67 progIndPanel.add(ticks);
68 progIndPanel.add(spacesouth);
69 progIndPanel.add(measuresValue);
70 progIndPanel.add(beatsValue);
71 progIndPanel.add(ticksValue);
76 * Updates the progress indicator.
77 * @param tickPosition the tick position to visualize
79 public void updateProgInd(long tickPosition) {
80 System.out.print("Updating to " + tickPosition + " = ");
81 if (tickPosition == 0) {
83 System.out.println("1:1:1");
85 int[] position = Moosique.getPositionForTicks(tickPosition);
86 System.out.println("" + (position[0] + 1) + ":" + (position[1] + 1) + ":" + (position[2] + 1));
87 measuresValue.setText(Integer.toString(position[0] + 1));
88 beatsValue.setText(Long.toString(position[1] + 1));
89 ticksValue.setText(Long.toString(position[2] + 1));
94 * Resets the progress indicator.
96 public void resetProgInd() {
97 measuresValue.setText("1");
98 beatsValue.setText("1");
99 ticksValue.setText("1");
100 updatePlayButton("Play", playIcon);
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);
135 * Updates the play button with the given command and icon.
137 private void updatePlayButton(String command, Icon icon) {
138 playpause.setIcon(icon);
139 playpause.setActionCommand(command);
140 playpause.setToolTipText(command);
143 class MAdapter extends MouseAdapter {
144 public void mouseClicked(MouseEvent e) {
145 if (e.getSource() instanceof JButton) {
146 String command = ((JButton)e.getSource()).getActionCommand();
147 if (command == "Play") {
148 updatePlayButton("Pause", pauseIcon);
150 } else if (command == "Pause") {
151 updatePlayButton("Resume", playIcon);
153 } else if (command == "Resume") {
154 updatePlayButton("Pause", pauseIcon);
156 } else if (command == "Stop") {
157 updatePlayButton("Play", playIcon);
159 } else if (command == "Record") {
160 MooDialog recordDialog = new MooDialog(MooDialog.RECORD);
161 } else if (command == "Rewind") {
162 updatePlayButton("Play", playIcon);
163 Moosique.setEditPosition(0);
165 } else if (command == "Fast forward") {
168 } else if (e.getSource() instanceof JLabel) {
169 long position = Moosique.getEditPosition();
170 if (e.getSource().equals(measuresValue)) {
171 if (SwingUtilities.isRightMouseButton(e)) {
172 position += beatsPerMeasure * ticksPerBeat;
173 } else if (SwingUtilities.isLeftMouseButton(e) && Integer.parseInt(measuresValue.getText()) > 1) {
174 position -= beatsPerMeasure * ticksPerBeat;
176 } else if (e.getSource().equals(beatsValue)) {
177 if (SwingUtilities.isRightMouseButton(e)) {
178 position += ticksPerBeat;
179 } else if (SwingUtilities.isLeftMouseButton(e) && Integer.parseInt(beatsValue.getText()) > 1) {
180 position -= ticksPerBeat;
182 } else if (e.getSource().equals(ticksValue)) {
183 if (SwingUtilities.isRightMouseButton(e)) {
185 } else if (SwingUtilities.isLeftMouseButton(e) && Integer.parseInt(ticksValue.getText()) > 1) {
189 Moosique.setEditPosition(position);
190 Moosique.getGUI().update(position);
194 /* public void mousePressed(MouseEvent e) {
195 if (e.getSource() instanceof JButton) {
196 if (((JButton)e.getSource()).getToolTipText() == "Rewind") {
197 Moosique.rewind(beatsPerMeasure * ticksPerBeat);
198 } else if (((JButton)e.getSource()).getToolTipText() == "Fast forward") {
199 Moosique.forward(beatsPerMeasure * ticksPerBeat);
204 public void mouseReleased(MouseEvent e) {}