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 Rectangle box;
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;
26 public MooTrackView (Track track) {
31 setBackground(Color.white);
32 setBorder(BorderFactory.createLineBorder(Color.black));
34 setPreferredSize(new Dimension(VIEW_WIDTH, 140 * NOTE_HEIGHT));
36 // Creates temporary variables
39 rects = new ArrayList(track.size() / 2);
40 extraHeight = Toolkit.getDefaultToolkit().getScreenSize().height - 150;
42 // Places note elements
43 for (int i = 0; i < track.size(); 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);
50 layoutElement(elem, false);
52 setPreferredSize(new Dimension(VIEW_WIDTH, viewLength + extraHeight));
57 // Creates pop-up menu.
58 popup = new JPopupMenu();
59 PopupListener pList = new PopupListener();
60 popupAdd = new JMenuItem("Add note...");
61 popupAdd.addActionListener(pList);
64 // Adds listeners for popup menu and keyboard synthesizer.
65 addMouseListener(new MAdapter());
66 addKeyListener(new MooKeyboard());
69 public void layoutElementMooNoteElement elem, boolean old){
70 // If the element is currently in the view, removes its coordinates from the list.
71 Rectangle r = new Rectangle();
73 r = elem.getBounds(r);
74 for (Iterator i = rects.iterator(); i.hasNext();){
83 // Creates temporary variables.
84 int ticksPerSixteenth = Moosique.getSequence().getResolution() / 4;
85 MooNote mn = elem.getNote();
86 Insets insets = getInsets();
89 // Calculates coordinates.
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);
96 // Places the element in the appropriate place.
97 while(isOccupied(r)) r.translate(NOTE_WIDTH, 0);
100 if (viewLength < (y + height)){
101 viewLength = y + height;
102 if(old)setPreferredSize(new Dimension(VIEW_WIDTH, viewLength + extraHeight));
107 public Track getTrack() {
113 * Updates the track view.
115 public void update(long tickPosition) {
119 private boolean isOccupied(Rectangle r) {
120 Iterator it = rects.iterator();
121 while (it.hasNext()) {
122 if(r.intersects((Rectangle)it.next())) return true;
127 public void addNote(MooNote mn) {
129 MooNoteElement elem = new MooNoteElement(this, mn);
131 layoutElement(elem, false);
132 setPreferredSize(new Dimension(VIEW_WIDTH, viewLength + extraHeight));
135 public void removeNote(MooNoteElement elem, MooNote mn) {
136 mn.removeFrom(track);
138 elem.getNote().removeFrom(track);
143 public void paintComponent(Graphics g) {
144 super.paintComponent(g);
145 Graphics2D g2 = (Graphics2D)g;
146 for (int c = 0; c < viewLength || c < getHeight(); c += NOTE_HEIGHT) {
147 for (int r = 0; r < (10 * NOTE_WIDTH); r += NOTE_WIDTH) {
148 box = new Rectangle(r, c, NOTE_WIDTH, NOTE_HEIGHT);
149 g2.setColor(Color.gray);
155 class MAdapter extends MouseAdapter {
156 public void mousePressed(MouseEvent e) {
157 if (e.isPopupTrigger()) {
158 popup.show(e.getComponent(), e.getX(), e.getY());
162 public void mouseEntered(MouseEvent e) {
163 // Moosique.setActiveChannel(track.getChannel());
168 class PopupListener implements ActionListener {
169 public void actionPerformed(ActionEvent e) {
170 Object source = e.getSource();
171 if (source == popupAdd) {
172 addNote(new MooNote());
173 // int channel, int pitch, int velocity, long timestamp, int duration