]> 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 = createLabel("1", 16);
47                 measureValue.setBorder(BorderFactory.createLineBorder(Color.black));
48                 beatsValue = createLabel("1", 16);
49                 beatsValue.setBorder(BorderFactory.createLineBorder(Color.black));
50                 ticksValue = createLabel("1", 16);
51                 ticksValue.setBorder(BorderFactory.createLineBorder(Color.black));
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                 JPanel 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(measureValue);
67                 progIndPanel.add(beatsValue);
68                 progIndPanel.add(ticksValue);
69                 add(progIndPanel);
70
71         }
72
73         /**
74          * Updates the progress indicator.
75          */
76         public void updateProgInd() {
77                 long pos = Moosique.getSequencer().getTickPosition();
78                 int ticksPerBeat = Moosique.getSequence().getResolution();
79                 int beatsPerMeasure = 4;
80                 long measures = pos / (beatsPerMeasure * ticksPerBeat);
81                 long beats = (pos - measures * beatsPerMeasure * ticksPerBeat) / ticksPerBeat;
82                 long ticks = pos - measures * beatsPerMeasure * ticksPerBeat - beats * ticksPerBeat;
83                 measureValue.setText(Long.toString(measures));
84                 beatsValue.setText(Long.toString(beats));
85                 ticksValue.setText(Long.toString(ticks));
86         }
87
88         /**
89          * Creates a button with the specified image and tooltip.
90          * @param imagelocation         the imagelacation of the the image displayed in the button
91          * @param tooltip               the tooltip associated with this button
92          * @return button               the button to be attached to the toolbar
93          */
94         private JButton createButton(String imagelocation, String tooltip) {
95                 JButton button = new JButton (new ImageIcon(imagelocation));
96                 button.setToolTipText(tooltip);
97                 button.addMouseListener(mouseAdapter);
98                 return button;
99         }
100         
101         /**
102          * Creates labels with the specified text and font size. 
103          * @param title         the titel displayed on the label
104          * @param fontsize      the fontzise of the text displayed on the label
105          */
106         private JLabel createLabel(String title, int fontSize){
107                 JLabel label = new JLabel(title,JLabel.CENTER);
108                 label.setFont(new Font("Times New Roman", Font.PLAIN, fontSize));
109                 return label;
110         }
111
112         class MooMouseAdapter extends MouseAdapter {
113                 public void mouseClicked(MouseEvent e) {
114                         if (((JButton)e.getSource()).getToolTipText() == "Play") {
115                                         Moosique.play();
116                                         playpause.setIcon(pauseIcon);
117                                         playpause.setToolTipText("Pause");
118                                         while(Moosique.getSequencer().isRunning()) {
119                                                 Moosique.getGUI().update();
120                                         }
121                         } else if (((JButton)e.getSource()).getToolTipText() == "Pause") {
122                                         Moosique.pause();
123                                         playpause.setIcon(playIcon);
124                                         playpause.setToolTipText("Resume");
125                         } else if (((JButton)e.getSource()).getToolTipText() == "Resume") {
126                                         Moosique.resume();
127                                         playpause.setIcon(pauseIcon);
128                                         playpause.setToolTipText("Pause");
129                                         while(Moosique.getSequencer().isRunning()) {
130                                                 Moosique.getGUI().update();
131                                         }
132                         } else if (((JButton)e.getSource()).getToolTipText() == "Stop") {
133                                 Moosique.stop();
134                                 playpause.setIcon(playIcon);
135                                 playpause.setToolTipText("Play");
136                         }
137                 }
138
139                 public void mousePressed(MouseEvent e) {
140                         if (((JButton)e.getSource()).getToolTipText() == "Rewind") {
141                                 Moosique.rewind(96);
142                         } else if (((JButton)e.getSource()).getToolTipText() == "Fast forward") {
143                                 Moosique.forward(96);
144                         }
145                 }
146
147                 public void mouseReleased(MouseEvent e) {}
148         }
149 }