]> ruin.nu Git - moosique.git/blob - MooView.java
no message
[moosique.git] / MooView.java
1 import javax.sound.midi.*;
2 import javax.swing.*;
3 import java.awt.*;
4 import java.awt.event.*;
5
6 /**
7  * The main view, the container of the track views.
8  * 
9  * @author  Einar Pehrson
10  */
11
12 public class MooView extends JScrollPane {
13
14         private JPanel trackPanel;
15         private int numberOfTracks;
16
17         /** 
18          * Creates the main view
19          */
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);
24                 setTracks(tracks);
25                 setViewportView(trackPanel);
26         }
27
28         /** 
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
31          */
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]));
36                 } else {
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);
46         
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);
53                         }
54                         progressDialog.dispose();
55                 }
56                 trackPanel.validate();
57         }
58
59         /** 
60          * Calls on each track view to update itself.
61          */
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();
66                 }
67         }
68
69         /** 
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
73          */
74         public void addTrackView(Track track, int index) {
75                 ((GridLayout)trackPanel.getLayout()).setColumns(++numberOfTracks);
76                 trackPanel.add(new MooTrackView(track), index);
77                 validate();
78         }
79
80         /** 
81          * Removes the view for the given track.
82          * @param index         the index of the track for which to remove the view
83          */
84         public void removeTrackView(int index) {
85                 remove(index);
86                 ((GridLayout)trackPanel.getLayout()).setColumns(--numberOfTracks);
87                 validate();
88         }
89 }