]> ruin.nu Git - moosique.git/blob - MooView.java
FIXED THE MULTICOLUMN BUG!!!
[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                 numberOfTracks = tracks.length;
33                 setTracks(tracks);
34                 setLayout(new BorderLayout());
35                 add(trackTitles, BorderLayout.NORTH);
36                 add(trackViews, BorderLayout.CENTER);
37         }
38
39         /** 
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
42          */
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]));
48                 } else {
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);
58         
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);
66                         }
67                         progressDialog.dispose();
68                 }
69                 trackPanel.validate();
70                 trackViews.setViewportView(trackPanel);
71         }
72
73         /** 
74          * Calls on each track view to update itself.
75          */
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();
80                 }
81         }
82
83         /** 
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
87          */
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);
93                 validate();
94         }
95
96         /** 
97          * Removes the view for the given track.
98          * @param index         the index of the track for which to remove the view
99          */
100         public void removeTrackView(int index) {
101                 remove(index);
102                 ((GridLayout)trackPanel.getLayout()).setColumns(--numberOfTracks);
103                 validate();
104         }
105 }