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 trackViews.setColumnHeaderView(trackTitles);
33 numberOfTracks = tracks.length;
35 setLayout(new BorderLayout());
36 add(trackTitles, BorderLayout.NORTH);
37 add(trackViews, BorderLayout.CENTER);
41 * Fills the track panel with track views for all tracks in the current sequence.
42 * @param tracks the tracks for which to add views
44 public void setTracks(Track[] tracks) {
45 if (tracks.length == 1) {
46 // If MIDI file is of type 0, creates a view for the track.
47 trackPanel.add(new MooTrackView(tracks[0]));
48 titlePanel.add(new MooTrackTitle(tracks[0]));
50 // Creates dialog for progress bar.
51 JDialog progressDialog = new JDialog(Moosique.getGUI(), "Visualizing...", false);
52 JProgressBar progressBar = new JProgressBar(0, tracks.length);
53 progressBar.setValue(0);
54 progressBar.setStringPainted(true);
55 progressDialog.getContentPane().add(progressBar);
56 progressDialog.pack();
57 progressDialog.setLocation((Toolkit.getDefaultToolkit().getScreenSize().width - progressDialog.getWidth()) / 2, (Toolkit.getDefaultToolkit().getScreenSize().height - progressDialog.getHeight()) / 2);
58 progressDialog.setVisible(true);
60 // Starts filling the track panel with track views, while updating the progress bar.
61 trackPanel.removeAll();
62 trackPanel.setLayout(new GridLayout(1,tracks.length));
63 for (int i = 1; i < tracks.length; i++) {
64 trackPanel.add(new MooTrackView(tracks[i]));
65 titlePanel.add(new MooTrackTitle(tracks[i]));
66 progressBar.setValue(i+1);
68 progressDialog.dispose();
70 trackPanel.validate();
71 trackViews.setViewportView(trackPanel);
75 * Calls on each track view to update itself.
77 public void update() {
78 Component[] comps = getComponents();
79 for (int i = 0; i < comps.length; i++) {
80 if(comps[i] instanceof MooTrackView) ((MooTrackView)comps[i]).update();
85 * Creates a view for the given track and adds it to the main view.
86 * @param track the track for which to add a view
87 * @param index the index at which to insert the view
89 public void addTrackView(Track track, int index) {
90 ((GridLayout)trackPanel.getLayout()).setColumns(++numberOfTracks);
91 ((GridLayout)titlePanel.getLayout()).setColumns(++numberOfTracks);
92 trackPanel.add(new MooTrackView(track), index);
93 titlePanel.add(new MooTrackTitle(track), index);
98 * Removes the view for the given track.
99 * @param index the index of the track for which to remove the view
101 public void removeTrackView(int index) {
103 ((GridLayout)trackPanel.getLayout()).setColumns(--numberOfTracks);