]> ruin.nu Git - moosique.git/blob - MooToolbar.java
Tweaked the progress indicator
[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                 // setAlignmentX(LEFT_ALIGNMENT);
28                 setFloatable(false);
29                 mouseAdapter = new MAdapter();
30
31                 // Creates playback buttons
32                 rewind = createButton("images/rewind.gif", "Rewind");
33                 playpause = createButton("images/play.gif", "Play");
34                 stop = createButton("images/stop.gif", "Stop");
35                 fastforward = createButton("images/forward.gif", "Fast forward");
36                 playIcon = new ImageIcon("images/play.gif");
37                 pauseIcon = new ImageIcon("images/pause.gif");
38
39                 // Adds playback buttons
40                 add(rewind);
41                 add(playpause);
42                 add(stop);
43                 add(fastforward);
44                 
45                 // Creates progress indicator
46                 measure = createLabel("Msr", 10);
47                 beats = createLabel("Beat", 10);
48                 ticks = createLabel("Tick", 10);
49                 measuresValue = formatProgInd(createLabel("1", 16));
50                 beatsValue = formatProgInd(createLabel("1", 16));
51                 ticksValue = formatProgInd(createLabel("1", 16));
52                 JPanel spacenorth = new JPanel();
53                 spacenorth.setBackground(bgColor);
54                 JPanel spacesouth = new JPanel();
55                 spacesouth.setBackground(bgColor);
56
57                 // Creates progress indicator panel and adds components
58                 progIndPanel = new JPanel();
59                 progIndPanel.setMaximumSize(new Dimension(120,27));
60                 progIndPanel.setLayout(new GridLayout(2,4));
61                 progIndPanel.add(spacenorth);
62                 progIndPanel.add(measure);
63                 progIndPanel.add(beats);
64                 progIndPanel.add(ticks);
65                 progIndPanel.add(spacesouth);
66                 progIndPanel.add(measuresValue);
67                 progIndPanel.add(beatsValue);
68                 progIndPanel.add(ticksValue);
69                 add(progIndPanel);
70         }
71
72         /**
73          * Updates the progress indicator.
74          * @param tickPosition  the tick position to visualize
75          */
76         public void updateProgInd(long tickPosition) {
77                 if (tickPosition == 0) {
78                         resetProgInd();
79                 } else {
80                         // Otherwise, calculates the current song position in measures, beats and ticks.
81                         long measures = tickPosition / (beatsPerMeasure * ticksPerBeat);
82                         long beats = (tickPosition - measures * beatsPerMeasure * ticksPerBeat) / ticksPerBeat;
83                         long ticks = tickPosition - measures * beatsPerMeasure * ticksPerBeat - beats * ticksPerBeat;
84                         measuresValue.setText(Long.toString(1 + measures));
85                         beatsValue.setText(Long.toString(1 + beats));
86                         ticksValue.setText(Long.toString(1 + ticks));
87                 }
88         }
89
90         /**
91          * Resets the progress indicator.
92          */
93         public void resetProgInd() {
94                         measuresValue.setText("1");
95                         beatsValue.setText("1");
96                         ticksValue.setText("1");
97                         playpause.setIcon(playIcon);
98                         playpause.setToolTipText("Play");
99                         ticksPerBeat = Moosique.getSequence().getResolution();
100         }
101
102         /*
103          * Creates a button with the specified image and tooltip.
104          */
105         private JButton createButton(String imagelocation, String tooltip) {
106                 JButton button = new JButton (new ImageIcon(imagelocation));
107                 button.setToolTipText(tooltip);
108                 button.addMouseListener(mouseAdapter);
109                 return button;
110         }
111         
112         /*
113          * Creates labels with the specified text and font size. 
114          */
115         private JLabel createLabel(String title, int fontSize){
116                 JLabel label = new JLabel(title,JLabel.CENTER);
117                 label.setFont(new Font("Times New Roman", Font.PLAIN, fontSize));
118                 return label;
119         }
120
121         /*
122          * Formats the given label for the progress indicator.
123          */
124         private JLabel formatProgInd(JLabel label){
125                 label.addMouseListener(mouseAdapter);
126                 label.setBorder(BorderFactory.createLineBorder(Color.black));
127                 label.setBackground(Color.white);
128                 return label;
129         }
130
131         class MAdapter extends MouseAdapter {
132                 public void mouseClicked(MouseEvent e) {
133                         if (e.getSource() instanceof JButton) {
134                                 String toolTip = ((JButton)e.getSource()).getToolTipText();
135                                 if (toolTip == "Play") {
136                                         playpause.setIcon(pauseIcon);
137                                         playpause.setToolTipText("Pause");
138                                         Moosique.play();
139                                 } else if (toolTip == "Pause") {
140                                         playpause.setIcon(playIcon);
141                                         playpause.setToolTipText("Resume");
142                                         Moosique.pause();
143                                 } else if (toolTip == "Resume") {
144                                         playpause.setIcon(pauseIcon);
145                                         playpause.setToolTipText("Pause");
146                                         Moosique.resume();
147                                 } else if (toolTip == "Stop") {
148                                         Moosique.stop();
149                                 }
150                         } else if (e.getSource() instanceof JLabel) {
151                                 long position = Moosique.getPosition();
152                                 if (e.getSource().equals(measuresValue)) {
153                                         if (SwingUtilities.isRightMouseButton(e)) {
154                                                 position += beatsPerMeasure * ticksPerBeat;
155                                         } else if (SwingUtilities.isLeftMouseButton(e) && Integer.parseInt(measuresValue.getText()) > 1) {
156                                                 position -= beatsPerMeasure * ticksPerBeat;
157                                         }
158                                 } else if (e.getSource().equals(beatsValue)) {
159                                         if (SwingUtilities.isRightMouseButton(e)) {
160                                                 position += ticksPerBeat;
161                                         } else if (SwingUtilities.isLeftMouseButton(e) && Integer.parseInt(beatsValue.getText()) > 1) {
162                                                 position -= ticksPerBeat;
163                                         }
164                                 } else if (e.getSource().equals(ticksValue)) {
165                                         if (SwingUtilities.isRightMouseButton(e)) {
166                                                 position += 1;
167                                         } else if (SwingUtilities.isLeftMouseButton(e) && Integer.parseInt(ticksValue.getText()) > 1) {
168                                                 position -= 1;
169                                         }
170                                 }
171                                 Moosique.setPosition(position);
172                                 Moosique.getGUI().update(position);
173                         }
174                 }
175
176                 public void mousePressed(MouseEvent e) {
177                         if (e.getSource() instanceof JButton) {
178                                 if (((JButton)e.getSource()).getToolTipText() == "Rewind") {
179                                         Moosique.rewind(beatsPerMeasure * ticksPerBeat);
180                                 } else if (((JButton)e.getSource()).getToolTipText() == "Fast forward") {
181                                         Moosique.forward(beatsPerMeasure * ticksPerBeat);
182                                 }
183                         }
184                 }
185
186                 public void mouseReleased(MouseEvent e) {}
187         }
188 }