]> ruin.nu Git - moosique.git/blob - MooTrackTitle.java
no message
[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 tracktitle 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                                 // System.out.println("Program change " + programChangeMessage.getData1());
62                                 channel = status - 192;
63                         }
64                 }
65
66                 // Creates and places components.
67                 setLayout(new GridLayout(4,1));
68                 setBorder(BorderFactory.createLineBorder(Color.black));
69                 TitleListener tl = new TitleListener();
70
71                 setPreferredSize(new Dimension(MooTrackView.VIEW_WIDTH,70));
72                 title = new JTextField(trackName);
73                 title.setFont(Moosique.getGUI().FONT);
74                 title.addFocusListener(new TitleFocusListener());
75                 add(title);
76
77                 int type;
78                 if (channel == 9) type = MooInstrumentList.DRUMS;
79                 else type = MooInstrumentList.INSTRUMENTS;
80                 instruments = new MooInstrumentList(channel, type);
81                 // instruments = new MooInstrumentList(channel, type, programChangeMessage);
82                 add(instruments);
83
84                 channelBox = new JComboBox();
85                 channelBox.setFont(Moosique.getGUI().FONT);
86                 for (int i = 1; i <= 16; i++)
87                         channelBox.addItem(new Integer(i));
88
89                 channelBox.setSelectedIndex(channel);
90
91                 channelBox.addActionListener(tl);
92                 add(channelBox);
93
94                 JPanel checkboxes = new JPanel();
95                 checkboxes.setLayout(new GridLayout(1,3));
96
97                 mute = new JCheckBox("Mute");
98                 mute.setSelected(false);
99                 Moosique.setTrackMute(track, false);
100                 mute.setFont(Moosique.getGUI().FONT);
101                 mute.addActionListener(tl);
102                 checkboxes.add(mute);
103
104                 solo = new JCheckBox("Solo");
105                 solo.setSelected(false);
106                 Moosique.setTrackSolo(track, false);
107                 solo.setFont(Moosique.getGUI().FONT);
108                 solo.addActionListener(tl);
109                 checkboxes.add(solo);
110                 
111                 record = new JButton("Record");
112                 record.setFont(Moosique.getGUI().FONT);
113                 record.addActionListener(tl);
114                 checkboxes.add(record);
115
116                 add(checkboxes);
117         }
118         
119         /** 
120          * Returns the channel of the track that the view is visualising.
121          * @return the chanel of the visualised track
122          */
123         public int getChannel() {
124                 return channel;
125         }
126
127         /** 
128          * Sets the track view this title should update after recording.
129          * @param the track view
130          */
131         public void setTrackView(MooTrackView tv) {
132                 mtv = tv;
133         }
134
135         /**
136          * Checks if the focus is lost.
137          */
138         class TitleFocusListener extends FocusAdapter {
139                 public void focusLost(FocusEvent e) {
140                         // Update the MidiEvent containing the title of this track
141                 }
142         }
143
144         /**
145          * Takes the appropriate action when a user selects an item on the popup menu.
146          */
147         class TitleListener implements ActionListener {
148                 public void actionPerformed(ActionEvent e) {
149                         Object source = e.getSource();
150                         if (source == channelBox) {
151                                 channel = channelBox.getSelectedIndex();
152                                 MidiEvent me;
153                                 MooNote mn;
154                                 instruments.setChannel(channel);
155                                 // Query the user before rechannelling???
156                                 for (int j = 0; j < track.size(); j++) {
157                                         me = track.get(j);
158                                         if (me instanceof MooNote){
159                                                 mn = (MooNote)me;
160                                                 mn.setChannel(channel);
161                                         }
162                                 }
163                         } else if (source == solo) {
164                                 boolean selected = mute.isSelected();
165                                 if (selected){
166                                         solo.setSelected(false);
167                                         Moosique.setTrackSolo(track, false);
168                                 }
169                                 Moosique.setTrackMute(track, selected);
170                         } else if (source == mute) {
171                                 boolean selected = solo.isSelected();
172                                 if (selected){
173                                         mute.setSelected(false);
174                                         Moosique.setTrackMute(track, false);
175                                 }
176                                 Moosique.setTrackSolo(track, selected);
177                         } else if (source == record) {
178                                 Sequencer sequencer = Moosique.getSequencer();
179                                 boolean quantize = false;
180                                 if (record.getText() == "Record") {
181                                         /* Show a dialog with:
182                                                 "Track" combo box,
183                                                 "Channel" combo box (disabled?,
184                                                 "Quantize" checkbox and 
185                                                 "Start Recording" button.
186                                         */
187                                         record.setText("Stop");
188                                         mtv.enableKeyboardRecording();
189                                         sequencer.recordEnable(track, channel);
190                                         sequencer.startRecording();
191                                         Moosique.setEdited();
192                                 } else {
193                                         record.setText("Record");
194                                         mtv.disableKeyboardRecording();
195                                         sequencer.stopRecording();
196                                         sequencer.recordDisable(track);
197                                         Moosique.convertTrack(track, quantize);
198                                         mtv.placeNoteElements();
199                                 }
200                         }
201                 }
202         }
203 }