]> 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 title;
18         private Rectangle box;
19
20         private JPopupMenu popup;
21         private JMenuItem popupAdd;
22         private ArrayList rects;
23         private Insets insets;
24         private int ticksPerSixteenth, popupY = 0;
25         protected static int viewLength = 0;
26         protected static int extraHeight = 0;
27         public static final int NOTE_HEIGHT = 10, NOTE_WIDTH = 40, VIEW_WIDTH = 200;
28
29         public MooTrackView (Track track, MooTrackTitle title) {
30                 super(true);
31                 this.track = track;
32                 this.title = title;
33                 insets = getInsets();
34
35                 // Configures panel
36                 setBackground(Color.white);
37                 setBorder(BorderFactory.createLineBorder(Color.black));
38                 setLayout(null);
39                 setPreferredSize(new Dimension(VIEW_WIDTH, 140 * NOTE_HEIGHT));
40
41                 // Creates temporary variables
42                 MidiEvent note;
43                 MooNoteElement elem;
44                 rects = new ArrayList(track.size() / 2);
45                 extraHeight = Toolkit.getDefaultToolkit().getScreenSize().height - 150;
46
47                 // Places note elements
48                 for (int i = 0; i < track.size(); i++) {
49                         note = track.get(i);
50                         if (note instanceof MooNote) {
51                                 // Adds the note element to the note area and moves it to the appropriate place.
52                                 MooNote mn = (MooNote)note;
53                                 elem = new MooNoteElement(this, mn);
54                                 add(elem);
55                                 layoutElement(elem, false);
56                         }
57                         setPreferredSize(new Dimension(VIEW_WIDTH, viewLength + extraHeight));
58
59                 }
60
61                 // Creates pop-up menu.
62                 popup = new JPopupMenu();
63                 PopupListener pList = new PopupListener();
64                 popupAdd = new JMenuItem("Add note...");
65                 popupAdd.addActionListener(pList);
66                 popup.add(popupAdd);
67
68                 // Adds listeners for popup menu and keyboard synthesizer.
69                 addMouseListener(new MAdapter());
70                 addKeyListener(new MooKeyboard());
71         }
72
73         public void layoutElement(MooNoteElement elem, boolean old){
74                 // If the element is currently in the view, removes its coordinates from the list.
75                 Rectangle r = new Rectangle();
76                 if (old){
77                         r = elem.getBounds(r);
78                         for (Iterator i = rects.iterator(); i.hasNext();){
79                                 Object ob = i.next();
80                                 if (r.equals(ob)){
81                                         rects.remove(ob);
82                                         break;
83                                 }
84                         }
85                 }
86
87                 // Creates temporary variables.
88                 ticksPerSixteenth = Moosique.getSequence().getResolution() / 4;
89                 MooNote mn = elem.getNote();
90                 int x, y, height;
91
92                 // Calculates coordinates.
93                 x = insets.left;
94                 y = insets.top + (int)(mn.getTick() / ticksPerSixteenth) * NOTE_HEIGHT;
95                 height = (mn.getDuration() / ticksPerSixteenth) * NOTE_HEIGHT;
96                 if (height == 0) height = NOTE_HEIGHT;
97                 r = new Rectangle(x, y, NOTE_WIDTH, height);
98
99                 // Places the element in the appropriate place.
100                 while(isOccupied(r)) r.translate(NOTE_WIDTH, 0);
101                 elem.setBounds(r);
102                 rects.add(r);
103                 if (viewLength < (y + height)){
104                         viewLength = y + height;
105                         if(old)setPreferredSize(new Dimension(VIEW_WIDTH, viewLength + extraHeight));
106                 }
107         }
108
109         /** 
110          * Returns the track of this view.
111          * @return the track of this view
112          */
113         public Track getTrack() {
114                 return track;
115         }
116
117         /** 
118          * Returns the title of this view.
119          * @return the title of this view
120          */
121         public MooTrackTitle getTitle() {
122                 return title;
123         }
124
125         private boolean isOccupied(Rectangle r) {
126                 Iterator it = rects.iterator();
127                 while (it.hasNext()) {
128                         if(r.intersects((Rectangle)it.next())) return true;
129                 }
130                 return false;
131         }
132         
133         /** 
134          * Adds the given note to the current track, and visualises it.
135          * @param mn    the note to add
136          */
137         public void addNote(MooNote mn) {
138                 mn.addTo(track);
139                 MooNoteElement elem = new MooNoteElement(this, mn);
140                 add(elem);
141                 layoutElement(elem, false);
142                 setPreferredSize(new Dimension(VIEW_WIDTH, viewLength + extraHeight));
143                 repaint();
144         }
145
146         /** 
147          * Removes the given note element from the view and its note from the current track.
148          * @param elem  the note element to remove
149          */
150         public void removeNote(MooNoteElement elem) {
151                 elem.getNote().removeFrom(track);
152                 remove(elem);
153                 elem.getNote().removeFrom(track);
154                 repaint();
155         }
156
157         private void addStandardNote() {
158                 long timestamp = (long)(ticksPerSixteenth * (popupY - insets.top) / NOTE_HEIGHT);
159                 System.out.println(ticksPerSixteenth + ", " + popupY + ", " + insets.top + ", " + timestamp);
160                 addNote(new MooNote(title.getChannel(), 60, 100, timestamp, Moosique.getSequence().getResolution() / 4));
161         }
162
163         public void paintComponent(Graphics g) {
164                 super.paintComponent(g);
165                 Graphics2D g2 = (Graphics2D)g;
166                 for (int c = 0; c < viewLength || c < getHeight(); c += NOTE_HEIGHT) {
167                         for (int r = 0; r < (10 * NOTE_WIDTH); r += NOTE_WIDTH) {
168                                 box = new Rectangle(r, c, NOTE_WIDTH, NOTE_HEIGHT);
169                                 g2.setColor(Color.gray);
170                                 g2.draw(box);
171                         }
172                 }
173         }
174
175         class MAdapter extends MouseAdapter {
176                 public void mouseClicked(MouseEvent e) {
177                         if (e.getClickCount() == 2) {
178                                 popupY = e.getY();
179                                 addStandardNote();
180                         }
181                 }
182         
183                 public void mousePressed(MouseEvent e) {
184                         if (e.isPopupTrigger()) {
185                                 popupY = e.getY();
186                                 popup.show(e.getComponent(), e.getX(), e.getY());
187                         }
188                 }
189
190                 public void mouseEntered(MouseEvent e) {
191                         // Moosique.setActiveChannel(track.getChannel());
192                         grabFocus();
193                 }
194         }
195
196         class PopupListener implements ActionListener {
197                 public void actionPerformed(ActionEvent e) {
198                         Object source = e.getSource();
199                         if  (source == popupAdd) {
200                                 addStandardNote();
201                         }
202                         // new MooNote(int channel, int pitch, int velocity, long timestamp, int duration)
203                 }
204         }
205 }