]> 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 MooTrackTitle;
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                 validate();
58
59                 // Creates pop-up menu.
60                 popup = new JPopupMenu();
61                 PopupListener pList = new PopupListener();
62                 popupAdd = new JMenuItem("Add note...");
63                 popupAdd.addActionListener(pList);
64                 popup.add(popupAdd);
65
66                 // Adds listeners for popup menu and keyboard synthesizer.
67                 addMouseListener(new MAdapter());
68                 addKeyListener(new MooKeyboard());
69         }
70
71         public void layoutElement(MooNoteElement elem, boolean old){
72                 // If the element is currently in the view, removes its coordinates from the list.
73                 Rectangle r = new Rectangle();
74                 if (old){
75                         r = elem.getBounds(r);
76                         for (Iterator i = rects.iterator(); i.hasNext();){
77                                 Object ob = i.next();
78                                 if (r.equals(ob)){
79                                         rects.remove(ob);
80                                         break;
81                                 }
82                         }
83                 }
84
85                 // Creates temporary variables.
86                 int ticksPerSixteenth = Moosique.getSequence().getResolution() / 4;
87                 MooNote mn = elem.getNote();
88                 Insets insets = getInsets();
89                 int x, y, height;
90
91                 // Calculates coordinates.
92                 x = insets.left;
93                 y = insets.top + (int)(mn.getTick() / ticksPerSixteenth) * NOTE_HEIGHT;
94                 height = (mn.getDuration() / ticksPerSixteenth) * NOTE_HEIGHT;
95                 if (height == 0) height = NOTE_HEIGHT;
96                 r = new Rectangle(x, y, NOTE_WIDTH, height);
97
98                 // Places the element in the appropriate place.
99                 while(isOccupied(r)) r.translate(NOTE_WIDTH, 0);
100                 elem.setBounds(r);
101                 rects.add(r);
102                 if (viewLength < (y + height)){
103                         viewLength = y + height;
104                         if(old)setPreferredSize(new Dimension(VIEW_WIDTH, viewLength + extraHeight));
105                 }
106         }
107
108         public Track getTrack() {
109                 return track;
110         }
111
112
113         /** 
114          * Updates the track view.
115          */
116         public void update(long tickPosition) {
117                 repaint();
118         }
119
120         private boolean isOccupied(Rectangle r) {
121                 Iterator it = rects.iterator();
122                 while (it.hasNext()) {
123                         if(r.intersects((Rectangle)it.next())) return true;
124                 }
125                 return false;
126         }
127         
128         public void addNote(MooNote mn) {
129                 mn.addTo(track);
130                 MooNoteElement elem = new MooNoteElement(this, mn);
131                 add(elem);
132                 layoutElement(elem, false);
133                 setPreferredSize(new Dimension(VIEW_WIDTH, viewLength + extraHeight));
134         }
135
136         public void removeNote(MooNoteElement elem, MooNote mn) {
137                 mn.removeFrom(track);
138                 remove(elem);
139                 elem.getNote().removeFrom(track);
140                 validate();
141                 repaint();
142         }
143
144         public void paintComponent(Graphics g) {
145                 super.paintComponent(g);
146                 Graphics2D g2 = (Graphics2D)g;
147                 for (int c = 0; c < viewLength || c < getHeight(); c += NOTE_HEIGHT) {
148                         for (int r = 0; r < (10 * NOTE_WIDTH); r += NOTE_WIDTH) {
149                                 box = new Rectangle(r, c, NOTE_WIDTH, NOTE_HEIGHT);
150                                 g2.setColor(Color.gray);
151                                 g2.draw(box);
152                         }
153                 }
154         }
155
156         class MAdapter extends MouseAdapter {
157                 public void mousePressed(MouseEvent e) {
158                         if (e.isPopupTrigger()) {
159                                 popup.show(e.getComponent(), e.getX(), e.getY());
160                         }
161                 }
162
163                 public void mouseEntered(MouseEvent e) {
164                         // Moosique.setActiveChannel(track.getChannel());
165                         grabFocus();
166                 }
167         }
168
169         class PopupListener implements ActionListener {
170                 public void actionPerformed(ActionEvent e) {
171                         Object source = e.getSource();
172                         if  (source == popupAdd) {
173                                 //addNote(new MooNote());
174                                 // int channel, int pitch, int velocity, long timestamp, int duration
175                         }
176                 }
177         }
178 }