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