]> ruin.nu Git - moosique.git/blob - MooView.java
even better
[moosique.git] / MooView.java
1 import javax.sound.midi.*;
2 import javax.swing.*;
3 import java.awt.*;
4 import java.awt.event.*;
5 import java.beans.*;
6
7 /**
8  * The main view, the container of the track views.
9  * 
10  * @author  Einar Pehrson
11  */
12
13 public class MooView extends JScrollPane {
14
15         private JPanel trackPanel;
16         private JPanel titlePanel;
17         private int numberOfTracks;
18
19         /** 
20          * Creates the main view
21          */
22         public MooView(Track[] tracks) {
23                 super(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
24                 trackPanel = new JPanel(new GridLayout(1,3), true);
25                 setViewportView(trackPanel);
26
27                 titlePanel = new JPanel(new GridLayout(1,3),true);
28
29                 JViewport columnHeader = new JViewport();
30                 columnHeader.setView(titlePanel);
31                 setColumnHeaderView(columnHeader);
32                 numberOfTracks = tracks.length;
33                 setTracks(tracks);
34         }
35
36         /** 
37          * Fills the track panel with track views for all tracks in the current sequence.
38          * @param tracks        the tracks for which to add views
39          */
40         public void setTracks(Track[] tracks) {
41                 if (tracks.length == 1) {
42                         // If MIDI file is of type 0, creates a view for the track.
43                         trackPanel.add(new MooTrackView(tracks[0]));
44                         titlePanel.add(new MooTrackTitle(tracks[0]));
45                 } else {
46                         // Creates dialog for progress bar.
47                         JDialog progressDialog = new JDialog(Moosique.getGUI(), "Visualizing...", false);
48                         JProgressBar progressBar = new JProgressBar(0, tracks.length);
49                         progressBar.setValue(0);
50                         progressBar.setStringPainted(true);
51                         progressDialog.getContentPane().add(progressBar);
52                         progressDialog.pack();
53                         progressDialog.setLocation((Toolkit.getDefaultToolkit().getScreenSize().width - progressDialog.getWidth()) / 2, (Toolkit.getDefaultToolkit().getScreenSize().height - progressDialog.getHeight()) / 2);
54                         progressDialog.setVisible(true);
55         
56                         // Starts filling the track panel with track views, while updating the progress bar.
57                         trackPanel.removeAll();
58                         trackPanel.setLayout(new GridLayout(1,tracks.length));
59                         for (int i = 1; i < tracks.length; i++) {
60                                 trackPanel.add(new MooTrackView(tracks[i]));
61                                 titlePanel.add(new MooTrackTitle(tracks[i]));
62                                 progressBar.setValue(i+1);
63                         }
64                         progressDialog.dispose();
65                 }
66                 trackPanel.validate();
67                 setViewportView(trackPanel);
68         }
69
70         /** 
71          * Calls on each track view to update itself.
72          */
73         public void update(long tickPosition) {
74                 Component[] comps = getComponents();
75                 for (int i = 0; i < comps.length; i++) {
76                         if(comps[i] instanceof MooTrackView) ((MooTrackView)comps[i]).update(tickPosition);
77                 }
78         }
79
80         /** 
81          * Creates a view for the given track and adds it to the main view.
82          * @param track         the track for which to add a view
83          * @param index         the index at which to insert the view
84          */
85         public void addTrackView(Track track, int index) {
86                 ((GridLayout)trackPanel.getLayout()).setColumns(++numberOfTracks);
87                 ((GridLayout)titlePanel.getLayout()).setColumns(++numberOfTracks);
88                 trackPanel.add(new MooTrackView(track), index);
89                 titlePanel.add(new MooTrackTitle(track), index);
90                 validate();
91         }
92
93         /** 
94          * Removes the view for the given track.
95          * @param index         the index of the track for which to remove the view
96          */
97         public void removeTrackView(int index) {
98                 remove(index);
99                 ((GridLayout)trackPanel.getLayout()).setColumns(--numberOfTracks);
100                 validate();
101         }
102 }