]> ruin.nu Git - moosique.git/blob - MooTrackView.java
Fixed the note element length.
[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 implements ActionListener {
15
16         private Track track;
17         private MooTrackTitle title;
18         private NoteArea notes;
19         private Rectangle box;
20         private JPopupMenu popup;
21         private JMenuItem menuItem;
22         protected static int viewLength = 0;
23         
24
25         public MooTrackView (Track track) {
26                 this.track = track;
27                 //setPreferredSize(new Dimension(200, 9000));
28                 setLayout(new BorderLayout());
29                 this.setBorder(BorderFactory.createLineBorder(Color.black));
30
31                 //title = new MooTrackTitle(track);
32                 //title.setBorder(BorderFactory.createLineBorder(Color.black));
33                 //add(title, BorderLayout.NORTH);
34
35                 notes = new NoteArea(track);    
36                 notes.setBackground(Color.white);
37                 notes.setBorder(BorderFactory.createLineBorder(Color.black));
38
39                 popup = new JPopupMenu();
40                 menuItem = new JMenuItem("Add...");
41                 menuItem.addActionListener(this);
42                 popup.add(menuItem);
43                 menuItem = new JMenuItem("Preferences...");
44                 menuItem.addActionListener(this);
45                 popup.add(menuItem);
46
47                 notes.addMouseListener(new PopupListener());
48                 add(notes, BorderLayout.CENTER);
49         }
50
51         public void actionPerformed(ActionEvent e) {}
52
53         public Track getTrack() {
54                 return track;
55         }
56         
57
58         /** 
59          * Updates the track view.
60          */
61         public void update() {
62                 repaint();
63         }
64
65         class NoteArea extends JPanel {
66                 public static final int NOTE_HEIGHT = 10;
67                 public static final int NOTE_WIDTH = 40;
68                 private int trackLength;
69                 private ArrayList rects;
70
71                 public NoteArea(Track track) {
72                         // Configuring panel
73                         setLayout(null);
74                         trackLength = 140;
75                         setPreferredSize(new Dimension(200, 140 * NOTE_HEIGHT));
76
77                         // Creating temporary variables
78                         MidiEvent note;
79                         MooNoteElement elem;
80                         int x, y, height;
81                         rects = new ArrayList(track.size() / 2);
82
83                         // Placing note elements
84                         Insets insets = getInsets();
85                         for (int i = 0; i < track.size(); i++) {
86                                 note = track.get(i);
87                                 if (note instanceof MooNote) {
88                                         // Adds the note element to the note area.
89                                         MooNote mn = (MooNote)note;
90                                         elem = new MooNoteElement(mn);
91                                         add(elem);
92
93                                         // Moves the note element to the appropriate place.
94                                         x = insets.left;
95                                         y = insets.top + (int)(mn.getTick() / 24) * NOTE_HEIGHT;
96                                         height = (mn.getDuration() / 24) * NOTE_HEIGHT;
97                                         if (height == 0) height = NOTE_HEIGHT;
98                                         Rectangle r = new Rectangle(x, y, NOTE_WIDTH, height);
99                                         while(isOccupied(r)) r.translate(NOTE_WIDTH, 0);
100                                         elem.setBounds(r);
101                                         rects.add(r);
102                                         if (viewLength < (y + height)) viewLength = y + height;
103
104                                         // while(findComponentAt(x, y) instanceof MooNoteElement ||
105                                         //       findComponentAt(x, y + height - 1) instanceof MooNoteElement) x += NOTE_WIDTH;
106                                 }
107                                 setPreferredSize(new Dimension(200, viewLength));
108                         }
109                         validate();
110                 }
111                 
112                 private boolean isOccupied(Rectangle r) {
113                         Iterator it = rects.iterator();
114                         while (it.hasNext()) {
115                                 if(r.intersects((Rectangle)it.next())) return true;
116                         }
117                         return false;
118                 }
119
120                 public void paintComponent(Graphics g) {
121                         super.paintComponent(g);
122                         Graphics2D g2 = (Graphics2D)g;
123                         for (int c = 0; c < viewLength ; c += NOTE_HEIGHT) {
124                                 for (int r = 0; r < (10*NOTE_WIDTH); r += NOTE_WIDTH) {
125                                         box = new Rectangle(r, c, NOTE_WIDTH, NOTE_HEIGHT);
126                                         g2.setColor(Color.gray);
127                                         g2.draw(box);
128                                 }
129                         }
130                 }
131         }
132
133         class PopupListener extends MouseAdapter {
134                 public void mousePressed(MouseEvent e) {
135                         maybeShowPopup(e);
136                 }
137
138                 public void mouseReleased(MouseEvent e) {
139                         maybeShowPopup(e);
140                 }
141
142                 private void maybeShowPopup(MouseEvent e) {
143                         if (e.isPopupTrigger()) {
144                                 popup.show(e.getComponent(), e.getX(), e.getY());
145                         }
146                 }
147         }
148 }