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;
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 System.out.print("Updating to " + tickPosition + " = ");
79 if (tickPosition == 0) {
81 System.out.println("1:1:1");
83 int[] position = Moosique.getPositionForTicks(tickPosition);
84 System.out.println("" + position[0] + ":" + position[1] + ":" + position[2]);
85 measuresValue.setText(Integer.toString(position[0]));
86 beatsValue.setText(Long.toString(position[1]));
87 ticksValue.setText(Long.toString(position[2]));
92 * Resets the progress indicator.
94 public void resetProgInd() {
95 measuresValue.setText("1");
96 beatsValue.setText("1");
97 ticksValue.setText("1");
98 updatePlayButton("Play", playIcon);
99 ticksPerBeat = Moosique.getSequence().getResolution();
103 * Creates a button with the specified image and action command / tooltip.
105 private JButton createButton(String imagelocation, String command) {
106 JButton button = new JButton (new ImageIcon(imagelocation));
107 button.setToolTipText(command);
108 button.setActionCommand(command);
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);
133 * Updates the play button with the given command and icon.
135 private void updatePlayButton(String command, Icon icon) {
136 playpause.setIcon(icon);
137 playpause.setActionCommand(command);
138 playpause.setToolTipText(command);
141 class MAdapter extends MouseAdapter {
142 public void mouseClicked(MouseEvent e) {
143 if (e.getSource() instanceof JButton) {
144 String command = ((JButton)e.getSource()).getActionCommand();
145 if (command == "Play") {
146 updatePlayButton("Pause", pauseIcon);
148 } else if (command == "Pause") {
149 updatePlayButton("Resume", playIcon);
151 } else if (command == "Resume") {
152 updatePlayButton("Pause", pauseIcon);
154 } else if (command == "Rewind") {
155 System.out.println("Rewound!");
156 // Different implementation, perhaps?
157 updatePlayButton("Play", playIcon);
158 Moosique.setEditPosition(0);
160 } else if (command == "Fast forward") {
161 } else if (command == "Stop") {
162 updatePlayButton("Play", playIcon);
165 } else if (e.getSource() instanceof JLabel) {
166 long position = Moosique.getEditPosition();
167 if (e.getSource().equals(measuresValue)) {
168 if (SwingUtilities.isRightMouseButton(e)) {
169 position += beatsPerMeasure * ticksPerBeat;
170 } else if (SwingUtilities.isLeftMouseButton(e) && Integer.parseInt(measuresValue.getText()) > 1) {
171 position -= beatsPerMeasure * ticksPerBeat;
173 } else if (e.getSource().equals(beatsValue)) {
174 if (SwingUtilities.isRightMouseButton(e)) {
175 position += ticksPerBeat;
176 } else if (SwingUtilities.isLeftMouseButton(e) && Integer.parseInt(beatsValue.getText()) > 1) {
177 position -= ticksPerBeat;
179 } else if (e.getSource().equals(ticksValue)) {
180 if (SwingUtilities.isRightMouseButton(e)) {
182 } else if (SwingUtilities.isLeftMouseButton(e) && Integer.parseInt(ticksValue.getText()) > 1) {
186 Moosique.setEditPosition(position);
187 Moosique.getGUI().update(position);
191 /* public void mousePressed(MouseEvent e) {
192 if (e.getSource() instanceof JButton) {
193 if (((JButton)e.getSource()).getToolTipText() == "Rewind") {
194 Moosique.rewind(beatsPerMeasure * ticksPerBeat);
195 } else if (((JButton)e.getSource()).getToolTipText() == "Fast forward") {
196 Moosique.forward(beatsPerMeasure * ticksPerBeat);
201 public void mouseReleased(MouseEvent e) {}