3 import java.awt.event.*;
4 import javax.sound.midi.*;
8 * Graphical representation of a MIDI track.
10 * @author Andersson , Andreen, Lanneskog, Pehrson
14 public class MooTrackView extends JPanel {
17 private MooTrackTitle;
18 private Rectangle box;
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;
27 public MooTrackView (Track track, MooTrackTitle title;) {
33 setBackground(Color.white);
34 setBorder(BorderFactory.createLineBorder(Color.black));
36 setPreferredSize(new Dimension(VIEW_WIDTH, 140 * NOTE_HEIGHT));
38 // Creates temporary variables
41 rects = new ArrayList(track.size() / 2);
42 extraHeight = Toolkit.getDefaultToolkit().getScreenSize().height - 150;
44 // Places note elements
45 for (int i = 0; i < track.size(); 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);
52 layoutElement(elem, false);
54 setPreferredSize(new Dimension(VIEW_WIDTH, viewLength + extraHeight));
59 // Creates pop-up menu.
60 popup = new JPopupMenu();
61 PopupListener pList = new PopupListener();
62 popupAdd = new JMenuItem("Add note...");
63 popupAdd.addActionListener(pList);
66 // Adds listeners for popup menu and keyboard synthesizer.
67 addMouseListener(new MAdapter());
68 addKeyListener(new MooKeyboard());
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();
75 r = elem.getBounds(r);
76 for (Iterator i = rects.iterator(); i.hasNext();){
85 // Creates temporary variables.
86 int ticksPerSixteenth = Moosique.getSequence().getResolution() / 4;
87 MooNote mn = elem.getNote();
88 Insets insets = getInsets();
91 // Calculates coordinates.
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);
98 // Places the element in the appropriate place.
99 while(isOccupied(r)) r.translate(NOTE_WIDTH, 0);
102 if (viewLength < (y + height)){
103 viewLength = y + height;
104 if(old)setPreferredSize(new Dimension(VIEW_WIDTH, viewLength + extraHeight));
108 public Track getTrack() {
114 * Updates the track view.
116 public void update(long tickPosition) {
120 private boolean isOccupied(Rectangle r) {
121 Iterator it = rects.iterator();
122 while (it.hasNext()) {
123 if(r.intersects((Rectangle)it.next())) return true;
128 public void addNote(MooNote mn) {
130 MooNoteElement elem = new MooNoteElement(this, mn);
132 layoutElement(elem, false);
133 setPreferredSize(new Dimension(VIEW_WIDTH, viewLength + extraHeight));
136 public void removeNote(MooNoteElement elem, MooNote mn) {
137 mn.removeFrom(track);
139 elem.getNote().removeFrom(track);
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);
156 class MAdapter extends MouseAdapter {
157 public void mousePressed(MouseEvent e) {
158 if (e.isPopupTrigger()) {
159 popup.show(e.getComponent(), e.getX(), e.getY());
163 public void mouseEntered(MouseEvent e) {
164 // Moosique.setActiveChannel(track.getChannel());
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