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