]> ruin.nu Git - moosique.git/blob - MooView.java
c16699cbdbf3e473194e97ddece5ef9fd0286d41
[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 MooViewCounter viewCounter;
18         private int numberOfTracks;
19
20         /** 
21          * Creates the main view
22          */
23         public MooView(Track[] tracks) {
24                 super(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
25                 trackPanel = new JPanel(new GridLayout(1,3), true);
26                 setViewportView(trackPanel);
27                 getViewport().setScrollMode(JViewport.BACKINGSTORE_SCROLL_MODE);
28
29                 titlePanel = new JPanel(new GridLayout(1,3),true);
30                 JViewport columnHeader = new JViewport();
31                 columnHeader.setView(titlePanel);
32                 setColumnHeaderView(columnHeader);
33
34                 viewCounter = new MooViewCounter(null);
35                 JViewport rowHeader = new JViewport();
36                 rowHeader.setView(viewCounter);
37                 setRowHeaderView(rowHeader);
38
39                 setTracks(tracks);
40         }
41
42         /** 
43          * Fills the track panel with track views for all tracks in the current sequence.
44          * @param tracks        the tracks for which to add views
45          */
46         public void setTracks(Track[] tracks) {
47                 setDoubleBuffered(true);
48                 numberOfTracks = tracks.length;
49                 trackPanel.removeAll();
50                 titlePanel.removeAll();
51                 if (numberOfTracks == 1) {
52                         // If MIDI file is of type 0, creates a view for the track.
53                         trackPanel.setLayout(new FlowLayout());
54                         MooTrackTitle title = new MooTrackTitle(tracks[0]);
55                         titlePanel.add(title);
56                         trackPanel.add(new MooTrackView(tracks[0], title));
57                 } else {
58                         // Creates dialog for progress bar.
59                         JDialog progressDialog = new JDialog(Moosique.getGUI(), "Visualizing...", false);
60                         JProgressBar progressBar = new JProgressBar(0, tracks.length);
61                         progressBar.setValue(0);
62                         progressBar.setStringPainted(true);
63                         progressDialog.getContentPane().add(progressBar);
64                         progressDialog.pack();
65                         progressDialog.setLocation((Toolkit.getDefaultToolkit().getScreenSize().width - progressDialog.getWidth()) / 2, (Toolkit.getDefaultToolkit().getScreenSize().height - progressDialog.getHeight()) / 2);
66                         progressDialog.setVisible(true);
67         
68                         // Starts filling the track panel with track views, while updating the progress bar.
69                         GridLayout gL = new GridLayout(1,numberOfTracks);
70                         trackPanel.setLayout(gL);
71                         for (int i = 1; i < tracks.length; i++) {
72                                 if (Moosique.shouldBeDrawn(tracks[i])) {
73                                         MooTrackTitle title = new MooTrackTitle(tracks[i]);
74                                         titlePanel.add(title);
75                                         trackPanel.add(new MooTrackView(tracks[i], title));
76                                         progressBar.setValue(i);
77                                 } else {
78                                         gL.setColumns(--numberOfTracks);
79                                         trackPanel.setLayout(gL);
80                                 }
81                         }
82                         progressDialog.dispose();
83                 }
84 /*              JPanel filler = new JPanel();
85                 int totalViewLength = trackPanel.getComponents().length * MooTrackView.VIEW_WIDTH;
86                 if (totalViewLength < getWidth()) {
87                         System.out.println("Adding filler since width = " + getWidth() + " and tracks = " + totalViewLength);
88                         ((GridLayout)trackPanel.getLayout()).setColumns(numberOfTracks + 1);
89                         filler.setPreferredSize(new Dimension(getWidth() - totalViewLength, 140 * MooTrackView.NOTE_HEIGHT));
90                         trackPanel.add(filler);
91                         setPreferredSize(new Dimension(getWidth(), getHeight()));
92                 }
93 */
94                 trackPanel.validate();
95                 setViewportView(trackPanel);
96         }
97
98         /** 
99          * Calls on each track view to update itself.
100          */
101         public void update(long tickPosition) {
102                 getViewport().setViewPosition(new Point((int)getViewport().getViewPosition().getX(), (int)(tickPosition / (Moosique.getSequence().getResolution() / 4)) * MooTrackView.NOTE_HEIGHT));
103                 Component[] comps = getComponents();
104                 for (int i = 0; i < comps.length; i++) {
105                         if(comps[i] instanceof MooTrackView) ((MooTrackView)comps[i]).update(tickPosition);
106                 }
107         }
108
109         /** 
110          * Creates a view for the given track and adds it to the main view.
111          * @param track         the track for which to add a view
112          * @param index         the index at which to insert the view
113          */
114         public void addTrackView(Track track, int index) {
115                 ((GridLayout)trackPanel.getLayout()).setColumns(++numberOfTracks);
116                 ((GridLayout)titlePanel.getLayout()).setColumns(++numberOfTracks);
117                 MooTrackTitle title = new MooTrackTitle(track);
118                 titlePanel.add(title, index);
119                 trackPanel.add(new MooTrackView(track, title), index);
120                 validate();
121         }
122
123         /** 
124          * Removes the view for the given track.
125          * @param index         the index of the track for which to remove the view
126          */
127         public void removeTrackView(int index) {
128                 remove(index);
129                 ((GridLayout)trackPanel.getLayout()).setColumns(--numberOfTracks);
130                 validate();
131         }
132 }