]> 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, measureValue, beatsValue, ticksValue;
15         private ImageIcon playIcon, pauseIcon;
16         private MooMouseAdapter mouseAdapter;
17         public static final Color bgColor = new Color(192, 224, 255);
18         
19         /**
20          * Creates the toolbar.
21          */
22         
23         public MooToolbar() {
24                 // setAlignmentX(LEFT_ALIGNMENT);
25                 setFloatable(false);
26                 mouseAdapter = new MooMouseAdapter();
27
28                 // Creates playback buttons
29                 rewind = createButton("images/rewind.gif", "Rewind");
30                 playpause = createButton("images/play.gif", "Play");
31                 stop = createButton("images/stop.gif", "Stop");
32                 fastforward = createButton("images/forward.gif", "Fast forward");
33                 playIcon = new ImageIcon("images/play.gif");
34                 pauseIcon = new ImageIcon("images/pause.gif");
35
36                 // Adds playback buttons
37                 add(rewind);
38                 add(playpause);
39                 add(stop);
40                 add(fastforward);
41                 
42                 // Creates progress indicator
43                 measure = createLabel("Msr", 10);
44                 beats = createLabel("Beat", 10);
45                 ticks = createLabel("Tick", 10);
46                 measureValue = formatProgInd(createLabel("1", 16));
47                 beatsValue = formatProgInd(createLabel("1", 16));
48                 ticksValue = formatProgInd(createLabel("1", 16));
49                 JPanel spacenorth = new JPanel();
50                 spacenorth.setBackground(bgColor);
51                 JPanel spacesouth = new JPanel();
52                 spacesouth.setBackground(bgColor);
53
54                 // Creates progress indicator panel and adds components
55                 JPanel progIndPanel = new JPanel();
56                 progIndPanel.setMaximumSize(new Dimension(120,27));
57                 progIndPanel.setLayout(new GridLayout(2,4));
58                 progIndPanel.add(spacenorth);
59                 progIndPanel.add(measure);
60                 progIndPanel.add(beats);
61                 progIndPanel.add(ticks);
62                 progIndPanel.add(spacesouth);
63                 progIndPanel.add(measureValue);
64                 progIndPanel.add(beatsValue);
65                 progIndPanel.add(ticksValue);
66                 add(progIndPanel);
67
68         }
69
70         /**
71          * Updates the progress indicator.
72          */
73         public void updateProgInd() {
74                 long pos = Moosique.getSequencer().getTickPosition();
75                 int ticksPerBeat = Moosique.getSequence().getResolution();
76                 int beatsPerMeasure = 4;
77                 long measures = pos / (beatsPerMeasure * ticksPerBeat);
78                 long beats = (pos - measures * beatsPerMeasure * ticksPerBeat) / ticksPerBeat;
79                 long ticks = pos - measures * beatsPerMeasure * ticksPerBeat - beats * ticksPerBeat;
80                 measureValue.setText(Long.toString(measures));
81                 beatsValue.setText(Long.toString(beats));
82                 ticksValue.setText(Long.toString(ticks));
83         }
84
85         /*
86          * Creates a button with the specified image and tooltip.
87          */
88         private JButton createButton(String imagelocation, String tooltip) {
89                 JButton button = new JButton (new ImageIcon(imagelocation));
90                 button.setToolTipText(tooltip);
91                 button.addMouseListener(mouseAdapter);
92                 return button;
93         }
94         
95         /*
96          * Creates labels with the specified text and font size. 
97          */
98         private JLabel createLabel(String title, int fontSize){
99                 JLabel label = new JLabel(title,JLabel.CENTER);
100                 label.setFont(new Font("Times New Roman", Font.PLAIN, fontSize));
101                 return label;
102         }
103
104         /*
105          * Formats the given label for the progress indicator.
106          */
107         private JLabel formatProgInd(JLabel label){
108                 label.setBorder(BorderFactory.createLineBorder(Color.black));
109                 label.setBackground(Color.white);
110                 return label;
111         }
112
113         class MooMouseAdapter extends MouseAdapter {
114                 public void mouseClicked(MouseEvent e) {
115                         if (((JButton)e.getSource()).getToolTipText() == "Play") {
116                                         Moosique.play();
117                                         playpause.setIcon(pauseIcon);
118                                         playpause.setToolTipText("Pause");
119                                         while(Moosique.getSequencer().isRunning()) {
120                                                 Moosique.getGUI().update();
121                                         }
122                         } else if (((JButton)e.getSource()).getToolTipText() == "Pause") {
123                                         Moosique.pause();
124                                         playpause.setIcon(playIcon);
125                                         playpause.setToolTipText("Resume");
126                         } else if (((JButton)e.getSource()).getToolTipText() == "Resume") {
127                                         Moosique.resume();
128                                         playpause.setIcon(pauseIcon);
129                                         playpause.setToolTipText("Pause");
130                                         while(Moosique.getSequencer().isRunning()) {
131                                                 Moosique.getGUI().update();
132                                         }
133                         } else if (((JButton)e.getSource()).getToolTipText() == "Stop") {
134                                 Moosique.stop();
135                                 playpause.setIcon(playIcon);
136                                 playpause.setToolTipText("Play");
137                         }
138                 }
139
140                 public void mousePressed(MouseEvent e) {
141                         if (((JButton)e.getSource()).getToolTipText() == "Rewind") {
142                                 Moosique.rewind(96);
143                         } else if (((JButton)e.getSource()).getToolTipText() == "Fast forward") {
144                                 Moosique.forward(96);
145                         }
146                 }
147
148                 public void mouseReleased(MouseEvent e) {}
149         }
150 }