]> ruin.nu Git - moosique.git/blob - MooToolbar.java
Fixed mouse listener for 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                                         resetProgInd();
149                                         Moosique.stop();
150                                 }
151                         } else if (e.getSource() instanceof JLabel) {
152                                 long position = Moosique.getSequencer().getTickPosition();
153                                 if (e.getSource().equals(measuresValue)) {
154                                         if (SwingUtilities.isRightMouseButton(e)) {
155                                                 System.out.println("IncM");
156                                                 position += beatsPerMeasure * ticksPerBeat;
157                                         } else if (SwingUtilities.isLeftMouseButton(e) && Integer.parseInt(measuresValue.getText()) > 1) {
158                                                 System.out.println("DecM");
159                                                 position -= beatsPerMeasure * ticksPerBeat;
160                                         }
161                                 } else if (e.getSource().equals(beatsValue)) {
162                                         if (SwingUtilities.isRightMouseButton(e)) {
163                                                 System.out.println("IncB");
164                                                 position += ticksPerBeat;
165                                         } else if (SwingUtilities.isLeftMouseButton(e) && Integer.parseInt(beatsValue.getText()) > 1) {
166                                                 System.out.println("DecB");
167                                                 position -= ticksPerBeat;
168                                         }
169                                 } else if (e.getSource().equals(ticksValue)) {
170                                         if (SwingUtilities.isRightMouseButton(e)) {
171                                                 System.out.println("IncT");
172                                                 position += 1;
173                                         } else if (SwingUtilities.isLeftMouseButton(e) && Integer.parseInt(ticksValue.getText()) > 1) {
174                                                 System.out.println("DecT");
175                                                 position -= 1;
176                                         }
177                                 }
178                                 Moosique.getSequencer().setTickPosition(position);
179                                 Moosique.getGUI().update(position);
180                         }
181                 }
182
183                 public void mousePressed(MouseEvent e) {
184                         if (e.getSource() instanceof JButton) {
185                                 if (((JButton)e.getSource()).getToolTipText() == "Rewind") {
186                                         Moosique.rewind(beatsPerMeasure * ticksPerBeat);
187                                 } else if (((JButton)e.getSource()).getToolTipText() == "Fast forward") {
188                                         Moosique.forward(beatsPerMeasure * ticksPerBeat);
189                                 }
190                         }
191                 }
192
193                 public void mouseReleased(MouseEvent e) {}
194         }
195 }