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 int numberOfTracks;
20 * Creates the main view
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);
27 titlePanel = new JPanel(new GridLayout(1,3),true);
29 JViewport columnHeader = new JViewport();
30 columnHeader.setView(titlePanel);
31 setColumnHeaderView(columnHeader);
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
39 public void setTracks(Track[] tracks) {
40 setDoubleBuffered(true);
41 numberOfTracks = tracks.length;
42 trackPanel.removeAll();
43 titlePanel.removeAll();
44 if (numberOfTracks == 1) {
45 // If MIDI file is of type 0, creates a view for the track.
46 trackPanel.setLayout(new FlowLayout());
47 trackPanel.add(new MooTrackView(tracks[0]));
48 titlePanel.add(new MooTrackTitle(tracks[0],0));
50 // Creates dialog for progress bar.
51 JDialog progressDialog = new JDialog(Moosique.getGUI(), "Visualizing...", false);
52 JProgressBar progressBar = new JProgressBar(0, tracks.length);
53 progressBar.setValue(0);
54 progressBar.setStringPainted(true);
55 progressDialog.getContentPane().add(progressBar);
56 progressDialog.pack();
57 progressDialog.setLocation((Toolkit.getDefaultToolkit().getScreenSize().width - progressDialog.getWidth()) / 2, (Toolkit.getDefaultToolkit().getScreenSize().height - progressDialog.getHeight()) / 2);
58 progressDialog.setVisible(true);
60 // Starts filling the track panel with track views, while updating the progress bar.
61 trackPanel.setLayout(new GridLayout(1,numberOfTracks));
62 for (int i = 1; i < tracks.length; i++) {
63 trackPanel.add(new MooTrackView(tracks[i]));
64 titlePanel.add(new MooTrackTitle(tracks[i],i));
65 progressBar.setValue(i);
67 progressDialog.dispose();
69 /* JPanel filler = new JPanel();
70 int totalViewLength = trackPanel.getComponents().length * MooTrackView.VIEW_WIDTH;
71 if (totalViewLength < getWidth()) {
72 System.out.println("Adding filler since width = " + getWidth() + " and tracks = " + totalViewLength);
73 ((GridLayout)trackPanel.getLayout()).setColumns(numberOfTracks + 1);
74 filler.setPreferredSize(new Dimension(getWidth() - totalViewLength, 140 * MooTrackView.NOTE_HEIGHT));
75 trackPanel.add(filler);
76 setPreferredSize(new Dimension(getWidth(), getHeight()));
79 trackPanel.validate();
80 setViewportView(trackPanel);
84 * Calls on each track view to update itself.
86 public void update(long tickPosition) {
87 getViewport().setViewPosition(new Point((int)getViewport().getViewPosition().getX(), (int)(tickPosition / (Moosique.getSequence().getResolution() / 4)) * MooTrackView.NOTE_HEIGHT));
88 Component[] comps = getComponents();
89 for (int i = 0; i < comps.length; i++) {
90 if(comps[i] instanceof MooTrackView) ((MooTrackView)comps[i]).update(tickPosition);
95 * Creates a view for the given track and adds it to the main view.
96 * @param track the track for which to add a view
97 * @param index the index at which to insert the view
99 public void addTrackView(Track track, int index) {
100 ((GridLayout)trackPanel.getLayout()).setColumns(++numberOfTracks);
101 ((GridLayout)titlePanel.getLayout()).setColumns(++numberOfTracks);
102 trackPanel.add(new MooTrackView(track), index);
103 titlePanel.add(new MooTrackTitle(track,index), index);
108 * Removes the view for the given track.
109 * @param index the index of the track for which to remove the view
111 public void removeTrackView(int index) {
113 ((GridLayout)trackPanel.getLayout()).setColumns(--numberOfTracks);