]> ruin.nu Git - moosique.git/blob - MooTrackView.java
f8fe20c4b7400ade41c0b8ea990957d27290b59e
[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                 int extraHeight = Toolkit.getDefaultToolkit().getScreenSize().height - 150;
39                 int x, y, height;
40                 int beatsPerSixteenth = Moosique.getSequence().getResolution() / 4;
41                 rects = new ArrayList(track.size() / 2);
42
43                 // Places note elements
44                 Insets insets = getInsets();
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.
49                                 MooNote mn = (MooNote)note;
50                                 elem = new MooNoteElement(this, mn);
51                                 add(elem);
52
53                                 // Moves the note element to the appropriate place.
54                                 x = insets.left;
55                                 y = insets.top + (int)(mn.getTick() / beatsPerSixteenth) * NOTE_HEIGHT;
56                                 height = (mn.getDuration() / beatsPerSixteenth) * NOTE_HEIGHT;
57                                 if (height == 0) height = NOTE_HEIGHT;
58                                 Rectangle r = new Rectangle(x, y, NOTE_WIDTH, height);
59                                 while(isOccupied(r)) r.translate(NOTE_WIDTH, 0);
60                                 elem.setBounds(r);
61                                 rects.add(r);
62                                 if (viewLength < (y + height)) viewLength = y + height;
63                         }
64                         setPreferredSize(new Dimension(VIEW_WIDTH, viewLength + extraHeight));
65                 }
66                 validate();
67
68                 // Creates pop-up menu.
69                 popup = new JPopupMenu();
70                 menuItem = new JMenuItem("Add...");
71                 // menuItem.addActionListener();
72                 popup.add(menuItem);
73
74                 addMouseListener(new PopupListener());
75         }
76
77         public Track getTrack() {
78                 return track;
79         }
80
81
82         /** 
83          * Updates the track view.
84          */
85         public void update(long tickPosition) {
86                 repaint();
87         }
88
89         private boolean isOccupied(Rectangle r) {
90                 Iterator it = rects.iterator();
91                 while (it.hasNext()) {
92                         if(r.intersects((Rectangle)it.next())) return true;
93                 }
94                 return false;
95         }
96         
97         public void remove(MooNoteElement elem) {
98                 remove((Component)elem);
99                 elem.getNote().removeFrom(track);
100                 validate();
101                 repaint();
102         }
103
104         public void paintComponent(Graphics g) {
105                 super.paintComponent(g);
106                 Graphics2D g2 = (Graphics2D)g;
107                 for (int c = 0; c < viewLength || c < getHeight(); c += NOTE_HEIGHT) {
108                         for (int r = 0; r < (10 * NOTE_WIDTH); r += NOTE_WIDTH) {
109                                 box = new Rectangle(r, c, NOTE_WIDTH, NOTE_HEIGHT);
110                                 g2.setColor(Color.gray);
111                                 g2.draw(box);
112                         }
113                 }
114         }
115
116         class PopupListener extends MouseAdapter {
117                 public void mousePressed(MouseEvent e) {
118                         if (e.isPopupTrigger()) {
119                                 popup.show(e.getComponent(), e.getX(), e.getY());
120                         }
121                 }
122         }
123 }