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 JPanel {
15 private JPanel trackPanel;
16 private JPanel titlePanel;
17 private int numberOfTracks;
18 private JScrollPane trackViews;
21 * Creates the main view
23 public MooView(Track[] tracks) {
24 trackViews = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
25 trackPanel = new JPanel(new GridLayout(1,3), true);
26 trackViews.setViewportView(trackPanel);
28 titlePanel = new JPanel(new GridLayout(1,3),true);
30 JViewport columnHeader = new JViewport();
31 columnHeader.setView(titlePanel);
32 trackViews.setColumnHeaderView(columnHeader);
33 numberOfTracks = tracks.length;
35 setLayout(new BorderLayout());
36 add(trackViews, BorderLayout.CENTER);
38 trackViews.addPropertyChangeListener(new PropertyChangeListener(){
39 public void propertyChange(PropertyChangeEvent e){
40 System.out.println(e.getSource());
45 * Fills the track panel with track views for all tracks in the current sequence.
46 * @param tracks the tracks for which to add views
48 public void setTracks(Track[] tracks) {
49 if (tracks.length == 1) {
50 // If MIDI file is of type 0, creates a view for the track.
51 trackPanel.add(new MooTrackView(tracks[0]));
52 titlePanel.add(new MooTrackTitle(tracks[0]));
54 // Creates dialog for progress bar.
55 JDialog progressDialog = new JDialog(Moosique.getGUI(), "Visualizing...", false);
56 JProgressBar progressBar = new JProgressBar(0, tracks.length);
57 progressBar.setValue(0);
58 progressBar.setStringPainted(true);
59 progressDialog.getContentPane().add(progressBar);
60 progressDialog.pack();
61 progressDialog.setLocation((Toolkit.getDefaultToolkit().getScreenSize().width - progressDialog.getWidth()) / 2, (Toolkit.getDefaultToolkit().getScreenSize().height - progressDialog.getHeight()) / 2);
62 progressDialog.setVisible(true);
64 // Starts filling the track panel with track views, while updating the progress bar.
65 trackPanel.removeAll();
66 trackPanel.setLayout(new GridLayout(1,tracks.length));
67 for (int i = 1; i < tracks.length; i++) {
68 trackPanel.add(new MooTrackView(tracks[i]));
69 titlePanel.add(new MooTrackTitle(tracks[i]));
70 progressBar.setValue(i+1);
72 progressDialog.dispose();
74 trackPanel.validate();
75 trackViews.setViewportView(trackPanel);
79 * Calls on each track view to update itself.
81 public void update(long tickPosition) {
82 Component[] comps = getComponents();
83 for (int i = 0; i < comps.length; i++) {
84 if(comps[i] instanceof MooTrackView) ((MooTrackView)comps[i]).update(tickPosition);
89 * Creates a view for the given track and adds it to the main view.
90 * @param track the track for which to add a view
91 * @param index the index at which to insert the view
93 public void addTrackView(Track track, int index) {
94 ((GridLayout)trackPanel.getLayout()).setColumns(++numberOfTracks);
95 ((GridLayout)titlePanel.getLayout()).setColumns(++numberOfTracks);
96 trackPanel.add(new MooTrackView(track), index);
97 titlePanel.add(new MooTrackTitle(track), index);
102 * Removes the view for the given track.
103 * @param index the index of the track for which to remove the view
105 public void removeTrackView(int index) {
107 ((GridLayout)trackPanel.getLayout()).setColumns(--numberOfTracks);