]> ruin.nu Git - moosique.git/blob - MooView.java
solo mute and stuff
[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                 setTracks(tracks);
33         }
34
35         /** 
36          * Fills the track panel with track views for all tracks in the current sequence.
37          * @param tracks        the tracks for which to add views
38          */
39         public void setTracks(Track[] tracks) {
40                 numberOfTracks = tracks.length;
41                 trackPanel.removeAll();
42                 titlePanel.removeAll();
43                 if (numberOfTracks == 1) {
44                         // If MIDI file is of type 0, creates a view for the track.
45                         trackPanel.setLayout(new FlowLayout());
46                         trackPanel.add(new MooTrackView(tracks[0]));
47                         titlePanel.add(new MooTrackTitle(tracks[0],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.setLayout(new GridLayout(1,numberOfTracks));
61                         for (int i = 1; i < tracks.length; i++) {
62                                 trackPanel.add(new MooTrackView(tracks[i]));
63                                 titlePanel.add(new MooTrackTitle(tracks[i],i));
64                                 progressBar.setValue(i+1);
65                         }
66                         progressDialog.dispose();
67                 }
68 /*              JPanel filler = new JPanel();
69                 int totalViewLength = trackPanel.getComponents().length * MooTrackView.VIEW_WIDTH;
70                 if (totalViewLength < getWidth()) {
71                         System.out.println("Adding filler since width = " + getWidth() + " and tracks = " + totalViewLength);
72                         ((GridLayout)trackPanel.getLayout()).setColumns(numberOfTracks + 1);
73                         filler.setPreferredSize(new Dimension(getWidth() - totalViewLength, 140 * MooTrackView.NOTE_HEIGHT));
74                         trackPanel.add(filler);
75                         setPreferredSize(new Dimension(getWidth(), getHeight()));
76                 }
77 */
78                 trackPanel.validate();
79                 setViewportView(trackPanel);
80         }
81
82         /** 
83          * Calls on each track view to update itself.
84          */
85         public void update(long tickPosition) {
86                 getViewport().setViewPosition(new Point((int)getViewport().getViewPosition().getX(), (int)(tickPosition / 24) * MooTrackView.NOTE_HEIGHT));
87                 Component[] comps = getComponents();
88                 for (int i = 0; i < comps.length; i++) {
89                         if(comps[i] instanceof MooTrackView) ((MooTrackView)comps[i]).update(tickPosition);
90                 }
91         }
92
93         /** 
94          * Creates a view for the given track and adds it to the main view.
95          * @param track         the track for which to add a view
96          * @param index         the index at which to insert the view
97          */
98         public void addTrackView(Track track, int index) {
99                 ((GridLayout)trackPanel.getLayout()).setColumns(++numberOfTracks);
100                 ((GridLayout)titlePanel.getLayout()).setColumns(++numberOfTracks);
101                 trackPanel.add(new MooTrackView(track), index);
102                 titlePanel.add(new MooTrackTitle(track,index), index);
103                 validate();
104         }
105
106         /** 
107          * Removes the view for the given track.
108          * @param index         the index of the track for which to remove the view
109          */
110         public void removeTrackView(int index) {
111                 remove(index);
112                 ((GridLayout)trackPanel.getLayout()).setColumns(--numberOfTracks);
113                 validate();
114         }
115 }