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 JPanel {
14 private JPanel trackPanel;
15 private JPanel titlePanel;
16 private int numberOfTracks;
17 private JScrollPane trackViews;
18 private JScrollPane trackTitles;
21 * Creates the main view
23 public MooView(Track[] tracks) {
24 trackViews = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
25 trackPanel = new JPanel(new GridLayout(1,3), true);
26 trackViews.setViewportView(trackPanel);
28 trackTitles = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_NEVER, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
29 titlePanel = new JPanel(new GridLayout(1,3),true);
30 trackTitles.setViewportView(titlePanel);
32 numberOfTracks = tracks.length;
34 setLayout(new BorderLayout());
35 add(trackTitles, BorderLayout.NORTH);
36 add(trackViews, BorderLayout.CENTER);
40 * Fills the track panel with track views for all tracks in the current sequence.
41 * @param tracks the tracks for which to add views
43 public void setTracks(Track[] tracks) {
44 if (tracks.length == 1) {
45 // If MIDI file is of type 0, creates a view for the track.
46 trackPanel.add(new MooTrackView(tracks[0]));
47 titlePanel.add(new MooTrackTitle(tracks[0]));
49 // Creates dialog for progress bar.
50 JDialog progressDialog = new JDialog(Moosique.getGUI(), "Visualizing...", false);
51 JProgressBar progressBar = new JProgressBar(0, tracks.length);
52 progressBar.setValue(0);
53 progressBar.setStringPainted(true);
54 progressDialog.getContentPane().add(progressBar);
55 progressDialog.pack();
56 progressDialog.setLocation((Toolkit.getDefaultToolkit().getScreenSize().width - progressDialog.getWidth()) / 2, (Toolkit.getDefaultToolkit().getScreenSize().height - progressDialog.getHeight()) / 2);
57 progressDialog.setVisible(true);
59 // Starts filling the track panel with track views, while updating the progress bar.
60 trackPanel.removeAll();
61 trackPanel.setLayout(new GridLayout(1,tracks.length));
62 for (int i = 1; i < tracks.length; i++) {
63 trackPanel.add(new MooTrackView(tracks[i]));
64 titlePanel.add(new MooTrackTitle(tracks[i]));
65 progressBar.setValue(i+1);
67 progressDialog.dispose();
69 trackPanel.validate();
70 trackViews.setViewportView(trackPanel);
74 * Calls on each track view to update itself.
76 public void update() {
77 Component[] comps = getComponents();
78 for (int i = 0; i < comps.length; i++) {
79 if(comps[i] instanceof MooTrackView) ((MooTrackView)comps[i]).update();
84 * Creates a view for the given track and adds it to the main view.
85 * @param track the track for which to add a view
86 * @param index the index at which to insert the view
88 public void addTrackView(Track track, int index) {
89 ((GridLayout)trackPanel.getLayout()).setColumns(++numberOfTracks);
90 ((GridLayout)titlePanel.getLayout()).setColumns(++numberOfTracks);
91 trackPanel.add(new MooTrackView(track), index);
92 titlePanel.add(new MooTrackTitle(track), index);
97 * Removes the view for the given track.
98 * @param index the index of the track for which to remove the view
100 public void removeTrackView(int index) {
102 ((GridLayout)trackPanel.getLayout()).setColumns(--numberOfTracks);