1 import javax.sound.midi.*;
4 import java.awt.event.*;
7 * The main view, the container of the track views.
9 * @author Einar Pehrson
12 public class MooView extends JScrollPane {
14 private JPanel trackPanel;
15 private int numberOfTracks;
18 * Creates the main view
20 public MooView(Track[] tracks) {
21 super(VERTICAL_SCROLLBAR_ALWAYS, HORIZONTAL_SCROLLBAR_AS_NEEDED);
22 numberOfTracks = tracks.length;
23 trackPanel = new JPanel(new GridLayout(1,3), true);
25 setViewportView(trackPanel);
29 * Fills the track panel with track views for all tracks in the current sequence.
30 * @param tracks the tracks for which to add views
32 public void setTracks(Track[] tracks) {
33 if (tracks.length == 1) {
34 // If MIDI file is of type 0, creates a view for the track.
35 trackPanel.add(new MooTrackView(tracks[0]));
37 // Creates dialog for progress bar.
38 JDialog progressDialog = new JDialog(Moosique.getGUI(), "Visualizing MIDI file...", false);
39 JProgressBar progressBar = new JProgressBar(0, tracks.length);
40 progressBar.setValue(0);
41 progressBar.setStringPainted(true);
42 progressDialog.getContentPane().add(progressBar);
43 progressDialog.pack();
44 progressDialog.setLocation((Toolkit.getDefaultToolkit().getScreenSize().width - progressDialog.getWidth()) / 2, (Toolkit.getDefaultToolkit().getScreenSize().height - progressDialog.getHeight()) / 2);
45 progressDialog.setVisible(true);
47 // Starts filling the track panel with track views, while updating the progress bar.
48 trackPanel.removeAll();
49 trackPanel.setLayout(new GridLayout(1,tracks.length));
50 for (int i = 1; i < tracks.length; i++) {
51 trackPanel.add(new MooTrackView(tracks[i]));
52 progressBar.setValue(i+1);
54 progressDialog.dispose();
56 trackPanel.validate();
60 * Calls on each track view to update itself.
62 public void update() {
63 Component[] comps = getComponents();
64 for (int i = 0; i < comps.length; i++) {
65 if(comps[i] instanceof MooTrackView) ((MooTrackView)comps[i]).update();
70 * Creates a view for the given track and adds it to the main view.
71 * @param track the track for which to add a view
72 * @param index the index at which to insert the view
74 public void addTrackView(Track track, int index) {
75 ((GridLayout)trackPanel.getLayout()).setColumns(++numberOfTracks);
76 trackPanel.add(new MooTrackView(track), index);
81 * Removes the view for the given track.
82 * @param index the index of the track for which to remove the view
84 public void removeTrackView(int index) {
86 ((GridLayout)trackPanel.getLayout()).setColumns(--numberOfTracks);