]> ruin.nu Git - moosique.git/blob - MooTrackView.java
513792de17c09597de455aa3600e081bfd84cb07
[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         public Track getTrack() {
110                 return track;
111         }
112
113
114         /** 
115          * Updates the track view.
116          */
117         public void update(long tickPosition) {
118                 repaint();
119         }
120
121         private boolean isOccupied(Rectangle r) {
122                 Iterator it = rects.iterator();
123                 while (it.hasNext()) {
124                         if(r.intersects((Rectangle)it.next())) return true;
125                 }
126                 return false;
127         }
128         
129         public void addNote(MooNote mn) {
130                 mn.addTo(track);
131                 MooNoteElement elem = new MooNoteElement(this, mn);
132                 add(elem);
133                 layoutElement(elem, false);
134                 setPreferredSize(new Dimension(VIEW_WIDTH, viewLength + extraHeight));
135                 repaint();
136         }
137
138         public void removeNote(MooNoteElement elem, MooNote mn) {
139                 mn.removeFrom(track);
140                 remove(elem);
141                 elem.getNote().removeFrom(track);
142                 repaint();
143         }
144
145         private void addStandardNote() {
146                 long timestamp = (long)(ticksPerSixteenth * (popupY - insets.top) / NOTE_HEIGHT);
147                 System.out.println(ticksPerSixteenth + ", " + popupY + ", " + insets.top + ", " + timestamp);
148                 addNote(new MooNote(title.getChannel(), 60, 100, timestamp, Moosique.getSequence().getResolution() / 4));
149         }
150
151         public void paintComponent(Graphics g) {
152                 super.paintComponent(g);
153                 Graphics2D g2 = (Graphics2D)g;
154                 for (int c = 0; c < viewLength || c < getHeight(); c += NOTE_HEIGHT) {
155                         for (int r = 0; r < (10 * NOTE_WIDTH); r += NOTE_WIDTH) {
156                                 box = new Rectangle(r, c, NOTE_WIDTH, NOTE_HEIGHT);
157                                 g2.setColor(Color.gray);
158                                 g2.draw(box);
159                         }
160                 }
161         }
162
163         class MAdapter extends MouseAdapter {
164                 public void mouseClicked(MouseEvent e) {
165                         if (e.getClickCount() == 2) {
166                                 popupY = e.getY();
167                                 addStandardNote();
168                         }
169                 }
170         
171                 public void mousePressed(MouseEvent e) {
172                         if (e.isPopupTrigger()) {
173                                 popupY = e.getY();
174                                 popup.show(e.getComponent(), e.getX(), e.getY());
175                         }
176                 }
177
178                 public void mouseEntered(MouseEvent e) {
179                         // Moosique.setActiveChannel(track.getChannel());
180                         grabFocus();
181                 }
182         }
183
184         class PopupListener implements ActionListener {
185                 public void actionPerformed(ActionEvent e) {
186                         Object source = e.getSource();
187                         if  (source == popupAdd) {
188                                 addStandardNote();
189                         }
190                         // new MooNote(int channel, int pitch, int velocity, long timestamp, int duration)
191                 }
192         }
193 }