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 title;
18 private Rectangle box;
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;
29 public MooTrackView (Track track, MooTrackTitle title) {
36 setBackground(Color.white);
37 setBorder(BorderFactory.createLineBorder(Color.black));
39 setPreferredSize(new Dimension(VIEW_WIDTH, 140 * NOTE_HEIGHT));
41 // Creates temporary variables
44 rects = new ArrayList(track.size() / 2);
45 extraHeight = Toolkit.getDefaultToolkit().getScreenSize().height - 150;
47 // Places note elements
48 for (int i = 0; i < track.size(); 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);
55 layoutElement(elem, false);
57 setPreferredSize(new Dimension(VIEW_WIDTH, viewLength + extraHeight));
61 // Creates pop-up menu.
62 popup = new JPopupMenu();
63 PopupListener pList = new PopupListener();
64 popupAdd = new JMenuItem("Add note...");
65 popupAdd.addActionListener(pList);
68 // Adds listeners for popup menu and keyboard synthesizer.
69 addMouseListener(new MAdapter());
70 addKeyListener(new MooKeyboard());
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();
77 r = elem.getBounds(r);
78 for (Iterator i = rects.iterator(); i.hasNext();){
87 // Creates temporary variables.
88 ticksPerSixteenth = Moosique.getSequence().getResolution() / 4;
89 MooNote mn = elem.getNote();
92 // Calculates coordinates.
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);
99 // Places the element in the appropriate place.
100 while(isOccupied(r)) r.translate(NOTE_WIDTH, 0);
103 if (viewLength < (y + height)){
104 viewLength = y + height;
105 if(old)setPreferredSize(new Dimension(VIEW_WIDTH, viewLength + extraHeight));
109 public Track getTrack() {
115 * Updates the track view.
117 public void update(long tickPosition) {
121 private boolean isOccupied(Rectangle r) {
122 Iterator it = rects.iterator();
123 while (it.hasNext()) {
124 if(r.intersects((Rectangle)it.next())) return true;
129 public void addNote(MooNote mn) {
131 MooNoteElement elem = new MooNoteElement(this, mn);
133 layoutElement(elem, false);
134 setPreferredSize(new Dimension(VIEW_WIDTH, viewLength + extraHeight));
138 public void removeNote(MooNoteElement elem, MooNote mn) {
139 mn.removeFrom(track);
141 elem.getNote().removeFrom(track);
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));
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);
163 class MAdapter extends MouseAdapter {
164 public void mouseClicked(MouseEvent e) {
165 if (e.getClickCount() == 2) {
171 public void mousePressed(MouseEvent e) {
172 if (e.isPopupTrigger()) {
174 popup.show(e.getComponent(), e.getX(), e.getY());
178 public void mouseEntered(MouseEvent e) {
179 // Moosique.setActiveChannel(track.getChannel());
184 class PopupListener implements ActionListener {
185 public void actionPerformed(ActionEvent e) {
186 Object source = e.getSource();
187 if (source == popupAdd) {
190 // new MooNote(int channel, int pitch, int velocity, long timestamp, int duration)