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