]> ruin.nu Git - moosique.git/blob - MooToolbar.java
no message
[moosique.git] / MooToolbar.java
1 import javax.swing.*;
2 import java.awt.*;
3 import java.awt.event.*;
4 import javax.sound.midi.*;
5
6 /**
7  * The application's toolbar, with the most frequently used commands.
8  * 
9  * @author  Björn Lanneskog
10  */
11 public class MooToolbar extends JToolBar {
12
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);
21         
22         /**
23          * Creates the toolbar.
24          */
25         
26         public MooToolbar() {
27                 setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
28                 // setAlignmentX(LEFT_ALIGNMENT);
29                 setFloatable(false);
30                 mouseAdapter = new MAdapter();
31
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");
39
40                 // Adds playback buttons
41                 add(rewind);
42                 add(playpause);
43                 add(stop);
44                 add(fastforward);
45                 
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);
57
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);
70                 add(progIndPanel);
71         }
72
73         /**
74          * Updates the progress indicator.
75          * @param tickPosition  the tick position to visualize
76          */
77         public void updateProgInd(long tickPosition) {
78                 ticksPerBeat = Moosique.getSequence().getResolution();
79                 if (tickPosition == 0) {
80                         resetProgInd();
81                 } else {
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));
89                 }
90         }
91
92         /**
93          * Resets the progress indicator.
94          */
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();
102         }
103
104         /**
105          * Creates a button with the specified image and action command / tooltip.
106          */
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);
112                 return button;
113         }
114         
115         /**
116          * Creates labels with the specified text and font size. 
117          */
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));
121                 return label;
122         }
123
124         /**
125          * Formats the given label for the progress indicator.
126          */
127         private JLabel formatProgInd(JLabel label){
128                 label.addMouseListener(mouseAdapter);
129                 label.setBorder(BorderFactory.createLineBorder(Color.black));
130                 label.setBackground(Color.white);
131                 return label;
132         }
133
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");
142                                         Moosique.play();
143                                 } else if (command == "Pause") {
144                                         playpause.setIcon(playIcon);
145                                         playpause.setActionCommand("Resume");
146                                         playpause.setToolTipText("Resume");
147                                         Moosique.pause();
148                                 } else if (command == "Resume") {
149                                         playpause.setIcon(pauseIcon);
150                                         playpause.setActionCommand("Pause");
151                                         playpause.setToolTipText("Pause");
152                                         Moosique.resume();
153                                 } else if (command == "Rewind") {
154                                         // Different implementation, perhaps?
155                                         Moosique.setEditPosition(0);
156                                         Moosique.stop();
157                                 } else if (command == "Fast forward") {
158
159                                 } else if (command == "Stop") {
160                                         Moosique.stop();
161                                 }
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;
169                                         }
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;
175                                         }
176                                 } else if (e.getSource().equals(ticksValue)) {
177                                         if (SwingUtilities.isRightMouseButton(e)) {
178                                                 position += 1;
179                                         } else if (SwingUtilities.isLeftMouseButton(e) && Integer.parseInt(ticksValue.getText()) > 1) {
180                                                 position -= 1;
181                                         }
182                                 }
183                                 Moosique.setEditPosition(position);
184                                 Moosique.getGUI().update(position);
185                         }
186                 }
187
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);
194                                 }
195                         }
196                 }
197
198                 public void mouseReleased(MouseEvent e) {}
199         }
200 }