]> ruin.nu Git - moosique.git/blob - MooView.java
no message
[moosique.git] / MooView.java
1 import javax.sound.midi.*;
2 import javax.swing.*;
3 import java.awt.*;
4 import java.awt.event.*;
5
6 /**
7  * The main view, the container of the track views.
8  * 
9  * @author  Einar Pehrson
10  */
11
12 public class MooView extends JScrollPane {
13
14         private JPanel trackPanel;
15         private int numberOfTracks;
16
17         /** 
18          * Creates the main view
19          */
20         public MooView(Track[] tracks) {
21                 super(VERTICAL_SCROLLBAR_ALWAYS, HORIZONTAL_SCROLLBAR_AS_NEEDED);
22                 numberOfTracks = tracks.length;
23                 trackPanel = new JPanel(new GridLayout(1,3), true);
24                 setTracks(tracks);
25                 setViewportView(trackPanel);
26         }
27
28         /** 
29          * Fills the track panel with track views for all tracks in the current sequence.
30          * @param tracks        the tracks for which to add views
31          */
32         public void setTracks(Track[] tracks) {
33                 trackPanel.removeAll();
34                 trackPanel.setLayout(new GridLayout(1,tracks.length));
35                 for (int i = 0; i < tracks.length; i++) {
36                         trackPanel.add(new MooTrackView(tracks[i]));
37                 }
38                 trackPanel.validate();
39         }
40
41         /** 
42          * Calls on each track view to update itself.
43          */
44         public void update() {
45                 Component[] comps = getComponents();
46                 for (int i = 0; i < comps.length; i++) {
47                         if(comps[i] instanceof MooTrackView) ((MooTrackView)comps[i]).update();
48                 }
49         }
50
51         /** 
52          * Creates a view for the given track and adds it to the main view.
53          * @param track         the track for which to add a view
54          * @param index         the index at which to insert the view
55          */
56         public void addTrackView(Track track, int index) {
57                 ((GridLayout)trackPanel.getLayout()).setColumns(++numberOfTracks);
58                 trackPanel.add(new MooTrackView(track), index);
59                 validate();
60         }
61
62         /** 
63          * Removes the view for the given track.
64          * @param index         the index of the track for which to remove the view
65          */
66         public void removeTrackView(int index) {
67                 remove(index);
68                 ((GridLayout)trackPanel.getLayout()).setColumns(--numberOfTracks);
69                 validate();
70         }
71 }