1 import javax.sound.midi.*;
4 import java.awt.event.*;
8 * The main view, the container of the track views.
10 * @author Einar Pehrson
13 public class MooView extends JScrollPane {
15 private JPanel trackPanel;
16 private JPanel titlePanel;
17 private MooViewCounter viewCounter;
18 private int numberOfTracks;
21 * Creates the main view
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);
29 titlePanel = new JPanel(new GridLayout(1,3),true);
30 JViewport columnHeader = new JViewport();
31 columnHeader.setView(titlePanel);
32 setColumnHeaderView(columnHeader);
34 viewCounter = new MooViewCounter(null);
35 JViewport rowHeader = new JViewport();
36 rowHeader.setView(viewCounter);
37 setRowHeaderView(rowHeader);
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
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));
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);
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);
79 System.out.println("Doesn't draw track " + i);
80 gL.setColumns(--numberOfTracks);
81 trackPanel.setLayout(gL);
84 progressDialog.dispose();
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()));
96 trackPanel.validate();
97 setViewportView(trackPanel);
101 * Calls on each track view to update itself.
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);
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
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);
126 * Removes the view for the given track.
127 * @param index the index of the track for which to remove the view
129 public void removeTrackView(int index) {
131 ((GridLayout)trackPanel.getLayout()).setColumns(--numberOfTracks);