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