]> 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 ImageIcon playIcon, pauseIcon;
16         private MooMouseAdapter mouseAdapter;
17         
18         /**
19          * Creates the toolbar.
20          */
21         
22         public MooToolbar() {
23                 setFloatable(false);
24                 mouseAdapter = new MooMouseAdapter();
25
26                 // Creates playback buttons
27                 rewind = createButton("images/rewind.gif", "Rewind");
28                 add(rewind);
29                 playpause = createButton("images/play.gif", "Play");
30                 add(playpause);
31                 playIcon = new ImageIcon("images/play.gif");
32                 pauseIcon = new ImageIcon("images/pause.gif");
33                 stop = createButton("images/stop.gif", "Stop");
34                 add(stop);
35                 fastforward = createButton("images/forward.gif", "Fast forward");
36                 add(fastforward);
37                 
38                 // Creates progress indicator
39                 JPanel progIndPanel = new JPanel();
40                 progIndPanel.setMaximumSize(new Dimension(120,27));
41                 progIndPanel.setLayout(new GridLayout(2,4));
42                 measure = createLabel("Mrs",10);
43                 beats = createLabel("Beat",10);
44                 ticks = createLabel("Tick",10);
45                 measurevalue = createLabel("1", 16);
46                 beatsvalue = createLabel("1",16);
47                 ticksvalue = createLabel("1",16);
48                 progIndPanel.add(new JPanel());
49                 progIndPanel.add(measure);
50                 progIndPanel.add(beats);
51                 progIndPanel.add(ticks);
52                 progIndPanel.add(new JPanel());
53                 progIndPanel.add(measurevalue);
54                 progIndPanel.add(beatsvalue);
55                 progIndPanel.add(ticksvalue);
56                 add(progIndPanel);
57         }
58                 
59         /**
60          * Creates a button with the specified image and tooltip.
61          * @param imagelocation         the imagelacation of the the image displayed in the button
62          * @param tooltip               the tooltip associated with this button
63          * @return button               the button to be attached to the toolbar
64          */
65         private JButton createButton(String imagelocation, String tooltip) {
66                 JButton button = new JButton (new ImageIcon(imagelocation));
67                 button.setToolTipText(tooltip);
68                 button.addMouseListener(mouseAdapter);
69                 return button;
70         }
71         
72         /**
73          * Creates labels with the specified text and font size. 
74          * @param title         the titel displayed on the label
75          * @param fontsize      the fontzise of the text displayed on the label
76          */
77         private JLabel createLabel(String title, int fontSize){
78                 JLabel value = new JLabel(title,JLabel.CENTER);
79                 value.setFont(new Font("Times New Roman", Font.PLAIN, fontSize));
80                 value.setBorder(BorderFactory.createLineBorder(Color.black));
81                 return value;
82         }
83         
84         // int pos = Moosique.getSequencer().getTickPosition();
85         // int measures = pos / (4 * Moosique.getSequence.get)
86         // int beats = (pos - measures * 4 * 96) / 96;
87         // int ticks = (pos - measures*4*96 - beats*96);
88         
89         class MooMouseAdapter extends MouseAdapter {
90                 public void mouseClicked(MouseEvent e) {
91                         if (((JButton)e.getSource()).getToolTipText() == "Play") {
92                                         Moosique.play();
93                                         playpause.setIcon(pauseIcon);
94                                         playpause.setToolTipText("Pause");
95                         } else if (((JButton)e.getSource()).getToolTipText() == "Pause") {
96                                         Moosique.pause();
97                                         playpause.setIcon(playIcon);
98                                         playpause.setToolTipText("Resume");
99                         } else if (((JButton)e.getSource()).getToolTipText() == "Resume") {
100                                         Moosique.resume();
101                                         playpause.setIcon(pauseIcon);
102                                         playpause.setToolTipText("Pause");
103                         } else if (((JButton)e.getSource()).getToolTipText() == "Stop") {
104                                 Moosique.stop();
105                                 playpause.setIcon(playIcon);
106                                 playpause.setToolTipText("Play");
107                         }
108                 }
109
110                 public void mousePressed(MouseEvent e) {
111                         if (((JButton)e.getSource()).getToolTipText() == "Rewind") {
112                                 Moosique.rewind(96);
113                         } else if (((JButton)e.getSource()).getToolTipText() == "Fast forward") {
114                                 Moosique.forward(96);
115                         }
116                 }
117
118                 public void mouseReleased(MouseEvent e) {}
119         }
120 }