]> ruin.nu Git - moosique.git/blob - MooToolbar.java
no message
[moosique.git] / MooToolbar.java
1 import javax.sound.midi.*;
2 import javax.swing.*;
3 import java.awt.*;
4 import java.awt.event.*;
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                 System.out.print("Updating to " + tickPosition + " = ");
79                 if (tickPosition == 0) {
80                         resetProgInd();
81                         System.out.println("1:1:1");
82                 } else {
83                         int[] position = Moosique.getPositionForTicks(tickPosition);
84                         System.out.println("" + position[0] + ":" + position[1] + ":" + position[2]);
85                         measuresValue.setText(Integer.toString(position[0]));
86                         beatsValue.setText(Long.toString(position[1]));
87                         ticksValue.setText(Long.toString(position[2]));
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                 updatePlayButton("Play", playIcon);
99                 ticksPerBeat = Moosique.getSequence().getResolution();
100         }
101
102         /**
103          * Creates a button with the specified image and action command / tooltip.
104          */
105         private JButton createButton(String imagelocation, String command) {
106                 JButton button = new JButton (new ImageIcon(imagelocation));
107                 button.setToolTipText(command);
108                 button.setActionCommand(command);
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         /**
133          * Updates the play button with the given command and icon.
134          */
135         private void updatePlayButton(String command, Icon icon) {
136                 playpause.setIcon(icon);
137                 playpause.setActionCommand(command);
138                 playpause.setToolTipText(command);
139         }
140
141         class MAdapter extends MouseAdapter {
142                 public void mouseClicked(MouseEvent e) {
143                         if (e.getSource() instanceof JButton) {
144                                 String command = ((JButton)e.getSource()).getActionCommand();
145                                 if (command == "Play") {
146                                         updatePlayButton("Pause", pauseIcon);
147                                         Moosique.play();
148                                 } else if (command == "Pause") {
149                                         updatePlayButton("Resume", playIcon);
150                                         Moosique.pause();
151                                 } else if (command == "Resume") {
152                                         updatePlayButton("Pause", pauseIcon);
153                                         Moosique.resume();
154                                 } else if (command == "Rewind") {
155                                         System.out.println("Rewound!");
156                                         // Different implementation, perhaps?
157                                         updatePlayButton("Play", playIcon);
158                                         Moosique.setEditPosition(0);
159                                         Moosique.stop();
160                                 } else if (command == "Fast forward") {
161                                 } else if (command == "Stop") {
162                                         updatePlayButton("Play", playIcon);
163                                         Moosique.stop();
164                                 }
165                         } else if (e.getSource() instanceof JLabel) {
166                                 long position = Moosique.getEditPosition();
167                                 if (e.getSource().equals(measuresValue)) {
168                                         if (SwingUtilities.isRightMouseButton(e)) {
169                                                 position += beatsPerMeasure * ticksPerBeat;
170                                         } else if (SwingUtilities.isLeftMouseButton(e) && Integer.parseInt(measuresValue.getText()) > 1) {
171                                                 position -= beatsPerMeasure * ticksPerBeat;
172                                         }
173                                 } else if (e.getSource().equals(beatsValue)) {
174                                         if (SwingUtilities.isRightMouseButton(e)) {
175                                                 position += ticksPerBeat;
176                                         } else if (SwingUtilities.isLeftMouseButton(e) && Integer.parseInt(beatsValue.getText()) > 1) {
177                                                 position -= ticksPerBeat;
178                                         }
179                                 } else if (e.getSource().equals(ticksValue)) {
180                                         if (SwingUtilities.isRightMouseButton(e)) {
181                                                 position += 1;
182                                         } else if (SwingUtilities.isLeftMouseButton(e) && Integer.parseInt(ticksValue.getText()) > 1) {
183                                                 position -= 1;
184                                         }
185                                 }
186                                 Moosique.setEditPosition(position);
187                                 Moosique.getGUI().update(position);
188                         }
189                 }
190
191 /*              public void mousePressed(MouseEvent e) {
192                         if (e.getSource() instanceof JButton) {
193                                 if (((JButton)e.getSource()).getToolTipText() == "Rewind") {
194                                         Moosique.rewind(beatsPerMeasure * ticksPerBeat);
195                                 } else if (((JButton)e.getSource()).getToolTipText() == "Fast forward") {
196                                         Moosique.forward(beatsPerMeasure * ticksPerBeat);
197                                 }
198                         }
199                 }
200
201                 public void mouseReleased(MouseEvent e) {}
202 */      }
203 }