]> ruin.nu Git - moosique.git/blob - MooView.java
moving TrackTitle outside the scroll..
[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 MIDI file...", 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         }
71
72         /** 
73          * Calls on each track view to update itself.
74          */
75         public void update() {
76                 Component[] comps = getComponents();
77                 for (int i = 0; i < comps.length; i++) {
78                         if(comps[i] instanceof MooTrackView) ((MooTrackView)comps[i]).update();
79                 }
80         }
81
82         /** 
83          * Creates a view for the given track and adds it to the main view.
84          * @param track         the track for which to add a view
85          * @param index         the index at which to insert the view
86          */
87         public void addTrackView(Track track, int index) {
88                 ((GridLayout)trackPanel.getLayout()).setColumns(++numberOfTracks);
89                 ((GridLayout)titlePanel.getLayout()).setColumns(++numberOfTracks);
90                 trackPanel.add(new MooTrackView(track), index);
91                 titlePanel.add(new MooTrackTitle(track), index);
92                 validate();
93         }
94
95         /** 
96          * Removes the view for the given track.
97          * @param index         the index of the track for which to remove the view
98          */
99         public void removeTrackView(int index) {
100                 remove(index);
101                 ((GridLayout)trackPanel.getLayout()).setColumns(--numberOfTracks);
102                 validate();
103         }
104 }