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