]> 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
26         private String trackName = "";
27         private int channel = 0;
28
29         /** 
30          * Creates the title bar for an empty track, and therefore an initial channel is required.
31          * @param aTrack the track that this tracktitle is operating on.
32          * @param chan  the initial channel
33          */
34         public MooTrackTitle (Track aTrack, int chan) {
35                 this(aTrack);
36                 channel = chan;
37         }
38
39         /** 
40          * Creates the title bar.
41          * @param aTrack the track that this track title is operating on.
42          */
43         public MooTrackTitle (Track aTrack) {
44                 setDoubleBuffered(true);
45                 track = aTrack;
46
47                 // Finds track name, program change and channel.
48                 MidiMessage msg;
49                 int status;
50                 for (int i = 0; i < track.size(); i++) {
51                         msg = track.get(i).getMessage();
52                         status = msg.getStatus();
53                         if (status == MetaMessage.META) {
54                                 if (((MetaMessage)msg).getType() == 3) {
55                                         trackNameMessage = (MetaMessage)msg;
56                                         trackName = new String(trackNameMessage.getData());
57                                 }
58                         } else if (status >= 192 && status < 208) {
59                                 programChangeMessage = (ShortMessage)msg;
60                                 channel = programChangeMessage.getChannel();
61                         }
62                 }
63
64                 // Creates and places components.
65                 setLayout(new GridLayout(4,1));
66                 setBorder(BorderFactory.createLineBorder(Color.black));
67                 TitleListener tl = new TitleListener();
68
69                 setPreferredSize(new Dimension(MooTrackView.VIEW_WIDTH,70));
70                 title = new JTextField(trackName);
71                 title.setFont(Moosique.getGUI().FONT);
72                 title.addFocusListener(new TitleFocusListener());
73                 add(title);
74
75                 int type;
76                 if (channel == 9) type = MooInstrumentList.DRUMS;
77                 else type = MooInstrumentList.INSTRUMENTS;
78                 instruments = new MooInstrumentList(channel, type, programChangeMessage);
79                 add(instruments);
80
81                 channelBox = new JComboBox();
82                 channelBox.setFont(Moosique.getGUI().FONT);
83                 for (int i = 1; i <= 16; i++)
84                         channelBox.addItem(new Integer(i));
85
86                 channelBox.setSelectedIndex(channel);
87
88                 channelBox.addActionListener(tl);
89                 add(channelBox);
90
91                 JPanel checkboxes = new JPanel();
92                 checkboxes.setLayout(new GridLayout(1,3));
93
94                 mute = new JCheckBox("Mute");
95                 mute.setSelected(false);
96                 Moosique.setTrackMute(track, false);
97                 mute.setFont(Moosique.getGUI().FONT);
98                 mute.addActionListener(tl);
99                 checkboxes.add(mute);
100
101                 solo = new JCheckBox("Solo");
102                 solo.setSelected(false);
103                 Moosique.setTrackSolo(track, false);
104                 solo.setFont(Moosique.getGUI().FONT);
105                 solo.addActionListener(tl);
106                 checkboxes.add(solo);
107                 
108                 add(checkboxes);
109         }
110         
111         /** 
112          * Returns the channel of the track that the view is visualising.
113          * @return the chanel of the visualised track
114          */
115         public int getChannel() {
116                 return channel;
117         }
118
119         /** 
120          * Sets the track view this title should update after recording.
121          * @param the track view
122          */
123         public void setTrackView(MooTrackView tv) {
124                 mtv = tv;
125         }
126
127         /**
128          * When the title field loses focus, updates the corresponding MidiEvent.
129          */
130         class TitleFocusListener extends FocusAdapter {
131                 public void focusLost(FocusEvent e) {
132                         try {
133                                 trackNameMessage.setMessage(3, title.getText().getBytes(), title.getText().length());
134                         } catch (InvalidMidiDataException ex) {}
135                 }
136         }
137
138         /**
139          * Takes the appropriate action when a user selects an item on the popup menu.
140          */
141         class TitleListener implements ActionListener {
142                 public void actionPerformed(ActionEvent e) {
143                         Object source = e.getSource();
144                         if (source == channelBox) {
145                                 channel = channelBox.getSelectedIndex();
146                                 MidiEvent me;
147                                 MooNote mn;
148                                 instruments.setChannel(channel);
149                                 // Prompt the user before rechannelling???
150                                 for (int j = 0; j < track.size(); j++) {
151                                         me = track.get(j);
152                                         if (me instanceof MooNote){
153                                                 mn = (MooNote)me;
154                                                 mn.setChannel(channel);
155                                         }
156                                 }
157                         } else if (source == solo) {
158                                 boolean selected = mute.isSelected();
159                                 if (selected){
160                                         solo.setSelected(false);
161                                         Moosique.setTrackSolo(track, false);
162                                 }
163                                 Moosique.setTrackMute(track, selected);
164                         } else if (source == mute) {
165                                 boolean selected = solo.isSelected();
166                                 if (selected){
167                                         mute.setSelected(false);
168                                         Moosique.setTrackMute(track, false);
169                                 }
170                                 Moosique.setTrackSolo(track, selected);
171                         }
172                 }
173         }
174 }