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