]> ruin.nu Git - moosique.git/blob - MooTrackTitle.java
Fixed accessors, encoders and decoders for tempo and time signature.
[moosique.git] / MooTrackTitle.java
1 import javax.swing.*;
2 import java.awt.*;
3 import java.awt.event.*;
4 import javax.sound.midi.*;
5
6 /**
7  * The title bar for each track with track name, channelBox, instrument etc.
8  * 
9  * @author  Andersson, Andreen, Lanneskog, Pehrson
10  * @version 1
11  */
12  
13 public class MooTrackTitle extends JPanel {
14
15         private Track track;
16         private MetaMessage trackNameMessage;
17         private ShortMessage programChangeMessage;
18         private MooTrackView mtv;
19
20         private JTextField title;
21         private MooInstrumentList instruments;
22         private JComboBox channelBox;
23         private JCheckBox mute;
24         private JCheckBox solo;
25         private JButton record;
26
27         private String trackName = "";
28         private int channel = 0;
29
30         /** 
31          * Creates the title bar for an empty track, and therefore an initial channel is required.
32          * @param aTrack the track that this tracktitle is operating on.
33          * @param chan  the initial channel
34          */
35         public MooTrackTitle (Track aTrack, int chan) {
36                 this(aTrack);
37                 channel = chan;
38         }
39
40         /** 
41          * Creates the title bar.
42          * @param aTrack the track that this track title is operating on.
43          */
44         public MooTrackTitle (Track aTrack) {
45                 setDoubleBuffered(true);
46                 track = aTrack;
47
48                 // Finds track name, program change and channel.
49                 MidiMessage msg;
50                 int status;
51                 for (int i = 0; i < track.size(); i++) {
52                         msg = track.get(i).getMessage();
53                         status = msg.getStatus();
54                         if (status == MetaMessage.META) {
55                                 if (((MetaMessage)msg).getType() == 3) {
56                                         trackNameMessage = (MetaMessage)msg;
57                                         trackName = new String(trackNameMessage.getData());
58                                 }
59                         } else if (status >= 192 && status < 208) {
60                                 programChangeMessage = (ShortMessage)msg;
61                                 channel = programChangeMessage.getChannel();
62                         }
63                 }
64
65                 // Creates and places components.
66                 setLayout(new GridLayout(4,1));
67                 setBorder(BorderFactory.createLineBorder(Color.black));
68                 TitleListener tl = new TitleListener();
69
70                 setPreferredSize(new Dimension(MooTrackView.VIEW_WIDTH,70));
71                 title = new JTextField(trackName);
72                 title.setFont(Moosique.getGUI().FONT);
73                 title.addFocusListener(new TitleFocusListener());
74                 add(title);
75
76                 int type;
77                 if (channel == 9) type = MooInstrumentList.DRUMS;
78                 else type = MooInstrumentList.INSTRUMENTS;
79                 instruments = new MooInstrumentList(channel, type, programChangeMessage);
80                 add(instruments);
81
82                 channelBox = new JComboBox();
83                 channelBox.setFont(Moosique.getGUI().FONT);
84                 for (int i = 1; i <= 16; i++)
85                         channelBox.addItem(new Integer(i));
86
87                 channelBox.setSelectedIndex(channel);
88
89                 channelBox.addActionListener(tl);
90                 add(channelBox);
91
92                 JPanel checkboxes = new JPanel();
93                 checkboxes.setLayout(new GridLayout(1,3));
94
95                 mute = new JCheckBox("Mute");
96                 mute.setSelected(false);
97                 Moosique.setTrackMute(track, false);
98                 mute.setFont(Moosique.getGUI().FONT);
99                 mute.addActionListener(tl);
100                 checkboxes.add(mute);
101
102                 solo = new JCheckBox("Solo");
103                 solo.setSelected(false);
104                 Moosique.setTrackSolo(track, false);
105                 solo.setFont(Moosique.getGUI().FONT);
106                 solo.addActionListener(tl);
107                 checkboxes.add(solo);
108                 
109                 record = new JButton("Record");
110                 record.setFont(Moosique.getGUI().FONT);
111                 record.addActionListener(tl);
112                 checkboxes.add(record);
113
114                 add(checkboxes);
115         }
116         
117         /** 
118          * Returns the channel of the track that the view is visualising.
119          * @return the chanel of the visualised track
120          */
121         public int getChannel() {
122                 return channel;
123         }
124
125         /** 
126          * Sets the track view this title should update after recording.
127          * @param the track view
128          */
129         public void setTrackView(MooTrackView tv) {
130                 mtv = tv;
131         }
132
133         /**
134          * Checks if the focus is lost.
135          */
136         class TitleFocusListener extends FocusAdapter {
137                 public void focusLost(FocusEvent e) {
138                         // Update the MidiEvent containing the title of this track
139                 }
140         }
141
142         /**
143          * Takes the appropriate action when a user selects an item on the popup menu.
144          */
145         class TitleListener implements ActionListener {
146                 public void actionPerformed(ActionEvent e) {
147                         Object source = e.getSource();
148                         if (source == channelBox) {
149                                 channel = channelBox.getSelectedIndex();
150                                 MidiEvent me;
151                                 MooNote mn;
152                                 instruments.setChannel(channel);
153                                 // Query the user before rechannelling???
154                                 for (int j = 0; j < track.size(); j++) {
155                                         me = track.get(j);
156                                         if (me instanceof MooNote){
157                                                 mn = (MooNote)me;
158                                                 mn.setChannel(channel);
159                                         }
160                                 }
161                         } else if (source == solo) {
162                                 boolean selected = mute.isSelected();
163                                 if (selected){
164                                         solo.setSelected(false);
165                                         Moosique.setTrackSolo(track, false);
166                                 }
167                                 Moosique.setTrackMute(track, selected);
168                         } else if (source == mute) {
169                                 boolean selected = solo.isSelected();
170                                 if (selected){
171                                         mute.setSelected(false);
172                                         Moosique.setTrackMute(track, false);
173                                 }
174                                 Moosique.setTrackSolo(track, selected);
175                         } else if (source == record) {
176                                 Sequencer sequencer = Moosique.getSequencer();
177                                 boolean quantize = false;
178                                 if (record.getText() == "Record") {
179                                         /* Show a dialog with:
180                                                 "Track" combo box,
181                                                 "Channel" combo box (disabled?,
182                                                 "Quantize" checkbox and 
183                                                 "Start Recording" button.
184                                         */
185                                         record.setText("Stop");
186                                         mtv.enableKeyboardRecording();
187                                         sequencer.recordEnable(track, channel);
188                                         sequencer.startRecording();
189                                         Moosique.setEdited();
190                                 } else {
191                                         record.setText("Record");
192                                         mtv.disableKeyboardRecording();
193                                         sequencer.stopRecording();
194                                         sequencer.recordDisable(track);
195                                         java.util.List newNotes = Moosique.convertTrack(track, quantize);
196                                         mtv.placeNewNotes(newNotes);
197                                 }
198                         }
199                 }
200         }
201 }