]> ruin.nu Git - moosique.git/blob - MooTrackView.java
*** empty log message ***
[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 Rectangle box;
18         private JPopupMenu popup;
19         private JMenuItem menuItem;
20         private ArrayList rects;
21         protected static int viewLength = 0;
22         public static final int NOTE_HEIGHT = 10, NOTE_WIDTH = 40, VIEW_WIDTH = 200;
23
24         public MooTrackView (Track track) {
25                 super(true);
26                 this.track = track;
27
28                 // Configures panel
29                 setBackground(Color.white);
30                 setBorder(BorderFactory.createLineBorder(Color.black));
31                 setLayout(null);
32                 setPreferredSize(new Dimension(VIEW_WIDTH, 140 * NOTE_HEIGHT));
33
34                 // Creates temporary variables
35                 MidiEvent note;
36                 MooNoteElement elem;
37                 int extraHeight = Toolkit.getDefaultToolkit().getScreenSize().height - 150;
38                 int x, y, height;
39                 int beatsPerSixteenth = Moosique.getSequence().getResolution() / 4;
40                 rects = new ArrayList(track.size() / 2);
41
42                 // Places note elements
43                 Insets insets = getInsets();
44                 for (int i = 0; i < track.size(); i++) {
45                         note = track.get(i);
46                         if (note instanceof MooNote) {
47                                 // Adds the note element to the note area.
48                                 MooNote mn = (MooNote)note;
49                                 elem = new MooNoteElement(this, mn);
50                                 add(elem);
51
52                                 // Moves the note element to the appropriate place.
53                                 x = insets.left;
54                                 y = insets.top + (int)(mn.getTick() / beatsPerSixteenth) * NOTE_HEIGHT;
55                                 height = (mn.getDuration() / beatsPerSixteenth) * NOTE_HEIGHT;
56                                 if (height == 0) height = NOTE_HEIGHT;
57                                 Rectangle r = new Rectangle(x, y, NOTE_WIDTH, height);
58                                 while(isOccupied(r)) r.translate(NOTE_WIDTH, 0);
59                                 elem.setBounds(r);
60                                 rects.add(r);
61                                 if (viewLength < (y + height)) viewLength = y + height;
62                         }
63                         setPreferredSize(new Dimension(VIEW_WIDTH, viewLength + extraHeight));
64                 }
65                 validate();
66
67                 // Creates pop-up menu.
68                 popup = new JPopupMenu();
69                 menuItem = new JMenuItem("Add...");
70                 // menuItem.addActionListener();
71                 popup.add(menuItem);
72
73                 addMouseListener(new PopupListener());
74         }
75
76         public Track getTrack() {
77                 return track;
78         }
79
80         private boolean isOccupied(Rectangle r) {
81                 Iterator it = rects.iterator();
82                 while (it.hasNext()) {
83                         if(r.intersects((Rectangle)it.next())) return true;
84                 }
85                 return false;
86         }
87         
88         public void remove(MooNoteElement elem) {
89                 remove((Component)elem);
90                 validate();
91         }
92
93         public void paintComponent(Graphics g) {
94                 super.paintComponent(g);
95                 Graphics2D g2 = (Graphics2D)g;
96                 for (int c = 0; c < viewLength || c < getHeight(); c += NOTE_HEIGHT) {
97                         for (int r = 0; r < (10 * NOTE_WIDTH); r += NOTE_WIDTH) {
98                                 box = new Rectangle(r, c, NOTE_WIDTH, NOTE_HEIGHT);
99                                 g2.setColor(Color.gray);
100                                 g2.draw(box);
101                         }
102                 }
103         }
104
105         /** 
106          * Updates the track view.
107          */
108         public void update(long tickPosition) {
109                 repaint();
110         }
111
112         class PopupListener extends MouseAdapter {
113                 public void mousePressed(MouseEvent e) {
114                         if (e.isPopupTrigger()) {
115                                 popup.show(e.getComponent(), e.getX(), e.getY());
116                         }
117                 }
118         }
119 }