]> ruin.nu Git - moosique.git/blob - MooView.java
minor changes.
[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 JPanel {
13
14         private JPanel trackPanel;
15         private JPanel titlePanel;
16         private int numberOfTracks;
17         private JScrollPane trackViews;
18         private JScrollPane trackTitles;
19
20         /** 
21          * Creates the main view
22          */
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);
27
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);
31
32                 trackViews.setColumnHeaderView(trackTitles);
33                 numberOfTracks = tracks.length;
34                 setTracks(tracks);
35                 setLayout(new BorderLayout());
36                 add(trackTitles, BorderLayout.NORTH);
37                 add(trackViews, BorderLayout.CENTER);
38         }
39
40         /** 
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
43          */
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]));
49                 } else {
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);
59         
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);
67                         }
68                         progressDialog.dispose();
69                 }
70                 trackPanel.validate();
71                 trackViews.setViewportView(trackPanel);
72         }
73
74         /** 
75          * Calls on each track view to update itself.
76          */
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();
81                 }
82         }
83
84         /** 
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
88          */
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);
94                 validate();
95         }
96
97         /** 
98          * Removes the view for the given track.
99          * @param index         the index of the track for which to remove the view
100          */
101         public void removeTrackView(int index) {
102                 remove(index);
103                 ((GridLayout)trackPanel.getLayout()).setColumns(--numberOfTracks);
104                 validate();
105         }
106 }