]> ruin.nu Git - moosique.git/blob - MooToolbar.java
font settings..
[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, measuresValue, beatsValue, ticksValue;
15         private JPanel progIndPanel;
16         private ImageIcon playIcon, pauseIcon;
17         private MAdapter mouseAdapter;
18         private int ticksPerBeat; 
19         public static final int beatsPerMeasure = 4;
20         public static final Color bgColor = new Color(192, 224, 255);
21         
22         /**
23          * Creates the toolbar.
24          */
25         
26         public MooToolbar() {
27                 // setAlignmentX(LEFT_ALIGNMENT);
28                 setFloatable(false);
29                 mouseAdapter = new MAdapter();
30
31                 // Creates playback buttons
32                 rewind = createButton("images/rewind.gif", "Rewind");
33                 playpause = createButton("images/play.gif", "Play");
34                 stop = createButton("images/stop.gif", "Stop");
35                 fastforward = createButton("images/forward.gif", "Fast forward");
36                 playIcon = new ImageIcon("images/play.gif");
37                 pauseIcon = new ImageIcon("images/pause.gif");
38
39                 // Adds playback buttons
40                 add(rewind);
41                 add(playpause);
42                 add(stop);
43                 add(fastforward);
44                 
45                 // Creates progress indicator
46                 measure = createLabel("Msr", 10);
47                 beats = createLabel("Beat", 10);
48                 ticks = createLabel("Tick", 10);
49                 measuresValue = formatProgInd(createLabel("1", 16));
50                 beatsValue = formatProgInd(createLabel("1", 16));
51                 ticksValue = formatProgInd(createLabel("1", 16));
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                 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(measuresValue);
67                 progIndPanel.add(beatsValue);
68                 progIndPanel.add(ticksValue);
69                 add(progIndPanel);
70         }
71
72         /**
73          * Updates the progress indicator.
74          * @param tickPosition  the tick position to visualize
75          */
76         public void updateProgInd(long tickPosition) {
77                 ticksPerBeat = Moosique.getSequence().getResolution();
78                 if (tickPosition == 0) {
79                         resetProgInd();
80                 } else {
81                         // Otherwise, calculates the current song position in measures, beats and ticks.
82                         long measures = tickPosition / (beatsPerMeasure * ticksPerBeat);
83                         long beats = (tickPosition - measures * beatsPerMeasure * ticksPerBeat) / ticksPerBeat;
84                         long ticks = tickPosition - measures * beatsPerMeasure * ticksPerBeat - beats * ticksPerBeat;
85                         measuresValue.setText(Long.toString(1 + measures));
86                         beatsValue.setText(Long.toString(1 + beats));
87                         ticksValue.setText(Long.toString(1 + ticks));
88                 }
89         }
90
91         /**
92          * Resets the progress indicator.
93          */
94         public void resetProgInd() {
95                         measuresValue.setText("1");
96                         beatsValue.setText("1");
97                         ticksValue.setText("1");
98                         playpause.setIcon(playIcon);
99                         playpause.setToolTipText("Play");
100                         ticksPerBeat = Moosique.getSequence().getResolution();
101         }
102
103         /**
104          * Creates a button with the specified image and tooltip.
105          */
106         private JButton createButton(String imagelocation, String tooltip) {
107                 JButton button = new JButton (new ImageIcon(imagelocation));
108                 button.setToolTipText(tooltip);
109                 button.addMouseListener(mouseAdapter);
110                 return button;
111         }
112         
113         /**
114          * Creates labels with the specified text and font size. 
115          */
116         private JLabel createLabel(String title, int fontSize){
117                 JLabel label = new JLabel(title,JLabel.CENTER);
118                 label.setFont(new Font("Times New Roman", Font.PLAIN, fontSize));
119                 return label;
120         }
121
122         /**
123          * Formats the given label for the progress indicator.
124          */
125         private JLabel formatProgInd(JLabel label){
126                 label.addMouseListener(mouseAdapter);
127                 label.setBorder(BorderFactory.createLineBorder(Color.black));
128                 label.setBackground(Color.white);
129                 return label;
130         }
131
132         class MAdapter extends MouseAdapter {
133                 public void mouseClicked(MouseEvent e) {
134                         if (e.getSource() instanceof JButton) {
135                                 String toolTip = ((JButton)e.getSource()).getToolTipText();
136                                 if (toolTip == "Play") {
137                                         playpause.setIcon(pauseIcon);
138                                         playpause.setToolTipText("Pause");
139                                         Moosique.play();
140                                 } else if (toolTip == "Pause") {
141                                         playpause.setIcon(playIcon);
142                                         playpause.setToolTipText("Resume");
143                                         Moosique.pause();
144                                 } else if (toolTip == "Resume") {
145                                         playpause.setIcon(pauseIcon);
146                                         playpause.setToolTipText("Pause");
147                                         Moosique.resume();
148                                 } else if (toolTip == "Stop") {
149                                         Moosique.stop();
150                                 }
151                         } else if (e.getSource() instanceof JLabel) {
152                                 long position = Moosique.getEditPosition();
153                                 if (e.getSource().equals(measuresValue)) {
154                                         if (SwingUtilities.isRightMouseButton(e)) {
155                                                 position += beatsPerMeasure * ticksPerBeat;
156                                         } else if (SwingUtilities.isLeftMouseButton(e) && Integer.parseInt(measuresValue.getText()) > 1) {
157                                                 position -= beatsPerMeasure * ticksPerBeat;
158                                         }
159                                 } else if (e.getSource().equals(beatsValue)) {
160                                         if (SwingUtilities.isRightMouseButton(e)) {
161                                                 position += ticksPerBeat;
162                                         } else if (SwingUtilities.isLeftMouseButton(e) && Integer.parseInt(beatsValue.getText()) > 1) {
163                                                 position -= ticksPerBeat;
164                                         }
165                                 } else if (e.getSource().equals(ticksValue)) {
166                                         if (SwingUtilities.isRightMouseButton(e)) {
167                                                 position += 1;
168                                         } else if (SwingUtilities.isLeftMouseButton(e) && Integer.parseInt(ticksValue.getText()) > 1) {
169                                                 position -= 1;
170                                         }
171                                 }
172                                 Moosique.setEditPosition(position);
173                                 Moosique.getGUI().update(position);
174                         }
175                 }
176
177                 public void mousePressed(MouseEvent e) {
178                         if (e.getSource() instanceof JButton) {
179                                 if (((JButton)e.getSource()).getToolTipText() == "Rewind") {
180                                         Moosique.rewind(beatsPerMeasure * ticksPerBeat);
181                                 } else if (((JButton)e.getSource()).getToolTipText() == "Fast forward") {
182                                         Moosique.forward(beatsPerMeasure * ticksPerBeat);
183                                 }
184                         }
185                 }
186
187                 public void mouseReleased(MouseEvent e) {}
188         }
189 }