]> ruin.nu Git - moosique.git/blob - MooView.java
har fixat den där jävla jump-dialogen!!!
[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                                         System.out.println("Draws track " + i);
74                                         MooTrackTitle title = new MooTrackTitle(tracks[i]);
75                                         titlePanel.add(title);
76                                         trackPanel.add(new MooTrackView(tracks[i], title));
77                                         progressBar.setValue(i);
78                                 } else {
79                                         System.out.println("Doesn't draw track " + i);
80                                         gL.setColumns(--numberOfTracks);
81                                         trackPanel.setLayout(gL);
82                                 }
83                         }
84                         progressDialog.dispose();
85                 }
86 /*              JPanel filler = new JPanel();
87                 int totalViewLength = trackPanel.getComponents().length * MooTrackView.VIEW_WIDTH;
88                 if (totalViewLength < getWidth()) {
89                         System.out.println("Adding filler since width = " + getWidth() + " and tracks = " + totalViewLength);
90                         ((GridLayout)trackPanel.getLayout()).setColumns(numberOfTracks + 1);
91                         filler.setPreferredSize(new Dimension(getWidth() - totalViewLength, 140 * MooTrackView.NOTE_HEIGHT));
92                         trackPanel.add(filler);
93                         setPreferredSize(new Dimension(getWidth(), getHeight()));
94                 }
95 */
96                 trackPanel.validate();
97                 setViewportView(trackPanel);
98         }
99
100         /** 
101          * Calls on each track view to update itself.
102          */
103         public void update(long tickPosition) {
104                 getViewport().setViewPosition(new Point((int)getViewport().getViewPosition().getX(), (int)(tickPosition / (Moosique.getSequence().getResolution() / 4)) * MooTrackView.NOTE_HEIGHT));
105                 Component[] comps = getComponents();
106                 for (int i = 0; i < comps.length; i++) {
107                         if(comps[i] instanceof MooTrackView) ((MooTrackView)comps[i]).update(tickPosition);
108                 }
109         }
110
111         /** 
112          * Creates a view for the given track and adds it to the main view.
113          * @param track         the track for which to add a view
114          * @param index         the index at which to insert the view
115          */
116         public void addTrackView(Track track, int index) {
117                 ((GridLayout)trackPanel.getLayout()).setColumns(++numberOfTracks);
118                 ((GridLayout)titlePanel.getLayout()).setColumns(++numberOfTracks);
119                 MooTrackTitle title = new MooTrackTitle(track);
120                 titlePanel.add(title, index);
121                 trackPanel.add(new MooTrackView(track, title), index);
122                 validate();
123         }
124
125         /** 
126          * Removes the view for the given track.
127          * @param index         the index of the track for which to remove the view
128          */
129         public void removeTrackView(int index) {
130                 remove(index);
131                 ((GridLayout)trackPanel.getLayout()).setColumns(--numberOfTracks);
132                 validate();
133         }
134 }