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