]> ruin.nu Git - moosique.git/blob - MooTrackView.java
setting mute and solo on channel instead..
[moosique.git] / MooTrackView.java
1 import javax.swing.*;
2 import java.awt.*;
3 import java.awt.event.*;
4 import javax.sound.midi.*;
5 import java.util.*;
6
7 /**
8  * Graphical representation of a MIDI track.
9  * 
10  * @author  Andersson , Andreen, Lanneskog, Pehrson
11  * @version 1
12  */
13
14 public class MooTrackView extends JPanel {
15
16         private Track track;
17         private MooTrackTitle title;
18         private Rectangle box;
19
20         private JPopupMenu popup;
21         private JMenuItem popupAdd;
22         private ArrayList rects;
23         protected static int viewLength = 0;
24         protected static int extraHeight = 0;
25         public static final int NOTE_HEIGHT = 10, NOTE_WIDTH = 40, VIEW_WIDTH = 200;
26
27         public MooTrackView (Track track, MooTrackTitle title) {
28                 super(true);
29                 this.track = track;
30                 this.title = title;
31
32                 // Configures panel
33                 setBackground(Color.white);
34                 setBorder(BorderFactory.createLineBorder(Color.black));
35                 setLayout(null);
36                 setPreferredSize(new Dimension(VIEW_WIDTH, 140 * NOTE_HEIGHT));
37
38                 // Creates temporary variables
39                 MidiEvent note;
40                 MooNoteElement elem;
41                 rects = new ArrayList(track.size() / 2);
42                 extraHeight = Toolkit.getDefaultToolkit().getScreenSize().height - 150;
43
44                 // Places note elements
45                 for (int i = 0; i < track.size(); i++) {
46                         note = track.get(i);
47                         if (note instanceof MooNote) {
48                                 // Adds the note element to the note area and moves it to the appropriate place.
49                                 MooNote mn = (MooNote)note;
50                                 elem = new MooNoteElement(this, mn);
51                                 add(elem);
52                                 layoutElement(elem, false);
53                         }
54                         setPreferredSize(new Dimension(VIEW_WIDTH, viewLength + extraHeight));
55
56                 }
57
58                 // Creates pop-up menu.
59                 popup = new JPopupMenu();
60                 PopupListener pList = new PopupListener();
61                 popupAdd = new JMenuItem("Add note...");
62                 popupAdd.addActionListener(pList);
63                 popup.add(popupAdd);
64
65                 // Adds listeners for popup menu and keyboard synthesizer.
66                 addMouseListener(new MAdapter());
67                 addKeyListener(new MooKeyboard());
68         }
69
70         public void layoutElement(MooNoteElement elem, boolean old){
71                 // If the element is currently in the view, removes its coordinates from the list.
72                 Rectangle r = new Rectangle();
73                 if (old){
74                         r = elem.getBounds(r);
75                         for (Iterator i = rects.iterator(); i.hasNext();){
76                                 Object ob = i.next();
77                                 if (r.equals(ob)){
78                                         rects.remove(ob);
79                                         break;
80                                 }
81                         }
82                 }
83
84                 // Creates temporary variables.
85                 int ticksPerSixteenth = Moosique.getSequence().getResolution() / 4;
86                 MooNote mn = elem.getNote();
87                 Insets insets = getInsets();
88                 int x, y, height;
89
90                 // Calculates coordinates.
91                 x = insets.left;
92                 y = insets.top + (int)(mn.getTick() / ticksPerSixteenth) * NOTE_HEIGHT;
93                 height = (mn.getDuration() / ticksPerSixteenth) * NOTE_HEIGHT;
94                 if (height == 0) height = NOTE_HEIGHT;
95                 r = new Rectangle(x, y, NOTE_WIDTH, height);
96
97                 // Places the element in the appropriate place.
98                 while(isOccupied(r)) r.translate(NOTE_WIDTH, 0);
99                 elem.setBounds(r);
100                 rects.add(r);
101                 if (viewLength < (y + height)){
102                         viewLength = y + height;
103                         if(old)setPreferredSize(new Dimension(VIEW_WIDTH, viewLength + extraHeight));
104                 }
105         }
106
107         public Track getTrack() {
108                 return track;
109         }
110
111
112         /** 
113          * Updates the track view.
114          */
115         public void update(long tickPosition) {
116                 repaint();
117         }
118
119         private boolean isOccupied(Rectangle r) {
120                 Iterator it = rects.iterator();
121                 while (it.hasNext()) {
122                         if(r.intersects((Rectangle)it.next())) return true;
123                 }
124                 return false;
125         }
126         
127         public void addNote(MooNote mn) {
128                 mn.addTo(track);
129                 MooNoteElement elem = new MooNoteElement(this, mn);
130                 add(elem);
131                 layoutElement(elem, false);
132                 setPreferredSize(new Dimension(VIEW_WIDTH, viewLength + extraHeight));
133                 repaint();
134         }
135
136         public void removeNote(MooNoteElement elem, MooNote mn) {
137                 mn.removeFrom(track);
138                 remove(elem);
139                 elem.getNote().removeFrom(track);
140                 repaint();
141         }
142
143         public void paintComponent(Graphics g) {
144                 super.paintComponent(g);
145                 Graphics2D g2 = (Graphics2D)g;
146                 for (int c = 0; c < viewLength || c < getHeight(); c += NOTE_HEIGHT) {
147                         for (int r = 0; r < (10 * NOTE_WIDTH); r += NOTE_WIDTH) {
148                                 box = new Rectangle(r, c, NOTE_WIDTH, NOTE_HEIGHT);
149                                 g2.setColor(Color.gray);
150                                 g2.draw(box);
151                         }
152                 }
153         }
154
155         class MAdapter extends MouseAdapter {
156                 public void mousePressed(MouseEvent e) {
157                         if (e.isPopupTrigger()) {
158                                 popup.show(e.getComponent(), e.getX(), e.getY());
159                         }
160                 }
161
162                 public void mouseEntered(MouseEvent e) {
163                         // Moosique.setActiveChannel(track.getChannel());
164                         grabFocus();
165                 }
166         }
167
168         class PopupListener implements ActionListener {
169                 public void actionPerformed(ActionEvent e) {
170                         Object source = e.getSource();
171                         if  (source == popupAdd) {
172                                 //addNote(new MooNote());
173                                 // int channel, int pitch, int velocity, long timestamp, int duration
174                         }
175                 }
176         }
177 }